[llvm] [OptBisect] Add opt-bisect-skip option for skipping passes during bisection (PR #152393)

Yonah Goldberg via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 6 14:46:46 PDT 2025


https://github.com/YonahGoldberg updated https://github.com/llvm/llvm-project/pull/152393

>From 329098d16054afc1975327e75ebf02fb0ebe37a5 Mon Sep 17 00:00:00 2001
From: Yonah Goldberg <ygoldberg at nvidia.com>
Date: Wed, 6 Aug 2025 21:28:33 +0000
Subject: [PATCH 1/4] opt bisect skip

---
 llvm/include/llvm/IR/OptBisect.h   |  8 +++++++-
 llvm/lib/IR/OptBisect.cpp          | 10 ++++++++--
 llvm/test/Other/opt-bisect-skip.ll | 15 +++++++++++++++
 3 files changed, 30 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/Other/opt-bisect-skip.ll

diff --git a/llvm/include/llvm/IR/OptBisect.h b/llvm/include/llvm/IR/OptBisect.h
index d813ae933d65e..7b244c833f767 100644
--- a/llvm/include/llvm/IR/OptBisect.h
+++ b/llvm/include/llvm/IR/OptBisect.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_IR_OPTBISECT_H
 #define LLVM_IR_OPTBISECT_H
 
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/Compiler.h"
@@ -67,7 +68,11 @@ class LLVM_ABI OptBisect : public OptPassGate {
                      StringRef IRDescription) const override;
 
   /// isEnabled() should return true before calling shouldRunPass().
-  bool isEnabled() const override { return BisectLimit != Disabled; }
+  bool isEnabled() const override { return BisectLimit != Disabled || !BisectSkipNumbers.empty(); }
+
+  void addSkip(int SkipNumber) {
+    BisectSkipNumbers.insert(SkipNumber);
+  }
 
   /// Set the new optimization limit and reset the counter. Passing
   /// OptBisect::Disabled disables the limiting.
@@ -81,6 +86,7 @@ class LLVM_ABI OptBisect : public OptPassGate {
 private:
   int BisectLimit = Disabled;
   mutable int LastBisectNum = 0;
+  SmallSet<int, 4> BisectSkipNumbers;
 };
 
 /// This class implements a mechanism to disable passes and individual
diff --git a/llvm/lib/IR/OptBisect.cpp b/llvm/lib/IR/OptBisect.cpp
index 29ca268408265..59639f3b1981d 100644
--- a/llvm/lib/IR/OptBisect.cpp
+++ b/llvm/lib/IR/OptBisect.cpp
@@ -37,6 +37,13 @@ static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
                                    }),
                                    cl::desc("Maximum optimization to perform"));
 
+static cl::opt<int> OptBisectSkip("opt-bisect-skip", cl::Hidden,
+  cl::init(OptBisect::Disabled), cl::Optional,
+  cl::cb<void, int>([](int PassNum) {
+    getOptBisector().addSkip(PassNum);
+  }),
+  cl::desc("Skip pass at the given index in the optimization pipeline"));
+
 static cl::opt<bool> OptBisectVerbose(
     "opt-bisect-verbose",
     cl::desc("Show verbose output when opt-bisect-limit is set"), cl::Hidden,
@@ -66,8 +73,7 @@ bool OptBisect::shouldRunPass(StringRef PassName,
   assert(isEnabled());
 
   int CurBisectNum = ++LastBisectNum;
-  bool ShouldRun = (BisectLimit == -1 || CurBisectNum <= BisectLimit);
-  if (OptBisectVerbose)
+  bool ShouldRun = (BisectLimit == -1 || BisectLimit == Disabled || CurBisectNum <= BisectLimit) && !BisectSkipNumbers.contains(CurBisectNum);  if (OptBisectVerbose)
     printPassMessage(PassName, CurBisectNum, IRDescription, ShouldRun);
   return ShouldRun;
 }
diff --git a/llvm/test/Other/opt-bisect-skip.ll b/llvm/test/Other/opt-bisect-skip.ll
new file mode 100644
index 0000000000000..da94ad6230527
--- /dev/null
+++ b/llvm/test/Other/opt-bisect-skip.ll
@@ -0,0 +1,15 @@
+; Test that verifies functionality for -opt-bisect-skip
+
+; RUN: opt -O1 -opt-bisect-skip=3 -opt-bisect-skip=7 %s 2>&1 | FileCheck %s --check-prefix=CHECK-DISABLE-PASS
+; CHECK-DISABLE-PASS: BISECT: running pass (1) annotation2metadata on [module]
+; CHECK-DISABLE-PASS: BISECT: running pass (2) forceattrs on [module]
+; CHECK-DISABLE-PASS: BISECT: NOT running pass (3) inferattrs on [module]
+; CHECK-DISABLE-PASS: BISECT: running pass (4) lower-expect on foo
+; CHECK-DISABLE-PASS: BISECT: running pass (5) simplifycfg on foo
+; CHECK-DISABLE-PASS: BISECT: running pass (6) sroa on foo
+; CHECK-DISABLE-PASS: BISECT: NOT running pass (7) early-cse on foo
+; CHECK-DISABLE-PASS: BISECT: running pass (8) openmp-opt on [module]
+
+define void @foo() {
+  ret void
+}
\ No newline at end of file

>From 723514de5f46c562591fdf634442866498673969 Mon Sep 17 00:00:00 2001
From: Yonah Goldberg <ygoldberg at nvidia.com>
Date: Wed, 6 Aug 2025 21:28:51 +0000
Subject: [PATCH 2/4] format

---
 llvm/include/llvm/IR/OptBisect.h |  8 ++++----
 llvm/lib/IR/OptBisect.cpp        | 15 ++++++++-------
 2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/llvm/include/llvm/IR/OptBisect.h b/llvm/include/llvm/IR/OptBisect.h
index 7b244c833f767..ee787e0580032 100644
--- a/llvm/include/llvm/IR/OptBisect.h
+++ b/llvm/include/llvm/IR/OptBisect.h
@@ -68,12 +68,12 @@ class LLVM_ABI OptBisect : public OptPassGate {
                      StringRef IRDescription) const override;
 
   /// isEnabled() should return true before calling shouldRunPass().
-  bool isEnabled() const override { return BisectLimit != Disabled || !BisectSkipNumbers.empty(); }
-
-  void addSkip(int SkipNumber) {
-    BisectSkipNumbers.insert(SkipNumber);
+  bool isEnabled() const override {
+    return BisectLimit != Disabled || !BisectSkipNumbers.empty();
   }
 
+  void addSkip(int SkipNumber) { BisectSkipNumbers.insert(SkipNumber); }
+
   /// Set the new optimization limit and reset the counter. Passing
   /// OptBisect::Disabled disables the limiting.
   void setLimit(int Limit) {
diff --git a/llvm/lib/IR/OptBisect.cpp b/llvm/lib/IR/OptBisect.cpp
index 59639f3b1981d..defb8a98f9716 100644
--- a/llvm/lib/IR/OptBisect.cpp
+++ b/llvm/lib/IR/OptBisect.cpp
@@ -37,12 +37,10 @@ static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
                                    }),
                                    cl::desc("Maximum optimization to perform"));
 
