Skip to content

Commit 0b51e64

Browse files
committed
[LLD] [MinGW] Hook up more LTO options
Many of these options can be passed to the linker by the Clang driver based on other options passed to Clang, after a23bf17. Before commit 5c92c9f, these were ignored by lld, but now we're erroring out on the unrecognized options. The ELF linker has even more LTO options available, but not all of these are currently settable via the lld-link option interface, and some aren't set automatically by Clang but only if the user manually passes them - and thus probably aren't in wide use so far. (Previously LLD/MinGW would have accepted them silently but ignored them though.) Differential Revision: https://reviews.llvm.org/D158887
1 parent 69ffd49 commit 0b51e64

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

lld/MinGW/Driver.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
282282
add("-align:" + StringRef(a->getValue()));
283283
if (auto *a = args.getLastArg(OPT_heap))
284284
add("-heap:" + StringRef(a->getValue()));
285+
if (auto *a = args.getLastArg(OPT_threads))
286+
add("-threads:" + StringRef(a->getValue()));
285287

286288
if (auto *a = args.getLastArg(OPT_o))
287289
add("-out:" + StringRef(a->getValue()));
@@ -420,6 +422,18 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
420422

421423
if (auto *arg = args.getLastArg(OPT_plugin_opt_mcpu_eq))
422424
add("-mllvm:-mcpu=" + StringRef(arg->getValue()));
425+
if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
426+
add("-opt:lldltojobs=" + StringRef(arg->getValue()));
427+
if (auto *arg = args.getLastArg(OPT_lto_O))
428+
add("-opt:lldlto=" + StringRef(arg->getValue()));
429+
if (auto *arg = args.getLastArg(OPT_lto_CGO))
430+
add("-opt:lldltocgo=" + StringRef(arg->getValue()));
431+
if (auto *arg = args.getLastArg(OPT_plugin_opt_dwo_dir_eq))
432+
add("-dwodir:" + StringRef(arg->getValue()));
433+
if (args.hasArg(OPT_lto_cs_profile_generate))
434+
add("-lto-cs-profile-generate");
435+
if (auto *arg = args.getLastArg(OPT_lto_cs_profile_file))
436+
add("-lto-cs-profile-file:" + StringRef(arg->getValue()));
423437

424438
for (auto *a : args.filtered(OPT_plugin_opt_eq_minus))
425439
add("-mllvm:-" + StringRef(a->getValue()));

lld/MinGW/Options.td

+41
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,25 @@ class F<string name>: Flag<["--", "-"], name>;
44
class J<string name>: Joined<["--", "-"], name>;
55
class S<string name>: Separate<["--", "-"], name>;
66

7+
// Convenience classes for long options which only accept two dashes. For lld
8+
// specific or newer long options, we prefer two dashes to avoid collision with
9+
// short options. For many others, we have to accept both forms to be compatible
10+
// with GNU ld.
11+
class FF<string name> : Flag<["--"], name>;
12+
class JJ<string name>: Joined<["--"], name>;
13+
714
multiclass Eq<string name, string help> {
815
def NAME: Separate<["--", "-"], name>;
916
def NAME # _eq: Joined<["--", "-"], name # "=">, Alias<!cast<Separate>(NAME)>,
1017
HelpText<help>;
1118
}
1219

