Skip to content

Commit e48ae0d

Browse files
committed
[clang-offload-bundler] Standardize TargetID field for bundler
The bundler accepts both of the following for the --target option: hip-amdgcn-amd-amdhsa-gfx900 (no env field) hip-amdgcn-amd-amdhsa--gfx900 (blank env field) The environment field is defined as optional for Triples in Triple.h. However, in this patch we update the bundler to internally standardize to include the env field. While users aren't required to specify an env field when listing targets on the commandline, bundles generated by the offload-bundler will include the ABI field. This standardization simplifies things for APIs that deal with bundles generated by the clang-offload-bundler tool. Differential Revision: https://reviews.llvm.org/D145770
1 parent c361741 commit e48ae0d

File tree

6 files changed

+78
-14
lines changed

6 files changed

+78
-14
lines changed

clang/docs/ClangOffloadBundler.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,20 @@ Where:
174174
============= ==============================================================
175175

176176
**target-triple**
177-
The target triple of the code object.
177+
The target triple of the code object. See `Target Triple
178+
<https://clang.llvm.org/docs/CrossCompilation.html#target-triple>.
179+
180+
The bundler accepts target triples with or without the optional environment
181+
field:
182+
183+
``<arch><sub>-<vendor>-<sys>``, or
184+
``<arch><sub>-<vendor>-<sys>-<env>``
185+
186+
However, in order to standardize outputs for tools that consume bitcode
187+
bundles, bundles written by the bundler internally use only the 4-field
188+
target triple:
189+
190+
``<arch><sub>-<vendor>-<sys>-<env>``
178191

179192
**target-id**
180193
The canonical target ID of the code object. Present only if the target

clang/lib/Driver/OffloadBundler.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,22 @@ OffloadTargetInfo::OffloadTargetInfo(const StringRef Target,
7272
if (clang::StringToCudaArch(TripleOrGPU.second) != clang::CudaArch::UNKNOWN) {
7373
auto KindTriple = TripleOrGPU.first.split('-');
7474
this->OffloadKind = KindTriple.first;
75-
this->Triple = llvm::Triple(KindTriple.second);
75+
76+
// Enforce optional env field to standardize bundles
77+
llvm::Triple t = llvm::Triple(KindTriple.second);
78+
this->Triple = llvm::Triple(t.getArchName(), t.getVendorName(),
79+
t.getOSName(), t.getEnvironmentName());
80+
7681
this->TargetID = Target.substr(Target.find(TripleOrGPU.second));
7782
} else {
7883
auto KindTriple = TargetFeatures.first.split('-');
7984
this->OffloadKind = KindTriple.first;
80-
this->Triple = llvm::Triple(KindTriple.second);
85+
86+
// Enforce optional env field to standardize bundles
87+
llvm::Triple t = llvm::Triple(KindTriple.second);
88+
this->Triple = llvm::Triple(t.getArchName(), t.getVendorName(),
89+
t.getOSName(), t.getEnvironmentName());
90+
8191
this->TargetID = "";
8292
}
8393
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121

2222
// Tests to check compatibility between Bundle Entry ID formats i.e. between presence/absence of extra hyphen in case of missing environment field
2323
// RUN: clang-offload-bundler -unbundle -type=a -targets=openmp-amdgcn-amd-amdhsa--gfx906,openmp-amdgcn-amd-amdhsa-gfx908 -input=%t.input-archive.a -output=%t-archive-gfx906-simple.a -output=%t-archive-gfx908-simple.a -debug-only=CodeObjectCompatibility 2>&1 | FileCheck %s -check-prefix=BUNDLECOMPATIBILITY
24-
// BUNDLECOMPATIBILITY: Compatible: Exact match: [CodeObject: openmp-amdgcn-amd-amdhsa-gfx906] : [Target: openmp-amdgcn-amd-amdhsa--gfx906]
25-
// BUNDLECOMPATIBILITY: Compatible: Exact match: [CodeObject: openmp-amdgcn-amd-amdhsa--gfx908] : [Target: openmp-amdgcn-amd-amdhsa-gfx908]
24+
// BUNDLECOMPATIBILITY: Compatible: Exact match: [CodeObject: openmp-amdgcn-amd-amdhsa--gfx906] : [Target: openmp-amdgcn-amd-amdhsa--gfx906]
25+
// BUNDLECOMPATIBILITY: Compatible: Exact match: [CodeObject: openmp-amdgcn-amd-amdhsa--gfx908] : [Target: openmp-amdgcn-amd-amdhsa--gfx908]
2626

2727
// RUN: clang-offload-bundler -unbundle -type=a -targets=hip-amdgcn-amd-amdhsa--gfx906,hipv4-amdgcn-amd-amdhsa-gfx908 -input=%t.input-archive.a -output=%t-hip-archive-gfx906-simple.a -output=%t-hipv4-archive-gfx908-simple.a -hip-openmp-compatible -debug-only=CodeObjectCompatibility 2>&1 | FileCheck %s -check-prefix=HIPOpenMPCOMPATIBILITY
28-
// HIPOpenMPCOMPATIBILITY: Compatible: Code Objects are compatible [CodeObject: openmp-amdgcn-amd-amdhsa-gfx906] : [Target: hip-amdgcn-amd-amdhsa--gfx906]
29-
// HIPOpenMPCOMPATIBILITY: Compatible: Code Objects are compatible [CodeObject: openmp-amdgcn-amd-amdhsa--gfx908] : [Target: hipv4-amdgcn-amd-amdhsa-gfx908]
28+
// HIPOpenMPCOMPATIBILITY: Compatible: Code Objects are compatible [CodeObject: openmp-amdgcn-amd-amdhsa--gfx906] : [Target: hip-amdgcn-amd-amdhsa--gfx906]
29+
// HIPOpenMPCOMPATIBILITY: Compatible: Code Objects are compatible [CodeObject: openmp-amdgcn-amd-amdhsa--gfx908] : [Target: hipv4-amdgcn-amd-amdhsa--gfx908]
3030

3131
// Some code so that we can create a binary out of this file.
3232
int A = 0;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// REQUIRES: x86-registered-target
2+
// UNSUPPORTED: target={{.*}}-darwin{{.*}}, target={{.*}}-aix{{.*}}
3+
4+
// Generate the file we can bundle.
5+
// RUN: %clang -O0 -target %itanium_abi_triple %s -c -o %t.o
6+
7+
//
8+
// Generate a couple of files to bundle with.
9+
//
10+
// RUN: echo 'Content of device file 1' > %t.tgt1
11+
// RUN: echo 'Content of device file 2' > %t.tgt2
12+
13+
//
14+
// Check code object compatibility for archive unbundling
15+
//
16+
// Create an object bundle with and without env fields
17+
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,hip-amdgcn-amd-amdhsa-gfx906,hip-amdgcn-amd-amdhsa-gfx908 -input=%t.o -input=%t.tgt1 -input=%t.tgt2 -output=%t.bundle.no.env
18+
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple-,hip-amdgcn-amd-amdhsa--gfx906,hip-amdgcn-amd-amdhsa--gfx908 -input=%t.o -input=%t.tgt1 -input=%t.tgt2 -output=%t.bundle.env
19+
20+
21+
// Unbundle bundle.no.env while providing targets with env
22+
// RUN: clang-offload-bundler -unbundle -type=o -targets=hip-amdgcn-amd-amdhsa--gfx906,hip-amdgcn-amd-amdhsa--gfx908 -input=%t.bundle.no.env -output=%t-hip-amdgcn-amd-amdhsa--gfx906.bc -output=%t-hip-amdgcn-amd-amdhsa--gfx908.bc -debug-only=CodeObjectCompatibility 2>&1 | FileCheck %s -check-prefix=BUNDLE-NO-ENV
23+
// BUNDLE-NO-ENV: Compatible: Exact match: [CodeObject: hip-amdgcn-amd-amdhsa--gfx906] : [Target: hip-amdgcn-amd-amdhsa--gfx906]
24+
// BUNDLE-NO-ENV: Compatible: Exact match: [CodeObject: hip-amdgcn-amd-amdhsa--gfx908] : [Target: hip-amdgcn-amd-amdhsa--gfx908]
25+
26+
// Unbundle bundle.env while providing targets with no env
27+
// RUN: clang-offload-bundler -unbundle -type=o -targets=hip-amdgcn-amd-amdhsa-gfx906,hip-amdgcn-amd-amdhsa-gfx908 -input=%t.bundle.env -output=%t-hip-amdgcn-amd-amdhsa-gfx906.bc -output=%t-hip-amdgcn-amd-amdhsa-gfx908.bc -debug-only=CodeObjectCompatibility 2>&1 | FileCheck %s -check-prefix=BUNDLE-ENV
28+
// BUNDLE-ENV: Compatible: Exact match: [CodeObject: hip-amdgcn-amd-amdhsa--gfx906] : [Target: hip-amdgcn-amd-amdhsa--gfx906]
29+
// BUNDLE-ENV: Compatible: Exact match: [CodeObject: hip-amdgcn-amd-amdhsa--gfx908] : [Target: hip-amdgcn-amd-amdhsa--gfx908]
30+
31+
// Some code so that we can create a binary out of this file.
32+
int A = 0;
33+
void test_func(void) {
34+
++A;
35+
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// REQUIRES: x86-registered-target
2-
// REQUIRES: powerpc-registered-target
32
// UNSUPPORTED: target={{.*}}-darwin{{.*}}, target={{.*}}-aix{{.*}}
43

54
//
@@ -297,11 +296,11 @@
297296

298297
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-powerpc64le-ibm-linux-gnu,openmp-x86_64-pc-linux-gnu -input=%t.o -input=%t.tgt1 -input=%t.tgt2 -output=%t.bundle3.o -### 2>&1 \
299298
// RUN: | FileCheck %s -DHOST=%itanium_abi_triple -DINOBJ1=%t.o -DINOBJ2=%t.tgt1 -DINOBJ3=%t.tgt2 -DOUTOBJ=%t.bundle3.o --check-prefix CK-OBJ-CMD
300-
// CK-OBJ-CMD: llvm-objcopy{{(.exe)?}}" "--add-section=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]={{.*}}" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]=readonly,exclude" "--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu=[[INOBJ2]]" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu=readonly,exclude" "--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu=[[INOBJ3]]" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu=readonly,exclude" "--" "[[INOBJ1]]" "[[OUTOBJ]]"
299+
// CK-OBJ-CMD: llvm-objcopy{{(.exe)?}}" "--add-section=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]-={{.*}}" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]-=readonly,exclude" "--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu-=[[INOBJ2]]" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu-=readonly,exclude" "--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu-=[[INOBJ3]]" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu-=readonly,exclude" "--" "[[INOBJ1]]" "[[OUTOBJ]]"
301300

302301
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-powerpc64le-ibm-linux-gnu,openmp-x86_64-pc-linux-gnu -input=%t.o -input=%t.tgt1 -input=%t.tgt2 -output=%t.bundle3.o -### 2>&1 \
303302
// RUN: | FileCheck %s -DHOST=%itanium_abi_triple -DINOBJ1=%t.o -DINOBJ2=%t.tgt1 -DINOBJ3=%t.tgt2 -DOUTOBJ=%t.bundle3.o --check-prefix CK-OBJ-CMD-INPUTS
304-
// CK-OBJ-CMD-INPUTS: llvm-objcopy{{(.exe)?}}" "--add-section=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]={{.*}}" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]=readonly,exclude" "--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu=[[INOBJ2]]" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu=readonly,exclude" "--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu=[[INOBJ3]]" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu=readonly,exclude" "--" "[[INOBJ1]]" "[[OUTOBJ]]"
303+
// CK-OBJ-CMD-INPUTS: llvm-objcopy{{(.exe)?}}" "--add-section=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]-={{.*}}" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]-=readonly,exclude" "--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu-=[[INOBJ2]]" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu-=readonly,exclude" "--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu-=[[INOBJ3]]" "--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu-=readonly,exclude" "--" "[[INOBJ1]]" "[[OUTOBJ]]"
305304

306305
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-powerpc64le-ibm-linux-gnu,openmp-x86_64-pc-linux-gnu -input=%t.o -input=%t.tgt1 -input=%t.tgt2 -output=%t.bundle3.o
307306
// RUN: clang-offload-bundler -type=o -input=%t.bundle3.o -list | FileCheck -check-prefix=CKLST %s
@@ -482,15 +481,15 @@
482481
// RUN: llvm-ar t %t-archive-gfx906-simple.a | FileCheck %s -check-prefix=GFX906
483482
// RUN: clang-offload-bundler -unbundle -type=a -targets=openmp-amdgcn-amd-amdhsa-gfx906:xnack+ -input=%t.input-archive.a -output=%t-archive-gfx906-simple.a
484483
// RUN: llvm-ar t %t-archive-gfx906-simple.a | FileCheck %s -check-prefix=GFX906
485-
// GFX906: simple-openmp-amdgcn-amd-amdhsa-gfx906
484+
// GFX906: simple-openmp-amdgcn-amd-amdhsa--gfx906
486485
// RUN: llvm-ar t %t-archive-gfx908-simple.a | FileCheck %s -check-prefix=GFX908
487486
// GFX908-NOT: {{gfx906}}
488487
// RUN: not clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-amdgcn-amd-amdhsa-gfx906,openmp-amdgcn-amd-amdhsa-gfx906:sramecc+ -input=%t.o -input=%t.tgt1 -input=%t.tgt2 -output=%t.bad.bundle 2>&1 | FileCheck %s -check-prefix=BADTARGETS
489-
// BADTARGETS: error: Cannot bundle inputs with conflicting targets: 'openmp-amdgcn-amd-amdhsa-gfx906' and 'openmp-amdgcn-amd-amdhsa-gfx906:sramecc+'
488+
// BADTARGETS: error: Cannot bundle inputs with conflicting targets: 'openmp-amdgcn-amd-amdhsa--gfx906' and 'openmp-amdgcn-amd-amdhsa--gfx906:sramecc+'
490489

491490
// Check for error if no compatible code object is found in the heterogeneous archive library
492491
// RUN: not clang-offload-bundler -unbundle -type=a -targets=openmp-amdgcn-amd-amdhsa-gfx803 -input=%t.input-archive.a -output=%t-archive-gfx803-incompatible.a 2>&1 | FileCheck %s -check-prefix=INCOMPATIBLEARCHIVE
493-
// INCOMPATIBLEARCHIVE: error: no compatible code object found for the target 'openmp-amdgcn-amd-amdhsa-gfx803' in heterogeneous archive library
492+
// INCOMPATIBLEARCHIVE: error: no compatible code object found for the target 'openmp-amdgcn-amd-amdhsa--gfx803' in heterogeneous archive library
494493

495494
// Check creation of empty archive if allow-missing-bundles is present and no compatible code object is found in the heterogeneous archive library
496495
// RUN: clang-offload-bundler -unbundle -type=a -targets=openmp-amdgcn-amd-amdhsa-gfx803 -input=%t.input-archive.a -output=%t-archive-gfx803-empty.a -allow-missing-bundles
@@ -500,7 +499,7 @@
500499
// Check compatibility of OpenMP code objects found in the heterogeneous archive library with HIP code objects of the target
501500
// RUN: clang-offload-bundler -unbundle -type=a -targets=hip-amdgcn-amd-amdhsa-gfx906,hipv4-amdgcn-amd-amdhsa-gfx908 -input=%t.input-archive.a -output=%t-hip-archive-gfx906-simple.a -output=%t-hipv4-archive-gfx908-simple.a -hip-openmp-compatible
502501
// RUN: llvm-ar t %t-hip-archive-gfx906-simple.a | FileCheck %s -check-prefix=HIPOPENMPCOMPAT
503-
// HIPOPENMPCOMPAT: simple-openmp-amdgcn-amd-amdhsa-gfx906
502+
// HIPOPENMPCOMPAT: simple-openmp-amdgcn-amd-amdhsa--gfx906
504503
// RUN: llvm-ar t %t-hipv4-archive-gfx908-simple.a | FileCheck %s -check-prefix=HIPv4OPENMPCOMPAT
505504
// HIPv4OPENMPCOMPAT: simple-openmp-amdgcn-amd-amdhsa--gfx908
506505

clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ int main(int argc, const char **argv) {
313313
llvm::DenseSet<StringRef> ParsedTargets;
314314
// Map {offload-kind}-{triple} to target IDs.
315315
std::map<std::string, std::set<StringRef>> TargetIDs;
316+
// Standardize target names to include env field
317+
std::vector<std::string> StandardizedTargetNames;
316318
for (StringRef Target : TargetNames) {
317319
if (ParsedTargets.contains(Target)) {
318320
reportError(createStringError(errc::invalid_argument,
@@ -324,6 +326,8 @@ int main(int argc, const char **argv) {
324326
bool KindIsValid = OffloadInfo.isOffloadKindValid();
325327
bool TripleIsValid = OffloadInfo.isTripleValid();
326328

329+
StandardizedTargetNames.push_back(OffloadInfo.str());
330+
327331
if (!KindIsValid || !TripleIsValid) {
328332
SmallVector<char, 128u> Buf;
329333
raw_svector_ostream Msg(Buf);
@@ -348,6 +352,9 @@ int main(int argc, const char **argv) {
348352

349353
++Index;
350354
}
355+
356+
BundlerConfig.TargetNames = StandardizedTargetNames;
357+
351358
for (const auto &TargetID : TargetIDs) {
352359
if (auto ConflictingTID =
353360
clang::getConflictTargetIDCombination(TargetID.second)) {

0 commit comments

Comments
 (0)