Skip to content

Commit f7ce532

Browse files
committed
[clang-offload-bundler] Add unbundling of archives containing bundled object files into device specific archives
This patch adds unbundling support of an archive file. It takes an archive file along with a set of offload targets as input. Output is a device specific archive for each given offload target. Input archive contains bundled code objects bundled using clang-offload-bundler. Each generated device specific archive contains a set of device code object files which are named as <Parent Bundle Name>-<CodeObject-GPUArch>. Entries in input archive can be of any binary type which is supported by clang-offload-bundler, like *.bc. Output archives will contain files in same type. Example Usuage: clang-offload-bundler --unbundle --inputs=lib-generic.a -type=a -targets=openmp-amdgcn-amdhsa--gfx906,openmp-amdgcn-amdhsa--gfx908 -outputs=devicelib-gfx906.a,deviceLib-gfx908.a Reviewed By: jdoerfert, yaxunl Differential Revision: https://reviews.llvm.org/D93525
1 parent fcd0cb3 commit f7ce532

File tree

6 files changed

+383
-73
lines changed

6 files changed

+383
-73
lines changed

clang/docs/ClangOffloadBundler.rst

+9-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,15 @@ Where:
121121
============= ==============================================================
122122

123123
**target-triple**
124-
The target triple of the code object.
124+
The target triple of the code object:
125+
126+
.. code::
127+
128+
<Architecture>-<Vendor>-<OS>-<Environment>
129+
130+
It is required to have all four components present, if target-id is present.
131+
Components are hyphen separated. If a component is not specified then the
132+
empty string must be used in its place.
125133

126134
**target-id**
127135
The canonical target ID of the code object. Present only if the target

clang/lib/Driver/ToolChains/Clang.cpp

+21-9
Original file line numberDiff line numberDiff line change
@@ -7629,10 +7629,16 @@ void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA,
76297629
});
76307630
}
76317631
Triples += Action::GetOffloadKindName(CurKind);
7632-
Triples += '-';
7633-
Triples += CurTC->getTriple().normalize();
7634-
if (CurKind == Action::OFK_HIP && CurDep->getOffloadingArch()) {
7635-
Triples += '-';
7632+
Triples += "-";
7633+
std::string NormalizedTriple = CurTC->getTriple().normalize();
7634+
Triples += NormalizedTriple;
7635+
7636+
if (CurDep->getOffloadingArch() != nullptr) {
7637+
// If OffloadArch is present it can only appear as the 6th hypen
7638+
// sepearated field of Bundle Entry ID. So, pad required number of
7639+
// hyphens in Triple.
7640+
for (int i = 4 - StringRef(NormalizedTriple).count("-"); i > 0; i--)
7641+
Triples += "-";
76367642
Triples += CurDep->getOffloadingArch();
76377643
}
76387644
}
@@ -7702,11 +7708,17 @@ void OffloadBundler::ConstructJobMultipleOutputs(
77027708

77037709
auto &Dep = DepInfo[I];
77047710
Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind);
7705-
Triples += '-';
7706-
Triples += Dep.DependentToolChain->getTriple().normalize();
7707-
if (Dep.DependentOffloadKind == Action::OFK_HIP &&
7708-
!Dep.DependentBoundArch.empty()) {
7709-
Triples += '-';
7711+
Triples += "-";
7712+
std::string NormalizedTriple =
7713+
Dep.DependentToolChain->getTriple().normalize();
7714+
Triples += NormalizedTriple;
7715+
7716+
if (!Dep.DependentBoundArch.empty()) {
7717+
// If OffloadArch is present it can only appear as the 6th hypen
7718+
// sepearated field of Bundle Entry ID. So, pad required number of
7719+
// hyphens in Triple.
7720+
for (int i = 4 - StringRef(NormalizedTriple).count("-"); i > 0; i--)
7721+
Triples += "-";
77107722
Triples += Dep.DependentBoundArch;
77117723
}
77127724
}

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

