[llvm] r328637 - [NFC] OptPassGate extracted from OptBisect

Fedor Sergeev via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 27 09:57:21 PDT 2018


Author: fedor.sergeev
Date: Tue Mar 27 09:57:20 2018
New Revision: 328637

URL: http://llvm.org/viewvc/llvm-project?rev=328637&view=rev
Log:
[NFC] OptPassGate extracted from OptBisect

Summary:
This is an NFC refactoring of the OptBisect class to split it into an optional pass gate interface used by LLVMContext and the Optional Pass Bisector (OptBisect) used for debugging of optional passes.

This refactoring is needed for D44464, which introduces setOptPassGate() method to allow implementations other than OptBisect.

Patch by Yevgeny Rouban.

Reviewers: andrew.w.kaylor, fedor.sergeev, vsk, dberlin, Eugene.Zelenko, reames, skatkov
Reviewed By: fedor.sergeev
Differential Revision: https://reviews.llvm.org/D44821

Modified:
    llvm/trunk/include/llvm/IR/LLVMContext.h
    llvm/trunk/include/llvm/IR/OptBisect.h
    llvm/trunk/lib/Analysis/CallGraphSCCPass.cpp
    llvm/trunk/lib/Analysis/LoopPass.cpp
    llvm/trunk/lib/Analysis/RegionPass.cpp
    llvm/trunk/lib/IR/LLVMContext.cpp
    llvm/trunk/lib/IR/LLVMContextImpl.cpp
    llvm/trunk/lib/IR/LLVMContextImpl.h
    llvm/trunk/lib/IR/OptBisect.cpp
    llvm/trunk/lib/IR/Pass.cpp

Modified: llvm/trunk/include/llvm/IR/LLVMContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/LLVMContext.h?rev=328637&r1=328636&r2=328637&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/LLVMContext.h (original)
+++ llvm/trunk/include/llvm/IR/LLVMContext.h Tue Mar 27 09:57:20 2018
@@ -31,7 +31,7 @@ class Function;
 class Instruction;
 class LLVMContextImpl;
 class Module;
-class OptBisect;
+class OptPassGate;
 template <typename T> class SmallVectorImpl;
 class SMDiagnostic;
 class StringRef;
@@ -317,7 +317,8 @@ public:
 
   /// \brief Access the object which manages optimization bisection for failure
   /// analysis.
-  OptBisect &getOptBisect();
+  OptPassGate &getOptPassGate();
+
 private:
   // Module needs access to the add/removeModule methods.
   friend class Module;

