Skip to content

Commit d10a290

Browse files
Reintroduce reverted commit "[clang-offload-bundler] Standardize TargetID field for bundler (#9631)
This commit reintroduces the following LLVM open-source PR: https://reviews.llvm.org/D145770 Some changes on top of the community change have been made as well. Thanks --------- Signed-off-by: Arvind Sudarsanam <[email protected]> Co-authored-by: Alexey Sachkov <[email protected]>
1 parent 3a02b06 commit d10a290

15 files changed

+158
-46
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/Driver.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ using namespace clang::driver;
115115
using namespace clang;
116116
using namespace llvm::opt;
117117

118+
// clang-offload-bundler is currently generating a 'standardized' target triple.
119+
// Triple's format - Architecture-Vendor-OS-Environment.
120+
// Bundle sections created by clang-offload-bundler contain the 'standardized'
121+
// triple. This routine transforms the triple specified by user as input to this
122+
// 'standardized' format to facilitate checks.
123+
static std::string standardizedTriple(std::string OrigTriple) {
124+
if (OrigTriple.back() == '-') // Already standardized
125+
return OrigTriple;
126+
llvm::Triple t = llvm::Triple(OrigTriple);
127+
return llvm::Triple(t.getArchName(), t.getVendorName(), t.getOSName(),
128+
t.getEnvironmentName())
129+
.str() +
130+
"-";
131+
}
132+
118133
static std::optional<llvm::Triple> getOffloadTargetTriple(const Driver &D,
119134
const ArgList &Args) {
120135
auto OffloadTargets = Args.getAllArgValues(options::OPT_offload_EQ);
@@ -5975,7 +5990,7 @@ class OffloadingActionBuilder final {
59755990
Arch = C.getDriver().MakeSYCLDeviceTriple("spir64_fpga").str();
59765991
if (std::find(UniqueSections.begin(), UniqueSections.end(), Arch) ==
59775992
UniqueSections.end())
5978-
UniqueSections.push_back(Arch);
5993+
UniqueSections.push_back(standardizedTriple(Arch));
59795994
}
59805995
}
59815996

@@ -5988,7 +6003,7 @@ class OffloadingActionBuilder final {
59886003
SectionTriple += "-";
59896004
SectionTriple += SyclTarget.BoundArch;
59906005
}
5991-
6006+
SectionTriple = standardizedTriple(SectionTriple);
59926007
// If any matching section is found, we are good.
59936008
if (std::find(UniqueSections.begin(), UniqueSections.end(),
59946009
SectionTriple) != UniqueSections.end())

clang/lib/Driver/OffloadBundler.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,25 @@ OffloadTargetInfo::OffloadTargetInfo(const StringRef Target,
8585
if (clang::StringToCudaArch(TripleOrGPU.second) != clang::CudaArch::UNKNOWN) {
8686
auto KindTriple = TripleOrGPU.first.split('-');
8787
this->OffloadKind = KindTriple.first;
88-
this->Triple = llvm::Triple(KindTriple.second);
89-
this->TargetID = Target.substr(Target.find(TripleOrGPU.second));
88+
89+
// Enforce optional env field to standardize bundles
90+
llvm::Triple t = llvm::Triple(KindTriple.second);
91+
this->Triple = llvm::Triple(t.getArchName(), t.getVendorName(),
92+
t.getOSName(), t.getEnvironmentName());
93+
94+
if (TripleOrGPU.second.empty())
95+
this->TargetID = "";
96+
else
97+
this->TargetID = Target.substr(Target.find(TripleOrGPU.second));
9098
} else {
9199
auto KindTriple = TargetFeatures.first.split('-');
92100
this->OffloadKind = KindTriple.first;
93-
this->Triple = llvm::Triple(KindTriple.second);
101+
102+
// Enforce optional env field to standardize bundles
103+
llvm::Triple t = llvm::Triple(KindTriple.second);
104+
this->Triple = llvm::Triple(t.getArchName(), t.getVendorName(),
105+
t.getOSName(), t.getEnvironmentName());
106+
94107
this->TargetID = "";
95108
}
96109
}
@@ -1577,34 +1590,34 @@ Error OffloadBundler::UnbundleFiles() {
15771590
return Error::success();
15781591
}
15791592

