Skip to content

Comment SimplifyCFG JumpThreadingCost. #35627

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

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/SILOptimizer/Transforms/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ class SimplifyCFG {
// this set may or may not still be LoopHeaders.
// (ultimately this can be used to eliminate findLoopHeaders)
SmallPtrSet<SILBasicBlock *, 4> ClonedLoopHeaders;
// The cost (~ number of copied instructions) of jump threading per basic
// block. Used to prevent infinite jump threading loops.
// The accumulated cost of jump threading per basic block. Initially
// zero. Each clone increases the cost by ~ the number of copied instructions.
// Effectively multiplying a block's cost is by the number of times it has
// been cloned prevents any one block from being cloned indefinitely. Cloned
// blocks inherit their original block's current cost to avoid indefinitely
// optimizing the newly cloned blocks (primarily relevant for loops where the
// number of predecessors can remain the same).
llvm::SmallDenseMap<SILBasicBlock *, int, 8> JumpThreadingCost;

// Dominance and post-dominance info for the current function
Expand Down Expand Up @@ -318,8 +323,8 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
return false;

Cloner.cloneBranchTarget(SrcTerm);
JumpThreadingCost[Cloner.getNewBB()] = JumpThreadingCost[SrcTerm->getDestBB()];

JumpThreadingCost[Cloner.getNewBB()] =
JumpThreadingCost[SrcTerm->getDestBB()];

// We have copied the threaded block into the edge.
auto *clonedSrc = Cloner.getNewBB();
Expand Down Expand Up @@ -1090,7 +1095,7 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
}
}
}

// Deduct the prior cost of cloning these blocks (initially zero).
ThreadingBudget -= JumpThreadingCost[SrcBB];
ThreadingBudget -= JumpThreadingCost[DestBB];

Expand Down Expand Up @@ -1126,6 +1131,7 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
LLVM_DEBUG(llvm::dbgs() << "jump thread from bb" << SrcBB->getDebugID()
<< " to bb" << DestBB->getDebugID() << '\n');

// Accumulate the cost of cloning the block to avoid indefinite cloning.
JumpThreadingCost[DestBB] += copyCosts;

// Duplicate the destination block into this one, rewriting uses of the BBArgs
Expand All @@ -1134,7 +1140,8 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
Cloner.updateSSAAfterCloning();

// Also account the costs to the cloned DestBB, so the jump threading cannot
// loop by cloning the cloned block again.
// loop by cloning the cloned block again. This is primarily relevant for
// loops where the number of predecessors might not decrease with each clone.
JumpThreadingCost[Cloner.getNewBB()] += copyCosts;

// Once all the instructions are copied, we can nuke BI itself. We also add
Expand Down