Index: include/llvm/Support/Plugin.h =================================================================== --- /dev/null +++ include/llvm/Support/Plugin.h @@ -0,0 +1,48 @@ +//===-- llvm/Support/PluginLoader.h - Plugin Loader for Tools ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_PLUGIN_H +#define LLVM_SUPPORT_PLUGIN_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_SUPPORT_PLUGIN_H */ Index: include/llvm/Support/PluginLoader.h =================================================================== --- include/llvm/Support/PluginLoader.h +++ include/llvm/Support/PluginLoader.h @@ -20,15 +20,18 @@ #include "llvm/Support/CommandLine.h" namespace llvm { - struct PluginLoader { - void operator=(const std::string &Filename); - static unsigned getNumPlugins(); - static std::string& getPlugin(unsigned num); - }; +struct PluginInfo; + +struct PluginLoader { + void operator=(const std::string &Filename); + static unsigned getNumPlugins(); + static std::string &getPlugin(unsigned num); + static const PluginInfo &getPluginInfo(unsigned num); +}; #ifndef DONT_GET_PLUGIN_LOADER_OPTION - // This causes operator= above to be invoked for every -load option. - static cl::opt > +// This causes operator= above to be invoked for every -load option. +static cl::opt> LoadOpt("load", cl::ZeroOrMore, cl::value_desc("pluginfilename"), cl::desc("Load the specified plugin")); #endif Index: lib/Support/PluginLoader.cpp =================================================================== --- lib/Support/PluginLoader.cpp +++ lib/Support/PluginLoader.cpp @@ -16,22 +16,69 @@ #include "llvm/Support/DynamicLibrary.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/Mutex.h" +#include "llvm/Support/Plugin.h" #include "llvm/Support/raw_ostream.h" #include using namespace llvm; -static ManagedStatic > Plugins; -static ManagedStatic > PluginsLock; +class Plugin { +public: + static Optional Load(const std::string &Filename, + const sys::DynamicLibrary &Library); + 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) {} + std::string Filename; + sys::DynamicLibrary Library; + PluginInfo Info; +}; + +static ManagedStatic> Plugins; +static ManagedStatic> PluginsLock; + +Optional Plugin::Load(const std::string &Filename, + const sys::DynamicLibrary &Library) { + 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; +} void PluginLoader::operator=(const std::string &Filename) { sys::SmartScopedLock Lock(*PluginsLock); std::string Error; - if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) { + auto Library = + sys::DynamicLibrary::getPermanentLibrary(Filename.c_str(), &Error); + if (!Library.isValid()) { errs() << "Error opening '" << Filename << "': " << Error << "\n -load request ignored.\n"; - } else { - Plugins->push_back(Filename); + return; + } + + auto P = Plugin::Load(Filename, Library); + if (!P) { + errs() << "Failed to load plugin '" << Filename << "'\n"; + return; } + + Plugins->emplace_back(std::move(*P)); } unsigned PluginLoader::getNumPlugins() { @@ -43,5 +90,11 @@ sys::SmartScopedLock Lock(*PluginsLock); assert(Plugins.isConstructed() && num < Plugins->size() && "Asking for an out of bounds plugin"); - return (*Plugins)[num]; + return (*Plugins)[num].getFilename(); +} +const PluginInfo &PluginLoader::getPluginInfo(unsigned num) { + sys::SmartScopedLock Lock(*PluginsLock); + assert(Plugins.isConstructed() && num < Plugins->size() && + "Asking for an out of bounds plugin"); + return (*Plugins)[num].getInfo(); } Index: tools/opt/NewPMDriver.cpp =================================================================== --- tools/opt/NewPMDriver.cpp +++ tools/opt/NewPMDriver.cpp @@ -32,6 +32,10 @@ #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" #include "llvm/Transforms/Scalar/LoopPassManager.h" +#define DONT_GET_PLUGIN_LOADER_OPTION +#include "llvm/Support/Plugin.h" +#include "llvm/Support/PluginLoader.h" + using namespace llvm; using namespace opt_tool; @@ -156,6 +160,10 @@ PassBuilder PB(TM); registerEPCallbacks(PB, VerifyEachPass, DebugPM); + for (unsigned p = 0; p < PluginLoader::getNumPlugins(); ++p) + if (PluginLoader::getPluginInfo(p).RegisterPassBuilderCallbacks) + PluginLoader::getPluginInfo(p).RegisterPassBuilderCallbacks(PB); + // Specially handle the alias analysis manager so that we can register // a custom pipeline of AA passes with it. AAManager AA; Index: unittests/Support/CMakeLists.txt =================================================================== --- unittests/Support/CMakeLists.txt +++ unittests/Support/CMakeLists.txt @@ -76,3 +76,4 @@ target_link_libraries(SupportTests LLVMTestingSupport ${LLVM_PTHREAD_LIB}) add_subdirectory(DynamicLibrary) +add_subdirectory(Plugins) Index: unittests/Support/Plugins/CMakeLists.txt =================================================================== --- /dev/null +++ unittests/Support/Plugins/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/Support/Plugins/Plugin.cxx =================================================================== --- /dev/null +++ unittests/Support/Plugins/Plugin.cxx @@ -0,0 +1,27 @@ +#include "llvm/Support/Plugin.h" +#include "llvm/Config/config.h" +#include "llvm/Passes/PassBuilder.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/Support/Plugins/PluginsTest.cpp =================================================================== --- /dev/null +++ unittests/Support/Plugins/PluginsTest.cpp @@ -0,0 +1,64 @@ +//===- llvm/unittest/Support/Plugins/PluginsTest.cpp --------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#define DONT_GET_PLUGIN_LOADER_OPTION +#include "llvm/Analysis/CGSCCPassManager.h" +#include "llvm/Config/config.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Passes/PassBuilder.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Plugin.h" +#include "llvm/Support/PluginLoader.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) { + ASSERT_EQ(0u, PluginLoader::getNumPlugins()); + + auto PluginPath = LibPath(); + ASSERT_NE("", PluginPath); + + PluginLoader() = PluginPath; + ASSERT_EQ(1u, PluginLoader::getNumPlugins()) << "Plugin path: " << PluginPath; + + ASSERT_EQ(PluginPath, PluginLoader::getPlugin(0)); + const auto &Info = PluginLoader::getPluginInfo(0); + + 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/Support/Plugins/TestPlugin.h =================================================================== --- /dev/null +++ unittests/Support/Plugins/TestPlugin.h @@ -0,0 +1,2 @@ +#define TEST_PLUGIN_NAME "TestPlugin" +#define TEST_PLUGIN_VERSION "0.1-unit"