[Driver] Don't pass -plugin LLVMgold.so when the linker is ld.lld
authorFangrui Song <maskray@google.com>
Fri, 7 Feb 2020 22:17:36 +0000 (14:17 -0800)
committerFangrui Song <maskray@google.com>
Sat, 8 Feb 2020 06:53:14 +0000 (22:53 -0800)
This is does not cover the case when ld is lld (e.g. /usr/bin/ld on
modern FreeBSD systems).

clang/lib/Driver/ToolChains/CommonArgs.cpp
clang/test/Driver/lto.c
clang/test/Driver/lto.cu

index 122872f..0b2f8ce 100644 (file)
@@ -356,25 +356,29 @@ bool tools::isUseSeparateSections(const llvm::Triple &Triple) {
 void tools::AddGoldPlugin(const ToolChain &ToolChain, const ArgList &Args,
                           ArgStringList &CmdArgs, const InputInfo &Output,
                           const InputInfo &Input, bool IsThinLTO) {
-  // Tell the linker to load the plugin. This has to come before AddLinkerInputs
-  // as gold requires -plugin to come before any -plugin-opt that -Wl might
-  // forward.
-  CmdArgs.push_back("-plugin");
+  const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
+  if (llvm::sys::path::filename(Linker) != "ld.lld" &&
+      llvm::sys::path::stem(Linker) != "ld.lld") {
+    // Tell the linker to load the plugin. This has to come before
+    // AddLinkerInputs as gold requires -plugin to come before any -plugin-opt
+    // that -Wl might forward.
+    CmdArgs.push_back("-plugin");
 
 #if defined(_WIN32)
-  const char *Suffix = ".dll";
+    const char *Suffix = ".dll";
 #elif defined(__APPLE__)
-  const char *Suffix = ".dylib";
+    const char *Suffix = ".dylib";
 #else
-  const char *Suffix = ".so";
+    const char *Suffix = ".so";
 #endif
 
-  SmallString<1024> Plugin;
-  llvm::sys::path::native(Twine(ToolChain.getDriver().Dir) +
-                              "/../lib" CLANG_LIBDIR_SUFFIX "/LLVMgold" +
-                              Suffix,
-                          Plugin);
-  CmdArgs.push_back(Args.MakeArgString(Plugin));
+    SmallString<1024> Plugin;
+    llvm::sys::path::native(Twine(ToolChain.getDriver().Dir) +
+                                "/../lib" CLANG_LIBDIR_SUFFIX "/LLVMgold" +
+                                Suffix,
+                            Plugin);
+    CmdArgs.push_back(Args.MakeArgString(Plugin));
+  }
 
   // Try to pass driver level flags relevant to LTO code generation down to
   // the plugin.
index becaa9e..34e6076 100644 (file)
@@ -41,6 +41,9 @@
 // RUN:   -fuse-ld=gold -fno-lto -flto -### 2>&1 | FileCheck --check-prefix=LLVMGOLD %s
 // LLVMGOLD: "-plugin" "{{.*}}{{[/\\]}}LLVMgold.{{dll|dylib|so}}"
 
+/// lld does not need LLVMgold.
+// RUN: %clang -target x86_64-unknown-linux-gnu --sysroot %S/Inputs/basic_cross_linux_tree %s \
+// RUN:   -fuse-ld=lld -flto -### 2>&1 | FileCheck --check-prefix=NO-LLVMGOLD %s
 // RUN: %clang -target x86_64-unknown-linux-gnu --sysroot %S/Inputs/basic_cross_linux_tree %s \
 // RUN:   -fuse-ld=gold -flto -fno-lto -### 2>&1 | FileCheck --check-prefix=NO-LLVMGOLD %s
 // NO-LLVMGOLD-NOT: "-plugin" "{{.*}}{{[/\\]}}LLVMgold.{{dll|dylib|so}}"
index d3d71f3..131ed83 100644 (file)
@@ -55,7 +55,9 @@
 //
 // LLVMGOLD: "-plugin" "{{.*}}{{[/\\]}}LLVMgold.{{dll|dylib|so}}"
 
-// Check that subsequent -fno-lto takes precedence
+/// lld does not need LLVMgold.
+// RUN: %clangxx -nocudainc -nocudalib -target x86_64-unknown-linux-gnu --sysroot %S/Inputs/basic_cross_linux_tree %s \
+// RUN:   -fuse-ld=lld -flto=full -### 2>&1 | FileCheck --check-prefix=NO-LLVMGOLD %s
 // RUN: %clangxx -nocudainc -nocudalib -target x86_64-unknown-linux-gnu --sysroot %S/Inputs/basic_cross_linux_tree %s \
 // RUN:   -fuse-ld=gold -flto=full -fno-lto -### 2>&1 | FileCheck --check-prefix=NO-LLVMGOLD %s
 //