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

Justin Fargnoli via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 19 09:09:16 PDT 2026


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

>From e48a2f0bde5bd131144d99860fa6faaa00769ae5 Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Tue, 18 Aug 2026 21:36:17 +0000
Subject: [PATCH 1/5] [SLSR] Count SCEV candidate differences

---
 .../Scalar/StraightLineStrengthReduce.cpp     |  5 +++++
 .../NVPTX/scev-candidate-differences.ll       | 22 +++++++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll

diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
index 8826127dd7ae3..072575fe6d8ee 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(NumSCEVCandidateDifferences,
+          "Number of SCEV candidate 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;
+    ++NumSCEVCandidateDifferences;
     const SCEV *Diff = SE->getMinusSCEV(CandPart, BasisPart);
     return getNearestValueOfSCEV(Diff, C.Ins);
   }
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..08f1d5c473494
--- /dev/null
+++ b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
@@ -0,0 +1,22 @@
+; REQUIRES: asserts
+; RUN: opt -mtriple=nvptx64-nvidia-cuda -mcpu=sm_100 -passes=slsr -stats \
+; RUN:   -disable-output < %s 2>&1 | FileCheck %s
+
+; CHECK: 2 slsr - Number of SCEV candidate differences computed by SLSR
+
+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)
+  ret void
+}

>From bb6adbedaa7f4bff34aa8475103b8f67b011203b Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Tue, 18 Aug 2026 21:57:57 +0000
Subject: [PATCH 2/5] [SLSR] Count path compression SCEV differences

---
 .../Transforms/Scalar/StraightLineStrengthReduce.cpp  |  1 +
 .../NVPTX/scev-candidate-differences.ll               | 11 ++++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
index 072575fe6d8ee..be6ca3619784b 100644
--- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
@@ -911,6 +911,7 @@ auto StraightLineStrengthReduce::compressPath(Candidate &C,
                                 cast<GetElementPtrInst>(NextRoot->Ins), DL))
       break;
 
+    ++NumSCEVCandidateDifferences;
     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
index 08f1d5c473494..cbaa09a40e2d1 100644
--- a/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
+++ b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
@@ -2,7 +2,10 @@
 ; RUN: opt -mtriple=nvptx64-nvidia-cuda -mcpu=sm_100 -passes=slsr -stats \
 ; RUN:   -disable-output < %s 2>&1 | FileCheck %s
 
-; CHECK: 2 slsr - Number of SCEV candidate differences computed by SLSR
+; The third candidate uses the second as its basis, then compressPath examines
+; the deeper basis. Count both basis selection and path compression.
+;
+; CHECK: 8 slsr - Number of SCEV candidate differences computed by SLSR
 
 declare i64 @source(i32)
 declare void @use(ptr)
@@ -18,5 +21,11 @@ define void @base_delta(ptr %root, i64 %common) {
   %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
 }

>From 655887eb07e5fb03d357d3fd19a23ccefd725b72 Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Tue, 18 Aug 2026 22:04:43 +0000
Subject: [PATCH 3/5] [SLSR] Clarify candidate-basis statistic terminology

---
 llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp | 8 ++++----
 .../NVPTX/scev-candidate-differences.ll                   | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
index be6ca3619784b..b1f1f6efc59b1 100644
--- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
@@ -121,8 +121,8 @@ static cl::opt<bool>
     EnablePoisonReuseGuard("enable-poison-reuse-guard", cl::init(true),
                            cl::desc("Enable poison-reuse guard"));
 
-STATISTIC(NumSCEVCandidateDifferences,
-          "Number of SCEV candidate differences computed by SLSR");
+STATISTIC(NumSCEVCandidateBasisDifferences,
+          "Number of candidate-basis SCEV differences computed by SLSR");
 
 namespace {
 
@@ -695,7 +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;
-    ++NumSCEVCandidateDifferences;
+    ++NumSCEVCandidateBasisDifferences;
     const SCEV *Diff = SE->getMinusSCEV(CandPart, BasisPart);
     return getNearestValueOfSCEV(Diff, C.Ins);
   }
@@ -911,7 +911,7 @@ auto StraightLineStrengthReduce::compressPath(Candidate &C,
                                 cast<GetElementPtrInst>(NextRoot->Ins), DL))
       break;
 
-    ++NumSCEVCandidateDifferences;
+    ++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
index cbaa09a40e2d1..8f22f38668763 100644
--- a/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
+++ b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
@@ -5,7 +5,7 @@
 ; The third candidate uses the second as its basis, then compressPath examines
 ; the deeper basis. Count both basis selection and path compression.
 ;
-; CHECK: 8 slsr - Number of SCEV candidate differences computed by SLSR
+; CHECK: 8 slsr - Number of candidate-basis SCEV differences computed by SLSR
 
 declare i64 @source(i32)
 declare void @use(ptr)

>From 1111b00bca1e0c8a287aea0604f888ac6d9c0948 Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Tue, 18 Aug 2026 22:09:07 +0000
Subject: [PATCH 4/5] [SLSR] Encode NVPTX target in statistic test

---
 .../NVPTX/scev-candidate-differences.ll                      | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
index 8f22f38668763..abccd07f3e129 100644
--- a/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
+++ b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
@@ -1,12 +1,13 @@
 ; REQUIRES: asserts
-; RUN: opt -mtriple=nvptx64-nvidia-cuda -mcpu=sm_100 -passes=slsr -stats \
-; RUN:   -disable-output < %s 2>&1 | FileCheck %s
+; RUN: opt -passes=slsr -stats -disable-output < %s 2>&1 | FileCheck %s
 
 ; The third candidate uses the second as its basis, then compressPath examines
 ; the deeper basis. Count both basis selection and path compression.
 ;
 ; 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)
 

>From fd90844bb294f1ee20975db97f35a014eac1722a Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Tue, 18 Aug 2026 22:11:01 +0000
Subject: [PATCH 5/5] [SLSR] Remove redundant statistic test comment

---
 .../NVPTX/scev-candidate-differences.ll                        | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
index abccd07f3e129..ddd1bf4748f5d 100644
--- a/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
+++ b/llvm/test/Transforms/StraightLineStrengthReduce/NVPTX/scev-candidate-differences.ll
@@ -1,9 +1,6 @@
 ; REQUIRES: asserts
 ; RUN: opt -passes=slsr -stats -disable-output < %s 2>&1 | FileCheck %s
 
-; The third candidate uses the second as its basis, then compressPath examines
-; the deeper basis. Count both basis selection and path compression.
-;
 ; CHECK: 8 slsr - Number of candidate-basis SCEV differences computed by SLSR
 
 target triple = "nvptx64-nvidia-cuda"



More information about the llvm-commits mailing list