20+
multiclass EEq<string name, string help> {
21+
def NAME: Separate<["--"], name>;
22+
def NAME # _eq: Joined<["--"], name # "=">, Alias<!cast<Separate>(NAME)>,
23+
HelpText<help>;
24+
}
25+
1326
multiclass EqLong<string name, string help> {
1427
def NAME: Separate<["--"], name>;
1528
def NAME # _eq: Joined<["--"], name # "=">, Alias<!cast<Separate>(NAME)>,
@@ -119,6 +132,10 @@ defm reproduce: Eq<"reproduce",
119132
"Write a tar file containing input files and command line options to reproduce link">;
120133
defm require_defined: Eq<"require-defined",
121134
"Force symbol to be added to symbol table as an undefined one">;
135+
defm threads
136+
: EEq<"threads",
137+
"Number of threads. '1' disables multi-threading. By default all "
138+
"available hardware threads are used">;
122139
defm tsaware: B_disable<"tsaware",
123140
"Set the 'Terminal Server aware' flag", "Don't set the 'Terminal Server aware' flag">;
124141
defm undefined: Eq<"undefined", "Include symbol in the link, if available">;
@@ -131,9 +148,33 @@ def version: F<"version">, HelpText<"Display the version number and exit">;
131148
defm wrap: Eq<"wrap", "Use wrapper functions for symbol">,
132149
MetaVarName<"<symbol>">;
133150

151+
152+
def lto_O: JJ<"lto-O">, MetaVarName<"<opt-level>">,
153+
HelpText<"Optimization level for LTO">;
154+
def lto_CGO: JJ<"lto-CGO">, MetaVarName<"<cgopt-level>">,
155+
HelpText<"Codegen optimization level for LTO">;
156+
def lto_cs_profile_generate: FF<"lto-cs-profile-generate">,
157+
HelpText<"Perform context sensitive PGO instrumentation">;
158+
def lto_cs_profile_file: JJ<"lto-cs-profile-file=">,
159+
HelpText<"Context sensitive profile file path">;
160+
161+
def thinlto_jobs_eq: JJ<"thinlto-jobs=">,
162+
HelpText<"Number of ThinLTO jobs. Default to --threads=">;
163+
134164
def plugin_opt_eq_minus: J<"plugin-opt=-">,
135165
HelpText<"Specify an LLVM option for compatibility with LLVMgold.so">;
166+
def: J<"plugin-opt=thinlto">;
167+
168+
def: J<"plugin-opt=O">, Alias<lto_O>, HelpText<"Alias for --lto-O">;
169+
def: F<"plugin-opt=cs-profile-generate">,
170+
Alias<lto_cs_profile_generate>, HelpText<"Alias for --lto-cs-profile-generate">;
171+
def: J<"plugin-opt=cs-profile-path=">,
172+
Alias<lto_cs_profile_file>, HelpText<"Alias for --lto-cs-profile-file">;
173+
def plugin_opt_dwo_dir_eq: J<"plugin-opt=dwo_dir=">,
174+
HelpText<"Directory to store .dwo files when LTO and debug fission are used">;
175+
def: J<"plugin-opt=jobs=">, Alias<thinlto_jobs_eq>, HelpText<"Alias for --thinlto-jobs=">;
136176
def plugin_opt_mcpu_eq: J<"plugin-opt=mcpu=">;
177+
137178
// This may be either an unhandled LLVMgold.so feature or GCC passed
138179
// -plugin-opt=path/to/{liblto_plugin.so,lto-wrapper}
139180
def plugin_opt_eq : J<"plugin-opt=">;

lld/test/MinGW/driver.test

+9-2
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,15 @@ RUN: ld.lld -### foo.o -m i386pep --guard-longjmp 2>&1 | FileCheck -check-prefix
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
374374

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
375+
RUN: ld.lld -### foo.o -m i386pep --threads 3 --thinlto-jobs=4 2>&1 | FileCheck -check-prefix=THREADS %s
376+
RUN: ld.lld -### foo.o -m i386pep --threads 3 -plugin-opt=jobs=4 2>&1 | FileCheck -check-prefix=THREADS %s
377+
THREADS: -threads:3 {{.*}} -opt:lldltojobs=4
378+
379+
RUN: ld.lld -### foo.o -m i386pep -plugin-opt=mcpu=x86-64 -plugin-opt=-emulated-tls -plugin-opt=thinlto -plugin-opt=O2 -plugin-opt=dwo_dir=foo -plugin-opt=cs-profile-generate -plugin-opt=cs-profile-path=bar 2>&1 | FileCheck -check-prefix=LTO_OPTS %s
380+
LTO_OPTS: -mllvm:-mcpu=x86-64 -opt:lldlto=2 -dwodir:foo -lto-cs-profile-generate -lto-cs-profile-file:bar -mllvm:-emulated-tls
381+
382+
RUN: ld.lld -### foo.o -m i386pep --lto-O2 --lto-CGO1 --lto-cs-profile-generate --lto-cs-profile-file=foo 2>&1 | FileCheck -check-prefix=LTO_OPTS2 %s
383+
LTO_OPTS2:-opt:lldlto=2 -opt:lldltocgo=1 -lto-cs-profile-generate -lto-cs-profile-file:foo
377384

378385
Test GCC specific LTO options that GCC passes unconditionally, that we ignore.
379386

0 commit comments

Comments
 (0)