Skip to content

Commit 9c870cb

Browse files
committed
Revert "[OffloadBundler] Rework the ctor of OffloadTargetInfo to support AMDGPU's generic target (llvm#122629)"
This reverts commit d85a81b.
1 parent 62143ff commit 9c870cb

17 files changed

+85
-135
lines changed

clang/docs/ClangOffloadBundler.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,15 @@ without differentiation based on offload kind.
266266
The target triple of the code object. See `Target Triple
267267
<https://clang.llvm.org/docs/CrossCompilation.html#target-triple>`_.
268268

269-
LLVM target triples can be with or without the optional environment field:
269+
The bundler accepts target triples with or without the optional environment
270+
field:
270271

271272
``<arch><sub>-<vendor>-<sys>``, or
272273
``<arch><sub>-<vendor>-<sys>-<env>``
273274

274-
However, in order to standardize outputs for tools that consume bitcode bundles
275-
and to parse target ID containing dashes, the bundler only accepts target
276-
triples in the 4-field format:
275+
However, in order to standardize outputs for tools that consume bitcode
276+
bundles, bundles written by the bundler internally use only the 4-field
277+
target triple:
277278

278279
``<arch><sub>-<vendor>-<sys>-<env>``
279280

@@ -542,4 +543,4 @@ The compressed offload bundle begins with a header followed by the compressed bi
542543
- **Compressed Data**:
543544
The actual compressed binary data follows the header. Its size can be inferred from the total size of the file minus the header size.
544545

545-
> **Note**: Version 3 of the format is under development. It uses 64-bit fields for Total File Size and Uncompressed Binary Size to support files larger than 4GB. To experiment with version 3, set the environment variable `COMPRESSED_BUNDLE_FORMAT_VERSION=3`. This support is experimental and not recommended for production use.
546+
> **Note**: Version 3 of the format is under development. It uses 64-bit fields for Total File Size and Uncompressed Binary Size to support files larger than 4GB. To experiment with version 3, set the environment variable `COMPRESSED_BUNDLE_FORMAT_VERSION=3`. This support is experimental and not recommended for production use.

clang/include/clang/Driver/OffloadBundler.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,6 @@ class CompressedOffloadBundle {
158158
static llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
159159
decompress(const llvm::MemoryBuffer &Input, bool Verbose = false);
160160
};
161-
162-
/// Check whether the bundle id is in the following format:
163-
/// <kind>-<triple>[-<target id>[:target features]]
164-
/// <triple> := <arch>-<vendor>-<os>-<env>
165-
bool checkOffloadBundleID(const llvm::StringRef Str);
166161
} // namespace clang
167162

168163
#endif // LLVM_CLANG_DRIVER_OFFLOADBUNDLER_H

clang/lib/Driver/OffloadBundler.cpp

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,32 @@ OffloadTargetInfo::OffloadTargetInfo(const StringRef Target,
8383
const OffloadBundlerConfig &BC)
8484
: BundlerConfig(BC) {
8585

86-
// <kind>-<triple>[-<target id>[:target features]]
87-
// <triple> := <arch>-<vendor>-<os>-<env>
88-
SmallVector<StringRef, 6> Components;
89-
Target.split(Components, '-', /*MaxSplit=*/5);
90-
assert((Components.size() == 5 || Components.size() == 6) &&
91-
"malformed target string");
92-
93-
StringRef TargetIdWithFeature =
94-
Components.size() == 6 ? Components.back() : "";
95-
StringRef TargetId = TargetIdWithFeature.split(':').first;
96-
if (!TargetId.empty() &&
97-
clang::StringToOffloadArch(TargetId) != clang::OffloadArch::UNKNOWN)
98-
this->TargetID = TargetIdWithFeature;
99-
else
100-
this->TargetID = "";
86+
// TODO: Add error checking from ClangOffloadBundler.cpp
87+
auto TargetFeatures = Target.split(':');
88+
auto TripleOrGPU = TargetFeatures.first.rsplit('-');
89+
90+
if (clang::StringToOffloadArch(TripleOrGPU.second) !=
91+
clang::OffloadArch::UNKNOWN) {
92+
auto KindTriple = TripleOrGPU.first.split('-');
93+
this->OffloadKind = KindTriple.first;
94+
95+
// Enforce optional env field to standardize bundles
96+
llvm::Triple t = llvm::Triple(KindTriple.second);
97+
this->Triple = llvm::Triple(t.getArchName(), t.getVendorName(),
98+
t.getOSName(), t.getEnvironmentName());
99+
100+
this->TargetID = Target.substr(Target.find(TripleOrGPU.second));
101+
} else {
102+
auto KindTriple = TargetFeatures.first.split('-');
103+
this->OffloadKind = KindTriple.first;
104+
105+
// Enforce optional env field to standardize bundles
106+
llvm::Triple t = llvm::Triple(KindTriple.second);
107+
this->Triple = llvm::Triple(t.getArchName(), t.getVendorName(),
108+
t.getOSName(), t.getEnvironmentName());
101109

102-
this->OffloadKind = Components.front();
103-
ArrayRef<StringRef> TripleSlice{&Components[1], /*length=*/4};
104-
llvm::Triple T = llvm::Triple(llvm::join(TripleSlice, "-"));
105-
this->Triple = llvm::Triple(T.getArchName(), T.getVendorName(), T.getOSName(),
106-
T.getEnvironmentName());
110+
this->TargetID = "";
111+
}
107112
}
108113

