diff --git a/clang/lib/Driver/ToolChains/Arch/RISCV.h b/clang/lib/Driver/ToolChains/Arch/RISCV.h --- a/clang/lib/Driver/ToolChains/Arch/RISCV.h +++ b/clang/lib/Driver/ToolChains/Arch/RISCV.h @@ -26,6 +26,9 @@ const llvm::Triple &Triple); StringRef getRISCVArch(const llvm::opt::ArgList &Args, const llvm::Triple &Triple); +void addRISCVGoldPluginAdditionFlags(const ToolChain &ToolChain, + const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs); } // end namespace riscv } // namespace tools } // end namespace driver diff --git a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp --- a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp +++ b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp @@ -579,3 +579,14 @@ return "rv64imafdc"; } } + +void riscv::addRISCVGoldPluginAdditionFlags(const ToolChain &ToolChain, + const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs) { + // -mabi is not encoded in bitcode so we need to pass it to LTO code + // generator. + StringRef ABIName = getRISCVABI(Args, ToolChain.getTriple()); + CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=-target-" + "abi=") + + ABIName)); +} diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -11,6 +11,7 @@ #include "Arch/ARM.h" #include "Arch/Mips.h" #include "Arch/PPC.h" +#include "Arch/RISCV.h" #include "Arch/SystemZ.h" #include "Arch/X86.h" #include "HIP.h" @@ -485,6 +486,17 @@ if (!StatsFile.empty()) CmdArgs.push_back( Args.MakeArgString(Twine("-plugin-opt=stats-file=") + StatsFile)); + + // pass more options in specific target + switch (ToolChain.getArch()) { + default: + break; + case llvm::Triple::riscv32: + case llvm::Triple::riscv64: { + riscv::addRISCVGoldPluginAdditionFlags(ToolChain, Args, CmdArgs); + break; + } + } } void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args, diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.h b/clang/lib/Driver/ToolChains/RISCVToolchain.h --- a/clang/lib/Driver/ToolChains/RISCVToolchain.h +++ b/clang/lib/Driver/ToolChains/RISCVToolchain.h @@ -28,6 +28,7 @@ RuntimeLibType GetDefaultRuntimeLibType() const override; UnwindLibType GetUnwindLibType(const llvm::opt::ArgList &Args) const override; + bool HasNativeLLVMSupport() const override { return true; } void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override; diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp --- a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp +++ b/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" @@ -145,6 +146,12 @@ 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); + } + bool WantCRTs = !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles); diff --git a/clang/test/Driver/gold-lto.c b/clang/test/Driver/gold-lto.c --- a/clang/test/Driver/gold-lto.c +++ b/clang/test/Driver/gold-lto.c @@ -26,3 +26,17 @@ // 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-BAREMETAL +// +// CHECK-RISCV-BAREMETAL: "-plugin" "{{.*}}{{[/\\]}}LLVMgold.{{dll|dylib|so}}" +// CHECK-RISCV-BAREMETAL: "-plugin-opt=-target-abi=lp64f" +// +// RUN: %clang -target riscv64-unknown-linux-gnu -### %t.o -flto 2>&1 \ +// RUN: -march=rv64imf -mabi=lp64f \ +// RUN: | FileCheck %s --check-prefix=CHECK-RISCV-LINUX +// +// CHECK-RISCV-LINUX: "-plugin" "{{.*}}{{[/\\]}}LLVMgold.{{dll|dylib|so}}" +// CHECK-RISCV-LINUX: "-plugin-opt=-target-abi=lp64f"