Modified: llvm/trunk/include/llvm/IR/OptBisect.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/OptBisect.h?rev=328637&r1=328636&r2=328637&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/OptBisect.h (original)
+++ llvm/trunk/include/llvm/IR/OptBisect.h Tue Mar 27 09:57:20 2018
@@ -20,12 +20,32 @@
 namespace llvm {
 
 class Pass;
+class Module;
+class Function;
+class BasicBlock;
+class Region;
+class Loop;
+class CallGraphSCC;
+
+/// Extensions to this class implement mechanisms to disable passes and
+/// individual optimizations at compile time.
+class OptPassGate {
+public:
+  virtual ~OptPassGate() = default;
+
+  virtual bool shouldRunPass(const Pass *P, const Module &U) { return true; }
+  virtual bool shouldRunPass(const Pass *P, const Function &U)  {return true; }
+  virtual bool shouldRunPass(const Pass *P, const BasicBlock &U)  { return true; }
+  virtual bool shouldRunPass(const Pass *P, const Region &U)  { return true; }
+  virtual bool shouldRunPass(const Pass *P, const Loop &U)  { return true; }
+  virtual bool shouldRunPass(const Pass *P, const CallGraphSCC &U)  { return true; }
+};
 
 /// This class implements 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.
-class OptBisect {
+class OptBisect : public OptPassGate {
 public:
   /// \brief Default constructor, initializes the OptBisect state based on the
   /// -opt-bisect-limit command line argument.
@@ -36,20 +56,26 @@ public:
   /// through LLVMContext.
   OptBisect();
 
+  virtual ~OptBisect() = default;
+
   /// Checks the bisect limit to determine if the specified pass should run.
   ///
-  /// This function will immediate return true if bisection is disabled. If the
-  /// bisect limit is set to -1, the function will print a message describing
+  /// These functions immediately return true if bisection is disabled. If the
+  /// bisect limit is set to -1, the functions print a message describing
   /// the pass and the bisect number assigned to it and return true.  Otherwise,
-  /// the function will print a message with the bisect number assigned to the
+  /// the functions print a message with the bisect number assigned to the
   /// pass and indicating whether or not the pass will be run and return true if
-  /// the bisect limit has not yet been exceded or false if it has.
+  /// the bisect limit has not yet been exceeded or false if it has.
   ///
-  /// Most passes should not call this routine directly.  Instead, it is called
-  /// through a helper routine provided by the pass base class.  For instance,
-  /// function passes should call FunctionPass::skipFunction().
-  template <class UnitT>
-  bool shouldRunPass(const Pass *P, const UnitT &U);
+  /// Most passes should not call these routines 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, const Module &U) override;
+  bool shouldRunPass(const Pass *P, const Function &U) override;
+  bool shouldRunPass(const Pass *P, const BasicBlock &U) override;
+  bool shouldRunPass(const Pass *P, const Region &U) override;
+  bool shouldRunPass(const Pass *P, const Loop &U) override;
+  bool shouldRunPass(const Pass *P, const CallGraphSCC &U) override;
 
 private:
   bool checkPass(const StringRef PassName, const StringRef TargetDesc);

Modified: llvm/trunk/lib/Analysis/CallGraphSCCPass.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/CallGraphSCCPass.cpp?rev=328637&r1=328636&r2=328637&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/CallGraphSCCPass.cpp (original)
+++ llvm/trunk/lib/Analysis/CallGraphSCCPass.cpp Tue Mar 27 09:57:20 2018
@@ -648,7 +648,7 @@ Pass *CallGraphSCCPass::createPrinterPas
 bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const {
   return !SCC.getCallGraph().getModule()
               .getContext()
-              .getOptBisect()
+              .getOptPassGate()
               .shouldRunPass(this, SCC);
 }
 

Modified: llvm/trunk/lib/Analysis/LoopPass.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/LoopPass.cpp?rev=328637&r1=328636&r2=328637&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/LoopPass.cpp (original)
+++ llvm/trunk/lib/Analysis/LoopPass.cpp Tue Mar 27 09:57:20 2018
@@ -357,7 +357,7 @@ bool LoopPass::skipLoop(const Loop *L) c
     return false;
   // Check the opt bisect limit.
   LLVMContext &Context = F->getContext();
-  if (!Context.getOptBisect().shouldRunPass(this, *L))
+  if (!Context.getOptPassGate().shouldRunPass(this, *L))
     return true;
   // Check for the OptimizeNone attribute.
   if (F->hasFnAttribute(Attribute::OptimizeNone)) {

Modified: llvm/trunk/lib/Analysis/RegionPass.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/RegionPass.cpp?rev=328637&r1=328636&r2=328637&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/RegionPass.cpp (original)
+++ llvm/trunk/lib/Analysis/RegionPass.cpp Tue Mar 27 09:57:20 2018
@@ -283,7 +283,7 @@ Pass *RegionPass::createPrinterPass(raw_
 
 bool RegionPass::skipRegion(Region &R) const {
   Function &F = *R.getEntry()->getParent();
-  if (!F.getContext().getOptBisect().shouldRunPass(this, R))
+  if (!F.getContext().getOptPassGate().shouldRunPass(this, R))
     return true;
 
   if (F.hasFnAttribute(Attribute::OptimizeNone)) {

Modified: llvm/trunk/lib/IR/LLVMContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/LLVMContext.cpp?rev=328637&r1=328636&r2=328637&view=diff
==============================================================================
--- llvm/trunk/lib/IR/LLVMContext.cpp (original)
+++ llvm/trunk/lib/IR/LLVMContext.cpp Tue Mar 27 09:57:20 2018
@@ -332,8 +332,8 @@ void LLVMContext::setDiscardValueNames(b
   pImpl->DiscardValueNames = Discard;
 }
 
-OptBisect &LLVMContext::getOptBisect() {
-  return pImpl->getOptBisect();
+OptPassGate &LLVMContext::getOptPassGate() {
+  return pImpl->getOptPassGate();
 }
 
 const DiagnosticHandler *LLVMContext::getDiagHandlerPtr() const {

Modified: llvm/trunk/lib/IR/LLVMContextImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/LLVMContextImpl.cpp?rev=328637&r1=328636&r2=328637&view=diff
==============================================================================
--- llvm/trunk/lib/IR/LLVMContextImpl.cpp (original)
+++ llvm/trunk/lib/IR/LLVMContextImpl.cpp Tue Mar 27 09:57:20 2018
@@ -222,8 +222,8 @@ void LLVMContextImpl::getSyncScopeNames(
 
 /// Singleton instance of the OptBisect class.
 ///
-/// This singleton is accessed via the LLVMContext::getOptBisect() function.  It
-/// provides a mechanism to disable passes and individual optimizations at
+/// 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.
 ///
@@ -233,6 +233,6 @@ void LLVMContextImpl::getSyncScopeNames(
 /// enabled in order to enable a consistent bisect count.
 static ManagedStatic<OptBisect> OptBisector;
 
-OptBisect &LLVMContextImpl::getOptBisect() {
+OptPassGate &LLVMContextImpl::getOptPassGate() {
   return *OptBisector;
 }

Modified: llvm/trunk/lib/IR/LLVMContextImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/LLVMContextImpl.h?rev=328637&r1=328636&r2=328637&view=diff
==============================================================================
--- llvm/trunk/lib/IR/LLVMContextImpl.h (original)
+++ llvm/trunk/lib/IR/LLVMContextImpl.h Tue Mar 27 09:57:20 2018
@@ -1357,7 +1357,7 @@ public:
 
   /// \brief Access the object which manages optimization bisection for failure
   /// analysis.
-  OptBisect &getOptBisect();
+  OptPassGate &getOptPassGate();
 };
 
 } // end namespace llvm

Modified: llvm/trunk/lib/IR/OptBisect.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/OptBisect.cpp?rev=328637&r1=328636&r2=328637&view=diff
==============================================================================
--- llvm/trunk/lib/IR/OptBisect.cpp (original)
+++ llvm/trunk/lib/IR/OptBisect.cpp Tue Mar 27 09:57:20 2018
@@ -36,7 +36,7 @@ static cl::opt<int> OptBisectLimit("opt-
                                    cl::Optional,
                                    cl::desc("Maximum optimization to perform"));
 
-OptBisect::OptBisect() {
+OptBisect::OptBisect() : OptPassGate() {
   BisectEnabled = OptBisectLimit != std::numeric_limits<int>::max();
 }
 
@@ -92,19 +92,28 @@ static std::string getDescription(const
   return Desc;
 }
 
-// Force instantiations.
-template bool OptBisect::shouldRunPass(const Pass *, const Module &);
-template bool OptBisect::shouldRunPass(const Pass *, const Function &);
-template bool OptBisect::shouldRunPass(const Pass *, const BasicBlock &);
-template bool OptBisect::shouldRunPass(const Pass *, const Loop &);
-template bool OptBisect::shouldRunPass(const Pass *, const CallGraphSCC &);
-template bool OptBisect::shouldRunPass(const Pass *, const Region &);
-
-template <class UnitT>
-bool OptBisect::shouldRunPass(const Pass *P, const UnitT &U) {
-  if (!BisectEnabled)
-    return true;
-  return checkPass(P->getPassName(), getDescription(U));
+bool OptBisect::shouldRunPass(const Pass *P, const Module &U) {
+  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
+}
+
+bool OptBisect::shouldRunPass(const Pass *P, const Function &U) {
+  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
+}
+
+bool OptBisect::shouldRunPass(const Pass *P, const BasicBlock &U) {
+  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
+}
+
+bool OptBisect::shouldRunPass(const Pass *P, const Region &U) {
+  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
+}
+
+bool OptBisect::shouldRunPass(const Pass *P, const Loop &U) {
+  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
+}
+
+bool OptBisect::shouldRunPass(const Pass *P, const CallGraphSCC &U) {
+  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
 }
 
 bool OptBisect::checkPass(const StringRef PassName,

Modified: llvm/trunk/lib/IR/Pass.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Pass.cpp?rev=328637&r1=328636&r2=328637&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Pass.cpp (original)
+++ llvm/trunk/lib/IR/Pass.cpp Tue Mar 27 09:57:20 2018
@@ -56,7 +56,7 @@ PassManagerType ModulePass::getPotential
 }
 
 bool ModulePass::skipModule(Module &M) const {
-  return !M.getContext().getOptBisect().shouldRunPass(this, M);
+  return !M.getContext().getOptPassGate().shouldRunPass(this, M);
 }
 
 bool Pass::mustPreserveAnalysisID(char &AID) const {
@@ -155,7 +155,7 @@ PassManagerType FunctionPass::getPotenti
 }
 
 bool FunctionPass::skipFunction(const Function &F) const {
-  if (!F.getContext().getOptBisect().shouldRunPass(this, F))
+  if (!F.getContext().getOptPassGate().shouldRunPass(this, F))
     return true;
 
   if (F.hasFnAttribute(Attribute::OptimizeNone)) {
@@ -189,7 +189,7 @@ bool BasicBlockPass::skipBasicBlock(cons
   const Function *F = BB.getParent();
   if (!F)
     return false;
-  if (!F->getContext().getOptBisect().shouldRunPass(this, BB))
+  if (!F->getContext().getOptPassGate().shouldRunPass(this, BB))
     return true;
   if (F->hasFnAttribute(Attribute::OptimizeNone)) {
     // Report this only once per function.




More information about the llvm-commits mailing list