109114
bool OffloadTargetInfo::hasHostKind() const {
@@ -143,18 +148,7 @@ bool OffloadTargetInfo::operator==(const OffloadTargetInfo &Target) const {
143148
}
144149

145150
std::string OffloadTargetInfo::str() const {
146-
std::string NormalizedTriple;
147-
// Unfortunately we need some special sauce for AMDGPU because all the runtime
148-
// assumes the triple to be "amdgcn-amd-amdhsa-" (empty environment) instead
149-
// of "amdgcn-amd-amdhsa-unknown". It's gonna be very tricky to patch
150-
// different layers of runtime.
151-
if (Triple.isAMDGPU()) {
152-
NormalizedTriple = Triple.normalize(Triple::CanonicalForm::THREE_IDENT);
153-
NormalizedTriple.push_back('-');
154-
} else {
155-
NormalizedTriple = Triple.normalize(Triple::CanonicalForm::FOUR_IDENT);
156-
}
157-
return Twine(OffloadKind + "-" + NormalizedTriple + "-" + TargetID).str();
151+
return Twine(OffloadKind + "-" + Triple.str() + "-" + TargetID).str();
158152
}
159153

160154
static StringRef getDeviceFileExtension(StringRef Device,
@@ -1513,9 +1507,6 @@ Error OffloadBundler::UnbundleFiles() {
15131507
StringMap<StringRef> Worklist;
15141508
auto Output = BundlerConfig.OutputFileNames.begin();
15151509
for (auto &Triple : BundlerConfig.TargetNames) {
1516-
if (!checkOffloadBundleID(Triple))
1517-
return createStringError(errc::invalid_argument,
1518-
"invalid bundle id from bundle config");
15191510
Worklist[Triple] = *Output;
15201511
++Output;
15211512
}
@@ -1535,9 +1526,6 @@ Error OffloadBundler::UnbundleFiles() {
15351526

15361527
StringRef CurTriple = **CurTripleOrErr;
15371528
assert(!CurTriple.empty());
1538-
if (!checkOffloadBundleID(CurTriple))
1539-
return createStringError(errc::invalid_argument,
1540-
"invalid bundle id read from the bundle");
15411529

15421530
auto Output = Worklist.begin();
15431531
for (auto E = Worklist.end(); Output != E; Output++) {
@@ -1596,8 +1584,6 @@ Error OffloadBundler::UnbundleFiles() {
15961584
return createFileError(E.second, EC);
15971585

15981586
// If this entry has a host kind, copy the input file to the output file.
1599-
// We don't need to check E.getKey() here through checkOffloadBundleID
1600-
// because the entire WorkList has been checked above.
16011587
auto OffloadInfo = OffloadTargetInfo(E.getKey(), BundlerConfig);
16021588
if (OffloadInfo.hasHostKind())
16031589
OutputFile.write(Input.getBufferStart(), Input.getBufferSize());
@@ -1827,10 +1813,6 @@ Error OffloadBundler::UnbundleArchive() {
18271813
// archive.
18281814
while (!CodeObject.empty()) {
18291815
SmallVector<StringRef> CompatibleTargets;
1830-
if (!checkOffloadBundleID(CodeObject)) {
1831-
return createStringError(errc::invalid_argument,
1832-
"Invalid bundle id read from code object");
1833-
}
18341816
auto CodeObjectInfo = OffloadTargetInfo(CodeObject, BundlerConfig);
18351817
if (getCompatibleOffloadTargets(CodeObjectInfo, CompatibleTargets,
18361818
BundlerConfig)) {
@@ -1912,11 +1894,3 @@ Error OffloadBundler::UnbundleArchive() {
19121894

19131895
return Error::success();
19141896
}
1915-
1916-
bool clang::checkOffloadBundleID(const llvm::StringRef Str) {
1917-
// <kind>-<triple>[-<target id>[:target features]]
1918-
// <triple> := <arch>-<vendor>-<os>-<env>
1919-
SmallVector<StringRef, 6> Components;
1920-
Str.split(Components, '-', /*MaxSplit=*/5);
1921-
return Components.size() == 5 || Components.size() == 6;
1922-
}

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9160,8 +9160,7 @@ void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA,
91609160
}
91619161
Triples += Action::GetOffloadKindName(CurKind);
91629162
Triples += '-';
9163-
Triples +=
9164-
CurTC->getTriple().normalize(llvm::Triple::CanonicalForm::FOUR_IDENT);
9163+
Triples += CurTC->getTriple().normalize();
91659164
if ((CurKind == Action::OFK_HIP || CurKind == Action::OFK_Cuda) &&
91669165
!StringRef(CurDep->getOffloadingArch()).empty()) {
91679166
Triples += '-';
@@ -9264,8 +9263,7 @@ void OffloadBundler::ConstructJobMultipleOutputs(
92649263
auto OffloadKind = Dep.DependentOffloadKind;
92659264
Triples += Action::GetOffloadKindName(OffloadKind);
92669265
Triples += '-';
9267-
Triples += Dep.DependentToolChain->getTriple().normalize(
9268-
llvm::Triple::CanonicalForm::FOUR_IDENT);
9266+
Triples += Dep.DependentToolChain->getTriple().normalize();
92699267
if ((Dep.DependentOffloadKind == Action::OFK_HIP ||
92709268
Dep.DependentOffloadKind == Action::OFK_Cuda) &&
92719269
!Dep.DependentBoundArch.empty()) {

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,8 +2752,7 @@ static void GetSDLFromOffloadArchive(
27522752
SmallString<128> DeviceTriple;
27532753
DeviceTriple += Action::GetOffloadKindName(JA.getOffloadingDeviceKind());
27542754
DeviceTriple += '-';
2755-
std::string NormalizedTriple = T.getToolChain().getTriple().normalize(
2756-
llvm::Triple::CanonicalForm::FOUR_IDENT);
2755+
std::string NormalizedTriple = T.getToolChain().getTriple().normalize();
27572756
DeviceTriple += NormalizedTriple;
27582757
if (!Target.empty()) {
27592758
DeviceTriple += '-';

clang/lib/Driver/ToolChains/HIPUtility.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static std::string normalizeForBundler(const llvm::Triple &T,
4545
return HasTargetID ? (T.getArchName() + "-" + T.getVendorName() + "-" +
4646
T.getOSName() + "-" + T.getEnvironmentName())
4747
.str()
48-
: T.normalize(llvm::Triple::CanonicalForm::FOUR_IDENT);
48+
: T.normalize();
4949
}
5050

5151
// Collect undefined __hip_fatbin* and __hip_gpubin_handle* symbols from all

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
// Check code object compatibility for archive unbundling
1616
//
1717
// Create few code object bundles and archive them to create an input archive
18-
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-amdgcn-amd-amdhsa--gfx906,openmp-amdgcn-amd-amdhsa--gfx908 -input=%t.o -input=%t.tgt1 -input=%t.tgt2 -output=%t.simple.bundle
18+
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-amdgcn-amd-amdhsa-gfx906,openmp-amdgcn-amd-amdhsa--gfx908 -input=%t.o -input=%t.tgt1 -input=%t.tgt2 -output=%t.simple.bundle
1919
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-amdgcn-amd-amdhsa--gfx906:sramecc+:xnack+,openmp-amdgcn-amd-amdhsa--gfx908:sramecc+:xnack+ -inputs=%t.o,%t.tgt1,%t.tgt1 -outputs=%t.targetID1.bundle
2020
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-amdgcn-amd-amdhsa--gfx906:sramecc+:xnack-,openmp-amdgcn-amd-amdhsa--gfx908:sramecc+:xnack- -inputs=%t.o,%t.tgt1,%t.tgt1 -outputs=%t.targetID2.bundle
2121
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-amdgcn-amd-amdhsa--gfx906:xnack-,openmp-amdgcn-amd-amdhsa--gfx908:xnack- -inputs=%t.o,%t.tgt1,%t.tgt1 -outputs=%t.targetID3.bundle
2222
// RUN: llvm-ar cr %t.input-archive.a %t.simple.bundle %t.targetID1.bundle %t.targetID2.bundle %t.targetID3.bundle
2323

2424
// Tests to check compatibility between Bundle Entry ID formats i.e. between presence/absence of extra hyphen in case of missing environment field
25-
// 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
25+
// 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
2626
// BUNDLECOMPATIBILITY: Compatible: Exact match: [CodeObject: openmp-amdgcn-amd-amdhsa--gfx906] : [Target: openmp-amdgcn-amd-amdhsa--gfx906]
2727
// BUNDLECOMPATIBILITY: Compatible: Exact match: [CodeObject: openmp-amdgcn-amd-amdhsa--gfx908] : [Target: openmp-amdgcn-amd-amdhsa--gfx908]
2828

29-
// 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
29+
// 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
3030
// HIPOpenMPCOMPATIBILITY: Compatible: Target IDs are compatible [CodeObject: openmp-amdgcn-amd-amdhsa--gfx906] : [Target: hip-amdgcn-amd-amdhsa--gfx906]
3131
// HIPOpenMPCOMPATIBILITY: Compatible: Target IDs are compatible [CodeObject: openmp-amdgcn-amd-amdhsa--gfx908] : [Target: hipv4-amdgcn-amd-amdhsa--gfx908]
3232

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@
1515
//
1616
// Check code object compatibility for archive unbundling
1717
//
18-
// Create an object bundle
19-
// 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
18+
// Create an object bundle with and without env fields
19+
// 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
20+
// 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
2021

21-
// RUN: clang-offload-bundler -unbundle -type=o -targets=hip-amdgcn-amd-amdhsa--gfx906,hip-amdgcn-amd-amdhsa--gfx908 -input=%t.bundle -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
22-
// BUNDLE: Compatible: Exact match: [CodeObject: hip-amdgcn-amd-amdhsa--gfx906] : [Target: hip-amdgcn-amd-amdhsa--gfx906]
23-
// BUNDLE: Compatible: Exact match: [CodeObject: hip-amdgcn-amd-amdhsa--gfx908] : [Target: hip-amdgcn-amd-amdhsa--gfx908]
22+
23+
// Unbundle bundle.no.env while providing targets with env
24+
// 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
25+
// BUNDLE-NO-ENV: Compatible: Exact match: [CodeObject: hip-amdgcn-amd-amdhsa--gfx906] : [Target: hip-amdgcn-amd-amdhsa--gfx906]
26+
// BUNDLE-NO-ENV: Compatible: Exact match: [CodeObject: hip-amdgcn-amd-amdhsa--gfx908] : [Target: hip-amdgcn-amd-amdhsa--gfx908]
27+
28+
// Unbundle bundle.env while providing targets with no env
29+
// 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
30+
// BUNDLE-ENV: Compatible: Exact match: [CodeObject: hip-amdgcn-amd-amdhsa--gfx906] : [Target: hip-amdgcn-amd-amdhsa--gfx906]
31+
// BUNDLE-ENV: Compatible: Exact match: [CodeObject: hip-amdgcn-amd-amdhsa--gfx908] : [Target: hip-amdgcn-amd-amdhsa--gfx908]
2432

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

0 commit comments

Comments
 (0)