+30-14
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
// CK-HELP: {{.*}}bc {{.*}}- llvm-bc
4747
// CK-HELP: {{.*}}s {{.*}}- assembler
4848
// CK-HELP: {{.*}}o {{.*}}- object
49+
// CK-HELP: {{.*}}a {{.*}}- archive of objects
4950
// CK-HELP: {{.*}}gch {{.*}}- precompiled-header
5051
// CK-HELP: {{.*}}ast {{.*}}- clang AST file
5152
// CK-HELP: {{.*}}-unbundle {{.*}}- Unbundle bundled file into several output files.
@@ -103,6 +104,9 @@
103104
// RUN: not clang-offload-bundler -type=i -targets=host-%itanium_abi_triple,host-%itanium_abi_triple,openmp-x86_64-pc-linux-gnu -inputs=%t.i,%t.tgt1,%t.tgt2 -outputs=%t.bundle.i 2>&1 | FileCheck %s --check-prefix CK-ERR9B
104105
// CK-ERR9B: error: Duplicate targets are not allowed
105106

107+
// RUN: not clang-offload-bundler -type=a -targets=hxst-powerpcxxle-ibm-linux-gnu,openxp-pxxerpc64le-ibm-linux-gnu,xpenmp-x86_xx-pc-linux-gnu -inputs=%t.i,%t.tgt1,%t.tgt2 -outputs=%t.bundle.i 2>&1 | FileCheck %s --check-prefix CK-ERR10A
108+
// CK-ERR10A: error: Archive files are only supported for unbundling
109+
106110
//
107111
// Check text bundle. This is a readable format, so we check for the format we expect to find.
108112
//
@@ -313,30 +317,30 @@
313317
//
314318
// Check error due to missing bundles
315319
//
316-
// RUN: clang-offload-bundler -type=bc -targets=host-%itanium_abi_triple,hip-amdgcn-amd-amdhsa-gfx900 -inputs=%t.bc,%t.tgt1 -outputs=%t.hip.bundle.bc
320+
// RUN: clang-offload-bundler -type=bc -targets=host-%itanium_abi_triple,hip-amdgcn-amd-amdhsa--gfx900 -inputs=%t.bc,%t.tgt1 -outputs=%t.hip.bundle.bc
317321
// RUN: not clang-offload-bundler -type=bc -inputs=%t.hip.bundle.bc -outputs=%t.tmp.bc -unbundle \
318-
// RUN: -targets=hip-amdgcn-amd-amdhsa-gfx906 \
322+
// RUN: -targets=hip-amdgcn-amd-amdhsa--gfx906 \
319323
// RUN: 2>&1 | FileCheck -check-prefix=MISS1 %s
320324
// RUN: not clang-offload-bundler -type=bc -inputs=%t.hip.bundle.bc -outputs=%t.tmp.bc,%t.tmp2.bc -unbundle \
321-
// RUN: -targets=hip-amdgcn-amd-amdhsa-gfx906,hip-amdgcn-amd-amdhsa-gfx900 \
325+
// RUN: -targets=hip-amdgcn-amd-amdhsa--gfx906,hip-amdgcn-amd-amdhsa--gfx900 \
322326
// RUN: 2>&1 | FileCheck -check-prefix=MISS1 %s
323-
// MISS1: error: Can't find bundles for hip-amdgcn-amd-amdhsa-gfx906
327+
// MISS1: error: Can't find bundles for hip-amdgcn-amd-amdhsa--gfx906
324328
// RUN: not clang-offload-bundler -type=bc -inputs=%t.hip.bundle.bc -outputs=%t.tmp.bc,%t.tmp2.bc -unbundle \
325-
// RUN: -targets=hip-amdgcn-amd-amdhsa-gfx906,hip-amdgcn-amd-amdhsa-gfx803 \
329+
// RUN: -targets=hip-amdgcn-amd-amdhsa--gfx906,hip-amdgcn-amd-amdhsa--gfx803 \
326330
// RUN: 2>&1 | FileCheck -check-prefix=MISS2 %s
327-
// MISS2: error: Can't find bundles for hip-amdgcn-amd-amdhsa-gfx803 and hip-amdgcn-amd-amdhsa-gfx906
331+
// MISS2: error: Can't find bundles for hip-amdgcn-amd-amdhsa--gfx803 and hip-amdgcn-amd-amdhsa--gfx906
328332
// RUN: not clang-offload-bundler -type=bc -inputs=%t.hip.bundle.bc -outputs=%t.tmp.bc,%t.tmp2.bc,%t.tmp3.bc -unbundle \
329-
// RUN: -targets=hip-amdgcn-amd-amdhsa-gfx906,hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx1010 \
333+
// RUN: -targets=hip-amdgcn-amd-amdhsa--gfx906,hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx1010 \
330334
// RUN: 2>&1 | FileCheck -check-prefix=MISS3 %s
331-
// MISS3: error: Can't find bundles for hip-amdgcn-amd-amdhsa-gfx1010, hip-amdgcn-amd-amdhsa-gfx803, and hip-amdgcn-amd-amdhsa-gfx906
335+
// MISS3: error: Can't find bundles for hip-amdgcn-amd-amdhsa--gfx1010, hip-amdgcn-amd-amdhsa--gfx803, and hip-amdgcn-amd-amdhsa--gfx906
332336

