[llvm] 80304c5 - [LoopUnroll] Always respect user unroll pragma

Whitney Tsang via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 11 11:33:31 PDT 2022


Author: Whitney Tsang
Date: 2022-04-11T14:33:24-04:00
New Revision: 80304c5f88f02cb189f6777313fdea146fcc595a

URL: https://github.com/llvm/llvm-project/commit/80304c5f88f02cb189f6777313fdea146fcc595a
DIFF: https://github.com/llvm/llvm-project/commit/80304c5f88f02cb189f6777313fdea146fcc595a.diff

LOG: [LoopUnroll] Always respect user unroll pragma

IMO when user provide unroll pragma, compiler should always respect it.
It is not clear to me why loop unroll pass currently ensure that the
unrolled loop size is limited by PragmaUnrollThreshold.

Reviewed By: Meinersbur

Differential Revision: https://reviews.llvm.org/D119148

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
    llvm/test/Transforms/LoopUnroll/unroll-pragmas.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 25cb9a9958338..9a3cd07a7f98c 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -787,15 +787,13 @@ shouldPragmaUnroll(Loop *L, const PragmaInfo &PInfo,
 
   // 2nd priority is unroll count set by pragma.
   if (PInfo.PragmaCount > 0) {
-    if ((UP.AllowRemainder || (TripMultiple % PInfo.PragmaCount == 0)) &&
-        UCE.getUnrolledLoopSize(UP, PInfo.PragmaCount) < PragmaUnrollThreshold)
+    if ((UP.AllowRemainder || (TripMultiple % PInfo.PragmaCount == 0)))
       return PInfo.PragmaCount;
   }
 
-  if (PInfo.PragmaFullUnroll && TripCount != 0) {
-    if (UCE.getUnrolledLoopSize(UP, TripCount) < PragmaUnrollThreshold)
-      return TripCount;
-  }
+  if (PInfo.PragmaFullUnroll && TripCount != 0)
+    return TripCount;
+
   // if didn't return until here, should continue to other priorties
   return None;
 }

diff  --git a/llvm/test/Transforms/LoopUnroll/unroll-pragmas.ll b/llvm/test/Transforms/LoopUnroll/unroll-pragmas.ll
index afc70fbbe13aa..e19491beb272f 100644
--- a/llvm/test/Transforms/LoopUnroll/unroll-pragmas.ll
+++ b/llvm/test/Transforms/LoopUnroll/unroll-pragmas.ll
@@ -235,33 +235,6 @@ for.end:                                          ; preds = %for.body
 !10 = !{!10, !11}
 !11 = !{!"llvm.loop.unroll.count", i32 1}
 
-; #pragma clang loop unroll(full)
-; Loop has very high loop count (1 million) and full unrolling was requested.
-; Loop should unrolled up to the pragma threshold, but not completely.
-;
-; CHECK-LABEL: @unroll_1M(
-; CHECK: store i32
-; CHECK: store i32
-; CHECK: br i1
-define void @unroll_1M(i32* nocapture %a, i32 %b) {
-entry:
-  br label %for.body
-
-for.body:                                         ; preds = %for.body, %entry
-  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
-  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
-  %0 = load i32, i32* %arrayidx, align 4
-  %inc = add nsw i32 %0, 1
-  store i32 %inc, i32* %arrayidx, align 4
-  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
-  %exitcond = icmp eq i64 %indvars.iv.next, 1000000
-  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !12
-
-for.end:                                          ; preds = %for.body
-  ret void
-}
-!12 = !{!12, !4}
-
 ; #pragma clang loop unroll(enable)
 ; Loop should be fully unrolled.
 ;
@@ -279,13 +252,13 @@ for.body:                                         ; preds = %for.body, %entry
   store i32 %inc, i32* %arrayidx, align 4
   %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
   %exitcond = icmp eq i64 %indvars.iv.next, 64
-  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !13
+  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !12
 
 for.end:                                          ; preds = %for.body
   ret void
 }
-!13 = !{!13, !14}
-!14 = !{!"llvm.loop.unroll.enable"}
+!12 = !{!12, !13}
+!13 = !{!"llvm.loop.unroll.enable"}
 
 ; #pragma clang loop unroll(enable)
 ; Loop has a runtime trip count and should be runtime unrolled and duplicated
@@ -324,12 +297,12 @@ for.body:                                         ; preds = %entry, %for.body
   %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
   %lftr.wideiv = trunc i64 %indvars.iv.next to i32
   %exitcond = icmp eq i32 %lftr.wideiv, %b
-  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !15
+  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !14
 
 for.end:                                          ; preds = %for.body, %entry
   ret void
 }
-!15 = !{!15, !14}
+!14 = !{!14, !13}
 
 ; #pragma clang loop unroll_count(3)
 ; Loop has a runtime trip count.  Runtime unrolling should occur and loop
@@ -363,10 +336,10 @@ for.body:                                         ; preds = %entry, %for.body
   %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
   %lftr.wideiv = trunc i64 %indvars.iv.next to i32
   %exitcond = icmp eq i32 %lftr.wideiv, %b
-  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !16
+  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !15
 
 for.end:                                          ; preds = %for.body, %entry
   ret void
 }
-!16 = !{!16, !17}
-!17 = !{!"llvm.loop.unroll.count", i32 3}
+!15 = !{!15, !16}
+!16 = !{!"llvm.loop.unroll.count", i32 3}


        


More information about the llvm-commits mailing list