-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
[SimplifyCFG] Avoid increasing too many phi entries when removing empty blocks #104887
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd3f41f
Avoid introducing complex phis after removing empty blocks in the cfg
Chengjunp f07e435
Update llvm/lib/Transforms/Utils/Local.cpp
Chengjunp 4d5faa9
Update llvm/lib/Transforms/Utils/Local.cpp
Chengjunp fa4dd77
Update introduceTooComplexPhi
Chengjunp 2e89288
Change heuristic to check the increase in # phi entries
Chengjunp 9d39fa7
Merge branch 'main' into chengjunp/AvoidComplexPhi
Chengjunp 8c94c45
Fix code style issues and update tests
Chengjunp a248bd0
Format
Chengjunp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,12 @@ static cl::opt<unsigned> PHICSENumPHISmallSize( | |
"When the basic block contains not more than this number of PHI nodes, " | ||
"perform a (faster!) exhaustive search instead of set-driven one.")); | ||
|
||
static cl::opt<unsigned> MaxPhiEntriesIncreaseAfterRemovingEmptyBlock( | ||
"max-phi-entries-increase-after-removing-empty-block", cl::init(1000), | ||
cl::Hidden, | ||
cl::desc("Stop removing an empty block if removing it will introduce more " | ||
"than this number of phi entries in its successor")); | ||
|
||
// Max recursion depth for collectBitParts used when detecting bswap and | ||
// bitreverse idioms. | ||
static const unsigned BitPartRecursionMaxDepth = 48; | ||
|
@@ -1047,6 +1053,34 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ, | |
return true; | ||
} | ||
|
||
// Check whether removing BB will make the phis in its Succ have too | ||
// many incoming entries. This function does not check whether BB is foldable | ||
// or not. | ||
static bool introduceTooManyPhiEntries(BasicBlock *BB, BasicBlock *Succ) { | ||
// If BB only has one predecessor, then removing it will not introduce more | ||
// incoming edges for phis. | ||
if (BB->hasNPredecessors(1)) | ||
return false; | ||
unsigned NumPreds = pred_size(BB); | ||
unsigned NumChangedPhi = 0; | ||
for (auto &Phi : Succ->phis()) { | ||
// If the incoming value is a phi and the phi is defined in BB, | ||
// then removing BB will not increase the total phi entries of the ir. | ||
if (PHINode *IncomingPhi = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test for this case.
Chengjunp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dyn_cast<PHINode>(Phi.getIncomingValueForBlock(BB))) | ||
if (IncomingPhi->getParent() == BB) | ||
continue; | ||
// Otherwise, we need to add entries to the phi | ||
NumChangedPhi++; | ||
} | ||
// For every phi that needs to be changed, (NumPreds - 1) new entries will be | ||
// added. If the total increase in phi entries exceeds | ||
// MaxPhiEntriesIncreaseAfterRemovingEmptyBlock, it will be considered as | ||
// introducing too many new phi entries. | ||
return (NumPreds - 1) * NumChangedPhi > | ||
MaxPhiEntriesIncreaseAfterRemovingEmptyBlock; | ||
} | ||
|
||
/// Replace a value flowing from a block to a phi with | ||
/// potentially multiple instances of that value flowing from the | ||
/// block's predecessors to the phi. | ||
|
@@ -1146,7 +1180,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, | |
BBKillable || | ||
CanRedirectPredsOfEmptyBBToSucc(BB, Succ, BBPreds, SuccPreds, CommonPred); | ||
|
||
if (!BBKillable && !BBPhisMergeable) | ||
if ((!BBKillable && !BBPhisMergeable) || introduceTooManyPhiEntries(BB, Succ)) | ||
return false; | ||
|
||
// Check to see if merging these blocks/phis would cause conflicts for any of | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.