Skip to content

Commit 97c62b8

Browse files
authored
[LinkerWrapper] Forward -mllvm and --offload-opt arguments to device (#100424)
Summary: Previously we could parse these internally as they would be used by the embedded LTO job. Now, this LTO is passed to the linker utilities which means these need to be forwarded. So this can now either be done with `--offload-opt` which works in the clang job, or with `-Xoffload-linker` manually. Fixes #100212
1 parent e106537 commit 97c62b8

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

clang/test/Driver/linker-wrapper.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,13 @@ __attribute__((visibility("protected"), used)) int x;
234234
// RUN: | FileCheck %s --check-prefix=OVERRIDE
235235
// OVERRIDE-NOT: clang
236236
// OVERRIDE: /usr/bin/ld
237+
238+
// RUN: clang-offload-packager -o %t.out \
239+
// RUN: --image=file=%t.elf.o,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx908
240+
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o -fembed-offload-object=%t.out
241+
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --offload-opt=-pass-remarks=foo \
242+
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=OFFLOAD-OPT
243+
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run -mllvm -pass-remarks=foo \
244+
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=OFFLOAD-OPT
245+
246+
// OFFLOAD-OPT: clang{{.*}}-Wl,--plugin-opt=-pass-remarks=foo

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ Expected<std::string> findProgram(StringRef Name, ArrayRef<StringRef> Paths) {
297297
/// supported by the toolchain.
298298
bool linkerSupportsLTO(const ArgList &Args) {
299299
llvm::Triple Triple(Args.getLastArgValue(OPT_triple_EQ));
300-
return Triple.isNVPTX() || Triple.isAMDGPU();
300+
return Triple.isNVPTX() || Triple.isAMDGPU() ||
301+
Args.getLastArgValue(OPT_linker_path_EQ).ends_with("ld.lld");
301302
}
302303

303304
/// Returns the hashed value for a constant string.
@@ -524,6 +525,13 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
524525
Args.MakeArgString("-" + OptLevel),
525526
};
526527

528+
// Forward all of the `--offload-opt` and similar options to the device.
529+
if (linkerSupportsLTO(Args)) {
530+
for (auto &Arg : Args.filtered(OPT_offload_opt_eq_minus, OPT_mllvm))
531+
CmdArgs.push_back(
532+
Args.MakeArgString("-Wl,--plugin-opt=" + StringRef(Arg->getValue())));
533+
}
534+
527535
if (!Triple.isNVPTX())
528536
CmdArgs.push_back("-Wl,--no-undefined");
529537

@@ -1756,7 +1764,7 @@ int main(int Argc, char **Argv) {
17561764
for (const opt::Arg *Arg : Args.filtered(OPT_mllvm))
17571765
NewArgv.push_back(Arg->getValue());
17581766
for (const opt::Arg *Arg : Args.filtered(OPT_offload_opt_eq_minus))
1759-
NewArgv.push_back(Args.MakeArgString(StringRef("-") + Arg->getValue()));
1767+
NewArgv.push_back(Arg->getValue());
17601768
SmallVector<PassPlugin, 1> PluginList;
17611769
PassPlugins.setCallback([&](const std::string &PluginPath) {
17621770
auto Plugin = PassPlugin::Load(PluginPath);

clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def mllvm : Separate<["-"], "mllvm">, Flags<[WrapperOnlyOption]>,
9898
HelpText<"Arguments passed to LLVM, including Clang invocations, for which "
9999
"the '-mllvm' prefix is preserved. Use '-mllvm --help' for a list "
100100
"of options.">;
101-
def offload_opt_eq_minus : Joined<["--", "-"], "offload-opt=-">, Flags<[HelpHidden, WrapperOnlyOption]>,
101+
def offload_opt_eq_minus : Joined<["--", "-"], "offload-opt=">, Flags<[HelpHidden, WrapperOnlyOption]>,
102102
HelpText<"Options passed to LLVM, not including the Clang invocation. Use "
103103
"'--offload-opt=--help' for a list of options.">;
104104

0 commit comments

Comments
 (0)