Skip to content

Commit 76605f5

Browse files
jhuber6jdenny-ornl
andauthored
[LinkerWrapper] Make -Xoffload-linker match -Xlinker semantics (#101032)
Summary: `-Xlinker` is supposed to pass options to the linker, while `-Xoffload-linker` instead passes it to the `clang` job. This is unintuitive and results in unnecessarily complex command lines. Because passing it to the clang job is still useful, I've added `-device-compiler`. This changes the behavior of the flag, but I think it should be fine in this case since it's likely rarely used and has a direct replacement. --------- Co-authored-by: Joel E. Denny <[email protected]>
1 parent 460a86d commit 76605f5

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9155,7 +9155,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
91559155
// If we disable the GPU C library support it needs to be forwarded to the
91569156
// link job.
91579157
if (!Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, true))
9158-
CmdArgs.push_back("--device-linker=-nolibc");
9158+
CmdArgs.push_back("--device-compiler=-nolibc");
91599159

91609160
// Add the linker arguments to be forwarded by the wrapper.
91619161
CmdArgs.push_back(Args.MakeArgString(Twine("--linker-path=") +

clang/test/Driver/linker-wrapper.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ __attribute__((visibility("protected"), used)) int x;
129129
// RUN: -fembed-offload-object=%t.out
130130
// RUN: clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu \
131131
// RUN: --linker-path=/usr/bin/ld --device-linker=foo=bar --device-linker=a \
132-
// RUN: --device-linker=nvptx64-nvidia-cuda=b \
132+
// RUN: --device-linker=nvptx64-nvidia-cuda=b --device-compiler=foo\
133133
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=LINKER-ARGS
134134

135-
// LINKER-ARGS: clang{{.*}}--target=amdgcn-amd-amdhsa{{.*}}foo=bar{{.*}}a
136-
// LINKER-ARGS: clang{{.*}}--target=nvptx64-nvidia-cuda{{.*}}foo=bar{{.*}}a b
135+
// LINKER-ARGS: clang{{.*}}--target=amdgcn-amd-amdhsa{{.*}}-Xlinker foo=bar{{.*}}-Xlinker a{{.*}}foo
136+
// LINKER-ARGS: clang{{.*}}--target=nvptx64-nvidia-cuda{{.*}}-Xlinker foo=bar{{.*}}-Xlinker a -Xlinker b{{.*}}foo
137137

138-
// RUN: not clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu -ldummy \
139-
// RUN: --linker-path=/usr/bin/ld --device-linker=a --device-linker=nvptx64-nvidia-cuda=b \
138+
// RUN: not clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu \
139+
// RUN: -ldummy --linker-path=/usr/bin/ld \
140140
// RUN: -o a.out 2>&1 | FileCheck %s --check-prefix=MISSING-LIBRARY
141141

142142
// MISSING-LIBRARY: error: unable to find library -ldummy

clang/test/Driver/openmp-offload-gpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,4 @@
377377
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
378378
// RUN: --offload-arch=sm_52 -nogpulibc -nogpuinc %s 2>&1 \
379379
// RUN: | FileCheck --check-prefix=LIBC-GPU %s
380-
// LIBC-GPU: clang-linker-wrapper{{.*}}"--device-linker=-nolibc"
380+
// LIBC-GPU: clang-linker-wrapper{{.*}}"--device-compiler=-nolibc"

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
599599
std::back_inserter(CmdArgs));
600600

601601
for (StringRef Arg : Args.getAllArgValues(OPT_linker_arg_EQ))
602+
CmdArgs.append({"-Xlinker", Args.MakeArgString(Arg)});
603+
for (StringRef Arg : Args.getAllArgValues(OPT_compiler_arg_EQ))
602604
CmdArgs.push_back(Args.MakeArgString(Arg));
603605

604606
for (StringRef Arg : Args.getAllArgValues(OPT_builtin_bitcode_EQ)) {
@@ -1224,8 +1226,7 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
12241226
auto [Triple, Value] = Arg.split('=');
12251227
llvm::Triple TT(Triple);
12261228
// If this isn't a recognized triple then it's an `arg=value` option.
1227-
if (TT.getArch() <= Triple::ArchType::UnknownArch ||
1228-
TT.getArch() > Triple::ArchType::LastArchType)
1229+
if (TT.getArch() == Triple::ArchType::UnknownArch)
12291230
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_linker_arg_EQ),
12301231
Args.MakeArgString(Arg));
12311232
else if (Value.empty())
@@ -1236,6 +1237,22 @@ DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
12361237
Args.MakeArgString(Value));
12371238
}
12381239

1240+
// Forward '-Xoffload-compiler' options to the appropriate backend.
1241+
for (StringRef Arg : Args.getAllArgValues(OPT_device_compiler_args_EQ)) {
1242+
auto [Triple, Value] = Arg.split('=');
1243+
llvm::Triple TT(Triple);
1244+
// If this isn't a recognized triple then it's an `arg=value` option.
1245+
if (TT.getArch() == Triple::ArchType::UnknownArch)
1246+
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_compiler_arg_EQ),
1247+
Args.MakeArgString(Arg));
1248+
else if (Value.empty())
1249+
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_compiler_arg_EQ),
1250+
Args.MakeArgString(Triple));
1251+
else if (Triple == DAL.getLastArgValue(OPT_triple_EQ))
1252+
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_compiler_arg_EQ),
1253+
Args.MakeArgString(Value));
1254+
}
1255+
12391256
return DAL;
12401257
}
12411258

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def builtin_bitcode_EQ : Joined<["--"], "builtin-bitcode=">,
3232
def device_linker_args_EQ : Joined<["--"], "device-linker=">,
3333
Flags<[WrapperOnlyOption]>, MetaVarName<"<value> or <triple>=<value>">,
3434
HelpText<"Arguments to pass to the device linker invocation">;
35+
def device_compiler_args_EQ : Joined<["--"], "device-compiler=">,
36+
Flags<[WrapperOnlyOption]>, MetaVarName<"<value> or <triple>=<value>">,
37+
HelpText<"Arguments to pass to the device compiler invocation">;
3538
def clang_backend : Flag<["--"], "clang-backend">,
3639
Flags<[WrapperOnlyOption]>,
3740
HelpText<"Run the backend using clang rather than the LTO backend">;
@@ -91,6 +94,9 @@ def whole_program : Flag<["--"], "whole-program">,
9194
def linker_arg_EQ : Joined<["--"], "linker-arg=">,
9295
Flags<[DeviceOnlyOption, HelpHidden]>,
9396
HelpText<"An extra argument to be passed to the linker">;
97+
def compiler_arg_EQ : Joined<["--"], "compiler-arg=">,
98+
Flags<[DeviceOnlyOption, HelpHidden]>,
99+
HelpText<"An extra argument to be passed to the compiler">;
94100

95101
// Arguments for the LLVM backend.
96102
def mllvm : Separate<["-"], "mllvm">, Flags<[WrapperOnlyOption]>,

0 commit comments

Comments
 (0)