[llvm] aaaeb4b - [SCEV] Use mustprogress flag on loops (in addition to function attribute)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 10 13:20:35 PDT 2021


Author: Philip Reames
Date: 2021-06-10T13:20:28-07:00
New Revision: aaaeb4b160fe94e0ad3bcd6073eea4807f84a33a

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

LOG: [SCEV] Use mustprogress flag on loops (in addition to function attribute)

This addresses a performance regression reported against 3c6e4191.  That change (correctly) limited a transform based on assumed finiteness to mustprogress loops, but the previous change (38540d7) which introduced the mustprogress check utility only handled function attributes, not the loop metadata form.

It turns out that clang uses the function attribute form for C++, and the loop metadata form for C.  As a result, 3c6e4191 ended up being a large regression in practice for C code as loops weren't being considered mustprogress despite the language semantics.

Added: 
    

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp
    llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 70191f1e2612..06aa6cb49ae3 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -6579,8 +6579,8 @@ ScalarEvolution::getLoopProperties(const Loop *L) {
 }
 
 bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) {
-  // TODO: Use the loop metadata form of mustprogress as well.
-  if (!L->getHeader()->getParent()->mustProgress())
+  if (!L->getHeader()->getParent()->mustProgress() &&
+      !hasMustProgress(L))
     return false;
 
   // A loop without side effects must be finite.

diff  --git a/llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll b/llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll
index 7d41a1f3e546..6c599104dbbc 100644
--- a/llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll
+++ b/llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll
@@ -82,3 +82,29 @@ for.body:                                         ; preds = %entry, %for.body
 for.end:                                          ; preds = %for.body, %entry
   ret void
 }
+
+; Same as foo2, but with mustprogress on loop, not function
+; CHECK: Determining loop execution counts for: @foo4
+; CHECK: backedge-taken count is ((-1 + (%n smax %s)) /u %s)
+; CHECK: max backedge-taken count is -1
+
+define void @foo4(i32* nocapture %A, i32 %n, i32 %s) {
+entry:
+  br label %for.body
+
+for.body:                                         ; preds = %entry, %for.body
+  %i.05 = phi i32 [ %add, %for.body ], [ 0, %entry ]
+  %arrayidx = getelementptr inbounds i32, i32* %A, i32 %i.05
+  %0 = load i32, i32* %arrayidx, align 4
+  %inc = add nsw i32 %0, 1
+  store i32 %inc, i32* %arrayidx, align 4
+  %add = add nsw i32 %i.05, %s
+  %cmp = icmp slt i32 %add, %n
+  br i1 %cmp, label %for.body, label %for.end, !llvm.loop !8
+
+for.end:                                          ; preds = %for.body, %entry
+  ret void
+}
+
+!8 = distinct !{!8, !9}
+!9 = !{!"llvm.loop.mustprogress"}


        


More information about the llvm-commits mailing list