Skip to content

Commit 8d186e2

Browse files
authored
[LoopUnroll][NFCI] Clean up remainder followup metadata handling (#165272)
Followup metadata for remainder loops is handled by two implementations, both added by 7244852: 1. `tryToUnrollLoop` in `LoopUnrollPass.cpp`. 2. `CloneLoopBlocks` in `LoopUnrollRuntime.cpp`. As far as I can tell, 2 is useless: I added `assert(!NewLoopID)` for the `NewLoopID` returned by the `makeFollowupLoopID` call, and it never fails throughout check-all for my build. Moreover, if 2 were useful, it appears it would have a bug caused by 7cd826a. That commit skips adding loop metadata to a new remainder loop if the remainder loop itself is to be completely unrolled because it will then no longer be a loop. However, that commit incorrectly assumes that `UnrollRemainder` dictates complete unrolling of a remainder loop, and thus it skips adding loop metadata even if the remainder loop will be only partially unrolled. To avoid further confusion here, this patch removes 2. check-all continues to pass for my build. If 2 actually is useful, please advise so we can create a test that covers that usage. Near 2, this patch retains the `UnrollRemainder` guard on the `setLoopAlreadyUnrolled` call, which adds `llvm.loop.unroll.disable` to the remainder loop. That behavior exists both before and after 7cd826a. The logic appears to be that remainder loop unrolling (whether complete or partial) is opt-in. That is, unless `UnrollRemainder` is true, `UnrollRuntimeLoopRemainder` skips running remainder loop unrolling, and `llvm.loop.unroll.disable` suppresses any later attempt at it. This patch also extends testing of remainder loop followup metadata to be sure remainder loop partial unrolling is handled correctly by 1.
1 parent 57ff891 commit 8d186e2

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -460,25 +460,10 @@ CloneLoopBlocks(Loop *L, Value *NewIter, const bool UseEpilogRemainder,
460460

461461
Loop *NewLoop = NewLoops[L];
462462
assert(NewLoop && "L should have been cloned");
463-
MDNode *LoopID = NewLoop->getLoopID();
464-
465-
// Only add loop metadata if the loop is not going to be completely
466-
// unrolled.
467-
if (UnrollRemainder)
468-
return NewLoop;
469-
470-
std::optional<MDNode *> NewLoopID = makeFollowupLoopID(
471-
LoopID, {LLVMLoopUnrollFollowupAll, LLVMLoopUnrollFollowupRemainder});
472-
if (NewLoopID) {
473-
NewLoop->setLoopID(*NewLoopID);
474-
475-
// Do not setLoopAlreadyUnrolled if loop attributes have been defined
476-
// explicitly.
477-
return NewLoop;
478-
}
479463

480464
// Add unroll disable metadata to disable future unrolling for this loop.
481-
NewLoop->setLoopAlreadyUnrolled();
465+
if (!UnrollRemainder)
466+
NewLoop->setLoopAlreadyUnrolled();
482467
return NewLoop;
483468
}
484469

llvm/test/Transforms/LoopUnroll/followup.ll

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
; RUN: opt < %s -S -passes=loop-unroll -unroll-count=2 | FileCheck %s -check-prefixes=COUNT,COMMON
2-
; RUN: opt < %s -S -passes=loop-unroll -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=EPILOG,COMMON
3-
; RUN: opt < %s -S -passes=loop-unroll -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=PROLOG,COMMON
4-
;
5-
; Check that followup-attributes are applied after LoopUnroll.
1+
; Check that followup attributes are applied after LoopUnroll.
62
;
3+
; We choose -unroll-count=3 because it produces partial unrolling of remainder
4+
; loops. Complete unrolling would leave no remainder loop to which to copy
5+
; followup attributes.
6+
7+
; DEFINE: %{unroll} = opt < %s -S -passes=loop-unroll -unroll-count=3
8+
; DEFINE: %{epilog} = %{unroll} -unroll-runtime -unroll-runtime-epilog=true
9+
; DEFINE: %{prolog} = %{unroll} -unroll-runtime -unroll-runtime-epilog=false
10+
; DEFINE: %{fc} = FileCheck %s -check-prefixes
11+
12+
; RUN: %{unroll} | %{fc} COMMON,COUNT
13+
; RUN: %{epilog} | %{fc} COMMON,EPILOG,EPILOG-NO-UNROLL
14+
; RUN: %{prolog} | %{fc} COMMON,PROLOG,PROLOG-NO-UNROLL
15+
; RUN: %{epilog} -unroll-remainder | %{fc} COMMON,EPILOG,EPILOG-UNROLL
16+
; RUN: %{prolog} -unroll-remainder | %{fc} COMMON,PROLOG,PROLOG-UNROLL
17+
718
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
819

920
define i32 @test(ptr nocapture %a, i32 %n) nounwind uwtable readonly {
@@ -36,15 +47,17 @@ for.end: ; preds = %for.body, %entry
3647
; COMMON-LABEL: @test(
3748

3849

39-
; COUNT: br i1 %exitcond.1, label %for.end.loopexit, label %for.body, !llvm.loop ![[LOOP:[0-9]+]]
50+
; COUNT: br i1 %exitcond.2, label %for.end.loopexit, label %for.body, !llvm.loop ![[LOOP:[0-9]+]]
4051

4152
; COUNT: ![[FOLLOWUP_ALL:[0-9]+]] = !{!"FollowupAll"}
4253
; COUNT: ![[FOLLOWUP_UNROLLED:[0-9]+]] = !{!"FollowupUnrolled"}
4354
; COUNT: ![[LOOP]] = distinct !{![[LOOP]], ![[FOLLOWUP_ALL]], ![[FOLLOWUP_UNROLLED]]}
4455

4556

46-
; EPILOG: br i1 %niter.ncmp.7, label %for.end.loopexit.unr-lcssa, label %for.body, !llvm.loop ![[LOOP_0:[0-9]+]]
47-
; EPILOG: br i1 %epil.iter.cmp, label %for.body.epil, label %for.end.loopexit.epilog-lcssa, !llvm.loop ![[LOOP_2:[0-9]+]]
57+
; EPILOG: br i1 %niter.ncmp.2, label %for.end.loopexit.unr-lcssa, label %for.body, !llvm.loop ![[LOOP_0:[0-9]+]]
58+
; EPILOG-NO-UNROLL: br i1 %epil.iter.cmp, label %for.body.epil, label %for.end.loopexit.epilog-lcssa, !llvm.loop ![[LOOP_2:[0-9]+]]
59+
; EPILOG-UNROLL: br i1 %epil.iter.cmp, label %for.body.epil.1, label %for.end.loopexit.epilog-lcssa
60+
; EPILOG-UNROLL: br i1 %epil.iter.cmp.1, label %for.body.epil, label %for.end.loopexit.epilog-lcssa, !llvm.loop ![[LOOP_2:[0-9]+]]
4861

4962
; EPILOG: ![[LOOP_0]] = distinct !{![[LOOP_0]], ![[FOLLOWUP_ALL:[0-9]+]], ![[FOLLOWUP_UNROLLED:[0-9]+]]}
5063
; EPILOG: ![[FOLLOWUP_ALL]] = !{!"FollowupAll"}
@@ -53,8 +66,10 @@ for.end: ; preds = %for.body, %entry
5366
; EPILOG: ![[FOLLOWUP_REMAINDER]] = !{!"FollowupRemainder"}
5467

5568

56-
; PROLOG: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit.unr-lcssa, !llvm.loop ![[LOOP_0:[0-9]+]]
57-
; PROLOG: br i1 %exitcond.7, label %for.end.loopexit.unr-lcssa, label %for.body, !llvm.loop ![[LOOP_2:[0-9]+]]
69+
; PROLOG-UNROLL: br i1 %prol.iter.cmp, label %for.body.prol.1, label %for.body.prol.loopexit.unr-lcssa
70+
; PROLOG-UNROLL: br i1 %prol.iter.cmp.1, label %for.body.prol, label %for.body.prol.loopexit.unr-lcssa, !llvm.loop ![[LOOP_0:[0-9]+]]
71+
; PROLOG-NO-UNROLL: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit.unr-lcssa, !llvm.loop ![[LOOP_0:[0-9]+]]
72+
; PROLOG: br i1 %exitcond.2, label %for.end.loopexit.unr-lcssa, label %for.body, !llvm.loop ![[LOOP_2:[0-9]+]]
5873

5974
; PROLOG: ![[LOOP_0]] = distinct !{![[LOOP_0]], ![[FOLLOWUP_ALL:[0-9]+]], ![[FOLLOWUP_REMAINDER:[0-9]+]]}
6075
; PROLOG: ![[FOLLOWUP_ALL]] = !{!"FollowupAll"}

0 commit comments

Comments
 (0)