Skip to content

Commit 6197205

Browse files
authored
[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: #142584
1 parent eb0f1dc commit 6197205

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

llvm/lib/CodeGen/MachineBlockPlacement.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ static cl::opt<unsigned> MaxBytesForAlignmentOverride(
104104
"alignment"),
105105
cl::init(0), cl::Hidden);
106106

107+
static cl::opt<unsigned> PredecessorLimit(
108+
"block-placement-predecessor-limit",
109+
cl::desc("For blocks with more predecessors, certain layout optimizations"
110+
"will be disabled to prevent quadratic compile time."),
111+
cl::init(1000), cl::Hidden);
112+
107113
// FIXME: Find a good default for this flag and remove the flag.
108114
static cl::opt<unsigned> ExitBlockBias(
109115
"block-placement-exit-block-bias",
@@ -1030,6 +1036,11 @@ bool MachineBlockPlacement::isTrellis(
10301036
SmallPtrSet<const MachineBasicBlock *, 8> SeenPreds;
10311037

10321038
for (MachineBasicBlock *Succ : ViableSuccs) {
1039+
// Compile-time optimization: runtime is quadratic in the number of
1040+
// predecessors. For such uncommon cases, exit early.
1041+
if (Succ->pred_size() > PredecessorLimit)
1042+
return false;
1043+
10331044
int PredCount = 0;
10341045
for (auto *SuccPred : Succ->predecessors()) {
10351046
// Allow triangle successors, but don't count them.
@@ -1472,6 +1483,11 @@ bool MachineBlockPlacement::hasBetterLayoutPredecessor(
14721483
if (SuccChain.UnscheduledPredecessors == 0)
14731484
return false;
14741485

1486+
// Compile-time optimization: runtime is quadratic in the number of
1487+
// predecessors. For such uncommon cases, exit early.
1488+
if (Succ->pred_size() > PredecessorLimit)
1489+
return false;
1490+
14751491
// There are two basic scenarios here:
14761492
// -------------------------------------
14771493
// Case 1: triangular shape CFG (if-then):

llvm/test/CodeGen/RISCV/branch.ll

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
22
; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
33
; RUN: | FileCheck -check-prefix=RV32I %s
4+
; RUN: llc -mtriple=riscv32 -verify-machineinstrs -block-placement-predecessor-limit=10 < %s \
5+
; RUN: | FileCheck -check-prefix=RV32I-MBPLIMIT %s
46

57
define void @foo(i32 %a, ptr %b, i1 %c) nounwind {
68
; RV32I-LABEL: foo:
@@ -48,6 +50,53 @@ define void @foo(i32 %a, ptr %b, i1 %c) nounwind {
4850
; RV32I-NEXT: lw zero, 0(a1)
4951
; RV32I-NEXT: .LBB0_14: # %end
5052
; RV32I-NEXT: ret
53+
;
54+
; RV32I-MBPLIMIT-LABEL: foo:
55+
; RV32I-MBPLIMIT: # %bb.0:
56+
; RV32I-MBPLIMIT-NEXT: lw a3, 0(a1)
57+
; RV32I-MBPLIMIT-NEXT: bne a3, a0, .LBB0_2
58+
; RV32I-MBPLIMIT-NEXT: .LBB0_1: # %end
59+
; RV32I-MBPLIMIT-NEXT: ret
60+
; RV32I-MBPLIMIT-NEXT: .LBB0_2: # %test2
61+
; RV32I-MBPLIMIT-NEXT: lw a3, 0(a1)
62+
; RV32I-MBPLIMIT-NEXT: bne a3, a0, .LBB0_1
63+
; RV32I-MBPLIMIT-NEXT: # %bb.3: # %test3
64+
; RV32I-MBPLIMIT-NEXT: lw a3, 0(a1)
65+
; RV32I-MBPLIMIT-NEXT: blt a3, a0, .LBB0_1
66+
; RV32I-MBPLIMIT-NEXT: # %bb.4: # %test4
67+
; RV32I-MBPLIMIT-NEXT: lw a3, 0(a1)
68+
; RV32I-MBPLIMIT-NEXT: bge a3, a0, .LBB0_1
69+
; RV32I-MBPLIMIT-NEXT: # %bb.5: # %test5
70+
; RV32I-MBPLIMIT-NEXT: lw a3, 0(a1)
71+
; RV32I-MBPLIMIT-NEXT: bltu a3, a0, .LBB0_1
72+
; RV32I-MBPLIMIT-NEXT: # %bb.6: # %test6
73+
; RV32I-MBPLIMIT-NEXT: lw a3, 0(a1)
74+
; RV32I-MBPLIMIT-NEXT: bgeu a3, a0, .LBB0_1
75+
; RV32I-MBPLIMIT-NEXT: # %bb.7: # %test7
76+
; RV32I-MBPLIMIT-NEXT: lw a3, 0(a1)
77+
; RV32I-MBPLIMIT-NEXT: blt a0, a3, .LBB0_1
78+
; RV32I-MBPLIMIT-NEXT: # %bb.8: # %test8
79+
; RV32I-MBPLIMIT-NEXT: lw a3, 0(a1)
80+
; RV32I-MBPLIMIT-NEXT: bge a0, a3, .LBB0_1
81+
; RV32I-MBPLIMIT-NEXT: # %bb.9: # %test9
82+
; RV32I-MBPLIMIT-NEXT: lw a3, 0(a1)
83+
; RV32I-MBPLIMIT-NEXT: bltu a0, a3, .LBB0_1
84+
; RV32I-MBPLIMIT-NEXT: # %bb.10: # %test10
85+
; RV32I-MBPLIMIT-NEXT: lw a3, 0(a1)
86+
; RV32I-MBPLIMIT-NEXT: bgeu a0, a3, .LBB0_1
87+
; RV32I-MBPLIMIT-NEXT: # %bb.11: # %test11
88+
; RV32I-MBPLIMIT-NEXT: lw zero, 0(a1)
89+
; RV32I-MBPLIMIT-NEXT: andi a2, a2, 1
90+
; RV32I-MBPLIMIT-NEXT: bnez a2, .LBB0_1
91+
; RV32I-MBPLIMIT-NEXT: # %bb.12: # %test12
92+
; RV32I-MBPLIMIT-NEXT: lw a0, 0(a1)
93+
; RV32I-MBPLIMIT-NEXT: bgez a0, .LBB0_1
94+
; RV32I-MBPLIMIT-NEXT: # %bb.13: # %test13
95+
; RV32I-MBPLIMIT-NEXT: lw a0, 0(a1)
96+
; RV32I-MBPLIMIT-NEXT: blez a0, .LBB0_1
97+
; RV32I-MBPLIMIT-NEXT: # %bb.14: # %test14
98+
; RV32I-MBPLIMIT-NEXT: lw zero, 0(a1)
99+
; RV32I-MBPLIMIT-NEXT: ret
51100
%val1 = load volatile i32, ptr %b
52101
%tst1 = icmp eq i32 %val1, %a
53102
br i1 %tst1, label %end, label %test2

0 commit comments

Comments
 (0)