diff --git a/include/revng/Pipeline/GenericLLVMPipe.h b/include/revng/Pipeline/GenericLLVMPipe.h index 8d90a7ace..e753cb8b1 100644 --- a/include/revng/Pipeline/GenericLLVMPipe.h +++ b/include/revng/Pipeline/GenericLLVMPipe.h @@ -17,13 +17,14 @@ #include "revng/ADT/Concepts.h" #include "revng/Pipeline/ContainerFactorySet.h" #include "revng/Pipeline/Contract.h" -#include "revng/Pipeline/LLVMContainer.h" #include "revng/Pipeline/Step.h" #include "revng/Support/Debug.h" #include "revng/Support/ResourceFinder.h" namespace pipeline { +class LLVMContainer; + class LLVMPassWrapperBase { public: virtual ~LLVMPassWrapperBase() = default; @@ -70,10 +71,7 @@ public: ~PureLLVMPassWrapper() override = default; - void registerPasses(llvm::legacy::PassManager &Manager) override { - auto *Registry = llvm::PassRegistry::getPassRegistry(); - Manager.add(Registry->getPassInfo(PassName)->createPass()); - } + void registerPasses(llvm::legacy::PassManager &Manager) override; const std::vector &getContract() const override { static const std::vector Empty{}; @@ -137,7 +135,6 @@ public: /// Implementation of the LLVM pipes to be instantiated for a particular LLVM /// container -template class GenericLLVMPipe { private: llvm::SmallVector, 4> Passes; @@ -179,12 +176,7 @@ public: return Contract; } - void run(const Context &, LLVMContainer &Container) { - llvm::legacy::PassManager Manager; - for (const auto &Element : Passes) - Element->registerPasses(Manager); - Manager.run(Container.getModule()); - } + void run(const Context &, LLVMContainer &Container); void addPass(const PureLLVMPassWrapper &Pass) { Passes.emplace_back(Pass.clone()); @@ -241,6 +233,6 @@ public: void registerPasses(llvm::legacy::PassManager &Manager); }; -using LLVMPipe = GenericLLVMPipe; +using LLVMPipe = GenericLLVMPipe; } // namespace pipeline diff --git a/include/revng/Pipeline/LLVMContainer.h b/include/revng/Pipeline/LLVMContainer.h index 47ef63ad6..fa64eeae0 100644 --- a/include/revng/Pipeline/LLVMContainer.h +++ b/include/revng/Pipeline/LLVMContainer.h @@ -55,14 +55,7 @@ namespace pipeline { void makeGlobalObjectsArray(llvm::Module &Module, llvm::StringRef GlobalArrayName); -template -class GenericLLVMPipe; - -/// Implementation that can be derived by anyone so that multiple identical -/// LLVMContainers can exist so that inspector kinds do not pollute each other -template -class LLVMContainerBase - : public EnumerableContainer> { +class LLVMContainer : public EnumerableContainer { private: using LinkageRestoreMap = std::map; @@ -71,7 +64,7 @@ public: static const char ID; private: - using ThisType = LLVMContainerBase; + using ThisType = LLVMContainer; private: std::unique_ptr Module; @@ -79,22 +72,22 @@ private: public: inline static const llvm::StringRef MIMEType = "text/x.llvm.ir"; - LLVMContainerBase(llvm::StringRef Name, - Context *Ctx, - llvm::LLVMContext *LLVMCtx) : + LLVMContainer(llvm::StringRef Name, + Context *Ctx, + llvm::LLVMContext *LLVMCtx) : EnumerableContainer(*Ctx, Name), Module(std::make_unique("revng.module", *LLVMCtx)) {} - LLVMContainerBase(llvm::StringRef Name, - Context *Ctx, - std::unique_ptr M) : + LLVMContainer(llvm::StringRef Name, + Context *Ctx, + std::unique_ptr M) : EnumerableContainer(*Ctx, Name), Module(std::move(M)) {} public: template static PipeWrapper wrapLLVMPasses(std::string LLVMModuleName, LLVMPasses &&...P) { - return PipeWrapper::make(GenericLLVMPipe(std::move(P)...), + return PipeWrapper::make(GenericLLVMPipe(std::move(P)...), { std::move(LLVMModuleName) }); } @@ -309,9 +302,6 @@ private: } }; -extern char LLVMContainerTypeID; - -using LLVMContainer = LLVMContainerBase<&LLVMContainerTypeID>; using LLVMKind = LLVMGlobalKindBase; } // namespace pipeline diff --git a/lib/Pipeline/GenericLLVMPipe.cpp b/lib/Pipeline/GenericLLVMPipe.cpp index 2dcc2342d..1e47fa147 100644 --- a/lib/Pipeline/GenericLLVMPipe.cpp +++ b/lib/Pipeline/GenericLLVMPipe.cpp @@ -9,6 +9,7 @@ #include "llvm/Passes/PassBuilder.h" #include "revng/Pipeline/GenericLLVMPipe.h" +#include "revng/Pipeline/LLVMContainer.h" using namespace std; using namespace llvm; @@ -26,3 +27,15 @@ void O2Pipe::registerPasses(llvm::legacy::PassManager &Manager) { std::unique_ptr PureLLVMPassWrapper::clone() const { return std::make_unique(*this); } + +void GenericLLVMPipe::run(const Context &, LLVMContainer &Container) { + llvm::legacy::PassManager Manager; + for (const auto &Element : Passes) + Element->registerPasses(Manager); + Manager.run(Container.getModule()); +} + +void PureLLVMPassWrapper::registerPasses(llvm::legacy::PassManager &Manager) { + auto *Registry = llvm::PassRegistry::getPassRegistry(); + Manager.add(Registry->getPassInfo(PassName)->createPass()); +} diff --git a/lib/Pipeline/LLVMContainer.cpp b/lib/Pipeline/LLVMContainer.cpp index 28297f8e6..ddbc963c2 100644 --- a/lib/Pipeline/LLVMContainer.cpp +++ b/lib/Pipeline/LLVMContainer.cpp @@ -19,9 +19,6 @@ #include "revng/Pipeline/LLVMContainer.h" -char pipeline::LLVMContainerTypeID = '0'; - -template<> const char pipeline::LLVMContainer::ID = '0'; void pipeline::makeGlobalObjectsArray(llvm::Module &Module, diff --git a/tests/unit/Pipeline.cpp b/tests/unit/Pipeline.cpp index 743a8cf69..a9f7ac82c 100644 --- a/tests/unit/Pipeline.cpp +++ b/tests/unit/Pipeline.cpp @@ -42,13 +42,6 @@ #include "revng/Pipeline/Target.h" #include "revng/Support/Assert.h" -static char LLVMName = ' '; - -using ExampleLLVMInspectalbeContainer = pipeline::LLVMContainerBase<&LLVMName>; - -template<> -const char ExampleLLVMInspectalbeContainer::ID = '0'; - #define BOOST_TEST_MODULE Pipeline bool init_unit_test(); #include "boost/test/unit_test.hpp" @@ -1148,10 +1141,9 @@ BOOST_AUTO_TEST_CASE(EnumerableContainersTest) { BOOST_TEST(not Example.contains(T)); } -class LLVMInspectorExample - : public LLVMGlobalKindBase { +class LLVMInspectorExample : public LLVMGlobalKindBase { public: - using LLVMGlobalKindBase::LLVMGlobalKindBase; + using LLVMGlobalKindBase::LLVMGlobalKindBase; std::optional symbolToTarget(const llvm::Function &Symbol) const override { return Target({ Symbol.getName() }, FunctionKind); @@ -1164,10 +1156,9 @@ public: } }; -class LLVMRootInspectorExample - : public LLVMGlobalKindBase { +class LLVMRootInspectorExample : public LLVMGlobalKindBase { public: - using LLVMGlobalKindBase::LLVMGlobalKindBase; + using LLVMGlobalKindBase::LLVMGlobalKindBase; std::optional symbolToTarget(const llvm::Function &Symbol) const override { return Target({}, RootKind); @@ -1184,7 +1175,7 @@ static LLVMRootInspectorExample ExampleLLVMRootInspector("dc2", Root); BOOST_AUTO_TEST_CASE(LLVMKindTest) { llvm::LLVMContext C; - using Cont = ExampleLLVMInspectalbeContainer; + using Cont = LLVMContainer; Context Ctx; Runner Pipeline(Ctx); @@ -1214,12 +1205,10 @@ BOOST_AUTO_TEST_CASE(LLVMKindTest) { BOOST_TEST(F != nullptr); } -class InspectorKindExample - : public LLVMGlobalKindBase { +class InspectorKindExample : public LLVMGlobalKindBase { public: InspectorKindExample() : - LLVMGlobalKindBase("ExampleName", - FunctionRank) {} + LLVMGlobalKindBase("ExampleName", FunctionRank) {} std::optional symbolToTarget(const llvm::Function &Symbol) const final { @@ -1240,7 +1229,7 @@ BOOST_AUTO_TEST_CASE(InspectorKindTest) { Context Ctx; llvm::LLVMContext C; - using Cont = ExampleLLVMInspectalbeContainer; + using Cont = LLVMContainer; auto Factory = ContainerFactory::fromGlobal(&Ctx, &C); auto Container = Factory("dont_care");