Skip to content
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

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

andreisfr
Copy link
Contributor

No description provided.

Copy link

github-actions bot commented Oct 23, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

const XtensaInstrInfo &TII) {
unsigned FnSize = 0;
for (auto &MBB : MF) {
for (auto &MI : MBB) {
Copy link
Contributor

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?

Copy link
Contributor Author

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo Functiont?

Copy link
Contributor Author

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;
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines 278 to 281
// 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();
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make message more specific

Comment on lines 278 to 281
// 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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably directly in MachineFunction

Comment on lines +401 to +402
// 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.
Copy link
Contributor

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

Copy link
Contributor Author

@andreisfr andreisfr Nov 6, 2024

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)?

Copy link
Contributor

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?

Copy link
Contributor Author

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.
@andreisfr andreisfr merged commit 3b1b127 into llvm:main Nov 7, 2024
8 checks passed
MRI.clearVirtRegs();
}

unsigned XtensaInstrInfo::insertConstBranchAtInst(
Copy link
Contributor

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");
Copy link
Member

@MaskRay MaskRay Nov 10, 2024

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

Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants