[llvm] r329267 - allow custom OptBisect classes set to LLVMContext

Fedor Sergeev via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 5 03:29:37 PDT 2018


Author: fedor.sergeev
Date: Thu Apr  5 03:29:37 2018
New Revision: 329267

URL: http://llvm.org/viewvc/llvm-project?rev=329267&view=rev
Log:
allow custom OptBisect classes set to LLVMContext

This patch introduces a way to set custom OptPassGate instances to LLVMContext.
A new instance field OptBisector and a new method setOptBisect() are added
to the LLVMContext classes. These changes allow to set a custom OptBisect class
that can make its own decisions on skipping optional passes.

Another important feature of this change is ability to set different instances
of OptPassGate to different LLVMContexts. So the different contexts can be used
independently in several compiling threads of one process.

One unit test is added.

Patch by Yevgeny Rouban.

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

Modified:
    llvm/trunk/include/llvm/IR/LLVMContext.h
    llvm/trunk/lib/IR/LLVMContext.cpp
    llvm/trunk/lib/IR/LLVMContextImpl.cpp
    llvm/trunk/lib/IR/LLVMContextImpl.h
    llvm/trunk/unittests/IR/LegacyPassManagerTest.cpp

Modified: llvm/trunk/include/llvm/IR/LLVMContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/LLVMContext.h?rev=329267&r1=329266&r2=329267&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/LLVMContext.h (original)
+++ llvm/trunk/include/llvm/IR/LLVMContext.h Thu Apr  5 03:29:37 2018
@@ -315,9 +315,16 @@ public:
     return OptionRegistry::instance().template get<ValT, Base, Mem>();
   }
 
-  /// \brief Access the object which manages optimization bisection for failure
-  /// analysis.
-  OptPassGate &getOptPassGate();
+  /// \brief Access the object which can disable optional passes and individual
+  /// optimizations at compile time.
+  OptPassGate &getOptPassGate() const;
+
+  /// \brief Set the object which can disable optional passes and individual
+  /// optimizations at compile time.
+  ///
+  /// The lifetime of the object must be guaranteed to extend as long as the
+  /// LLVMContext is used by compilation.
+  void setOptPassGate(OptPassGate&);
 
 private:
   // Module needs access to the add/removeModule methods.

Modified: llvm/trunk/lib/IR/LLVMContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/LLVMContext.cpp?rev=329267&r1=329266&r2=329267&view=diff
==============================================================================
--- llvm/trunk/lib/IR/LLVMContext.cpp (original)
+++ llvm/trunk/lib/IR/LLVMContext.cpp Thu Apr  5 03:29:37 2018
@@ -332,10 +332,14 @@ void LLVMContext::setDiscardValueNames(b
   pImpl->DiscardValueNames = Discard;
 }
 
-OptPassGate &LLVMContext::getOptPassGate() {
+OptPassGate &LLVMContext::getOptPassGate() const {
   return pImpl->getOptPassGate();
 }
 
+void LLVMContext::setOptPassGate(OptPassGate& OPG) {
+  pImpl->setOptPassGate(OPG);
+}
+
 const DiagnosticHandler *LLVMContext::getDiagHandlerPtr() const {
   return pImpl->DiagHandler.get();
 }

Modified: llvm/trunk/lib/IR/LLVMContextImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/LLVMContextImpl.cpp?rev=329267&r1=329266&r2=329267&view=diff
==============================================================================
--- llvm/trunk/lib/IR/LLVMContextImpl.cpp (original)
+++ llvm/trunk/lib/IR/LLVMContextImpl.cpp Thu Apr  5 03:29:37 2018
@@ -233,6 +233,12 @@ void LLVMContextImpl::getSyncScopeNames(
 /// enabled in order to enable a consistent bisect count.
 static ManagedStatic<OptBisect> OptBisector;
 
-OptPassGate &LLVMContextImpl::getOptPassGate() {
-  return *OptBisector;
+OptPassGate &LLVMContextImpl::getOptPassGate() const {
+  if (!OPG)
+    OPG = &(*OptBisector);
+  return *OPG;
+}
+
+void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
+  this->OPG = &OPG;
 }

Modified: llvm/trunk/lib/IR/LLVMContextImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/LLVMContextImpl.h?rev=329267&r1=329266&r2=329267&view=diff
==============================================================================
--- llvm/trunk/lib/IR/LLVMContextImpl.h (original)
+++ llvm/trunk/lib/IR/LLVMContextImpl.h Thu Apr  5 03:29:37 2018
@@ -1355,9 +1355,18 @@ public:
   /// Destroy the ConstantArrays if they are not used.
   void dropTriviallyDeadConstantArrays();
 
-  /// \brief Access the object which manages optimization bisection for failure
-  /// analysis.
-  OptPassGate &getOptPassGate();
+  mutable OptPassGate *OPG = nullptr;
+
+  /// \brief Access the object which can disable optional passes and individual
+  /// optimizations at compile time.
+  OptPassGate &getOptPassGate() const;
+
+  /// \brief Set the object which can disable optional passes and individual
+  /// optimizations at compile time.
+  ///
+  /// The lifetime of the object must be guaranteed to extend as long as the
+  /// LLVMContext is used by compilation.
+  void setOptPassGate(OptPassGate&);
 };
 
 } // end namespace llvm