-static cl::opt<int> OptBisectSkip("opt-bisect-skip", cl::Hidden,
-  cl::init(OptBisect::Disabled), cl::Optional,
-  cl::cb<void, int>([](int PassNum) {
-    getOptBisector().addSkip(PassNum);
-  }),
-  cl::desc("Skip pass at the given index in the optimization pipeline"));
+static cl::opt<int> OptBisectSkip(
+    "opt-bisect-skip", cl::Hidden, cl::init(OptBisect::Disabled), cl::Optional,
+    cl::cb<void, int>([](int PassNum) { getOptBisector().addSkip(PassNum); }),
+    cl::desc("Skip pass at the given index in the optimization pipeline"));
 
 static cl::opt<bool> OptBisectVerbose(
     "opt-bisect-verbose",
@@ -73,7 +71,10 @@ bool OptBisect::shouldRunPass(StringRef PassName,
   assert(isEnabled());
 
   int CurBisectNum = ++LastBisectNum;
-  bool ShouldRun = (BisectLimit == -1 || BisectLimit == Disabled || CurBisectNum <= BisectLimit) && !BisectSkipNumbers.contains(CurBisectNum);  if (OptBisectVerbose)
+  bool ShouldRun = (BisectLimit == -1 || BisectLimit == Disabled ||
+                    CurBisectNum <= BisectLimit) &&
+                   !BisectSkipNumbers.contains(CurBisectNum);
+  if (OptBisectVerbose)
     printPassMessage(PassName, CurBisectNum, IRDescription, ShouldRun);
   return ShouldRun;
 }

>From 431c13be5ad020febf1fc2e51dd3af5f8384127f Mon Sep 17 00:00:00 2001
From: Yonah Goldberg <ygoldberg at nvidia.com>
Date: Wed, 6 Aug 2025 21:36:51 +0000
Subject: [PATCH 3/4] comment

---
 llvm/include/llvm/IR/OptBisect.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/llvm/include/llvm/IR/OptBisect.h b/llvm/include/llvm/IR/OptBisect.h
index ee787e0580032..9dcc0077552d4 100644
--- a/llvm/include/llvm/IR/OptBisect.h
+++ b/llvm/include/llvm/IR/OptBisect.h
@@ -72,6 +72,8 @@ class LLVM_ABI OptBisect : public OptPassGate {
     return BisectLimit != Disabled || !BisectSkipNumbers.empty();
   }
 
+  /// Add pass at index SkipNumber to the list of passes to skip
+  /// during bisection.
   void addSkip(int SkipNumber) { BisectSkipNumbers.insert(SkipNumber); }
 
   /// Set the new optimization limit and reset the counter. Passing

>From ec6842e2c65af9260284850d17dde4b7c3cf3bf8 Mon Sep 17 00:00:00 2001
From: Yonah Goldberg <ygoldberg at nvidia.com>
Date: Wed, 6 Aug 2025 21:46:32 +0000
Subject: [PATCH 4/4] newline

---
 llvm/test/Other/opt-bisect-skip.ll | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/Other/opt-bisect-skip.ll b/llvm/test/Other/opt-bisect-skip.ll
index da94ad6230527..0f26e5f46f742 100644
--- a/llvm/test/Other/opt-bisect-skip.ll
+++ b/llvm/test/Other/opt-bisect-skip.ll
@@ -12,4 +12,4 @@
 
 define void @foo() {
   ret void
-}
\ No newline at end of file
+}



More information about the llvm-commits mailing list