aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/MachineBlockPlacement.cpp
diff options
context:
space:
mode:
authorAlexis Engelke <engelke@in.tum.de>2025-06-20 11:23:00 +0200
committerGitHub <noreply@github.com>2025-06-20 11:23:00 +0200
commit61972054f3fcaf59096799342bac9c93dd9aa432 (patch)
tree348506334c0f79f1e2e48366f8da629a5dc86a2c /llvm/lib/CodeGen/MachineBlockPlacement.cpp
parenteb0f1dc00e5d0e591fe912c1aaf9dd9d01d94b8d (diff)
downloadllvm-61972054f3fcaf59096799342bac9c93dd9aa432.zip
llvm-61972054f3fcaf59096799342bac9c93dd9aa432.tar.gz
llvm-61972054f3fcaf59096799342bac9c93dd9aa432.tar.bz2
[CodeGen] Limit number of analyzed predecessors
MachineBlockPlacement has quadratic runtime in the number of predecessors: in some situation, for an edge, all predecessors of the successor are considered. Limit the number of considered predecessors to bound compile time for large functions. Pull Request: https://github.com/llvm/llvm-project/pull/142584
Diffstat (limited to 'llvm/lib/CodeGen/MachineBlockPlacement.cpp')
-rw-r--r--llvm/lib/CodeGen/MachineBlockPlacement.cpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
index 08fe3d4..2dbabfe 100644
--- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp
+++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
@@ -104,6 +104,12 @@ static cl::opt<unsigned> MaxBytesForAlignmentOverride(
"alignment"),
cl::init(0), cl::Hidden);
+static cl::opt<unsigned> PredecessorLimit(
+ "block-placement-predecessor-limit",
+ cl::desc("For blocks with more predecessors, certain layout optimizations"
+ "will be disabled to prevent quadratic compile time."),
+ cl::init(1000), cl::Hidden);
+
// FIXME: Find a good default for this flag and remove the flag.
static cl::opt<unsigned> ExitBlockBias(
"block-placement-exit-block-bias",
@@ -1030,6 +1036,11 @@ bool MachineBlockPlacement::isTrellis(
SmallPtrSet<const MachineBasicBlock *, 8> SeenPreds;
for (MachineBasicBlock *Succ : ViableSuccs) {
+ // Compile-time optimization: runtime is quadratic in the number of
+ // predecessors. For such uncommon cases, exit early.
+ if (Succ->pred_size() > PredecessorLimit)
+ return false;
+
int PredCount = 0;
for (auto *SuccPred : Succ->predecessors()) {
// Allow triangle successors, but don't count them.
@@ -1472,6 +1483,11 @@ bool MachineBlockPlacement::hasBetterLayoutPredecessor(
if (SuccChain.UnscheduledPredecessors == 0)
return false;
+ // Compile-time optimization: runtime is quadratic in the number of
+ // predecessors. For such uncommon cases, exit early.
+ if (Succ->pred_size() > PredecessorLimit)
+ return false;
+
// There are two basic scenarios here:
// -------------------------------------
// Case 1: triangular shape CFG (if-then):