Skip to content

[LinkerWrapper] Make -Xoffload-linker match -Xlinker semantics #101032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9155,7 +9155,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
// If we disable the GPU C library support it needs to be forwarded to the
// link job.
if (!Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, true))
CmdArgs.push_back("--device-linker=-nolibc");
CmdArgs.push_back("--device-compiler=-nolibc");

// Add the linker arguments to be forwarded by the wrapper.
CmdArgs.push_back(Args.MakeArgString(Twine("--linker-path=") +
Expand Down
10 changes: 5 additions & 5 deletions clang/test/Driver/linker-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ __attribute__((visibility("protected"), used)) int x;
// RUN: -fembed-offload-object=%t.out
// RUN: clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu \
// RUN: --linker-path=/usr/bin/ld --device-linker=foo=bar --device-linker=a \
// RUN: --device-linker=nvptx64-nvidia-cuda=b \
// RUN: --device-linker=nvptx64-nvidia-cuda=b --device-compiler=foo\
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=LINKER-ARGS

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

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

// MISSING-LIBRARY: error: unable to find library -ldummy
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Driver/openmp-offload-gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,4 @@
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
// RUN: --offload-arch=sm_52 -nogpulibc -nogpuinc %s 2>&1 \
// RUN: | FileCheck --check-prefix=LIBC-GPU %s
// LIBC-GPU: clang-linker-wrapper{{.*}}"--device-linker=-nolibc"
// LIBC-GPU: clang-linker-wrapper{{.*}}"--device-compiler=-nolibc"
21 changes: 19 additions & 2 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
std::back_inserter(CmdArgs));

for (StringRef Arg : Args.getAllArgValues(OPT_linker_arg_EQ))
CmdArgs.append({"-Xlinker", Args.MakeArgString(Arg)});
for (StringRef Arg : Args.getAllArgValues(OPT_compiler_arg_EQ))
CmdArgs.push_back(Args.MakeArgString(Arg));

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

// Forward '-Xoffload-compiler' options to the appropriate backend.
for (StringRef Arg : Args.getAllArgValues(OPT_device_compiler_args_EQ)) {
auto [Triple, Value] = Arg.split('=');
llvm::Triple TT(Triple);
// If this isn't a recognized triple then it's an `arg=value` option.
if (TT.getArch() == Triple::ArchType::UnknownArch)
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_compiler_arg_EQ),
Args.MakeArgString(Arg));
else if (Value.empty())
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_compiler_arg_EQ),
Args.MakeArgString(Triple));
else if (Triple == DAL.getLastArgValue(OPT_triple_EQ))
DAL.AddJoinedArg(nullptr, Tbl.getOption(OPT_compiler_arg_EQ),
Args.MakeArgString(Value));
}

return DAL;
}

Expand Down
6 changes: 6 additions & 0 deletions clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def builtin_bitcode_EQ : Joined<["--"], "builtin-bitcode=">,
def device_linker_args_EQ : Joined<["--"], "device-linker=">,
Flags<[WrapperOnlyOption]>, MetaVarName<"<value> or <triple>=<value>">,
HelpText<"Arguments to pass to the device linker invocation">;
def device_compiler_args_EQ : Joined<["--"], "device-compiler=">,
Flags<[WrapperOnlyOption]>, MetaVarName<"<value> or <triple>=<value>">,
HelpText<"Arguments to pass to the device compiler invocation">;
def clang_backend : Flag<["--"], "clang-backend">,
Flags<[WrapperOnlyOption]>,
HelpText<"Run the backend using clang rather than the LTO backend">;
Expand Down Expand Up @@ -91,6 +94,9 @@ def whole_program : Flag<["--"], "whole-program">,
def linker_arg_EQ : Joined<["--"], "linker-arg=">,
Flags<[DeviceOnlyOption, HelpHidden]>,
HelpText<"An extra argument to be passed to the linker">;
def compiler_arg_EQ : Joined<["--"], "compiler-arg=">,
Flags<[DeviceOnlyOption, HelpHidden]>,
HelpText<"An extra argument to be passed to the compiler">;

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