summaryrefslogtreecommitdiffstats
path: root/llvm/unittests
diff options
context:
space:
mode:
authorPhilip Pfaffe <philip.pfaffe@gmail.com>2018-04-05 11:29:37 +0000
committerPhilip Pfaffe <philip.pfaffe@gmail.com>2018-04-05 11:29:37 +0000
commite8f3ae9da07c47d5ee4d351a8085385e3df9390d (patch)
treeaeda50afb6b8bb71c6ff0ec65caed91f45fc167a /llvm/unittests
parente88b76a98995c08806b1fdc7a6b43b96fa709c60 (diff)
downloadbcm5719-llvm-e8f3ae9da07c47d5ee4d351a8085385e3df9390d.tar.gz
bcm5719-llvm-e8f3ae9da07c47d5ee4d351a8085385e3df9390d.zip
[Plugins] Add a slim plugin API to work together with the new PM
Summary: Add a new plugin API. This closes the gap between pass registration and out-of-tree passes for the new PassManager. Unlike with the existing API, interaction with a plugin is always initiated from the tools perspective. I.e., when a plugin is loaded, it resolves and calls a well-known symbol `llvmGetPassPluginInfo` to obtain details about the plugin. The fundamental motivation is to get rid of as many global constructors as possible. The API exposed by the plugin info is kept intentionally minimal. Reviewers: chandlerc Reviewed By: chandlerc Subscribers: bollu, grosser, lksbhm, mgorny, llvm-commits Differential Revision: https://reviews.llvm.org/D35258 llvm-svn: 329273
Diffstat (limited to 'llvm/unittests')
-rw-r--r--llvm/unittests/CMakeLists.txt1
-rw-r--r--llvm/unittests/Passes/CMakeLists.txt21
-rw-r--r--llvm/unittests/Passes/PluginsTest.cpp53
-rw-r--r--llvm/unittests/Passes/TestPlugin.cxx39
-rw-r--r--llvm/unittests/Passes/TestPlugin.h2
5 files changed, 116 insertions, 0 deletions
diff --git a/llvm/unittests/CMakeLists.txt b/llvm/unittests/CMakeLists.txt
index 94aca056625..f266b2fb3ad 100644
--- a/llvm/unittests/CMakeLists.txt
+++ b/llvm/unittests/CMakeLists.txt
@@ -22,6 +22,7 @@ add_subdirectory(Object)
add_subdirectory(BinaryFormat)
add_subdirectory(ObjectYAML)
add_subdirectory(Option)
+add_subdirectory(Passes)
add_subdirectory(ProfileData)
add_subdirectory(Support)
add_subdirectory(Target)
diff --git a/llvm/unittests/Passes/CMakeLists.txt b/llvm/unittests/Passes/CMakeLists.txt
new file mode 100644
index 00000000000..37daed356da
--- /dev/null
+++ b/llvm/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 TestPlugin.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)
diff --git a/llvm/unittests/Passes/PluginsTest.cpp b/llvm/unittests/Passes/PluginsTest.cpp
new file mode 100644
index 00000000000..df1ba1c5b2b
--- /dev/null
+++ b/llvm/unittests/Passes/PluginsTest.cpp
@@ -0,0 +1,53 @@
+//===- 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/IR/PassManager.h"
+#include "llvm/Passes/PassBuilder.h"
+#include "llvm/Passes/PassPlugin.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<testing::internal::string> &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();
+}
+
+TEST(PluginsTests, LoadPlugin) {
+ auto PluginPath = LibPath();
+ ASSERT_NE("", PluginPath);
+
+ Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath);
+ ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath;
+
+ ASSERT_EQ(TEST_PLUGIN_NAME, Plugin->getPluginName());
+ ASSERT_EQ(TEST_PLUGIN_VERSION, Plugin->getPluginVersion());
+
+ PassBuilder PB;
+ ModulePassManager PM;
+ ASSERT_FALSE(PB.parsePassPipeline(PM, "plugin-pass"));
+
+ Plugin->registerPassBuilderCallbacks(PB);
+ ASSERT_TRUE(PB.parsePassPipeline(PM, "plugin-pass"));
+}
diff --git a/llvm/unittests/Passes/TestPlugin.cxx b/llvm/unittests/Passes/TestPlugin.cxx
new file mode 100644
index 00000000000..adb666e69d7
--- /dev/null
+++ b/llvm/unittests/Passes/TestPlugin.cxx
@@ -0,0 +1,39 @@
+//===- 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/Passes/PassBuilder.h"
+#include "llvm/Passes/PassPlugin.h"
+
+#include "TestPlugin.h"
+
+using namespace llvm;
+
+struct TestModulePass : public PassInfoMixin<TestModulePass> {
+ PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) {
+ return PreservedAnalyses::all();
+ }
+};
+
+void registerCallbacks(PassBuilder &PB) {
+ PB.registerPipelineParsingCallback(
+ [](StringRef Name, ModulePassManager &PM,
+ ArrayRef<PassBuilder::PipelineElement> InnerPipeline) {
+ if (Name == "plugin-pass") {
+ PM.addPass(TestModulePass());
+ return true;
+ }
+ return false;
+ });
+}
+
+extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK LLVM_PLUGIN_EXPORT
+llvmGetPassPluginInfo() {
+ return {LLVM_PLUGIN_API_VERSION, TEST_PLUGIN_NAME, TEST_PLUGIN_VERSION,
+ registerCallbacks};
+}
diff --git a/llvm/unittests/Passes/TestPlugin.h b/llvm/unittests/Passes/TestPlugin.h
new file mode 100644
index 00000000000..801a89065cd
--- /dev/null
+++ b/llvm/unittests/Passes/TestPlugin.h
@@ -0,0 +1,2 @@
+#define TEST_PLUGIN_NAME "TestPlugin"
+#define TEST_PLUGIN_VERSION "0.1-unit"
OpenPOWER on IntegriCloud