333337
//
334338
// Check error due to duplicate targets
335339
//
336-
// RUN: not clang-offload-bundler -type=bc -targets=host-%itanium_abi_triple,hip-amdgcn-amd-amdhsa-gfx900,hip-amdgcn-amd-amdhsa-gfx900 \
340+
// RUN: not clang-offload-bundler -type=bc -targets=host-%itanium_abi_triple,hip-amdgcn-amd-amdhsa--gfx900,hip-amdgcn-amd-amdhsa--gfx900 \
337341
// RUN: -inputs=%t.bc,%t.tgt1,%t.tgt1 -outputs=%t.hip.bundle.bc 2>&1 | FileCheck -check-prefix=DUP %s
338342
// RUN: not clang-offload-bundler -type=bc -inputs=%t.hip.bundle.bc -outputs=%t.tmp.bc,%t.tmp2.bc -unbundle \
339-
// RUN: -targets=hip-amdgcn-amd-amdhsa-gfx906,hip-amdgcn-amd-amdhsa-gfx906 \
343+
// RUN: -targets=hip-amdgcn-amd-amdhsa--gfx906,hip-amdgcn-amd-amdhsa--gfx906 \
340344
// RUN: 2>&1 | FileCheck -check-prefix=DUP %s
341345
// DUP: error: Duplicate targets are not allowed
342346
//
@@ -364,17 +368,29 @@
364368
//
365369
// Check bundling without host target is allowed for HIP.
366370
//
367-
// RUN: clang-offload-bundler -type=bc -targets=hip-amdgcn-amd-amdhsa-gfx900,hip-amdgcn-amd-amdhsa-gfx906 \
371+
// RUN: clang-offload-bundler -type=bc -targets=hip-amdgcn-amd-amdhsa--gfx900,hip-amdgcn-amd-amdhsa--gfx906 \
368372
// RUN: -inputs=%t.tgt1,%t.tgt2 -outputs=%t.hip.bundle.bc
369373
// RUN: clang-offload-bundler -type=bc -list -inputs=%t.hip.bundle.bc | FileCheck -check-prefix=NOHOST %s
370-
// RUN: clang-offload-bundler -type=bc -targets=hip-amdgcn-amd-amdhsa-gfx900,hip-amdgcn-amd-amdhsa-gfx906 \
374+
// RUN: clang-offload-bundler -type=bc -targets=hip-amdgcn-amd-amdhsa--gfx900,hip-amdgcn-amd-amdhsa--gfx906 \
371375
// RUN: -outputs=%t.res.tgt1,%t.res.tgt2 -inputs=%t.hip.bundle.bc -unbundle
372376
// RUN: diff %t.tgt1 %t.res.tgt1
373377
// RUN: diff %t.tgt2 %t.res.tgt2
374378
//
375379
// NOHOST-NOT: host-
376-
// NOHOST-DAG: hip-amdgcn-amd-amdhsa-gfx900
377-
// NOHOST-DAG: hip-amdgcn-amd-amdhsa-gfx906
380+
// NOHOST-DAG: hip-amdgcn-amd-amdhsa--gfx900
381+
// NOHOST-DAG: hip-amdgcn-amd-amdhsa--gfx906
382+
// Check archive unbundling
383+
//
384+
// Create few code object bundles and archive them to create an input archive
385+
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-amdgcn-amd-amdhsa--gfx906,openmp-amdgcn-amd-amdhsa--gfx908 -inputs=%t.o,%t.tgt1,%t.tgt2 -outputs=%t.simple.bundle
386+
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-amdgcn-amd-amdhsa--gfx903 -inputs=%t.o,%t.tgt1 -outputs=%t.simple1.bundle
387+
// RUN: llvm-ar cr %t.input-archive.a %t.simple.bundle %t.simple1.bundle
388+
389+
// RUN: clang-offload-bundler -unbundle -type=a -targets=openmp-amdgcn-amd-amdhsa--gfx906,openmp-amdgcn-amd-amdhsa--gfx908 -inputs=%t.input-archive.a -outputs=%t-archive-gfx906-simple.a,%t-archive-gfx908-simple.a
390+
// RUN: llvm-ar t %t-archive-gfx906-simple.a | FileCheck %s -check-prefix=GFX906
391+
// GFX906: simple-openmp-amdgcn-amd-amdhsa--gfx906
392+
// RUN: llvm-ar t %t-archive-gfx908-simple.a | FileCheck %s -check-prefix=GFX908
393+
// GFX908-NOT: {{gfx906}}
378394

