Skip to content

Commit bed7626

Browse files
committed
[PowerPC][AIX] Make PIC the default relocation model for AIX
Summary: The `llc` tool currently defaults to Static relocation model and generates non-relocatable code for 32-bit Power. This is not desirable on AIX where we always generate Position Independent Code (PIC). This patch makes PIC the default relocation model for AIX. Reviewers: daltenty, hubert.reinterpretcast, DiggerLin, Xiangling_L, sfertile Reviewed By: hubert.reinterpretcast Subscribers: mgorny, wuzish, nemanjai, hiraditya, kbarton, jsji, shchenz, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D72479
1 parent cebba7c commit bed7626

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

llvm/lib/Target/PowerPC/PPCTargetMachine.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,18 @@ static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
223223

224224
static Reloc::Model getEffectiveRelocModel(const Triple &TT,
225225
Optional<Reloc::Model> RM) {
226+
assert((!TT.isOSAIX() || !RM.hasValue() || *RM == Reloc::PIC_) &&
227+
"Invalid relocation model for AIX.");
228+
226229
if (RM.hasValue())
227230
return *RM;
228231

229232
// Darwin defaults to dynamic-no-pic.
230233
if (TT.isOSDarwin())
231234
return Reloc::DynamicNoPIC;
232235

233-
// Big Endian PPC is PIC by default.
234-
if (TT.getArch() == Triple::ppc64)
236+
// Big Endian PPC and AIX default to PIC.
237+
if (TT.getArch() == Triple::ppc64 || TT.isOSAIX())
235238
return Reloc::PIC_;
236239

237240
// Rest are static by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; RUN: llc -mtriple=powerpc-ibm-aix < %s 2>&1 1>/dev/null | FileCheck --allow-empty %s
2+
; RUN: llc -mtriple=powerpc-ibm-aix --relocation-model=pic < %s 2>&1 1>/dev/null | FileCheck --allow-empty %s
3+
; RUN: llc -mtriple=powerpc64-ibm-aix --relocation-model=pic < %s 2>&1 1>/dev/null | FileCheck --allow-empty %s
4+
; RUN: not llc -mtriple=powerpc-ibm-aix --relocation-model=static < %s 2>&1 | FileCheck --check-prefix=CHECK-NON-PIC %s
5+
; RUN: not llc -mtriple=powerpc64-ibm-aix --relocation-model=ropi-rwpi < %s 2>&1 | FileCheck --check-prefix=CHECK-NON-PIC %s
6+
7+
; CHECK-NOT: {{.}}
8+
; CHECK-NON-PIC: invalid relocation model, AIX only supports PIC.

llvm/tools/llc/llc.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,17 @@ static int compileModule(char **argv, LLVMContext &Context) {
461461
Options.MCOptions.IASSearchPaths = IncludeDirs;
462462
Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
463463

464+
// On AIX, setting the relocation model to anything other than PIC is considered
465+
// a user error.
466+
Optional<Reloc::Model> RM = getRelocModel();
467+
if (TheTriple.isOSAIX() && RM.hasValue() && *RM != Reloc::PIC_) {
468+
WithColor::error(errs(), argv[0])
469+
<< "invalid relocation model, AIX only supports PIC.\n";
470+
return 1;
471+
}
472+
464473
std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
465-
TheTriple.getTriple(), CPUStr, FeaturesStr, Options, getRelocModel(),
474+
TheTriple.getTriple(), CPUStr, FeaturesStr, Options, RM,
466475
getCodeModel(), OLvl));
467476

468477
assert(Target && "Could not allocate target machine!");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "llvm/ADT/Triple.h"
2+
#include "llvm/Support/TargetRegistry.h"
3+
#include "llvm/Support/TargetSelect.h"
4+
#include "llvm/Target/TargetMachine.h"
5+
#include "gtest/gtest.h"
6+
7+
using namespace llvm;
8+
9+
namespace {
10+
11+
class AIXRelocModelTest : public ::testing::Test {
12+
protected:
13+
static void SetUpTestCase() {
14+
LLVMInitializePowerPCTargetInfo();
15+
LLVMInitializePowerPCTarget();
16+
LLVMInitializePowerPCTargetMC();
17+
}
18+
};
19+
20+
TEST_F(AIXRelocModelTest, DefalutToPIC) {
21+
Triple TheTriple(/*ArchStr*/ "powerpc", /*VendorStr*/ "", /*OSStr*/ "aix");
22+
std::string Error;
23+
const Target *TheTarget = TargetRegistry::lookupTarget("", TheTriple, Error);
24+
ASSERT_TRUE(TheTarget) << Error;
25+
26+
TargetOptions Options;
27+
// Create a TargetMachine for powerpc--aix target, and deliberately leave its
28+
// relocation model unset.
29+
std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
30+
/*TT*/ TheTriple.getTriple(), /*CPU*/ "", /*Features*/ "",
31+
/*Options*/ Options, /*RM*/ None, /*CM*/ None,
32+
/*OL*/ CodeGenOpt::Default));
33+
ASSERT_TRUE(Target) << "Could not allocate target machine!";
34+
35+
// The relocation model on AIX should be forced to PIC regardless.
36+
EXPECT_TRUE(Target->getRelocationModel() == Reloc::PIC_);
37+
}
38+
39+
} // end of anonymous namespace
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
include_directories(
2+
${CMAKE_SOURCE_DIR}/lib/Target/PowerPC
3+
${CMAKE_BINARY_DIR}/lib/Target/PowerPC
4+
)
5+
6+
set(LLVM_LINK_COMPONENTS
7+
Analysis
8+
CodeGen
9+
Core
10+
MC
11+
MIRParser
12+
Support
13+
Target
14+
PowerPCCodeGen
15+
PowerPCDesc
16+
PowerPCInfo
17+
)
18+
19+
add_llvm_unittest(PowerPCTests
20+
AIXRelocModelTest.cpp
21+
)

0 commit comments

Comments
 (0)