| 1 |
//===----------------------------------------------------------------------===// |
| 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-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "Plugin/CodeGenTestPass.h" |
| 10 |
|
| 11 |
#include "llvm/CodeGen/MachineModuleInfo.h" |
| 12 |
#include "llvm/CodeGen/Passes.h" |
| 13 |
#include "llvm/CodeGen/TargetPassConfig.h" |
| 14 |
#include "llvm/Config/config.h" |
| 15 |
#include "llvm/IR/LegacyPassManager.h" |
| 16 |
#include "llvm/MC/TargetRegistry.h" |
| 17 |
#include "llvm/Support/DynamicLibrary.h" |
| 18 |
#include "llvm/Support/FileSystem.h" |
| 19 |
#include "llvm/Support/Path.h" |
| 20 |
#include "llvm/Support/TargetSelect.h" |
| 21 |
#include "llvm/Target/RegisterTargetPassConfigCallback.h" |
| 22 |
#include "llvm/Target/TargetMachine.h" |
| 23 |
|
| 24 |
#include "gtest/gtest.h" |
| 25 |
|
| 26 |
using namespace llvm; |
| 27 |
|
| 28 |
namespace { |
| 29 |
void anchor() {} |
| 30 |
|
| 31 |
std::string libPath(const std::string &Name) { |
| 32 |
const auto &Argvs = testing::internal::GetArgvs(); |
| 33 |
const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "CGPluginTest"; |
| 34 |
void *Ptr = (void *)(intptr_t)anchor; |
| 35 |
std::string Path = sys::fs::getMainExecutable(Argv0, Ptr); |
| 36 |
SmallString<256> Buf{sys::path::parent_path(Path)}; |
| 37 |
sys::path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str()); |
| 38 |
return std::string(Buf.str()); |
| 39 |
} |
| 40 |
} // namespace |
| 41 |
|
| 42 |
namespace llvm { |
| 43 |
class CGPluginTests : public testing::Test { |
| 44 |
protected: |
| 45 |
static void SetUpTestCase() { |
| 46 |
InitializeAllTargetMCs(); |
| 47 |
InitializeAllTargetInfos(); |
| 48 |
InitializeAllTargets(); |
| 49 |
} |
| 50 |
}; |
| 51 |
} // namespace llvm |
| 52 |
|
| 53 |
TEST_F(CGPluginTests, LoadPlugin) { |
| 54 |
#if !defined(LLVM_ENABLE_PLUGINS) |
| 55 |
// Skip the test if plugins are disabled. |
| 56 |
GTEST_SKIP(); |
| 57 |
#endif |
| 58 |
|
| 59 |
auto PluginPath = libPath("CGTestPlugin"); |
| 60 |
ASSERT_NE("", PluginPath); |
| 61 |
|
| 62 |
std::string Error; |
| 63 |
auto Library = sys::DynamicLibrary::getLibrary(PluginPath.c_str(), &Error); |
| 64 |
ASSERT_TRUE(Library.isValid()) << Error; |
| 65 |
sys::DynamicLibrary::closeLibrary(Library); |
| 66 |
} |
| 67 |
|
| 68 |
TEST_F(CGPluginTests, ExecuteCallback) { |
| 69 |
#if !defined(LLVM_ENABLE_PLUGINS) |
| 70 |
// Skip the test if plugins are disabled. |
| 71 |
GTEST_SKIP(); |
| 72 |
#endif |
| 73 |
|
| 74 |
volatile bool CallbackExecuted = false; |
| 75 |
volatile bool MPassExecuted = false; |
| 76 |
|
| 77 |
RegisterTargetPassConfigCallback X{[&](auto &TM, auto &PM, auto *TPC) { |
| 78 |
CallbackExecuted = true; |
| 79 |
TPC->insertPass(&GCLoweringID, &CodeGenTest::ID); |
| 80 |
}}; |
| 81 |
|
| 82 |
CodeGenTest::RunCallback = [&] { MPassExecuted = true; }; |
| 83 |
|
| 84 |
TargetOptions Options; |
| 85 |
std::unique_ptr<MCContext> MCC; |
| 86 |
for (auto T : TargetRegistry::targets()) { |
| 87 |
if (!T.hasTargetMachine()) |
| 88 |
continue; |
| 89 |
Triple TT{T.getName(), "", ""}; |
| 90 |
auto *TM = T.createTargetMachine(TT, "", "", Options, std::nullopt, |
| 91 |
std::nullopt, CodeGenOptLevel::Default); |
| 92 |
ASSERT_TRUE(TM); |
| 93 |
|
| 94 |
legacy::PassManager PM; |
| 95 |
MCC.reset(new MCContext(TT, TM->getMCAsmInfo(), TM->getMCRegisterInfo(), |
| 96 |
TM->getMCSubtargetInfo())); |
| 97 |
auto *PtrMCC = MCC.get(); |
| 98 |
CallbackExecuted = false; |
| 99 |
MPassExecuted = false; |
| 100 |
if (TM->addPassesToEmitMC(PM, PtrMCC, outs()) == true) |
| 101 |
continue; |
| 102 |
ASSERT_TRUE(CallbackExecuted) << T.getName() << " callback failed"; |
| 103 |
ASSERT_TRUE(MPassExecuted) << T.getName() << " MachinePass failed"; |
| 104 |
} |
| 105 |
} |