-
Notifications
You must be signed in to change notification settings - Fork 12.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Xtensa] Implement support for the BranchRelaxation. #113450
Conversation
✅ With the latest revision this PR passed the C/C++ code formatter. |
77d3f45
to
bf10a83
Compare
const XtensaInstrInfo &TII) { | ||
unsigned FnSize = 0; | ||
for (auto &MBB : MF) { | ||
for (auto &MI : MBB) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Account for block (and function) alignment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for comment. I've implemented function size calculation like it is done in BranchRelaxation. Also I corrected insertIndirectBranch function.
@@ -258,13 +258,32 @@ void XtensaFrameLowering::determineCalleeSaves(MachineFunction &MF, | |||
|
|||
static unsigned estimateFunctionSizeInBytes(const MachineFunction &MF, | |||
const XtensaInstrInfo &TII) { | |||
unsigned FnSize = 0; | |||
const Align FunctiontAlignment = MF.getAlignment(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo Functiont?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
const Align FunctiontAlignment = MF.getAlignment(); | ||
/// Offset - Distance from the beginning of the function to the end | ||
/// of the basic block. | ||
unsigned Offset = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably should use int64_t
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
// The alignment of this MBB is larger than the function's alignment, so | ||
// we can't tell whether or not it will insert nops. Assume that it will. | ||
OffsetBB = alignTo(Offset, Alignment) + Alignment.value() - | ||
FunctiontAlignment.value(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this handled somewhere else already? Should this whole size estimate function be moved to a common helper function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this function to XtensaUtils.cpp.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean outside of the backend, a generic codegen utility.
Was this edge case already handled in other code? Is there a test for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to find code which may be substituted by such function. Currently I found couple of places in backends:
static uint64_t estimateFunctionSizeInBytes(const LoongArchInstrInfo *TII, |
and
static unsigned estimateFunctionSizeInBytes(const MachineFunction &MF, |
This backends use more simple approach to caclulate function size.
And also there is a code in BranchRelaxation use similar approach to calculate block offsets, but it doesn't need function size:
unsigned postOffset(const MachineBasicBlock &MBB) const { |
Should we find a code in other places, which can benefit by using such function before placing it in generic utils? Or maybe leave this function it in Xtensa backend?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a mess but I see RISCV is doing some target specific hacking in there I don't understand. The LoongArch one looks like a buggier version of the generic logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean outside of the backend, a generic codegen utility.
Was this edge case already handled in other code? Is there a test for it?
I added new test for branch relaxation. May I ask your for advice about appropriate place for codegen utility? Should I place it in support or maybe add it to some class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably directly in MachineFunction
|
||
int FrameIndex = XtensaFI->getBranchRelaxationScratchFrameIndex(); | ||
if (FrameIndex == -1) | ||
report_fatal_error("underestimated function size"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make message more specific
// The alignment of this MBB is larger than the function's alignment, so | ||
// we can't tell whether or not it will insert nops. Assume that it will. | ||
OffsetBB = alignTo(Offset, Alignment) + Alignment.value() - | ||
FunctiontAlignment.value(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably directly in MachineFunction
// The alignment of this MBB is larger than the function's alignment, so | ||
// we can't tell whether or not it will insert nops. Assume that it will. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if this should be a verifier error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean that this case shouldn't happen and verifier should throw an error? in such situation? Can we implement this in later commits (if we need this)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I'm not sure it makes sense to have a block with less alignment than the function. you'd have to have some strange code realignment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't seen an example for Xtensa at this time with block alignment large then function. Actually I implemented alignment handling like it is done in Branch Relaxation pass in postOffset function. I thought it may be useful for general case.
unsigned postOffset(const MachineBasicBlock &MBB) const { |
Implement insertIndirectBranch function and other functions needed by Branch Relaxation pass. Also implement estimateFunctionSizeInBytes function in MachineFunction class.
a444fa5
to
0460557
Compare
MRI.clearVirtRegs(); | ||
} | ||
|
||
unsigned XtensaInstrInfo::insertConstBranchAtInst( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is unused and it compiles with a warning.
MachineBasicBlock &MBB, MachineInstr *I, int64_t offset, | ||
ArrayRef<MachineOperand> Cond, DebugLoc DL, int *BytesAdded) const { | ||
// Shouldn't be a fall through. | ||
assert(&MBB && "InsertBranch must not be told to insert a fallthrough"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This led to a Clang warning -Wundefined-bool-conversion. We ensure that the code base is warning free for recent Clang versions. Fixed at head.
Personally I use https://raw.githubusercontent.com/chromium/chromium/main/tools/clang/scripts/update.py
No description provided.