[PATCH] D67409: [RISCV] enable LTO support, pass some options to linker.

Kuan Hsu Chen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 10 11:01:32 PDT 2019


khchen created this revision.
khchen added a project: clang.
Herald added subscribers: pzheng, s.egerton, lenary, Jim, benna, psnobl, jocewei, PkmX, rkruppe, dexonsmith, the_o, brucehoult, MartinMosbeck, rogfer01, steven_wu, edward-jones, zzheng, MaskRay, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, apazos, simoncook, johnrusso, rbar, asb, inglorion, mehdi_amini.

If enable -flto we need to pass the target feature and ABIName to linker for generate correct result.


Repository:
  rC Clang

https://reviews.llvm.org/D67409

Files:
  clang/lib/Driver/ToolChains/RISCVToolchain.cpp
  clang/lib/Driver/ToolChains/RISCVToolchain.h
  clang/test/Driver/gold-lto.c


Index: clang/test/Driver/gold-lto.c
===================================================================
--- clang/test/Driver/gold-lto.c
+++ clang/test/Driver/gold-lto.c
@@ -26,3 +26,11 @@
 // RUN: %clang -target i686-linux-android -### %t.o -flto 2>&1 \
 // RUN:     | FileCheck %s --check-prefix=CHECK-X86-ANDROID
 // CHECK-X86-ANDROID: "-plugin" "{{.*}}{{[/\\]}}LLVMgold.{{dll|dylib|so}}"
+//
+// RUN: %clang -target riscv64-unknown-elf -### %t.o -flto 2>&1 \
+// RUN:     -march=rv64imf -mabi=lp64f \
+// RUN:     | FileCheck %s --check-prefix=CHECK-RISCV
+// CHECK-RISCV: "-plugin-opt=--mattr=+m"
+// CHECK-RISCV: "-plugin-opt=--mattr=+f"
+// CHECK-RISCV: "-plugin-opt=--mattr=+relax"
+// CHECK-RISCV: "-plugin-opt=-target-abi=lp64f"
Index: clang/lib/Driver/ToolChains/RISCVToolchain.h
===================================================================
--- clang/lib/Driver/ToolChains/RISCVToolchain.h
+++ clang/lib/Driver/ToolChains/RISCVToolchain.h
@@ -25,6 +25,7 @@
   void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
                              llvm::opt::ArgStringList &CC1Args,
                              Action::OffloadKind) const override;
+  bool HasNativeLLVMSupport() const override { return true; }
   void
   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
                             llvm::opt::ArgStringList &CC1Args) const override;
Index: clang/lib/Driver/ToolChains/RISCVToolchain.cpp
===================================================================
--- clang/lib/Driver/ToolChains/RISCVToolchain.cpp
+++ clang/lib/Driver/ToolChains/RISCVToolchain.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "RISCVToolchain.h"
+#include "Arch/RISCV.h"
 #include "CommonArgs.h"
 #include "InputInfo.h"
 #include "clang/Driver/Compilation.h"
@@ -100,6 +101,46 @@
 
   std::string Linker = getToolChain().GetProgramPath(getShortName());
 
+  if (D.isUsingLTO()) {
+    assert(!Inputs.empty() && "Must have at least one input.");
+    AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
+                  D.getLTOMode() == LTOK_Thin);
+
+    // Pass the features to linker.
+    std::vector<StringRef> Features;
+    riscv::getRISCVTargetFeatures(D, Args, Features);
+
+    // Find the last of each feature.
+    llvm::StringMap<unsigned> LastOpt;
+    for (unsigned I = 0, N = Features.size(); I < N; ++I) {
+      StringRef Name = Features[I];
+      assert(Name[0] == '-' || Name[0] == '+');
+      LastOpt[Name.drop_front(1)] = I;
+    }
+
+    for (unsigned I = 0, N = Features.size(); I < N; ++I) {
+      // If this feature was overridden,
+      // ignore it.
+      StringRef Name = Features[I];
+      llvm::StringMap<unsigned>::iterator LastI =
+          LastOpt.find(Name.drop_front(1));
+      assert(LastI != LastOpt.end());
+      unsigned Last = LastI->second;
+      if (Last != I)
+        continue;
+
+      CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=--"
+                                                 "mattr=") +
+                                           Name));
+    }
+
+    // Pass the ABIName to linker.
+    StringRef ABIName = tools::riscv::getRISCVABI(Args, ToolChain.getTriple());
+    CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=-target-"
+                                               "abi=") +
+                                         ABIName));
+  }
+
   bool WantCRTs =
       !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67409.219571.patch
Type: text/x-patch
Size: 3532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190910/a8a5e495/attachment.bin>


More information about the cfe-commits mailing list