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

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 18 22:58:48 PST 2024


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

>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 1/8] 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

>From 4c9ac9f959a6ef864a663a9acdf1e52d21f062ff Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Tue, 17 Dec 2024 13:29:16 +0000
Subject: [PATCH 2/8] Implemented code review comments

---
 llvm/test/Other/debugcounter-slsr.ll | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/llvm/test/Other/debugcounter-slsr.ll b/llvm/test/Other/debugcounter-slsr.ll
index facfc38f9e0c54..3b3ad0649dcc9a 100644
--- a/llvm/test/Other/debugcounter-slsr.ll
+++ b/llvm/test/Other/debugcounter-slsr.ll
@@ -1,19 +1,17 @@
 ; 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.
+; 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
+; CHECK-NEXT: %s2 = shl i32 %s, 1
+; CHECK-NEXT: %t1 = add i32 %b, %s2
+; CHECK-NEXT: call void @foo(i32 %t1)
+; CHECK-NEXT: %s4 = shl i32 %s, 2
+; CHECK-NEXT: %t2 = add i32 %b, %s4
+; CHECK-NEXT: call void @foo(i32 %t2)
+; CHECK-NEXT: ret void
 ;
   %s2 = shl i32 %s, 1
   %t1 = add i32 %b, %s2
@@ -21,10 +19,7 @@ define void @stride_is_2s(i32 %b, i32 %s) {
   %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
+declare void @foo(i32)

>From 04a37ff55386ce763f5f8a95b3209bf0f71c9c32 Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Tue, 17 Dec 2024 13:36:40 +0000
Subject: [PATCH 3/8] Added new new line

---
 llvm/test/Other/debugcounter-slsr.ll | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/test/Other/debugcounter-slsr.ll b/llvm/test/Other/debugcounter-slsr.ll
index 3b3ad0649dcc9a..e98dbeed3ed04a 100644
--- a/llvm/test/Other/debugcounter-slsr.ll
+++ b/llvm/test/Other/debugcounter-slsr.ll
@@ -23,3 +23,4 @@ define void @stride_is_2s(i32 %b, i32 %s) {
 }
 
 declare void @foo(i32)
+

>From 9bf26df5f471f9f9fe1bbadd971d8c0b8c46b6a7 Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Tue, 17 Dec 2024 13:37:23 +0000
Subject: [PATCH 4/8] Removed additional new line

---
 llvm/test/Other/debugcounter-slsr.ll | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/test/Other/debugcounter-slsr.ll b/llvm/test/Other/debugcounter-slsr.ll
index e98dbeed3ed04a..3b3ad0649dcc9a 100644
--- a/llvm/test/Other/debugcounter-slsr.ll
+++ b/llvm/test/Other/debugcounter-slsr.ll
@@ -23,4 +23,3 @@ define void @stride_is_2s(i32 %b, i32 %s) {
 }
 
 declare void @foo(i32)
-

>From 8b5b15918e7f790bca93ff5009009b07d378b146 Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Tue, 17 Dec 2024 13:42:15 +0000
Subject: [PATCH 5/8] Hopefully newline is now added

---
 llvm/test/Other/debugcounter-slsr.ll | 50 ++++++++++++++--------------
 1 file changed, 25 insertions(+), 25 deletions(-)

diff --git a/llvm/test/Other/debugcounter-slsr.ll b/llvm/test/Other/debugcounter-slsr.ll
index 3b3ad0649dcc9a..20e86a2da2db76 100644
--- a/llvm/test/Other/debugcounter-slsr.ll
+++ b/llvm/test/Other/debugcounter-slsr.ll
@@ -1,25 +1,25 @@
-; 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: %s4 = shl i32 %s, 2
-; CHECK-NEXT: %t2 = add i32 %b, %s4
-; CHECK-NEXT: call void @foo(i32 %t2)
-; 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)
-  ret void
-}
-
-declare void @foo(i32)
+; 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: %s4 = shl i32 %s, 2
+; CHECK-NEXT: %t2 = add i32 %b, %s4
+; CHECK-NEXT: call void @foo(i32 %t2)
+; 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)
+  ret void
+}
+
+declare void @foo(i32)