379395
// Some code so that we can create a binary out of this file.
380396
int A = 0;

clang/test/Driver/hip-rdc-device-only.hip

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
// COMMON-SAME: {{.*}} {{".*a.cu"}}
8383

8484
// COMMON: "{{.*}}clang-offload-bundler" "-type={{(bc|ll)}}"
85-
// COMMON-SAME: "-targets=hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx900"
85+
// COMMON-SAME: "-targets=hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx900"
8686
// COMMON-SAME: "-outputs=a-hip-amdgcn-amd-amdhsa.{{(bc|ll)}}"
8787

8888
// COMMON: [[CLANG]] "-cc1" "-triple" "amdgcn-amd-amdhsa"
@@ -112,7 +112,7 @@
112112
// COMMON-SAME: {{.*}} {{".*b.hip"}}
113113

114114
// COMMON: "{{.*}}clang-offload-bundler" "-type={{(bc|ll)}}"
115-
// COMMON-SAME: "-targets=hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx900"
115+
// COMMON-SAME: "-targets=hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx900"
116116
// COMMON-SAME: "-outputs=b-hip-amdgcn-amd-amdhsa.{{(bc|ll)}}"
117117

118118
// SAVETEMP: [[CLANG:".*clang.*"]] "-cc1" "-triple" "amdgcn-amd-amdhsa" "-aux-triple" "x86_64-unknown-linux-gnu"
@@ -142,7 +142,7 @@
142142
// SAVETEMP-SAME: {{.*}} "-o" {{"a.*.ll"}} "-x" "ir" [[A_GFX900_TMP_BC]]
143143

144144
// SAVETEMP: "{{.*}}clang-offload-bundler" "-type=ll"
145-
// SAVETEMP-SAME: "-targets=hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx900"
145+
// SAVETEMP-SAME: "-targets=hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx900"
146146
// SAVETEMP-SAME: "-outputs=a-hip-amdgcn-amd-amdhsa.ll"
147147

148148
// SAVETEMP: [[CLANG]] "-cc1" "-triple" "amdgcn-amd-amdhsa" "-aux-triple" "x86_64-unknown-linux-gnu"
@@ -172,7 +172,7 @@
172172
// SAVETEMP-SAME: {{.*}} "-o" {{"b.*.ll"}} "-x" "ir" [[B_GFX900_TMP_BC]]
173173

