Skip to content

Commit 800ae50

Browse files
committed
[LinkerWrapper] Forward -mllvm and --offload-opt arguments to device
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 5bae81b commit 800ae50

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

clang/test/Driver/linker-wrapper-passes.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
// REQUIRES: x86-registered-target
55
// REQUIRES: amdgpu-registered-target
66

7-
// https://github.com/llvm/llvm-project/issues/100212
8-
// XFAIL: *
9-
107
// Setup.
118
// RUN: mkdir -p %t
129
// RUN: %clang -cc1 -emit-llvm-bc -o %t/host-x86_64-unknown-linux-gnu.bc \

clang/test/Driver/linker-wrapper.c

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

@@ -1750,7 +1758,7 @@ int main(int Argc, char **Argv) {
17501758
for (const opt::Arg *Arg : Args.filtered(OPT_mllvm))
17511759
NewArgv.push_back(Arg->getValue());
17521760
for (const opt::Arg *Arg : Args.filtered(OPT_offload_opt_eq_minus))
1753-
NewArgv.push_back(Args.MakeArgString(StringRef("-") + Arg->getValue()));
1761+
NewArgv.push_back(Arg->getValue());
17541762
SmallVector<PassPlugin, 1> PluginList;
17551763
PassPlugins.setCallback([&](const std::string &PluginPath) {
17561764
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)