>From e66fca9e09af3c03664e36f09029a552f88f2ac5 Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Tue, 17 Dec 2024 13:43:20 +0000
Subject: [PATCH 6/8] plz work

---
 llvm/test/Other/debugcounter-slsr.ll | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/test/Other/debugcounter-slsr.ll b/llvm/test/Other/debugcounter-slsr.ll
index 20e86a2da2db76..91b20f4c0b4570 100644
--- a/llvm/test/Other/debugcounter-slsr.ll
+++ b/llvm/test/Other/debugcounter-slsr.ll
@@ -23,3 +23,4 @@ define void @stride_is_2s(i32 %b, i32 %s) {
 }
 
 declare void @foo(i32)
+

>From f548c76c0640a24ccd794bddeb39424bdeae479e Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Wed, 18 Dec 2024 14:01:03 +0000
Subject: [PATCH 7/8] Removed gvn pass from test

---
 llvm/test/Other/debugcounter-slsr.ll | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/Other/debugcounter-slsr.ll b/llvm/test/Other/debugcounter-slsr.ll
index 91b20f4c0b4570..a9ca45222a5ccb 100644
--- a/llvm/test/Other/debugcounter-slsr.ll
+++ b/llvm/test/Other/debugcounter-slsr.ll
@@ -1,5 +1,5 @@
 ; REQUIRES: asserts
-; RUN: opt -passes=slsr,gvn -S -debug-counter=slsr-counter=1  < %s | FileCheck %s
+; RUN: opt -passes=slsr -S -debug-counter=slsr-counter=1  < %s | FileCheck %s
 
 ; Test that, with debug counters on, we will skip the first slsr opportunity.
 

>From e58f65e549eb1c75a3b279a3a72da3092fc576f8 Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Thu, 19 Dec 2024 06:57:22 +0000
Subject: [PATCH 8/8] Moved debug counter to beginning of
 rewriteCandidateWithBasis

---
 .../Scalar/StraightLineStrengthReduce.cpp           | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
index 4d4bea3113cec7..edb42e7f879899 100644
--- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
@@ -271,9 +271,10 @@ FunctionPass *llvm::createStraightLineStrengthReducePass() {
 
 bool StraightLineStrengthReduce::isBasisFor(const Candidate &Basis,
                                             const Candidate &C) {
-  return (Basis.Ins != C.Ins && // skip the same instruction
-          // They must have the same type too. Basis.Base == C.Base doesn't
-          // guarantee their types are the same (PR23975).
+  return (Basis.Ins !=
+              C.Ins && // skip the same instruction
+                       // They must have the same type too. Basis.Base == C.Base
+                       // doesn't guarantee their types are the same (PR23975).
           Basis.Ins->getType() == C.Ins->getType() &&
           // Basis must dominate C in order to rewrite C with respect to Basis.
           DT->dominates(Basis.Ins->getParent(), C.Ins->getParent()) &&
@@ -614,6 +615,9 @@ Value *StraightLineStrengthReduce::emitBump(const Candidate &Basis,
 
 void StraightLineStrengthReduce::rewriteCandidateWithBasis(
     const Candidate &C, const Candidate &Basis) {
+  if (!DebugCounter::shouldExecute(StraightLineStrengthReduceCounter))
+    return;
+
   assert(C.CandidateKind == Basis.CandidateKind && C.Base == Basis.Base &&
          C.Stride == Basis.Stride);
   // We run rewriteCandidateWithBasis on all candidates in a post-order, so the
@@ -694,8 +698,7 @@ bool StraightLineStrengthReduce::runOnFunction(Function &F) {
   while (!Candidates.empty()) {
     const Candidate &C = Candidates.back();
     if (C.Basis != nullptr) {
-      if (DebugCounter::shouldExecute(StraightLineStrengthReduceCounter))
-        rewriteCandidateWithBasis(C, *C.Basis);
+      rewriteCandidateWithBasis(C, *C.Basis);
     }
     Candidates.pop_back();
   }



More information about the llvm-commits mailing list