Skip to content

Conversation

@mikhailramalho
Copy link
Member

This patch adds support for configuring the BranchFolderPass with or without tail merging.

…option to enable tail merge

Signed-off-by: Mikhail R. Gadelha <[email protected]>
Signed-off-by: Mikhail R. Gadelha <[email protected]>
Signed-off-by: Mikhail R. Gadelha <[email protected]>
Copy link
Member Author

@mikhailramalho mikhailramalho left a comment

Choose a reason for hiding this comment

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

@arsenm this is a tentative patch do address your comments.

I do have a couple of questions:

// HW that requires structurized CFG.
bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() &&
PassConfig->getEnableTailMerge();
PassConfig->getEnableTailMerge() &&
Copy link
Member Author

Choose a reason for hiding this comment

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

This global state should be moved to only change the pass parameter

Do you mean I should remove the PassConfig->getEnableTailMerge()?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it can be passed as the EnableTailMerge parameter when TargetPassConfig creates this pass instance (where currently this is done through its ID).

Copy link
Member Author

Choose a reason for hiding this comment

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

Hey, so I did 9b95974, but I'm getting a weird error: when the branch folding pass runs now, I get that the pre-conditions are not valid:

MachineFunctionProperties required by Control Flow Optimizer pass are not met by function RWBufferStore_Vec4_I32.
Required properties: NoPHIs
Current properties: IsSSA, TracksLiveness, Legalized, Selected

This seems to only happen because of this change:

   // Branch folding must be run after regalloc and prolog/epilog insertion.
-  addPass(&BranchFolderPassID);
+  addPass(createBranchFolderPass(getEnableTailMerge()));

The calling addPass with the BranchFolderPassID, it doesn't seem to be added to the list of passes because char BranchFolderLegacy::ID = 0, whereas when we call createBranchFolderPass, the pass is added and fails.

Maybe I'm missing some detail here, but the pass doesn't seem to run at all before my changes.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the failing tests, the branch folding pass is disabled here:

disablePass(&BranchFolderPassID);

So by calling createBranchFolderPass, we don't check the disabled passed

Copy link
Member Author

Choose a reason for hiding this comment

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

This also seems to bypass the -disable-branch-fold.

Any suggestions on how to proceed?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the function properties error is from somewhere else. Most passes probably are not appropriately reporting cleared properties

Copy link
Contributor

Choose a reason for hiding this comment

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

What test hit the property error? I think SPIRV needs to cear NoPHIs property after its selection. It's bypassing the usual place it's cleared (though I'm not sure why it's cleared here)

Copy link
Member Author

Choose a reason for hiding this comment

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

All tests are passing with the latest version of this PR

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 avoided after the new version of #136327?

Copy link
Member Author

Choose a reason for hiding this comment

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

This piece of code is no longer part of the PR.


explicit BranchFolderLegacy() : MachineFunctionPass(ID) {}
explicit BranchFolderLegacy(bool EnableTailMerge = true)
: MachineFunctionPass(ID), EnableTailMerge(EnableTailMerge) {}
Copy link
Member Author

Choose a reason for hiding this comment

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

That added the initial port to the new pm. This is now changing the pass arguments in the old PM, without the matching new PM change

Are you talking about the default argument? I see that BranchFolderPass already takes a bool EnableTailMerge:

BranchFolderPass(bool EnableTailMerge) : EnableTailMerge(EnableTailMerge) {}

Signed-off-by: Mikhail R. Gadelha <[email protected]>
Signed-off-by: Mikhail R. Gadelha <[email protected]>
Signed-off-by: Mikhail R. Gadelha <[email protected]>
Signed-off-by: Mikhail R. Gadelha <[email protected]>
Signed-off-by: Mikhail R. Gadelha <[email protected]>
@mikhailramalho
Copy link
Member Author

ping

Comment on lines 710 to 712
IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
if (!overridePass(PassID, TargetID).isValid())
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't need to touch the override infrastructure?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I don't get it... What do you mean?

I had to add this to check if the pass was disabled. It's a copy-and-paste from the addPass(AnalysisID PassID) method.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's the problem. You shouldn't have to do anything like this

Copy link
Member Author

Choose a reason for hiding this comment

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

I've replaced this check with a isPassSubstitutedOrOverridden check, similar to how it's done for createPrologEpilogInserterPass. Does this address your comment?

arsenm added a commit that referenced this pull request Apr 18, 2025
There should be no PHIs after selection, as OpPhi is used
instead. This hopefully avoids errors in #135277.
@github-actions
Copy link

github-actions bot commented Apr 21, 2025

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

Signed-off-by: Mikhail R. Gadelha <[email protected]>
@mikhailramalho
Copy link
Member Author

ping

arsenm added a commit that referenced this pull request Apr 23, 2025
There should be no PHIs after selection, as OpPhi is used
instead. This hopefully avoids errors in #135277.
arsenm added a commit that referenced this pull request Apr 23, 2025
There should be no PHIs after selection, as OpPhi is used
instead. This hopefully avoids errors in #135277.
arsenm added a commit that referenced this pull request Apr 24, 2025
There should be no PHIs after selection, as OpPhi is used
 instead. This hopefully avoids errors in #135277.
@mikhailramalho
Copy link
Member Author

ping

PreservedAnalyses BranchFolderPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
MFPropsModifier _(*this, MF);
bool EnableTailMerge =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since you're removing it here, the NPM require the flag requiresStructuredCFG() to be incorporated here.

https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Passes/CodeGenPassBuilder.h#L1235

IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
There should be no PHIs after selection, as OpPhi is used
 instead. This hopefully avoids errors in llvm#135277.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants