Skip to content

Commit 4e9a9cc

Browse files
committed
SimplifyCFG: disable some expensive optimizations for huge functions.
Disable constant folding and jump threading for functions with > 10000 blocks. Those optimizations are not strictly linear with the number of blocks and cause compile time issues if the function is really huge. SR-10209 rdar://problem/49522869
1 parent d88419d commit 4e9a9cc

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ namespace {
8181

8282
ConstantFolder ConstFolder;
8383

84+
// True if the function has a large amount of blocks. In this case we turn off some expensive
85+
// optimizations.
86+
bool isVeryLargeFunction = false;
87+
8488
void constFoldingCallback(SILInstruction *I) {
8589
// If a terminal instruction gets constant folded (like cond_br), it
8690
// enables further simplify-CFG optimizations.
@@ -1226,11 +1230,13 @@ bool SimplifyCFG::simplifyBranchBlock(BranchInst *BI) {
12261230
if (DestBB->getArgument(i) != BI->getArg(i)) {
12271231
SILValue Val = BI->getArg(i);
12281232
DestBB->getArgument(i)->replaceAllUsesWith(Val);
1229-
if (auto *I = dyn_cast<SingleValueInstruction>(Val)) {
1230-
// Replacing operands may trigger constant folding which then could
1231-
// trigger other simplify-CFG optimizations.
1232-
ConstFolder.addToWorklist(I);
1233-
ConstFolder.processWorkList();
1233+
if (!isVeryLargeFunction) {
1234+
if (auto *I = dyn_cast<SingleValueInstruction>(Val)) {
1235+
// Replacing operands may trigger constant folding which then could
1236+
// trigger other simplify-CFG optimizations.
1237+
ConstFolder.addToWorklist(I);
1238+
ConstFolder.processWorkList();
1239+
}
12341240
}
12351241
} else {
12361242
// We must be processing an unreachable part of the cfg with a cycle.
@@ -1290,7 +1296,7 @@ bool SimplifyCFG::simplifyBranchBlock(BranchInst *BI) {
12901296
// If this unconditional branch has BBArgs, check to see if duplicating the
12911297
// destination would allow it to be simplified. This is a simple form of jump
12921298
// threading.
1293-
if (!BI->getArgs().empty() &&
1299+
if (!isVeryLargeFunction && !BI->getArgs().empty() &&
12941300
tryJumpThreading(BI))
12951301
return true;
12961302

@@ -3067,6 +3073,9 @@ bool SimplifyCFG::run() {
30673073

30683074
LLVM_DEBUG(llvm::dbgs() << "### Run SimplifyCFG on " << Fn.getName() << '\n');
30693075

3076+
// Disable some expensive optimizations if the function is huge.
3077+
isVeryLargeFunction = (Fn.size() > 10000);
3078+
30703079
// First remove any block not reachable from the entry.
30713080
bool Changed = removeUnreachableBlocks(Fn);
30723081

0 commit comments

Comments
 (0)