Skip to content

Commit fa0f90b

Browse files
committed
[HIP] Support linking archive of bundled bitcode
HIP programs compiled with -c -fgpu-rdc generate clang-offload-bundler bundles which contain bitcode for different GPU's. Such files can be archived to an archive file which can be linked with HIP programs with -fgpu-rdc. This patch adds suppor of linking archive of bundled bitcode. When an archive of bundled bitcode is passed to clang by -l, for each GPU specified through --offload-arch, clang extracts bitcode from the archive and creates a new archive for that GPU and pass it to lld. Reviewed by: Artem Belevich Differential Revision: https://reviews.llvm.org/D120070 Fixes: SWDEV-321741, SWDEV-315773
1 parent 8e79958 commit fa0f90b

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
121121
for (auto Input : Inputs)
122122
LldArgs.push_back(Input.getFilename());
123123

124+
// Look for archive of bundled bitcode in arguments, and add temporary files
125+
// for the extracted archive of bitcode to inputs.
126+
auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
127+
AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LldArgs, "amdgcn",
128+
TargetID,
129+
/*IsBitCodeSDL=*/true,
130+
/*PostClangLink=*/false);
131+
124132
const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
125133
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
126134
Lld, LldArgs, Inputs, Output));

clang/test/Driver/clang-offload-bundler.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,28 @@
365365
// CKLST2-NOT: openmp-powerpc64le-ibm-linux-gnu
366366
// CKLST2-NOT: openmp-x86_64-pc-linux-gnu
367367

368+
//
369+
// Check unbundling archive for HIP.
370+
//
371+
// When the input to clang-offload-bundler is an archive of bundled bitcodes,
372+
// for each target, clang-offload-bundler extracts the bitcode from each
373+
// bundle and archives them. Therefore for each target, the output is an
374+
// archive of unbundled bitcodes.
375+
//
376+
// RUN: clang-offload-bundler -type=bc -targets=hip-amdgcn-amd-amdhsa--gfx900,hip-amdgcn-amd-amdhsa--gfx906 \
377+
// RUN: -inputs=%t.tgt1,%t.tgt2 -outputs=%T/hip_bundle1.bc
378+
// RUN: clang-offload-bundler -type=bc -targets=hip-amdgcn-amd-amdhsa--gfx900,hip-amdgcn-amd-amdhsa--gfx906 \
379+
// RUN: -inputs=%t.tgt1,%t.tgt2 -outputs=%T/hip_bundle2.bc
380+
// RUN: llvm-ar cr %T/hip_archive.a %T/hip_bundle1.bc %T/hip_bundle2.bc
381+
// RUN: clang-offload-bundler -unbundle -type=a -targets=hip-amdgcn-amd-amdhsa--gfx900,hip-amdgcn-amd-amdhsa--gfx906 \
382+
// RUN: -outputs=%T/hip_900.a,%T/hip_906.a -inputs=%T/hip_archive.a
383+
// RUN: llvm-ar t %T/hip_900.a | FileCheck -check-prefix=HIP-AR-900 %s
384+
// RUN: llvm-ar t %T/hip_906.a | FileCheck -check-prefix=HIP-AR-906 %s
385+
// HIP-AR-900-DAG: hip_bundle1-hip-amdgcn-amd-amdhsa--gfx900
386+
// HIP-AR-900-DAG: hip_bundle2-hip-amdgcn-amd-amdhsa--gfx900
387+
// HIP-AR-906-DAG: hip_bundle1-hip-amdgcn-amd-amdhsa--gfx906
388+
// HIP-AR-906-DAG: hip_bundle2-hip-amdgcn-amd-amdhsa--gfx906
389+
368390
//
369391
// Check bundling without host target is allowed for HIP.
370392
//
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// REQUIRES: clang-driver, x86-registered-target, amdgpu-registered-target
2+
3+
// RUN: touch %T/libhipBundled.a
4+
5+
// Check clang unbundle the archive and link them by lld.
6+
7+
// RUN: %clang -### --offload-arch=gfx906 --offload-arch=gfx1030 \
8+
// RUN: -nogpulib %s -fgpu-rdc -L%T -lhipBundled \
9+
// RUN: 2>&1 | FileCheck -check-prefix=CHECK %s
10+
11+
// CHECK: "{{.*}}clang-offload-bundler" "-unbundle" "-type=a" "-inputs={{.*}}libhipBundled.a" "-targets=hip-amdgcn-amd-amdhsa-gfx1030" "-outputs=[[A1030:.*\.a]]" "-allow-missing-bundles"
12+
// CHECK: "{{.*}}lld" {{.*}}"-plugin-opt=mcpu=gfx1030" {{.*}} "[[A1030]]"
13+
// CHECK: "{{.*}}clang-offload-bundler" "-unbundle" "-type=a" "-inputs={{.*}}libhipBundled.a" "-targets=hip-amdgcn-amd-amdhsa-gfx906" "-outputs=[[A906:.*\.a]]" "-allow-missing-bundles"
14+
// CHECK: "{{.*}}lld" {{.*}}"-plugin-opt=mcpu=gfx906" {{.*}} "[[A906]]"

0 commit comments

Comments
 (0)