Skip to content

Commit d8c5609

Browse files
committed
[LoopUnroll] Introduce UnrollMaxIterations as a hard cap on how many iterations we try to unroll
1 parent 309dcc1 commit d8c5609

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

llvm/lib/Transforms/Utils/LoopUnroll.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ UnrollVerifyLoopInfo("unroll-verify-loopinfo", cl::Hidden,
109109
#endif
110110
);
111111

112+
static cl::opt<unsigned>
113+
UnrollMaxIterations("unroll-max-iterations", cl::init(1'000'000),
114+
cl::Hidden,
115+
cl::desc("Maximum allowed iterations to unroll."));
112116

113117
/// Check if unrolling created a situation where we need to insert phi nodes to
114118
/// preserve LCSSA form.
@@ -453,6 +457,14 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
453457
}
454458
}
455459

460+
// Certain cases with UBSAN can cause trip count to be calculated as INT_MAX,
461+
// Block unrolling at a reasonable limit so that the compiler doesn't hang
462+
// trying to unroll the loop. See PR77842
463+
if (ULO.Count > UnrollMaxIterations) {
464+
LLVM_DEBUG(dbgs() << "Won't unroll; trip count is too large\n");
465+
return LoopUnrollResult::Unmodified;
466+
}
467+
456468
using namespace ore;
457469
// Report the unrolling decision.
458470
if (CompletelyUnroll) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -passes=loop-unroll -disable-output -debug-only=loop-unroll %s 2>&1 | FileCheck %s
3+
4+
; Validate that loop unroll full doesn't try to fully unroll values whose trip counts are too large.
5+
6+
; CHECK: Exiting block %cont23: TripCount=2147483648, TripMultiple=0, BreakoutTrip=0
7+
; CHECK-NEXT: Won't unroll; trip count is too large
8+
9+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
10+
target triple = "x86_64-redhat-linux-gnu"
11+
12+
define void @foo(i64 %end) {
13+
entry:
14+
br label %loopheader
15+
16+
loopheader:
17+
%iv = phi i64 [ 0, %entry ], [ %iv_new, %backedge ]
18+
%exit = icmp eq i64 %iv, %end
19+
br i1 %exit, label %for.cond.cleanup.loopexit, label %cont23
20+
21+
for.cond.cleanup.loopexit:
22+
ret void
23+
24+
cont23:
25+
%exitcond241 = icmp eq i64 %iv, 2147483647
26+
br i1 %exitcond241, label %handler.add_overflow, label %backedge
27+
28+
handler.add_overflow:
29+
unreachable
30+
31+
backedge: ; preds = %cont23
32+
%iv_new = add i64 %iv, 1
33+
br label %loopheader, !llvm.loop !0
34+
}
35+
36+
!0 = distinct !{!0, !1}
37+
!1 = !{!"llvm.loop.unroll.full"}

0 commit comments

Comments
 (0)