1580-
// Unbundle the files. Return true if an error was found.
1593+
// Unbundle the files. Return false if an error was found.
15811594
Expected<bool>
15821595
clang::CheckBundledSection(const OffloadBundlerConfig &BundlerConfig) {
15831596
// Open Input file.
15841597
ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
15851598
MemoryBuffer::getFileOrSTDIN(BundlerConfig.InputFileNames.front());
15861599
if (std::error_code EC = CodeOrErr.getError())
1587-
return createFileError(BundlerConfig.InputFileNames.front(), EC);
1600+
return false;
15881601
MemoryBuffer &Input = *CodeOrErr.get();
15891602

15901603
// Select the right files handler.
15911604
Expected<std::unique_ptr<FileHandler>> FileHandlerOrErr =
15921605
CreateFileHandler(Input, BundlerConfig);
15931606
if (!FileHandlerOrErr)
1594-
return FileHandlerOrErr.takeError();
1607+
return false;
15951608

15961609
std::unique_ptr<FileHandler> &FH = *FileHandlerOrErr;
15971610

15981611
// Quit if we don't have a handler.
15991612
if (!FH)
1600-
return true;
1613+
return false;
16011614

16021615
// Seed temporary filename generation with the stem of the input file.
16031616
FH->SetTempFileNameBase(llvm::sys::path::stem(BundlerConfig.InputFileNames.front()));
16041617

16051618
// Read the header of the bundled file.
16061619
if (Error Err = FH->ReadHeader(Input))
1607-
return std::move(Err);
1620+
return false;
16081621

16091622
StringRef triple = BundlerConfig.TargetNames.front();
16101623

@@ -1615,13 +1628,15 @@ clang::CheckBundledSection(const OffloadBundlerConfig &BundlerConfig) {
16151628
Expected<std::optional<StringRef>> CurTripleOrErr =
16161629
FH->ReadBundleStart(Input);
16171630
if (!CurTripleOrErr)
1618-
return CurTripleOrErr.takeError();
1631+
return false;
16191632

16201633
// We don't have more bundles.
16211634
if (!*CurTripleOrErr)
16221635
break;
16231636

1624-
if (*CurTripleOrErr == triple) {
1637+
StringRef CurTriple = **CurTripleOrErr;
1638+
if (OffloadTargetInfo(CurTriple, BundlerConfig).Triple.str() ==
1639+
OffloadTargetInfo(triple, BundlerConfig).Triple.str()) {
16251640
found = true;
16261641
break;
16271642
}
Binary file not shown.
Binary file not shown.

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// REQUIRES: x86-registered-target
2+
// UNSUPPORTED: target={{.*}}-darwin{{.*}}, target={{.*}}-aix{{.*}}, system-windows
3+
4+
// Check working of bundler before and after standardization
5+
// RUN: clang-offload-bundler -type=o -targets=host-x86_64-unknown-linux-gnu,sycl-spir64-unknown-unknown -input=%S/Inputs/bundles/bundle_bef_standardization_of_target_triple.o -output=test-host-x86_64-unknown-linux-gnu.o -output=test-sycl-spir64-unknown-unknown.o -unbundle 2>&1 | FileCheck %s -check-prefix=CHECK-STD-OLD --allow-empty
6+
// CHECK-STD-OLD-NOT: error: Can't find bundles for
7+
// RUN: clang-offload-bundler -type=o -targets=host-x86_64-unknown-linux-gnu,sycl-spir64-unknown-unknown -input=%S/Inputs/bundles/bundle_aft_standardization_of_target_triple.o -output=test-host-x86_64-unknown-linux-gnu.o -output=test-sycl-spir64-unknown-unknown.o -unbundle 2>&1 | FileCheck %s -check-prefix=CHECK-STD-NEW --allow-empty
8+
// CHECK-STD-NEW-NOT: error: Can't find bundles for
9+
10+
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-tgtsym-asm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// RUN: llvm-readobj --string-dump=.tgtsym %t.fat.o | FileCheck %s
1414

1515
// CHECK: String dump of section '.tgtsym':
16-
// CHECK-DAG: openmp-x86_64-pc-linux-gnu.foo
17-
// CHECK-DAG: sycl-spir64.foo
16+
// CHECK-DAG: openmp-x86_64-pc-linux-gnu-.foo
17+
// CHECK-DAG: sycl-spir64----.foo
1818

1919
__asm__(".symver memcpy,memcpy@GLIBC_2.2.5");
2020
void foo(void) {}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
// RUN: llvm-readobj --string-dump=.tgtsym %t.fat.o | FileCheck %s
1313

1414
// CHECK: String dump of section '.tgtsym':
15-
// CHECK-DAG: openmp-x86_64-pc-linux-gnu.foo
16-
// CHECK-DAG: openmp-x86_64-pc-linux-gnu.bar
17-
// CHECK-DAG: sycl-spir64.foo
18-
// CHECK-DAG: sycl-spir64.bar
15+
// CHECK-DAG: openmp-x86_64-pc-linux-gnu-.foo
16+
// CHECK-DAG: openmp-x86_64-pc-linux-gnu-.bar
17+
// CHECK-DAG: sycl-spir64----.foo
18+
// CHECK-DAG: sycl-spir64----.bar
1919
// CHECK-NOT: undefined_func
2020
// CHECK-NOT: static_func
2121
// CHECK-NOT: static_used
22-
// CHECK-NOT: sycl-spir64.llvm.used
23-
// CHECK-NOT: sycl-spir64.llvm.compiler.used
24-
// CHECK-NOT: sycl-spir64.const_as
22+
// CHECK-NOT: sycl-spir64----.llvm.used
23+
// CHECK-NOT: sycl-spir64----.llvm.compiler.used
24+
// CHECK-NOT: sycl-spir64----.const_as
2525

2626
// RUN: clang-offload-bundler --add-target-symbols-to-bundled-object=false -type=o -targets=host-%itanium_abi_triple,openmp-x86_64-pc-linux-gnu,sycl-spir64 -input=%t.o -input=%t.tgt1 -input=%t.tgt2 -output=%t.fat.no.tgtsym.o
2727
// RUN: llvm-readobj --string-dump=.tgtsym %t.fat.no.tgtsym.o | FileCheck %s --check-prefix CHECK-NO-TGTSYM

0 commit comments

Comments
 (0)