[llvm] bed7626 - [PowerPC][AIX] Make PIC the default relocation model for AIX

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 16 10:07:41 PST 2020


Author: stevewan
Date: 2020-01-16T13:07:36-05:00
New Revision: bed7626f04f7442bed3674126ba6b658b4dfa505

URL: https://github.com/llvm/llvm-project/commit/bed7626f04f7442bed3674126ba6b658b4dfa505
DIFF: https://github.com/llvm/llvm-project/commit/bed7626f04f7442bed3674126ba6b658b4dfa505.diff

LOG: [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

Added: 
    llvm/test/tools/llc/aix-pic-setting.ll
    llvm/unittests/Target/PowerPC/AIXRelocModelTest.cpp
    llvm/unittests/Target/PowerPC/CMakeLists.txt

Modified: 
    llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
    llvm/tools/llc/llc.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp b/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
index 2caf4c99a1f8..71bad1a55500 100644
--- a/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
+++ b/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
@@ -223,6 +223,9 @@ static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
 
 static Reloc::Model getEffectiveRelocModel(const Triple &TT,
                                            Optional<Reloc::Model> RM) {
+  assert((!TT.isOSAIX() || !RM.hasValue() || *RM == Reloc::PIC_) &&
+         "Invalid relocation model for AIX.");
+
   if (RM.hasValue())
     return *RM;
 
@@ -230,8 +233,8 @@ static Reloc::Model getEffectiveRelocModel(const Triple &TT,
   if (TT.isOSDarwin())
     return Reloc::DynamicNoPIC;
 
-  // Big Endian PPC is PIC by default.
-  if (TT.getArch() == Triple::ppc64)
+  // Big Endian PPC and AIX default to PIC.
+  if (TT.getArch() == Triple::ppc64 || TT.isOSAIX())
     return Reloc::PIC_;
 
   // Rest are static by default.

diff  --git a/llvm/test/tools/llc/aix-pic-setting.ll b/llvm/test/tools/llc/aix-pic-setting.ll
new file mode 100644
index 000000000000..d5987f2adf2b
--- /dev/null
+++ b/llvm/test/tools/llc/aix-pic-setting.ll
@@ -0,0 +1,8 @@
+; RUN: llc -mtriple=powerpc-ibm-aix < %s 2>&1 1>/dev/null | FileCheck --allow-empty %s
+; RUN: llc -mtriple=powerpc-ibm-aix --relocation-model=pic < %s 2>&1 1>/dev/null | FileCheck --allow-empty %s
+; RUN: llc -mtriple=powerpc64-ibm-aix --relocation-model=pic < %s 2>&1 1>/dev/null | FileCheck --allow-empty %s
+; RUN: not llc -mtriple=powerpc-ibm-aix --relocation-model=static < %s 2>&1 | FileCheck --check-prefix=CHECK-NON-PIC %s
+; RUN: not llc -mtriple=powerpc64-ibm-aix --relocation-model=ropi-rwpi < %s 2>&1 | FileCheck --check-prefix=CHECK-NON-PIC %s
+
+; CHECK-NOT: {{.}}
+; CHECK-NON-PIC: invalid relocation model, AIX only supports PIC.

diff  --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp
index b35f8e853c30..0cf65139d5a3 100644
--- a/llvm/tools/llc/llc.cpp
+++ b/llvm/tools/llc/llc.cpp
@@ -461,8 +461,17 @@ static int compileModule(char **argv, LLVMContext &Context) {
   Options.MCOptions.IASSearchPaths = IncludeDirs;
   Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
 
+  // On AIX, setting the relocation model to anything other than PIC is considered
+  // a user error.
+  Optional<Reloc::Model> RM = getRelocModel();
+  if (TheTriple.isOSAIX() && RM.hasValue() && *RM != Reloc::PIC_) {
+    WithColor::error(errs(), argv[0])
+        << "invalid relocation model, AIX only supports PIC.\n";
+    return 1;
+  }
+
   std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
-      TheTriple.getTriple(), CPUStr, FeaturesStr, Options, getRelocModel(),
+      TheTriple.getTriple(), CPUStr, FeaturesStr, Options, RM,
       getCodeModel(), OLvl));
 
   assert(Target && "Could not allocate target machine!");

diff  --git a/llvm/unittests/Target/PowerPC/AIXRelocModelTest.cpp b/llvm/unittests/Target/PowerPC/AIXRelocModelTest.cpp
new file mode 100644
index 000000000000..fa5560910afc
--- /dev/null
+++ b/llvm/unittests/Target/PowerPC/AIXRelocModelTest.cpp
@@ -0,0 +1,39 @@
+#include "llvm/ADT/Triple.h"
+#include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Target/TargetMachine.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+class AIXRelocModelTest : public ::testing::Test {
+protected:
+  static void SetUpTestCase() {
+    LLVMInitializePowerPCTargetInfo();
+    LLVMInitializePowerPCTarget();
+    LLVMInitializePowerPCTargetMC();
+  }
+};
+
+TEST_F(AIXRelocModelTest, DefalutToPIC) {
+  Triple TheTriple(/*ArchStr*/ "powerpc", /*VendorStr*/ "", /*OSStr*/ "aix");
+  std::string Error;
+  const Target *TheTarget = TargetRegistry::lookupTarget("", TheTriple, Error);
+  ASSERT_TRUE(TheTarget) << Error;
+
+  TargetOptions Options;
+  // Create a TargetMachine for powerpc--aix target, and deliberately leave its
+  // relocation model unset.
+  std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
+      /*TT*/ TheTriple.getTriple(), /*CPU*/ "", /*Features*/ "",
+      /*Options*/ Options, /*RM*/ None, /*CM*/ None,
+      /*OL*/ CodeGenOpt::Default));
+  ASSERT_TRUE(Target) << "Could not allocate target machine!";
+
+  // The relocation model on AIX should be forced to PIC regardless.
+  EXPECT_TRUE(Target->getRelocationModel() == Reloc::PIC_);
+}
+
+} // end of anonymous namespace

diff  --git a/llvm/unittests/Target/PowerPC/CMakeLists.txt b/llvm/unittests/Target/PowerPC/CMakeLists.txt
new file mode 100644
index 000000000000..6927fc4c7c18
--- /dev/null
+++ b/llvm/unittests/Target/PowerPC/CMakeLists.txt
@@ -0,0 +1,21 @@
+include_directories(
+  ${CMAKE_SOURCE_DIR}/lib/Target/PowerPC
+  ${CMAKE_BINARY_DIR}/lib/Target/PowerPC
+  )
+
+set(LLVM_LINK_COMPONENTS
+  Analysis
+  CodeGen
+  Core
+  MC
+  MIRParser
+  Support
+  Target
+  PowerPCCodeGen
+  PowerPCDesc
+  PowerPCInfo
+  )
+
+add_llvm_unittest(PowerPCTests
+  AIXRelocModelTest.cpp
+  )


        


More information about the llvm-commits mailing list