diff --git a/clang/test/CodeGen/new-pass-manager-opt-bisect.c b/clang/test/CodeGen/new-pass-manager-opt-bisect.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/new-pass-manager-opt-bisect.c @@ -0,0 +1,10 @@ +// Make sure opt-bisect works through both pass managers +// +// RUN: %clang_cc1 -triple x86_64-linux-gnu -O1 -fexperimental-new-pass-manager %s -mllvm -opt-bisect-limit=-1 -emit-obj -o /dev/null 2>&1 | FileCheck %s + +// CHECK: BISECT: running pass (1) +// CHECK-NOT: BISECT: running pass (1) +// Make sure that legacy pass manager is running +// CHECK: Instruction Selection + +int func(int a) { return a; } diff --git a/llvm/include/llvm/IR/OptBisect.h b/llvm/include/llvm/IR/OptBisect.h --- a/llvm/include/llvm/IR/OptBisect.h +++ b/llvm/include/llvm/IR/OptBisect.h @@ -15,6 +15,7 @@ #define LLVM_IR_OPTBISECT_H #include "llvm/ADT/StringRef.h" +#include "llvm/Support/ManagedStatic.h" namespace llvm { @@ -53,6 +54,13 @@ virtual ~OptBisect() = default; + /// Checks the bisect limit to determine if the specified pass should run. + /// This calls checkPass + bool shouldRunPass(const Pass *P, StringRef IRDescription) override; + + /// isEnabled should return true before calling shouldRunPass + bool isEnabled() const override { return BisectEnabled; } + /// Checks the bisect limit to determine if the specified pass should run. /// /// If the bisect limit is set to -1, the function prints a message describing @@ -64,12 +72,6 @@ /// Most passes should not call this routine directly. Instead, they are /// called through helper routines provided by the pass base classes. For /// instance, function passes should call FunctionPass::skipFunction(). - bool shouldRunPass(const Pass *P, StringRef IRDescription) override; - - /// isEnabled should return true before calling shouldRunPass - bool isEnabled() const override { return BisectEnabled; } - -protected: bool checkPass(const StringRef PassName, const StringRef TargetDesc); private: @@ -77,6 +79,8 @@ unsigned LastBisectNum = 0; }; +/// Singleton instance of the OptBisect class. +extern ManagedStatic OptBisector; } // end namespace llvm #endif // LLVM_IR_OPTBISECT_H diff --git a/llvm/include/llvm/Passes/StandardInstrumentations.h b/llvm/include/llvm/Passes/StandardInstrumentations.h --- a/llvm/include/llvm/Passes/StandardInstrumentations.h +++ b/llvm/include/llvm/Passes/StandardInstrumentations.h @@ -72,7 +72,7 @@ bool shouldRun(StringRef PassID, Any IR); }; -class OptBisectInstrumentation : public OptBisect { +class OptBisectInstrumentation { public: OptBisectInstrumentation() {} void registerCallbacks(PassInstrumentationCallbacks &PIC); diff --git a/llvm/lib/IR/LLVMContextImpl.cpp b/llvm/lib/IR/LLVMContextImpl.cpp --- a/llvm/lib/IR/LLVMContextImpl.cpp +++ b/llvm/lib/IR/LLVMContextImpl.cpp @@ -219,19 +219,7 @@ SSNs[SSE.second] = SSE.first(); } -/// Singleton instance of the OptBisect class. -/// -/// This singleton is accessed via the LLVMContext::getOptPassGate() function. -/// It provides a mechanism to disable passes and individual optimizations at -/// compile time based on a command line option (-opt-bisect-limit) in order to -/// perform a bisecting search for optimization-related problems. -/// -/// Even if multiple LLVMContext objects are created, they will all return the -/// same instance of OptBisect in order to provide a single bisect count. Any -/// code that uses the OptBisect object should be serialized when bisection is -/// enabled in order to enable a consistent bisect count. -static ManagedStatic OptBisector; - +/// Gets the singleton instance OptBisect if not already set OptPassGate &LLVMContextImpl::getOptPassGate() const { if (!OPG) OPG = &(*OptBisector); diff --git a/llvm/lib/IR/OptBisect.cpp b/llvm/lib/IR/OptBisect.cpp --- a/llvm/lib/IR/OptBisect.cpp +++ b/llvm/lib/IR/OptBisect.cpp @@ -54,3 +54,5 @@ printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun); return ShouldRun; } + +ManagedStatic llvm::OptBisector; \ No newline at end of file diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp --- a/llvm/lib/Passes/StandardInstrumentations.cpp +++ b/llvm/lib/Passes/StandardInstrumentations.cpp @@ -21,9 +21,11 @@ #include "llvm/Analysis/LoopInfo.h" #include "llvm/IR/Function.h" #include "llvm/IR/Module.h" +#include "llvm/IR/OptBisect.h" #include "llvm/IR/PassInstrumentation.h" #include "llvm/IR/PrintPasses.h" #include "llvm/IR/Verifier.h" +#include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/FormatVariadic.h" @@ -624,11 +626,11 @@ void OptBisectInstrumentation::registerCallbacks( PassInstrumentationCallbacks &PIC) { - if (!isEnabled()) + if (!OptBisector->isEnabled()) return; - - PIC.registerShouldRunOptionalPassCallback([this](StringRef PassID, Any IR) { - return isIgnored(PassID) || checkPass(PassID, getBisectDescription(IR)); + PIC.registerShouldRunOptionalPassCallback([](StringRef PassID, Any IR) { + return isIgnored(PassID) || + OptBisector->checkPass(PassID, getBisectDescription(IR)); }); }