[llvm] [SLSR] Skip candidate-basis differences when `SCEVUnknown`s differ (PR #217451)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 19 15:58:31 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-nvptx

Author: Justin Fargnoli (justinfargnoli)

<details>
<summary>Changes</summary>

After SLSR finds a non-constant delta for a candidate, it will continue searching for a constant delta. However, when the set of `SCEVUnknown`s differs between a candidate and a basis, the offset cannot be constant. Thus, prune that candidate and avoid an expensive, fruitless call to `getSCEVMinus()`. 

---
Full diff: https://github.com/llvm/llvm-project/pull/217451.diff


6 Files Affected:

- (modified) llvm/include/llvm/Analysis/ScalarEvolution.h (+8) 
- (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+27) 
- (modified) llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp (+12) 
- (modified) llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll (+1-1) 
- (added) llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-unknown-filtering.ll (+34) 
- (modified) llvm/unittests/Analysis/ScalarEvolutionTest.cpp (+23) 


``````````diff
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index d17c21ea3401e..9c849b5a241b8 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -704,6 +704,10 @@ class ScalarEvolution {
   /// scAddRecExpr. The result will be cached in HasRecMap.
   LLVM_ABI bool containsAddRecurrence(const SCEV *S);
 
+  /// Return whether \p A and \p B contain the same set of SCEVUnknowns.
+  /// Results are cached in SCEVUnknownsCache.
+  LLVM_ABI bool hasSameSCEVUnknowns(const SCEV *A, const SCEV *B);
+
   /// Is operation \p BinOp between \p LHS and \p RHS provably does not have
   /// a signed/unsigned overflow (\p Signed)? If \p CtxI is specified, the
   /// no-overflow fact should be true in the context of this instruction.
@@ -1705,6 +1709,10 @@ class ScalarEvolution {
   /// This is a cache to record whether a SCEV contains any scAddRecExpr.
   HasRecMapType HasRecMap;
 
+  using SCEVUnknownSet = SmallPtrSet<const SCEVUnknown *, 4>;
+
+  DenseMap<const SCEV *, SCEVUnknownSet> SCEVUnknownsCache;
+
   /// The type for ExprValueMap.
   using ValueSetVector = SmallSetVector<Value *, 4>;
   using ExprValueMapType = DenseMap<const SCEV *, ValueSetVector>;
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 35d650377d719..395b63a91edbc 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -4582,6 +4582,30 @@ bool ScalarEvolution::containsAddRecurrence(const SCEV *S) {
   return FoundAddRec;
 }
 
+bool ScalarEvolution::hasSameSCEVUnknowns(const SCEV *A, const SCEV *B) {
+  auto CacheUnknowns = [&](const SCEV *Root) {
+    auto [It, Inserted] = SCEVUnknownsCache.try_emplace(Root);
+    if (!Inserted)
+      return;
+
+    struct Collector {
+      SCEVUnknownSet &Unknowns;
+
+      bool follow(const SCEV *S) {
+        if (auto *Unknown = dyn_cast<SCEVUnknown>(S))
+          Unknowns.insert(Unknown);
+        return true;
+      }
+      bool isDone() const { return false; }
+    } C{It->second};
+    visitAll(Root, C);
+  };
+  CacheUnknowns(A);
+  CacheUnknowns(B);
+
+  return SCEVUnknownsCache.find(A)->second == SCEVUnknownsCache.find(B)->second;
+}
+
 /// Return the ValueOffsetPair set for \p S. \p S can be represented
 /// by the value and offset from any ValueOffsetPair in the set.
 ArrayRef<Value *> ScalarEvolution::getSCEVValues(const SCEV *S) {
@@ -8687,6 +8711,7 @@ void ScalarEvolution::forgetAllLoops() {
   SignedRanges.clear();
   ExprValueMap.clear();
   HasRecMap.clear();
+  SCEVUnknownsCache.clear();
   ConstantMultipleCache.clear();
   PredicatedSCEVRewrites.clear();
   FoldCache.clear();
@@ -14111,6 +14136,7 @@ ScalarEvolution::~ScalarEvolution() {
   ExprValueMap.clear();
   ValueExprMap.clear();
   HasRecMap.clear();
+  SCEVUnknownsCache.clear();
   BackedgeTakenCounts.clear();
   PredicatedBackedgeTakenCounts.clear();
 
@@ -14658,6 +14684,7 @@ void ScalarEvolution::forgetMemoizedResultsImpl(const SCEV *S) {
   UnsignedRanges.erase(S);
   SignedRanges.erase(S);
   HasRecMap.erase(S);
+  SCEVUnknownsCache.erase(S);
   ConstantMultipleCache.erase(S);
 
   if (auto *AR = dyn_cast<SCEVAddRecExpr>(S)) {
diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
index b1f1f6efc59b1..ea3bebf2ffd5c 100644
--- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
@@ -731,6 +731,18 @@ bool StraightLineStrengthReduce::candidatePredicate(Candidate *Basis,
   if (!isSimilar(C, *Basis, K))
     return false;
 
+  // Once a reusable delta is found, only a constant delta can improve it.
+  // Different symbolic leaves cannot cancel to a constant, so such a basis
+  // cannot improve C. Skip it and continue searching older candidates.
+  if (C.Delta && K != Candidate::IndexDelta) {
+    const SCEV *CandidateSCEV =
+        K == Candidate::BaseDelta ? C.Base : C.StrideSCEV;
+    const SCEV *BasisSCEV =
+        K == Candidate::BaseDelta ? Basis->Base : Basis->StrideSCEV;
+    if (!SE->hasSameSCEVUnknowns(CandidateSCEV, BasisSCEV))
+      return false;
+  }
+
   assert(DT->dominates(Basis->Ins, C.Ins));
   Value *Delta = getDelta(C, *Basis, K);
   if (!Delta)
diff --git a/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
index ddd1bf4748f5d..ba015e72c4e51 100644
--- a/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
+++ b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
@@ -1,7 +1,7 @@
 ; REQUIRES: asserts
 ; RUN: opt -passes=slsr -stats -disable-output < %s 2>&1 | FileCheck %s
 
-; CHECK: 8 slsr - Number of candidate-basis SCEV differences computed by SLSR
+; CHECK: 6 slsr - Number of candidate-basis SCEV differences computed by SLSR
 
 target triple = "nvptx64-nvidia-cuda"
 
diff --git a/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-unknown-filtering.ll b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-unknown-filtering.ll
new file mode 100644
index 0000000000000..f52d88f2f761d
--- /dev/null
+++ b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-unknown-filtering.ll
@@ -0,0 +1,34 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; REQUIRES: asserts
+; RUN: opt -passes=slsr -stats -S < %s 2> %t.stats | FileCheck %s
+; RUN: FileCheck %s --check-prefix=STATS < %t.stats
+
+; For %candidate, %var.delta.basis first provides the variable delta %idx.
+; %other.basis cannot provide a constant delta because it has different
+; SCEVUnknown leaves. Skip it, then continue to %const.delta.basis and use its
+; constant delta. Without the leaf check, SLSR computes four candidate-basis
+; differences instead of three.
+;
+; STATS: 3 slsr - Number of candidate-basis SCEV differences computed by SLSR
+
+target triple = "nvptx64-nvidia-cuda"
+
+define ptr @different_unknowns(ptr %root, i64 %idx, i64 %other) {
+; CHECK-LABEL: define ptr @different_unknowns(
+; CHECK-SAME: ptr [[ROOT:%.*]], i64 [[IDX:%.*]], i64 [[OTHER:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[IDX_MINUS_4:%.*]] = sub i64 [[IDX]], 4
+; CHECK-NEXT:    [[CONST_DELTA_BASIS:%.*]] = getelementptr i8, ptr [[ROOT]], i64 [[IDX_MINUS_4]]
+; CHECK-NEXT:    [[OTHER_BASIS:%.*]] = getelementptr i8, ptr [[ROOT]], i64 [[OTHER]]
+; CHECK-NEXT:    [[VAR_DELTA_BASIS:%.*]] = getelementptr i8, ptr [[ROOT]], i64 0
+; CHECK-NEXT:    [[CANDIDATE:%.*]] = getelementptr i8, ptr [[CONST_DELTA_BASIS]], i64 4
+; CHECK-NEXT:    ret ptr [[CANDIDATE]]
+;
+entry:
+  %idx.minus.4 = sub i64 %idx, 4
+  %const.delta.basis = getelementptr i8, ptr %root, i64 %idx.minus.4
+  %other.basis = getelementptr i8, ptr %root, i64 %other
+  %var.delta.basis = getelementptr i8, ptr %root, i64 0
+  %candidate = getelementptr i8, ptr %root, i64 %idx
+  ret ptr %candidate
+}
diff --git a/llvm/unittests/Analysis/ScalarEvolutionTest.cpp b/llvm/unittests/Analysis/ScalarEvolutionTest.cpp
index a94952c3d0c44..decbc158a0e0b 100644
--- a/llvm/unittests/Analysis/ScalarEvolutionTest.cpp
+++ b/llvm/unittests/Analysis/ScalarEvolutionTest.cpp
@@ -125,6 +125,29 @@ TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) {
   EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V0);
 }
 
+TEST_F(ScalarEvolutionsTest, SameSCEVUnknowns) {
+  Type *Ty = Type::getInt64Ty(Context);
+  FunctionType *FTy =
+      FunctionType::get(Type::getVoidTy(Context), {Ty, Ty}, false);
+  Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
+  BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
+  ReturnInst::Create(Context, nullptr, BB);
+
+  ScalarEvolution SE = buildSE(*F);
+
+  const SCEV *S0 = SE.getSCEV(F->getArg(0));
+  const SCEV *S1 = SE.getSCEV(F->getArg(1));
+  const SCEV *One = SE.getOne(Ty);
+  const SCEV *Two = SE.getConstant(Ty, 2);
+  const SCEV *S0PlusOne = SE.getAddExpr(S0, One);
+  const SCEV *S0PlusTwo = SE.getAddExpr(S0, Two);
+  const SCEV *S0PlusS1 = SE.getAddExpr(S0, S1);
+
+  EXPECT_TRUE(SE.hasSameSCEVUnknowns(S0PlusOne, S0PlusTwo));
+  EXPECT_FALSE(SE.hasSameSCEVUnknowns(S0PlusOne, S0PlusS1));
+  EXPECT_TRUE(SE.hasSameSCEVUnknowns(One, Two));
+}
+
 TEST_F(ScalarEvolutionsTest, SimplifiedPHI) {
   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
                                               std::vector<Type *>(), false);

``````````

</details>


https://github.com/llvm/llvm-project/pull/217451


More information about the llvm-commits mailing list