Skip to content

Commit 0f8b529

Browse files
authored
[LinkerWrapper] Do not link device code under a relocatable link (#79314)
Summary: A relocatable link through `clang -r` can go through the clang-linker-wrapper if offloading is enabled. This will have the effect of linking the device code and creating the wrapper module. It will then be merged into the final file. This is useful behavior on its own, but is likely not what is expected for a `-r` job. This patch makes the linker wrapper ignore the device code when doing a reloctable link. This has the effect of the linker merging the `.llvm.offloading` sections in the output object. These will then be parsed as normal when the executable is finally created. Even though this doesn't actually perform a reloctable link on the device code itself, it has a similar effect of combining multiple files into a single one.
1 parent 298412b commit 0f8b529

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

clang/test/Driver/linker-wrapper.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,15 @@ __attribute__((visibility("protected"), used)) int x;
171171

172172
// AMD-TARGET-ID: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+ -O2 -Wl,--no-undefined {{.*}}.o {{.*}}.o
173173
// AMD-TARGET-ID: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack- -O2 -Wl,--no-undefined {{.*}}.o {{.*}}.o
174+
175+
// RUN: clang-offload-packager -o %t.out \
176+
// RUN: --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
177+
// RUN: --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
178+
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o -fembed-offload-object=%t.out
179+
// RUN: llvm-ar rcs %t.a %t.o
180+
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
181+
// RUN: --linker-path=/usr/bin/ld.lld -- -r --whole-archive %t.a --no-whole-archive \
182+
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=RELOCATABLE-LINK
183+
184+
// RELOCATABLE-LINK-NOT: clang{{.*}} -o {{.*}}.img --target=x86_64-unknown-linux-gnu
185+
// RELOCATABLE-LINK: /usr/bin/ld.lld{{.*}}-r

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,12 @@ Expected<SmallVector<SmallVector<OffloadFile>>>
13561356
getDeviceInput(const ArgList &Args) {
13571357
llvm::TimeTraceScope TimeScope("ExtractDeviceCode");
13581358

1359+
// If the user is requesting a reloctable link we ignore the device code. The
1360+
// actual linker will merge the embedded device code sections so they can be
1361+
// linked when the executable is finally created.
1362+
if (Args.hasArg(OPT_relocatable))
1363+
return SmallVector<SmallVector<OffloadFile>>{};
1364+
13591365
StringRef Root = Args.getLastArgValue(OPT_sysroot_EQ);
13601366
SmallVector<StringRef> LibraryPaths;
13611367
for (const opt::Arg *Arg : Args.filtered(OPT_library_path, OPT_libpath))

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ def version : Flag<["--", "-"], "version">, Flags<[HelpHidden]>, Alias<v>;
127127
def whole_archive : Flag<["--", "-"], "whole-archive">, Flags<[HelpHidden]>;
128128
def no_whole_archive : Flag<["--", "-"], "no-whole-archive">, Flags<[HelpHidden]>;
129129

130+
def relocatable : Flag<["--", "-"], "relocatable">, Flags<[HelpHidden]>;
131+
def r : Flag<["-"], "r">, Alias<relocatable>, Flags<[HelpHidden]>;
132+
130133
// link.exe-style linker options.
131134
def out : Joined<["/", "-", "/?", "-?"], "out:">, Flags<[HelpHidden]>;
132135
def libpath : Joined<["/", "-", "/?", "-?"], "libpath:">, Flags<[HelpHidden]>;

0 commit comments

Comments
 (0)