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