xref: /llvm-project/llvm/unittests/Passes/Plugins/PluginsTest.cpp (revision 74deadf19650f6f3b6392ba09caa20dd38ae41e0)
1e281d102Sibricchi //===- unittests/Passes/Plugins/PluginsTest.cpp ---------------------------===//
2e281d102Sibricchi //
3e281d102Sibricchi // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e281d102Sibricchi // See https://llvm.org/LICENSE.txt for license information.
5e281d102Sibricchi // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e281d102Sibricchi //
7e281d102Sibricchi //===----------------------------------------------------------------------===//
8e281d102Sibricchi 
9e281d102Sibricchi #include "llvm/Analysis/CGSCCPassManager.h"
10e281d102Sibricchi #include "llvm/AsmParser/Parser.h"
11e281d102Sibricchi #include "llvm/Config/config.h"
12e281d102Sibricchi #include "llvm/IR/GlobalVariable.h"
13*74deadf1SNikita Popov #include "llvm/IR/Module.h"
14e281d102Sibricchi #include "llvm/IR/PassManager.h"
15e281d102Sibricchi #include "llvm/Passes/PassBuilder.h"
16e281d102Sibricchi #include "llvm/Passes/PassPlugin.h"
17e281d102Sibricchi #include "llvm/Support/FileSystem.h"
18e281d102Sibricchi #include "llvm/Support/Path.h"
19e281d102Sibricchi #include "llvm/Support/SourceMgr.h"
20e281d102Sibricchi #include "llvm/Testing/Support/Error.h"
21e281d102Sibricchi #include "llvm/Transforms/Scalar/LoopPassManager.h"
22e281d102Sibricchi #include "gtest/gtest.h"
23e281d102Sibricchi 
24e281d102Sibricchi #include "TestPlugin.h"
25e281d102Sibricchi 
26e281d102Sibricchi #include <cstdint>
27e281d102Sibricchi 
28e281d102Sibricchi using namespace llvm;
29e281d102Sibricchi 
anchor()30e281d102Sibricchi void anchor() {}
31e281d102Sibricchi 
LibPath(const std::string Name="TestPlugin")32e281d102Sibricchi static std::string LibPath(const std::string Name = "TestPlugin") {
33e281d102Sibricchi   const auto &Argvs = testing::internal::GetArgvs();
34e281d102Sibricchi   const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "PluginsTests";
35e281d102Sibricchi   void *Ptr = (void *)(intptr_t)anchor;
36e281d102Sibricchi   std::string Path = sys::fs::getMainExecutable(Argv0, Ptr);
37e281d102Sibricchi   llvm::SmallString<256> Buf{sys::path::parent_path(Path)};
38e281d102Sibricchi   sys::path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str());
39e281d102Sibricchi   return std::string(Buf.str());
40e281d102Sibricchi }
41e281d102Sibricchi 
TEST(PluginsTests,LoadPlugin)42e281d102Sibricchi TEST(PluginsTests, LoadPlugin) {
43e281d102Sibricchi #if !defined(LLVM_ENABLE_PLUGINS)
44e281d102Sibricchi   // Skip the test if plugins are disabled.
45e281d102Sibricchi   GTEST_SKIP();
46e281d102Sibricchi #endif
47e281d102Sibricchi 
48e281d102Sibricchi   auto PluginPath = LibPath();
49e281d102Sibricchi   ASSERT_NE("", PluginPath);
50e281d102Sibricchi 
51e281d102Sibricchi   Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath);
52e281d102Sibricchi   ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath;
53e281d102Sibricchi 
54e281d102Sibricchi   ASSERT_EQ(TEST_PLUGIN_NAME, Plugin->getPluginName());
55e281d102Sibricchi   ASSERT_EQ(TEST_PLUGIN_VERSION, Plugin->getPluginVersion());
56e281d102Sibricchi 
57e281d102Sibricchi   PassBuilder PB;
58e281d102Sibricchi   ModulePassManager PM;
59e281d102Sibricchi   ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Failed());
60e281d102Sibricchi 
61e281d102Sibricchi   Plugin->registerPassBuilderCallbacks(PB);
62e281d102Sibricchi   ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Succeeded());
63e281d102Sibricchi }
64e281d102Sibricchi 
65e281d102Sibricchi // Test that llvmGetPassPluginInfo from DoublerPlugin is called twice with
66e281d102Sibricchi // -fpass-plugin=DoublerPlugin -fpass-plugin=TestPlugin
67e281d102Sibricchi // -fpass-plugin=DoublerPlugin.
TEST(PluginsTests,LoadMultiplePlugins)68e281d102Sibricchi TEST(PluginsTests, LoadMultiplePlugins) {
69e281d102Sibricchi #if !defined(LLVM_ENABLE_PLUGINS)
70e281d102Sibricchi   // Skip the test if plugins are disabled.
71e281d102Sibricchi   GTEST_SKIP();
72e281d102Sibricchi #endif
73e281d102Sibricchi 
74e281d102Sibricchi   auto DoublerPluginPath = LibPath("DoublerPlugin");
75e281d102Sibricchi   auto TestPluginPath = LibPath("TestPlugin");
76e281d102Sibricchi   ASSERT_NE("", DoublerPluginPath);
77e281d102Sibricchi   ASSERT_NE("", TestPluginPath);
78e281d102Sibricchi 
79e281d102Sibricchi   Expected<PassPlugin> DoublerPlugin1 = PassPlugin::Load(DoublerPluginPath);
80e281d102Sibricchi   ASSERT_TRUE(!!DoublerPlugin1)
81e281d102Sibricchi       << "Plugin path: " << DoublerPlugin1->getFilename();
82e281d102Sibricchi 
83e281d102Sibricchi   Expected<PassPlugin> TestPlugin = PassPlugin::Load(TestPluginPath);
84e281d102Sibricchi   ASSERT_TRUE(!!TestPlugin) << "Plugin path: " << TestPlugin->getFilename();
85e281d102Sibricchi 
86e281d102Sibricchi   // If llvmGetPassPluginInfo is resolved as a weak symbol taking into account
87e281d102Sibricchi   // all loaded symbols, the second call to PassPlugin::Load will actually
88e281d102Sibricchi   // return the llvmGetPassPluginInfo from the most recently loaded plugin, in
89e281d102Sibricchi   // this case TestPlugin.
90e281d102Sibricchi   Expected<PassPlugin> DoublerPlugin2 = PassPlugin::Load(DoublerPluginPath);
91e281d102Sibricchi   ASSERT_TRUE(!!DoublerPlugin2)
92e281d102Sibricchi       << "Plugin path: " << DoublerPlugin2->getFilename();
93e281d102Sibricchi 
94e281d102Sibricchi   ASSERT_EQ("DoublerPlugin", DoublerPlugin1->getPluginName());
95e281d102Sibricchi   ASSERT_EQ("2.2-unit", DoublerPlugin1->getPluginVersion());
96e281d102Sibricchi   ASSERT_EQ(TEST_PLUGIN_NAME, TestPlugin->getPluginName());
97e281d102Sibricchi   ASSERT_EQ(TEST_PLUGIN_VERSION, TestPlugin->getPluginVersion());
98e281d102Sibricchi   // Check that the plugin name/version is set correctly when loaded a second
99e281d102Sibricchi   // time
100e281d102Sibricchi   ASSERT_EQ("DoublerPlugin", DoublerPlugin2->getPluginName());
101e281d102Sibricchi   ASSERT_EQ("2.2-unit", DoublerPlugin2->getPluginVersion());
102e281d102Sibricchi 
103e281d102Sibricchi   PassBuilder PB;
104e281d102Sibricchi   ModulePassManager PM;
105e281d102Sibricchi   const char *PipelineText = "module(doubler-pass,plugin-pass,doubler-pass)";
106e281d102Sibricchi   ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, PipelineText), Failed());
107e281d102Sibricchi   TestPlugin->registerPassBuilderCallbacks(PB);
108e281d102Sibricchi   DoublerPlugin1->registerPassBuilderCallbacks(PB);
109e281d102Sibricchi   DoublerPlugin2->registerPassBuilderCallbacks(PB);
110e281d102Sibricchi   ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, PipelineText), Succeeded());
111e281d102Sibricchi 
112e281d102Sibricchi   LLVMContext C;
113e281d102Sibricchi   SMDiagnostic Err;
114e281d102Sibricchi   std::unique_ptr<Module> M =
115e281d102Sibricchi       parseAssemblyString(R"IR(@doubleme = constant i32 7)IR", Err, C);
116e281d102Sibricchi 
117e281d102Sibricchi   // Check that the initial value is 7
118e281d102Sibricchi   {
119e281d102Sibricchi     auto *GV = M->getNamedValue("doubleme");
120e281d102Sibricchi     auto *Init = cast<GlobalVariable>(GV)->getInitializer();
121e281d102Sibricchi     auto *CI = cast<ConstantInt>(Init);
122e281d102Sibricchi     ASSERT_EQ(CI->getSExtValue(), 7);
123e281d102Sibricchi   }
124e281d102Sibricchi 
125e281d102Sibricchi   ModuleAnalysisManager MAM;
126e281d102Sibricchi   // Register required pass instrumentation analysis.
127e281d102Sibricchi   MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
128e281d102Sibricchi   PM.run(*M, MAM);
129e281d102Sibricchi 
130e281d102Sibricchi   // Check that the final value is 28 because DoublerPlugin::run was called
131e281d102Sibricchi   // twice, indicating that the llvmGetPassPluginInfo and registerCallbacks
132e281d102Sibricchi   // were correctly called.
133e281d102Sibricchi   {
134e281d102Sibricchi     // Check the value was doubled twice
135e281d102Sibricchi     auto *GV = M->getNamedValue("doubleme");
136e281d102Sibricchi     auto *Init = cast<GlobalVariable>(GV)->getInitializer();
137e281d102Sibricchi     auto *CI = cast<ConstantInt>(Init);
138e281d102Sibricchi     ASSERT_EQ(CI->getSExtValue(), 28);
139e281d102Sibricchi   }
140e281d102Sibricchi }
141