[llvm] [LLVM][SLSR] Add a debug counter #119770 (PR #119981)

via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 14 11:04:30 PST 2024


https://github.com/GrumpyPigSkin created https://github.com/llvm/llvm-project/pull/119981

Added debug counter and test for slsr pass

fixes: https://github.com/llvm/llvm-project/issues/119770

>From 9c2ca90ac2c5cf97bece6094b3e32445c3a31beb Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Sat, 14 Dec 2024 18:56:05 +0000
Subject: [PATCH] Added debug counter and new test

---
 .../Scalar/StraightLineStrengthReduce.cpp     |  7 ++++-
 llvm/test/Other/debugcounter-slsr.ll          | 30 +++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Other/debugcounter-slsr.ll

diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
index 75585fcc802667..4d4bea3113cec7 100644
--- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
@@ -78,6 +78,7 @@
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/DebugCounter.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Transforms/Utils/Local.h"
@@ -93,6 +94,9 @@ using namespace PatternMatch;
 static const unsigned UnknownAddressSpace =
     std::numeric_limits<unsigned>::max();
 
+DEBUG_COUNTER(StraightLineStrengthReduceCounter, "slsr-counter",
+              "Controls whether rewriteCandidateWithBasis is executed.");
+
 namespace {
 
 class StraightLineStrengthReduceLegacyPass : public FunctionPass {
@@ -690,7 +694,8 @@ bool StraightLineStrengthReduce::runOnFunction(Function &F) {
   while (!Candidates.empty()) {
     const Candidate &C = Candidates.back();
     if (C.Basis != nullptr) {
-      rewriteCandidateWithBasis(C, *C.Basis);
+      if (DebugCounter::shouldExecute(StraightLineStrengthReduceCounter))
+        rewriteCandidateWithBasis(C, *C.Basis);
     }
     Candidates.pop_back();
   }
diff --git a/llvm/test/Other/debugcounter-slsr.ll b/llvm/test/Other/debugcounter-slsr.ll
new file mode 100644
index 00000000000000..facfc38f9e0c54
--- /dev/null
+++ b/llvm/test/Other/debugcounter-slsr.ll
@@ -0,0 +1,30 @@
+; REQUIRES: asserts
+; RUN: opt -passes=slsr,gvn -S -debug-counter=slsr-counter=1  < %s | FileCheck %s
+
+;; Test that, with debug counters on, we will skip the first slsr opportunity.
+
+define void @stride_is_2s(i32 %b, i32 %s) {
+; CHECK-LABEL: @stride_is_2s(
+; CHECK-NEXT:    %s2 = shl i32 %s, 1
+; CHECK-NEXT:    %t1 = add i32 %b, %s2
+; CHECK-NEXT:    call void @foo(i32 %t1)
+; CHECK-NEXT:    %t2 = add i32 %t1, %s2
+; CHECK-NEXT:    call void @foo(i32 %t2)
+; CHECK-NEXT:    %s6 = mul i32 %s, 6
+; CHECK-NEXT:    %t3 = add i32 %b, %s6
+; CHECK-NEXT:    call void @foo(i32 %t3)
+; CHECK-NEXT:    ret void
+;
+  %s2 = shl i32 %s, 1
+  %t1 = add i32 %b, %s2
+  call void @foo(i32 %t1)
+  %s4 = shl i32 %s, 2
+  %t2 = add i32 %b, %s4
+  call void @foo(i32 %t2)
+  %s6 = mul i32 %s, 6
+  %t3 = add i32 %b, %s6
+  call void @foo(i32 %t3)
+  ret void
+}
+
+declare void @foo(i32)
\ No newline at end of file



More information about the llvm-commits mailing list