[llvm] 49d040a - [SCEV] Fix ValuesAtScopesUsers consistency

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 3 01:03:21 PST 2021


Author: Nikita Popov
Date: 2021-12-03T10:03:10+01:00
New Revision: 49d040ac978c60d48710d327d5d8b1bd0c9ad1fb

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

LOG: [SCEV] Fix ValuesAtScopesUsers consistency

Fixes verification failure reported at:
https://reviews.llvm.org/rGc9f9be0381d1

The issue is that getSCEVAtScope() might compute a result without
inserting it in the ValuesAtScopes map in degenerate cases,
specifically if the ValuesAtScopes entry is invalidated during the
calculation. Arguably we should still insert the result if no
existing placeholder is found, but for now just tweak the logic
to only update ValuesAtScopesUsers if ValuesAtScopes is updated.

Added: 
    llvm/test/Analysis/ScalarEvolution/values-at-scopes-consistency.ll

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 7dc7f9904c709..2e2aa76329deb 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -8829,11 +8829,10 @@ const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
   for (auto &LS : reverse(ValuesAtScopes[V]))
     if (LS.first == L) {
       LS.second = C;
+      if (!isa<SCEVConstant>(C))
+        ValuesAtScopesUsers[C].push_back({L, V});
       break;
     }
-
-  if (!isa<SCEVConstant>(C))
-    ValuesAtScopesUsers[C].push_back({L, V});
   return C;
 }
 
@@ -13122,7 +13121,7 @@ void ScalarEvolution::verify() const {
             is_contained(It->second, std::make_pair(L, Value)))
           continue;
         dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: "
-               << ValueAtScope << " missing in ValuesAtScopesUsers\n";
+               << *ValueAtScope << " missing in ValuesAtScopesUsers\n";
         std::abort();
       }
     }
@@ -13139,7 +13138,7 @@ void ScalarEvolution::verify() const {
           is_contained(It->second, std::make_pair(L, ValueAtScope)))
         continue;
       dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: "
-             << ValueAtScope << " missing in ValuesAtScopes\n";
+             << *ValueAtScope << " missing in ValuesAtScopes\n";
       std::abort();
     }
   }

diff  --git a/llvm/test/Analysis/ScalarEvolution/values-at-scopes-consistency.ll b/llvm/test/Analysis/ScalarEvolution/values-at-scopes-consistency.ll
new file mode 100644
index 0000000000000..2aba1c7c64990
--- /dev/null
+++ b/llvm/test/Analysis/ScalarEvolution/values-at-scopes-consistency.ll
@@ -0,0 +1,30 @@
+; RUN: opt -S -passes='verify<scalar-evolution>' < %s
+
+; Make sure this does not fail ValuesAtScopes consistency verification.
+; This used to register a ValuesAtScopes user, even though nothing was
+; added to ValuesAtScope due to a prior invalidation.
+
+define void @main(i8* %p) {
+entry:
+  br label %loop1
+
+loop1:
+  br label %loop2
+
+loop2:
+  %i = phi i64 [ 0, %loop1 ], [ %i.next, %loop2.latch ]
+  %i.next = add nuw nsw i64 %i, 1
+  %gep = getelementptr i8, i8* %p, i64 %i
+  %val = load i8, i8* %gep
+  %c = icmp eq i8 %val, 0
+  br i1 %c, label %loop2.latch, label %exit
+
+loop2.latch:
+  br i1 false, label %loop2, label %loop1.latch
+
+loop1.latch:
+  br label %loop1
+
+exit:
+  ret void
+}


        


More information about the llvm-commits mailing list