Modified: llvm/trunk/unittests/IR/LegacyPassManagerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/LegacyPassManagerTest.cpp?rev=329267&r1=329266&r2=329267&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/LegacyPassManagerTest.cpp (original)
+++ llvm/trunk/unittests/IR/LegacyPassManagerTest.cpp Thu Apr  5 03:29:37 2018
@@ -26,6 +26,7 @@
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/OptBisect.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
@@ -396,6 +397,67 @@ namespace llvm {
       delete M;
     }
 
+    // Skips or runs optional passes.
+    struct CustomOptPassGate : public OptPassGate {
+      bool Skip;
+      CustomOptPassGate(bool Skip) : Skip(Skip) { }
+      bool shouldRunPass(const Pass *P, const Module &U) { return !Skip; }
+    };
+
+    // Optional module pass.
+    struct ModuleOpt: public ModulePass {
+      char run = 0;
+      static char ID;
+      ModuleOpt() : ModulePass(ID) { }
+      bool runOnModule(Module &M) override {
+        if (!skipModule(M))
+          run++;
+        return false;
+      }
+    };
+    char ModuleOpt::ID=0;
+
+    TEST(PassManager, CustomOptPassGate) {
+      LLVMContext Context0;
+      LLVMContext Context1;
+      LLVMContext Context2;
+      CustomOptPassGate SkipOptionalPasses(true);
+      CustomOptPassGate RunOptionalPasses(false);
+
+      Module M0("custom-opt-bisect", Context0);
+      Module M1("custom-opt-bisect", Context1);
+      Module M2("custom-opt-bisect2", Context2);
+      struct ModuleOpt *mOpt0 = new ModuleOpt();
+      struct ModuleOpt *mOpt1 = new ModuleOpt();
+      struct ModuleOpt *mOpt2 = new ModuleOpt();
+
+      mOpt0->run = mOpt1->run = mOpt2->run = 0;
+
+      legacy::PassManager Passes0;
+      legacy::PassManager Passes1;
+      legacy::PassManager Passes2;
+
+      Passes0.add(mOpt0);
+      Passes1.add(mOpt1);
+      Passes2.add(mOpt2);
+
+      Context1.setOptPassGate(SkipOptionalPasses);
+      Context2.setOptPassGate(RunOptionalPasses);
+
+      Passes0.run(M0);
+      Passes1.run(M1);
+      Passes2.run(M2);
+
+      // By default optional passes are run.
+      EXPECT_EQ(1, mOpt0->run);
+
+      // The first context skips optional passes.
+      EXPECT_EQ(0, mOpt1->run);
+
+      // The second context runs optional passes.
+      EXPECT_EQ(1, mOpt2->run);
+    }
+
     Module *makeLLVMModule(LLVMContext &Context) {
       // Module Construction
       Module *mod = new Module("test-mem", Context);




More information about the llvm-commits mailing list