174174
// SAVETEMP: "{{.*}}clang-offload-bundler" "-type=ll"
175-
// SAVETEMP-SAME: "-targets=hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx900"
175+
// SAVETEMP-SAME: "-targets=hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx900"
176176
// SAVETEMP-SAME: "-outputs=b-hip-amdgcn-amd-amdhsa.ll"
177177

178178
// FAIL: error: cannot specify -o when generating multiple output files

clang/test/Driver/hip-toolchain-rdc-separate.hip

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
// CHECK-SAME: {{.*}} [[A_SRC]]
4545

4646
// CHECK: [[BUNDLER:".*clang-offload-bundler"]] "-type=o"
47-
// CHECK-SAME: "-targets=hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx900,host-x86_64-unknown-linux-gnu"
47+
// CHECK-SAME: "-targets=hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx900,host-x86_64-unknown-linux-gnu"
4848
// CHECK-SAME: "-outputs=[[A_O:.*a.o]]" "-inputs=[[A_BC1]],[[A_BC2]],[[A_OBJ_HOST]]"
4949

5050
// CHECK: [[CLANG]] "-cc1" "-triple" "amdgcn-amd-amdhsa"
@@ -79,7 +79,7 @@
7979
// CHECK-SAME: {{.*}} [[B_SRC]]
8080

8181
// CHECK: [[BUNDLER:".*clang-offload-bundler"]] "-type=o"
82-
// CHECK-SAME: "-targets=hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx900,host-x86_64-unknown-linux-gnu"
82+
// CHECK-SAME: "-targets=hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx900,host-x86_64-unknown-linux-gnu"
8383
// CHECK-SAME: "-outputs=[[B_O:.*b.o]]" "-inputs=[[B_BC1]],[[B_BC2]],[[B_OBJ_HOST]]"
8484

8585
// RUN: touch %T/a.o
@@ -91,22 +91,22 @@
9191
// RUN: 2>&1 | FileCheck -check-prefix=LINK %s
9292

9393
// LINK: [[BUNDLER:".*clang-offload-bundler"]] "-type=o"
94-
// LINK-SAME: "-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx900"
94+
// LINK-SAME: "-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx900"
9595
// LINK-SAME: "-inputs=[[A_O:.*a.o]]" "-outputs=[[A_OBJ_HOST:.*o]],{{.*o}},{{.*o}}"
9696
// LINK: "-unbundle" "-allow-missing-bundles"
9797

9898
// LINK: [[BUNDLER:".*clang-offload-bundler"]] "-type=o"
99-
// LINK-SAME: "-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx900"
99+
// LINK-SAME: "-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx900"
100100
// LINK-SAME: "-inputs=[[B_O:.*b.o]]" "-outputs=[[B_OBJ_HOST:.*o]],{{.*o}},{{.*o}}"
101101
// LINK: "-unbundle" "-allow-missing-bundles"
102102

103103
// LINK: [[BUNDLER:".*clang-offload-bundler"]] "-type=o"
104-
// LINK-SAME: "-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx900"
104+
// LINK-SAME: "-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx900"
105105
// LINK-SAME: "-inputs=[[A_O]]" "-outputs={{.*o}},[[A_BC1:.*o]],[[A_BC2:.*o]]"
106106
// LINK: "-unbundle" "-allow-missing-bundles"
107107

108108
// LINK: [[BUNDLER:".*clang-offload-bundler"]] "-type=o"
109-
// LINK-SAME: "-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa-gfx803,hip-amdgcn-amd-amdhsa-gfx900"
109+
// LINK-SAME: "-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx803,hip-amdgcn-amd-amdhsa--gfx900"
110110
// LINK-SAME: "-inputs=[[B_O]]" "-outputs={{.*o}},[[B_BC1:.*o]],[[B_BC2:.*o]]"
111111
// LINK: "-unbundle" "-allow-missing-bundles"
112112

0 commit comments

Comments
 (0)