[llvm] [SLSR] Add a statistic counting candidate-basis SCEV differences (NFC) (PR #217143)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 18 15:58:57 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-nvptx

Author: Justin Fargnoli (justinfargnoli)

<details>
<summary>Changes</summary>

In worst-case O(n^2) traversal scenarios, `getMinusSCEV()` can cause long compile times. 

Add a statistic to track `getMinusSCEV()` calls to help debug these scenarios. 

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


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp (+6) 
- (added) llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll (+29) 


``````````diff
diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
index 8826127dd7ae3..b1f1f6efc59b1 100644
--- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
@@ -73,6 +73,7 @@
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/Statistic.h"
 #include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
@@ -120,6 +121,9 @@ static cl::opt<bool>
     EnablePoisonReuseGuard("enable-poison-reuse-guard", cl::init(true),
                            cl::desc("Enable poison-reuse guard"));
 
+STATISTIC(NumSCEVCandidateBasisDifferences,
+          "Number of candidate-basis SCEV differences computed by SLSR");
+
 namespace {
 
 class StraightLineStrengthReduceLegacyPass : public FunctionPass {
@@ -691,6 +695,7 @@ Value *StraightLineStrengthReduce::getDelta(const Candidate &C,
     const SCEV *BasisPart =
         (K == Candidate::BaseDelta) ? Basis.Base : Basis.StrideSCEV;
     const SCEV *CandPart = (K == Candidate::BaseDelta) ? C.Base : C.StrideSCEV;
+    ++NumSCEVCandidateBasisDifferences;
     const SCEV *Diff = SE->getMinusSCEV(CandPart, BasisPart);
     return getNearestValueOfSCEV(Diff, C.Ins);
   }
@@ -906,6 +911,7 @@ auto StraightLineStrengthReduce::compressPath(Candidate &C,
                                 cast<GetElementPtrInst>(NextRoot->Ins), DL))
       break;
 
+    ++NumSCEVCandidateBasisDifferences;
     if (auto DeltaVal =
             dyn_cast<SCEVConstant>(SE->getMinusSCEV(CandPart, BasisPart))) {
       Root = NextRoot;
diff --git a/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
new file mode 100644
index 0000000000000..ddd1bf4748f5d
--- /dev/null
+++ b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
@@ -0,0 +1,29 @@
+; 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
+
+target triple = "nvptx64-nvidia-cuda"
+
+declare i64 @source(i32)
+declare void @use(ptr)
+
+define void @base_delta(ptr %root, i64 %common) {
+  %offset.0 = call i64 @source(i32 0)
+  %base.0 = getelementptr i8, ptr %root, i64 %offset.0
+  %candidate.0 = getelementptr i8, ptr %base.0, i64 %common
+  call void @use(ptr %candidate.0)
+
+  %delta.1 = call i64 @source(i32 1)
+  %offset.1 = add i64 %offset.0, %delta.1
+  %base.1 = getelementptr i8, ptr %root, i64 %offset.1
+  %candidate.1 = getelementptr i8, ptr %base.1, i64 %common
+  call void @use(ptr %candidate.1)
+
+  %delta.2 = call i64 @source(i32 2)
+  %offset.2 = add i64 %offset.1, %delta.2
+  %base.2 = getelementptr i8, ptr %root, i64 %offset.2
+  %candidate.2 = getelementptr i8, ptr %base.2, i64 %common
+  call void @use(ptr %candidate.2)
+  ret void
+}

``````````

</details>


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


More information about the llvm-commits mailing list