Skip to content

Commit 07cda25

Browse files
committed
Restore ability for C++ API users to Enable IPRA.
Summary: Prior to r310876 one of our out-of-tree targets was enabling IPRA by modifying the TargetOptions::EnableIPRA. This no longer works on current trunk since the useIPRA() hook overrides any values that are set in advance. This patch adjusts the behaviour of the hook so that API users and useIPRA() can both enable it but useIPRA() cannot disable it if the API user already enabled it. Reviewers: arsenm Reviewed By: arsenm Subscribers: wdng, mgorny, llvm-commits Differential Revision: https://reviews.llvm.org/D38043 llvm-svn: 354692
1 parent c2d9579 commit 07cda25

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ TargetPassConfig::TargetPassConfig(LLVMTargetMachine &TM, PassManagerBase &pm)
407407
TM.Options.EnableIPRA = EnableIPRA;
408408
else {
409409
// If not explicitly specified, use target default.
410-
TM.Options.EnableIPRA = TM.useIPRA();
410+
TM.Options.EnableIPRA |= TM.useIPRA();
411411
}
412412

413413
if (TM.Options.EnableIPRA)

llvm/unittests/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_llvm_unittest(CodeGenTests
2020
MachineOperandTest.cpp
2121
ScalableVectorMVTsTest.cpp
2222
TypeTraitsTest.cpp
23+
TargetOptionsTest.cpp
2324
)
2425

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

0 commit comments

Comments
 (0)