Skip to content

Commit 5c92c9f

Browse files
committed
[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 mstorsjo/llvm-mingw#349. Differential Revision: https://reviews.llvm.org/D158412
1 parent a23bf17 commit 5c92c9f

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

lld/MinGW/Driver.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,23 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
418418
for (auto *a : args.filtered(OPT_mllvm))
419419
add("-mllvm:" + StringRef(a->getValue()));
420420

421+
if (auto *arg = args.getLastArg(OPT_plugin_opt_mcpu_eq))
422+
add("-mllvm:-mcpu=" + StringRef(arg->getValue()));
423+
424+
for (auto *a : args.filtered(OPT_plugin_opt_eq_minus))
425+
add("-mllvm:-" + StringRef(a->getValue()));
426+
427+
// GCC collect2 passes -plugin-opt=path/to/lto-wrapper with an absolute or
428+
// relative path. Just ignore. If not ended with "lto-wrapper" (or
429+
// "lto-wrapper.exe" for GCC cross-compiled for Windows), consider it an
430+
// unsupported LLVMgold.so option and error.
431+
for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq)) {
432+
StringRef v(arg->getValue());
433+
if (!v.ends_with("lto-wrapper") && !v.ends_with("lto-wrapper.exe"))
434+
error(arg->getSpelling() + ": unknown plugin option '" + arg->getValue() +
435+
"'");
436+
}
437+
421438
for (auto *a : args.filtered(OPT_Xlink))
422439
add(a->getValue());
423440

lld/MinGW/Options.td

+12-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ def version: F<"version">, HelpText<"Display the version number and exit">;
131131
defm wrap: Eq<"wrap", "Use wrapper functions for symbol">,
132132
MetaVarName<"<symbol>">;
133133

134+
def plugin_opt_eq_minus: J<"plugin-opt=-">,
135+
HelpText<"Specify an LLVM option for compatibility with LLVMgold.so">;
136+
def plugin_opt_mcpu_eq: J<"plugin-opt=mcpu=">;
137+
// This may be either an unhandled LLVMgold.so feature or GCC passed
138+
// -plugin-opt=path/to/{liblto_plugin.so,lto-wrapper}
139+
def plugin_opt_eq : J<"plugin-opt=">;
140+
134141
// LLD specific options
135142
def _HASH_HASH_HASH : Flag<["-"], "###">,
136143
HelpText<"Print (but do not run) the commands to run for this compilation">;
@@ -174,6 +181,10 @@ defm: EqNoHelp<"minor-image-version">;
174181
def: F<"no-undefined">;
175182
def: F<"pic-executable">;
176183
defm: EqNoHelp<"plugin">;
177-
defm: EqNoHelp<"plugin-opt">;
178184
defm: EqNoHelp<"sysroot">;
179185
def: F<"start-group">;
186+
187+
// Ignore GCC collect2 LTO plugin related options. Note that we don't support
188+
// GCC LTO, but GCC collect2 passes these options even in non-LTO mode.
189+
def: J<"plugin-opt=-fresolution=">;
190+
def: J<"plugin-opt=-pass-through=">;

lld/test/MinGW/driver.test

+8
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,11 @@ GUARD_CF_NOLONGJMP: -guard:cf,nolongjmp
371371
RUN: ld.lld -### foo.o -m i386pep --guard-longjmp 2>&1 | FileCheck -check-prefix=GUARD_LONGJMP_NO_CF %s
372372
RUN: ld.lld -### foo.o -m i386pep --no-guard-cf --guard-longjmp 2>&1 | FileCheck -check-prefix=GUARD_LONGJMP_NO_CF %s
373373
GUARD_LONGJMP_NO_CF: warning: parameter --guard-longjmp only takes effect when used with --guard-cf
374+
375+
RUN: ld.lld -### foo.o -m i386pep -plugin-opt=mcpu=x86-64 -plugin-opt=-emulated-tls 2>&1 | FileCheck -check-prefix=LTO_OPTS %s
376+
LTO_OPTS: -mllvm:-mcpu=x86-64 -mllvm:-emulated-tls
377+
378+
Test GCC specific LTO options that GCC passes unconditionally, that we ignore.
379+
380+
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
381+
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

0 commit comments

Comments
 (0)