#include "llvm/Analysis/CGSCCPassManager.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/Config/config.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Testing/Support/Error.h"
#include "llvm/Transforms/Scalar/LoopPassManager.h"
#include "gtest/gtest.h"
#include "TestPlugin.h"
#include <cstdint>
using namespace llvm;
void anchor() {}
static std::string LibPath(const std::string Name = "TestPlugin") {
const auto &Argvs = testing::internal::GetArgvs();
const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "PluginsTests";
void *Ptr = (void *)(intptr_t)anchor;
std::string Path = sys::fs::getMainExecutable(Argv0, Ptr);
llvm::SmallString<256> Buf{sys::path::parent_path(Path)};
sys::path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str());
return std::string(Buf.str());
}
TEST(PluginsTests, LoadPlugin) {
#if !defined(LLVM_ENABLE_PLUGINS)
return;
#endif
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_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Failed());
Plugin->registerPassBuilderCallbacks(PB);
ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Succeeded());
}
TEST(PluginsTests, LoadMultiplePlugins) {
#if !defined(LLVM_ENABLE_PLUGINS)
return;
#endif
auto DoublerPluginPath = LibPath("DoublerPlugin");
auto TestPluginPath = LibPath("TestPlugin");
ASSERT_NE("", DoublerPluginPath);
ASSERT_NE("", TestPluginPath);
Expected<PassPlugin> DoublerPlugin1 = PassPlugin::Load(DoublerPluginPath);
ASSERT_TRUE(!!DoublerPlugin1)
<< "Plugin path: " << DoublerPlugin1->getFilename();
Expected<PassPlugin> TestPlugin = PassPlugin::Load(TestPluginPath);
ASSERT_TRUE(!!TestPlugin) << "Plugin path: " << TestPlugin->getFilename();
Expected<PassPlugin> DoublerPlugin2 = PassPlugin::Load(DoublerPluginPath);
ASSERT_TRUE(!!DoublerPlugin2)
<< "Plugin path: " << DoublerPlugin2->getFilename();
ASSERT_EQ("DoublerPlugin", DoublerPlugin1->getPluginName());
ASSERT_EQ("2.2-unit", DoublerPlugin1->getPluginVersion());
ASSERT_EQ(TEST_PLUGIN_NAME, TestPlugin->getPluginName());
ASSERT_EQ(TEST_PLUGIN_VERSION, TestPlugin->getPluginVersion());
ASSERT_EQ("DoublerPlugin", DoublerPlugin2->getPluginName());
ASSERT_EQ("2.2-unit", DoublerPlugin2->getPluginVersion());
PassBuilder PB;
ModulePassManager PM;
const char *PipelineText = "module(doubler-pass,plugin-pass,doubler-pass)";
ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, PipelineText), Failed());
TestPlugin->registerPassBuilderCallbacks(PB);
DoublerPlugin1->registerPassBuilderCallbacks(PB);
DoublerPlugin2->registerPassBuilderCallbacks(PB);
ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, PipelineText), Succeeded());
LLVMContext C;
SMDiagnostic Err;
std::unique_ptr<Module> M =
parseAssemblyString(R"IR(@doubleme = constant i32 7)IR", Err, C);
{
auto *GV = M->getNamedValue("doubleme");
auto *Init = cast<GlobalVariable>(GV)->getInitializer();
auto *CI = cast<ConstantInt>(Init);
ASSERT_EQ(CI->getSExtValue(), 7);
}
ModuleAnalysisManager MAM;
MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
PM.run(*M, MAM);
{
auto *GV = M->getNamedValue("doubleme");
auto *Init = cast<GlobalVariable>(GV)->getInitializer();
auto *CI = cast<ConstantInt>(Init);
ASSERT_EQ(CI->getSExtValue(), 28);
}
}