Skip to content

Commit 37825ad

Browse files
authored
[LoopUnroll] Prevent LoopFullUnrollPass from performing partial unrolling when trip counts are unknown (llvm#165013)
Currently, `LoopFullUnrollPass` incorrectly performs partial unrolling when `#pragma unroll` is specified and both `TripCount` and `MaxTripCount` are unknown. This patch adds a check to prevent partial unrolling when `OnlyFullUnroll` parameter is true and both trip count values are zero.
1 parent af9a426 commit 37825ad

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,8 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
13271327
}
13281328

13291329
// Do not attempt partial/runtime unrolling in FullLoopUnrolling
1330-
if (OnlyFullUnroll && (UP.Count < TripCount || UP.Count < MaxTripCount)) {
1330+
if (OnlyFullUnroll && ((!TripCount && !MaxTripCount) ||
1331+
UP.Count < TripCount || UP.Count < MaxTripCount)) {
13311332
LLVM_DEBUG(
13321333
dbgs() << "Not attempting partial/runtime unroll in FullLoopUnroll.\n");
13331334
return LoopUnrollResult::Unmodified;

llvm/test/Transforms/LoopUnroll/full-unroll-avoid-partial.ll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,35 @@ for.body: ; preds = %for.body.preheader,
8585
br i1 %exitcond, label %for.body, label %for.cond.cleanup.loopexit, !llvm.loop !3
8686
}
8787

88+
; LOOP-UNROLL-LABEL: Loop Unroll: F[pragma_unroll_count2] Loop %for.body
89+
; LOOP-UNROLL-NEXT: Loop Size = 4
90+
; LOOP-UNROLL-NEXT: Exiting block %for.body: TripCount=0, TripMultiple=1, BreakoutTrip=1
91+
; LOOP-UNROLL-NEXT: Trying runtime unrolling on Loop:
92+
; LOOP-UNROLL-NEXT: Loop at depth 1 containing: %for.body<header><exiting>,%for.cond<latch>
93+
; LOOP-UNROLL-NEXT: Using epilog remainder.
94+
; LOOP-UNROLL-NEXT: Loop latch not terminated by a conditional branch.
95+
; LOOP-UNROLL-NEXT: UNROLLING loop %for.body by 5!
96+
97+
; LOOP-UNROLL-FULL-LABEL: Loop Unroll: F[pragma_unroll_count2] Loop %for.body
98+
; LOOP-UNROLL-FULL-NEXT: Loop Size = 4
99+
; LOOP-UNROLL-FULL-NEXT: Not attempting partial/runtime unroll in FullLoopUnroll
100+
define void @pragma_unroll_count2(i64 %n) {
101+
entry:
102+
br label %for.body
103+
104+
for.body: ; preds = %for.cond, %entry
105+
%i = phi i64 [ 0, %entry ], [ %inc, %for.cond ]
106+
%cmp = icmp ult i64 %i, %n
107+
br i1 %cmp, label %for.cond, label %for.cond.cleanup
108+
109+
for.cond: ; preds = %for.body
110+
%inc = add i64 %i, 8
111+
br label %for.body, !llvm.loop !3
112+
113+
for.cond.cleanup: ; preds = %for.body
114+
ret void
115+
}
116+
88117
; LOOP-UNROLL: llvm.loop.unroll.disable
89118
; LOOP-UNROLL-FULL: llvm.loop.unroll.enable
90119
!0 = !{!"llvm.loop.unroll.enable"}

0 commit comments

Comments
 (0)