[lld] 5c92c9f - [LLD] [MinGW] Pass LLVM specific LTO options through to lld-link

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 24 13:15:49 PDT 2023


Author: Martin Storsjö
Date: 2023-08-24T23:15:26+03:00
New Revision: 5c92c9f34a7dba804479acef62c576d1a170ef1f

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

LOG: [LLD] [MinGW] Pass LLVM specific LTO options through to lld-link

This is modelled after how the ELF driver does it (see e.g.
https://reviews.llvm.org/D78158 for the source of the ELF
implementation); we need to intercept some options, but ignore GCC
specific ones that GCC passes regardless of whether GCC is using
LTO or not.

This is the second part of the fix for
https://github.com/mstorsjo/llvm-mingw/issues/349.

Differential Revision: https://reviews.llvm.org/D158412

Added: 
    

Modified: 
    lld/MinGW/Driver.cpp
    lld/MinGW/Options.td
    lld/test/MinGW/driver.test

Removed: 
    


################################################################################
diff  --git a/lld/MinGW/Driver.cpp b/lld/MinGW/Driver.cpp
index 3a6d52ffbd5510..26bd7b3829f23c 100644
--- a/lld/MinGW/Driver.cpp
+++ b/lld/MinGW/Driver.cpp
@@ -418,6 +418,23 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
   for (auto *a : args.filtered(OPT_mllvm))
     add("-mllvm:" + StringRef(a->getValue()));
 
+  if (auto *arg = args.getLastArg(OPT_plugin_opt_mcpu_eq))
+    add("-mllvm:-mcpu=" + StringRef(arg->getValue()));
+
+  for (auto *a : args.filtered(OPT_plugin_opt_eq_minus))
+    add("-mllvm:-" + StringRef(a->getValue()));
+
+  // GCC collect2 passes -plugin-opt=path/to/lto-wrapper with an absolute or
+  // relative path. Just ignore. If not ended with "lto-wrapper" (or
+  // "lto-wrapper.exe" for GCC cross-compiled for Windows), consider it an
+  // unsupported LLVMgold.so option and error.
+  for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq)) {
+    StringRef v(arg->getValue());
+    if (!v.ends_with("lto-wrapper") && !v.ends_with("lto-wrapper.exe"))
+      error(arg->getSpelling() + ": unknown plugin option '" + arg->getValue() +
+            "'");
+  }
+
   for (auto *a : args.filtered(OPT_Xlink))
     add(a->getValue());
 

diff  --git a/lld/MinGW/Options.td b/lld/MinGW/Options.td
index c9c60638a4e59d..fad950b0a1adce 100644
--- a/lld/MinGW/Options.td
+++ b/lld/MinGW/Options.td
@@ -131,6 +131,13 @@ def version: F<"version">, HelpText<"Display the version number and exit">;
 defm wrap: Eq<"wrap", "Use wrapper functions for symbol">,
      MetaVarName<"<symbol>">;
 
+def plugin_opt_eq_minus: J<"plugin-opt=-">,
+  HelpText<"Specify an LLVM option for compatibility with LLVMgold.so">;
+def plugin_opt_mcpu_eq: J<"plugin-opt=mcpu=">;
+// This may be either an unhandled LLVMgold.so feature or GCC passed
+// -plugin-opt=path/to/{liblto_plugin.so,lto-wrapper}
+def plugin_opt_eq : J<"plugin-opt=">;
+
 // LLD specific options
 def _HASH_HASH_HASH : Flag<["-"], "###">,
     HelpText<"Print (but do not run) the commands to run for this compilation">;
@@ -174,6 +181,10 @@ defm: EqNoHelp<"minor-image-version">;
 def: F<"no-undefined">;
 def: F<"pic-executable">;
 defm: EqNoHelp<"plugin">;
-defm: EqNoHelp<"plugin-opt">;
 defm: EqNoHelp<"sysroot">;
 def: F<"start-group">;
+
+// Ignore GCC collect2 LTO plugin related options. Note that we don't support
+// GCC LTO, but GCC collect2 passes these options even in non-LTO mode.
+def: J<"plugin-opt=-fresolution=">;
+def: J<"plugin-opt=-pass-through=">;

diff  --git a/lld/test/MinGW/driver.test b/lld/test/MinGW/driver.test
index de222803530ac6..96563dab86c6f2 100644
--- a/lld/test/MinGW/driver.test
+++ b/lld/test/MinGW/driver.test
@@ -371,3 +371,11 @@ GUARD_CF_NOLONGJMP: -guard:cf,nolongjmp
 RUN: ld.lld -### foo.o -m i386pep --guard-longjmp 2>&1 | FileCheck -check-prefix=GUARD_LONGJMP_NO_CF %s
 RUN: ld.lld -### foo.o -m i386pep --no-guard-cf --guard-longjmp 2>&1 | FileCheck -check-prefix=GUARD_LONGJMP_NO_CF %s
 GUARD_LONGJMP_NO_CF: warning: parameter --guard-longjmp only takes effect when used with --guard-cf
+
+RUN: ld.lld -### foo.o -m i386pep -plugin-opt=mcpu=x86-64 -plugin-opt=-emulated-tls 2>&1 | FileCheck -check-prefix=LTO_OPTS %s
+LTO_OPTS: -mllvm:-mcpu=x86-64 -mllvm:-emulated-tls
+
+Test GCC specific LTO options that GCC passes unconditionally, that we ignore.
+
+RUN: ld.lld -### foo.o -m i386pep -plugin /usr/lib/gcc/x86_64-w64-mingw32/10-posix/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-w64-mingw32/10-posix/lto-wrapper -plugin-opt=-fresolution=/tmp/ccM9d4fP.res -plugin-opt=-pass-through=-lmingw32 2> /dev/null
+RUN: ld.lld -### foo.o -m i386pep -plugin C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/liblto_plugin.dll -plugin-opt=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:/msys64/tmp/cckbC7wB.res -plugin-opt=-pass-through=-lmingw32 2> /dev/null


        


More information about the llvm-commits mailing list