Index: include/llvm/Passes/PassPlugin.h =================================================================== --- /dev/null +++ include/llvm/Passes/PassPlugin.h @@ -0,0 +1,52 @@ +//===- llvm/Passes/PassPlugin.h - Public Plugin API -----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This defines the public entry point for new-PM pass plugins. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_PASSES_PASSPLUGIN_H +#define LLVM_PASSES_PASSPLUGIN_H + +#include +#include + +namespace llvm { +class PassBuilder; + +#define LLVM_PLUGIN_API_VERSION 1 + +#ifdef WIN32 +#define LLVM_PLUGIN_EXPORT __declspec(dllexport) +#else +#define LLVM_PLUGIN_EXPORT +#endif + +extern "C" { +struct PluginInfo { + uint32_t APIVersion; + const char *PluginName; + const char *PluginVersion; + + void (*RegisterPassBuilderCallbacks)(PassBuilder &); +}; + +PluginInfo LLVM_PLUGIN_EXPORT llvmPluginGetInfo(); +} +} + +#define LLVM_PLUGIN(NAME, PLUGIN_VERSION, REGISTER_CALLBACKS) \ + extern "C" { \ + ::llvm::PluginInfo LLVM_PLUGIN_EXPORT llvmPluginGetInfo() { \ + return {LLVM_PLUGIN_API_VERSION, NAME, PLUGIN_VERSION, \ + REGISTER_CALLBACKS}; \ + } \ + } + +#endif /* LLVM_PASSES_PASSPLUGIN_H */ Index: include/llvm/Passes/PassPluginLoader.h =================================================================== --- /dev/null +++ include/llvm/Passes/PassPluginLoader.h @@ -0,0 +1,33 @@ +//===- llvm/Passes/PassPluginLoader.h - Load Plugins for New PM Passes ----===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_PASSES_PASSPLUGINLOADER_H +#define LLVM_PASSES_PASSPLUGINLOADER_H + +#include "llvm/ADT/Optional.h" +#include "llvm/Passes/PassPlugin.h" +#include "llvm/Support/DynamicLibrary.h" + +namespace llvm { +class Plugin { +public: + static Optional Load(const std::string &Filename); + std::string &getFilename() { return Filename; } + const PluginInfo &getInfo() const { return Info; } + +private: + Plugin(const std::string &Filename, const sys::DynamicLibrary &Library) + : Filename(Filename), Library(Library), Info() {} + std::string Filename; + sys::DynamicLibrary Library; + PluginInfo Info; +}; +} + +#endif /* LLVM_PASSES_PASSPLUGINLOADER_H */ Index: lib/Passes/CMakeLists.txt =================================================================== --- lib/Passes/CMakeLists.txt +++ lib/Passes/CMakeLists.txt @@ -1,5 +1,6 @@ add_llvm_library(LLVMPasses PassBuilder.cpp + PassPluginLoader.cpp ADDITIONAL_HEADER_DIRS ${LLVM_MAIN_INCLUDE_DIR}/llvm/Passes Index: lib/Passes/PassPluginLoader.cpp =================================================================== --- /dev/null +++ lib/Passes/PassPluginLoader.cpp @@ -0,0 +1,44 @@ +//===- lib/Passes/PassPluginLoader.cpp - Load Plugins for New PM Passes ---===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Passes/PassPluginLoader.h" +#include "llvm/ADT/Optional.h" +#include "llvm/Passes/PassPlugin.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; + +Optional Plugin::Load(const std::string &Filename) { + std::string Error; + auto Library = + sys::DynamicLibrary::getPermanentLibrary(Filename.c_str(), &Error); + if (!Library.isValid()) { + errs() << "Error opening '" << Filename << "': " << Error << "\n"; + return None; + } + + Plugin P{Filename, Library}; + auto *getDetailsFn = Library.SearchForAddressOfSymbol("llvmPluginGetInfo"); + + if (!getDetailsFn) + // If the symbol isn't found, this is probably a legacy plugin. Just treat + // it as such. + return P; + + P.Info = reinterpret_cast(getDetailsFn)(); + + if (P.Info.APIVersion != LLVM_PLUGIN_API_VERSION) { + errs() << "Wrong API version on plugin '" << Filename << "'. Got version " + << P.Info.APIVersion << ", supported version is " + << LLVM_PLUGIN_API_VERSION << ".\n"; + return None; + } + + return P; +} Index: tools/opt/NewPMDriver.cpp =================================================================== --- tools/opt/NewPMDriver.cpp +++ tools/opt/NewPMDriver.cpp @@ -27,6 +27,8 @@ #include "llvm/IR/PassManager.h" #include "llvm/IR/Verifier.h" #include "llvm/Passes/PassBuilder.h" +#include "llvm/Passes/PassPlugin.h" +#include "llvm/Passes/PassPluginLoader.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ToolOutputFile.h" @@ -41,6 +43,9 @@ DebugPM("debug-pass-manager", cl::Hidden, cl::desc("Print pass management debugging information")); +static cl::list + PassPlugins("load-plugin", cl::desc("Load passes from plugin library")); + // This flag specifies a textual description of the alias analysis pipeline to // use when querying for aliasing information. It only works in concert with // the "passes" flag above. @@ -210,6 +215,16 @@ PassBuilder PB(TM, P); registerEPCallbacks(PB, VerifyEachPass, DebugPM); + // Call pass plugins + for (auto &PluginFN : PassPlugins) { + Optional PassPlugin = Plugin::Load(PluginFN); + if (!PassPlugin) + errs() << "Failed to load passes from '" << PluginFN + << "'. Request ignored.\n"; + else + PassPlugin->getInfo().RegisterPassBuilderCallbacks(PB); + } + // Register a callback that creates the debugify passes as needed. PB.registerPipelineParsingCallback( [](StringRef Name, ModulePassManager &MPM, Index: unittests/CMakeLists.txt =================================================================== --- unittests/CMakeLists.txt +++ unittests/CMakeLists.txt @@ -22,6 +22,7 @@ add_subdirectory(BinaryFormat) add_subdirectory(ObjectYAML) add_subdirectory(Option) +add_subdirectory(Passes) add_subdirectory(ProfileData) add_subdirectory(Support) add_subdirectory(Target) Index: unittests/Passes/CMakeLists.txt =================================================================== --- /dev/null +++ unittests/Passes/CMakeLists.txt @@ -0,0 +1,21 @@ +set(LLVM_LINK_COMPONENTS Support Passes Core) + +add_llvm_unittest(PluginsTests PluginsTest.cpp) +export_executable_symbols(PluginsTests) + +add_library(TestPlugin SHARED Plugin.cxx) + +set_output_directory(TestPlugin + BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR} + LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR} + ) + +set_target_properties(TestPlugin + PROPERTIES PREFIX "" + SUFFIX ".so" + ) + +llvm_map_components_to_libnames(LLVMDependencies ${LLVM_LINK_COMPONENTS}) +target_link_libraries(TestPlugin ${LLVMDependencies}) + +add_dependencies(PluginsTests TestPlugin) Index: unittests/Passes/Plugin.cxx =================================================================== --- /dev/null +++ unittests/Passes/Plugin.cxx @@ -0,0 +1,36 @@ +//===- unittests/Passes/Plugins/Plugin.cxx --------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Config/config.h" +#include "llvm/Passes/PassBuilder.h" +#include "llvm/Passes/PassPlugin.h" + +#include "TestPlugin.h" + +using namespace llvm; + +struct TestModulePass : public PassInfoMixin { + PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) { + return PreservedAnalyses::all(); + } +}; + +void registerCallbacks(PassBuilder &PB) { + PB.registerPipelineParsingCallback( + [](StringRef Name, ModulePassManager &PM, + ArrayRef InnerPipeline) { + if (Name == "plugin-pass") { + PM.addPass(TestModulePass()); + return true; + } + return false; + }); +} + +LLVM_PLUGIN(TEST_PLUGIN_NAME, TEST_PLUGIN_VERSION, registerCallbacks) Index: unittests/Passes/PluginsTest.cpp =================================================================== --- /dev/null +++ unittests/Passes/PluginsTest.cpp @@ -0,0 +1,60 @@ +//===- unittests/Passes/Plugins/PluginsTest.cpp ---------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/CGSCCPassManager.h" +#include "llvm/Config/config.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Passes/PassBuilder.h" +#include "llvm/Passes/PassPlugin.h" +#include "llvm/Passes/PassPluginLoader.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/Path.h" +#include "llvm/Transforms/Scalar/LoopPassManager.h" +#include "gtest/gtest.h" + +#include "TestPlugin.h" + +using namespace llvm; + +void anchor() {} + +static std::string LibPath(const std::string Name = "TestPlugin") { + const std::vector &Argvs = + testing::internal::GetArgvs(); + const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "PluginsTests"; + void *Ptr = (void *)anchor; + std::string Path = sys::fs::getMainExecutable(Argv0, Ptr); + llvm::SmallString<256> Buf{sys::path::parent_path(Path)}; + sys::path::append(Buf, (Name + ".so").c_str()); + return Buf.str(); +} + +static std::string StdString(const char *c_str) { return c_str ? c_str : ""; } + +TEST(PluginsTests, LoadPlugin) { + auto PluginPath = LibPath(); + ASSERT_NE("", PluginPath); + + auto PassPlugin = Plugin::Load(PluginPath); + ASSERT_TRUE(PassPlugin) << "Plugin path: " << PluginPath; + + const auto &Info = PassPlugin->getInfo(); + + ASSERT_EQ(TEST_PLUGIN_NAME, StdString(Info.PluginName)); + ASSERT_EQ(TEST_PLUGIN_VERSION, StdString(Info.PluginVersion)); + ASSERT_NE(nullptr, Info.RegisterPassBuilderCallbacks); + + PassBuilder PB; + ModulePassManager PM; + ASSERT_FALSE(PB.parsePassPipeline(PM, "plugin-pass")); + + Info.RegisterPassBuilderCallbacks(PB); + ASSERT_TRUE(PB.parsePassPipeline(PM, "plugin-pass")); +} Index: unittests/Passes/TestPlugin.h =================================================================== --- /dev/null +++ unittests/Passes/TestPlugin.h @@ -0,0 +1,2 @@ +#define TEST_PLUGIN_NAME "TestPlugin" +#define TEST_PLUGIN_VERSION "0.1-unit"