1 | #include "llvm/Target/TargetOptions.h" |
2 | #include "llvm/CodeGen/TargetPassConfig.h" |
3 | #include "llvm/IR/LLVMContext.h" |
4 | #include "llvm/IR/LegacyPassManager.h" |
5 | #include "llvm/InitializePasses.h" |
6 | #include "llvm/MC/TargetRegistry.h" |
7 | #include "llvm/Support/TargetSelect.h" |
8 | #include "llvm/Target/TargetMachine.h" |
9 | #include "gtest/gtest.h" |
10 | |
11 | using namespace llvm; |
12 | |
13 | namespace llvm { |
14 | void initializeTestPassPass(PassRegistry &); |
15 | } |
16 | |
17 | namespace { |
18 | |
19 | void initLLVM() { |
20 | InitializeAllTargets(); |
21 | InitializeAllTargetMCs(); |
22 | InitializeAllAsmPrinters(); |
23 | InitializeAllAsmParsers(); |
24 | |
25 | PassRegistry *Registry = PassRegistry::getPassRegistry(); |
26 | initializeCore(*Registry); |
27 | initializeCodeGen(*Registry); |
28 | } |
29 | |
30 | /// Create a TargetMachine. We need a target that doesn't have IPRA enabled by |
31 | /// default. That turns out to be all targets at the moment, so just use X86. |
32 | std::unique_ptr<TargetMachine> createTargetMachine(bool EnableIPRA) { |
33 | Triple TargetTriple("x86_64--" ); |
34 | std::string Error; |
35 | const Target *T = TargetRegistry::lookupTarget(ArchName: "" , TheTriple&: TargetTriple, Error); |
36 | if (!T) |
37 | return nullptr; |
38 | |
39 | TargetOptions Options; |
40 | Options.EnableIPRA = EnableIPRA; |
41 | return std::unique_ptr<TargetMachine>( |
42 | T->createTargetMachine(TT: "X86" , CPU: "" , Features: "" , Options, RM: std::nullopt, CM: std::nullopt, |
43 | OL: CodeGenOptLevel::Aggressive)); |
44 | } |
45 | |
46 | typedef std::function<void(bool)> TargetOptionsTest; |
47 | |
48 | static void targetOptionsTest(bool EnableIPRA) { |
49 | std::unique_ptr<TargetMachine> TM = createTargetMachine(EnableIPRA); |
50 | // This test is designed for the X86 backend; stop if it is not available. |
51 | if (!TM) |
52 | GTEST_SKIP(); |
53 | legacy::PassManager PM; |
54 | LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(TM.get()); |
55 | |
56 | TargetPassConfig *TPC = LLVMTM->createPassConfig(PM); |
57 | (void)TPC; |
58 | |
59 | ASSERT_TRUE(TM->Options.EnableIPRA == EnableIPRA); |
60 | |
61 | delete TPC; |
62 | } |
63 | |
64 | } // End of anonymous namespace. |
65 | |
66 | TEST(TargetOptionsTest, IPRASetToOff) { |
67 | targetOptionsTest(EnableIPRA: false); |
68 | } |
69 | |
70 | TEST(TargetOptionsTest, IPRASetToOn) { |
71 | targetOptionsTest(EnableIPRA: true); |
72 | } |
73 | |
74 | int main(int argc, char **argv) { |
75 | ::testing::InitGoogleTest(argc: &argc, argv); |
76 | initLLVM(); |
77 | return RUN_ALL_TESTS(); |
78 | } |
79 | |