Skip to content

Commit

Permalink
allow custom OptBisect classes set to LLVMContext
Browse files Browse the repository at this point in the history
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

llvm-svn: 329267
  • Loading branch information
Fedor Sergeev committed Apr 5, 2018
1 parent 978502f commit d29884c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 9 deletions.
13 changes: 10 additions & 3 deletions llvm/include/llvm/IR/LLVMContext.h
Expand Up @@ -315,9 +315,16 @@ class LLVMContext {
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.
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/IR/LLVMContext.cpp
Expand Up @@ -332,10 +332,14 @@ void LLVMContext::setDiscardValueNames(bool Discard) {
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();
}
Expand Down
10 changes: 8 additions & 2 deletions llvm/lib/IR/LLVMContextImpl.cpp
Expand Up @@ -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;
}
15 changes: 12 additions & 3 deletions llvm/lib/IR/LLVMContextImpl.h
Expand Up @@ -1355,9 +1355,18 @@ class LLVMContextImpl {
/// 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
Expand Down
62 changes: 62 additions & 0 deletions llvm/unittests/IR/LegacyPassManagerTest.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d29884c

Please sign in to comment.