[PATCH] D114738: Fully delete stale SCEVs from ValuesAtScope map

Philip Reames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 29 13:48:06 PST 2021


reames created this revision.
reames added reviewers: mkazantsev, nikic, fhahn, lebedev.ri.
Herald added subscribers: bollu, hiraditya, mcrosier.
reames requested review of this revision.
Herald added a project: LLVM.

This fixes the regression reported against D111533 <https://reviews.llvm.org/D111533>.  I don't actually believe this is a regression, but trying to message things enough to trigger without that change doesn't seem particular worthwhile either.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114738

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Transforms/IndVarSimplify/bbi-63564.ll


Index: llvm/test/Transforms/IndVarSimplify/bbi-63564.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/IndVarSimplify/bbi-63564.ll
@@ -0,0 +1,58 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes='loop-mssa(indvars,indvars)' -S < %s | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+ at c = external global i16, align 1
+ at a = external global i16, align 1
+
+; When we delete %c.promoted, we need to ensure we clear the getSCEVAtScope
+; cache or we'll crash trying to access loop disposition of the exit value
+; for the remaining IV.
+define void @f() {
+; CHECK-LABEL: @f(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[FOR_COND:%.*]]
+; CHECK:       for.cond:
+; CHECK-NEXT:    br i1 false, label [[FOR_BODY:%.*]], label [[FOR_END4:%.*]]
+; CHECK:       for.body:
+; CHECK-NEXT:    br label [[FOR_BODY2:%.*]]
+; CHECK:       for.body2:
+; CHECK-NEXT:    [[INC2:%.*]] = phi i16 [ undef, [[FOR_BODY]] ], [ [[INC:%.*]], [[FOR_BODY2]] ]
+; CHECK-NEXT:    [[INC]] = add nsw i16 [[INC2]], 1
+; CHECK-NEXT:    store i16 [[INC]], i16* undef, align 1
+; CHECK-NEXT:    br i1 true, label [[FOR_BODY2]], label [[CRIT_EDGE:%.*]]
+; CHECK:       crit_edge:
+; CHECK-NEXT:    [[INC_LCSSA:%.*]] = phi i16 [ [[INC]], [[FOR_BODY2]] ]
+; CHECK-NEXT:    store i16 [[INC_LCSSA]], i16* @a, align 1
+; CHECK-NEXT:    unreachable
+; CHECK:       for.end4:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %for.cond
+
+for.cond:                                         ; preds = %entry
+  br i1 false, label %for.body, label %for.end4
+
+for.body:                                         ; preds = %for.cond
+  %c.promoted = load i16, i16* @c, align 1
+  br label %for.body2
+
+for.body2:                                        ; preds = %for.body2, %for.body
+  %inc33 = phi i16 [ %c.promoted, %for.body ], [ %inc3, %for.body2 ]
+  %inc2 = phi i16 [ undef, %for.body ], [ %inc, %for.body2 ]
+  %inc = add nsw i16 %inc2, 1
+  store i16 %inc, i16* undef, align 1
+  %inc3 = add nsw i16 %inc33, 1
+  %tobool = icmp ne i16 %inc3, 0
+  br i1 %tobool, label %for.body2, label %crit_edge
+
+crit_edge:                                        ; preds = %for.body2
+  %inc.lcssa = phi i16 [ %inc, %for.body2 ]
+  store i16 %inc.lcssa, i16* @a, align 1
+  unreachable
+
+for.end4:                                         ; preds = %for.cond
+  ret void
+}
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -12893,6 +12893,15 @@
   for (auto *S : ToForget)
     forgetMemoizedResultsImpl(S);
 
+  for (auto &KV : ValuesAtScopes) {
+    // We could delete only the entry in the inner array.
+    typedef std::pair<const Loop *, const SCEV *> EntryTy;
+    if (llvm::any_of(KV.second,
+                     [&](const EntryTy &E) {
+                       return ToForget.count(E.second); }))
+      KV.second.clear();
+  }
+
   for (auto I = PredicatedSCEVRewrites.begin();
        I != PredicatedSCEVRewrites.end();) {
     std::pair<const SCEV *, const Loop *> Entry = I->first;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114738.390453.patch
Type: text/x-patch
Size: 3221 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211129/31079e40/attachment.bin>


More information about the llvm-commits mailing list