Skip to content

Commit 53308b1

Browse files
author
Artem Gindinson
authored
[SYCL] Fix backwards compatibility for libraries in archive format (#4685)
With `-sycldevice` triple environment removal (as performed in #3929), backwards compatibility has been enforced for SYCL objects/object-format libraries only. To ensure that `.a` libraries (archives) built by older compiler versions are usable with newer compiler version, adjust `clang-offload-deps` to consider `-sycldevice`-marked symbols when encountering a SYCL target. Signed-off-by: Artem Gindinson <artem.gindinson@intel.com>
1 parent b79eb91 commit 53308b1

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,28 @@
2323
// Generate dependencies for targets and check contents of the output bitcode files.
2424
//
2525
// RUN: clang-offload-deps -targets=openmp-x86_64-pc-linux-gnu,sycl-spir64 -outputs=%t.deps.x86_64,%t.deps.spir64 %t.fat
26+
// RUN: clang-offload-deps -targets=openmp-x86_64-pc-linux-gnu,sycl-spir64 -outputs=%t.deps.x86_64,%t.deps.spir64 %t.fat
2627
// RUN: llvm-dis -o - %t.deps.x86_64 | FileCheck %s --check-prefixes=CHECK-DEPS-X86_64
27-
// RUN: llvm-dis -o - %t.deps.spir64 | FileCheck %s --check-prefixes=CHECK-DEPS-SPIR64
28+
// RUN: llvm-dis -o - %t.deps.spir64 | FileCheck %s --check-prefixes=CHECK-DEPS-SPIR64 -DSPIRTriple=spir64
29+
//
30+
// Check that the legacy 'sycldevice' symbols are still identified correctly
31+
// when 'unknown' environment has been specified/implied for SYCL via
32+
// clang-offload-bundler's -targets
33+
//
34+
// RUN: clang-offload-bundler -type=o -targets=host-%itanium_abi_triple,openmp-x86_64-pc-linux-gnu,sycl-spir64-unknown-unknown-sycldevice -inputs=%t.host,%t.x86_64,%t.spir64 -outputs=%t.legacy-sycldevice.fat
35+
// Check correct behavior for multiple targets
36+
// RUN: clang-offload-deps -targets=openmp-x86_64-pc-linux-gnu,sycl-spir64-unknown-unknown -outputs=%t.deps.legacy.x86_64,%t.deps.legacy.spir64 %t.legacy-sycldevice.fat
37+
// RUN: llvm-dis -o - %t.deps.legacy.spir64 | FileCheck %s --check-prefixes=CHECK-DEPS-SPIR64 -DSPIRTriple=spir64-unknown-unknown
38+
// Check correct behavior for shortened triple
39+
// RUN: clang-offload-deps -targets=sycl-spir64 -outputs=%t.deps.legacy.spir64-short %t.legacy-sycldevice.fat
40+
// RUN: llvm-dis -o - %t.deps.legacy.spir64-short | FileCheck %s --check-prefixes=CHECK-DEPS-SPIR64 -DSPIRTriple=spir64
2841

2942
// CHECK-DEPS-X86_64: target triple = "x86_64-pc-linux-gnu"
3043
// CHECK-DEPS-X86_64: @bar = external global i8*
3144
// CHECK-DEPS-X86_64: @foo = external global i8*
3245
// CHECK-DEPS-X86_64: @offload.symbols = hidden local_unnamed_addr global [2 x i8*] [i8* bitcast (i8** @bar to i8*), i8* bitcast (i8** @foo to i8*)]
3346

34-
// CHECK-DEPS-SPIR64: target triple = "spir64"
47+
// CHECK-DEPS-SPIR64: target triple = "[[SPIRTriple]]"
3548
// CHECK-DEPS-SPIR64: @bar = external global i8*
3649
// CHECK-DEPS-SPIR64: @foo = external global i8*
3750
// CHECK-DEPS-SPIR64: @llvm.used = appending global [2 x i8*] [i8* bitcast (i8** @bar to i8*), i8* bitcast (i8** @foo to i8*)], section "llvm.metadata"

clang/tools/clang-offload-deps/ClangOffloadDeps.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,33 @@ int main(int argc, const char **argv) {
167167
for (StringRef Symbol = DataOrErr.get(); !Symbol.empty();) {
168168
unsigned Len = strlen(Symbol.data());
169169

170+
// TODO: Consider storing Targets and Kinds in a single map-like struct,
171+
// possibly reusing ClangOffloadBundler's 'OffloadTargetInfo'.
172+
auto KindsIter = Kinds.begin();
170173
for (const std::string &Target : Targets) {
171174
std::string Prefix = Target + ".";
172175
if (Symbol.startswith(Prefix))
173176
Target2Symbols[Target].insert(
174177
Symbol.substr(Prefix.size(), Len - Prefix.size()));
178+
else if (KindsIter->equals("sycl")) {
179+
// FIXME: Temporary solution for supporting libraries produced by old
180+
// versions of SYCL toolchain. Old versions used triples with
181+
// 'sycldevice' environment component of the triple, whereas new
182+
// toolchain use 'unknown' value for that triple component.
183+
// We check for the legacy 'sycldevice' variant upon the negative
184+
// check for a SYCL triple with 'unknown' environment.
185+
std::string LegacyPrefix(Target);
186+
// In case vendor and OS are not set for this target, fill these with
187+
// 'unknown' so that our target has the "canonical" form of:
188+
// <kind>-<arch>-<vendor>-<os>-<sycldevice>
189+
while (StringRef(LegacyPrefix).count("-") < 3)
190+
LegacyPrefix += "-unknown";
191+
LegacyPrefix += "-sycldevice.";
192+
if (Symbol.startswith(LegacyPrefix))
193+
Target2Symbols[Target].insert(
194+
Symbol.substr(LegacyPrefix.size(), Len - LegacyPrefix.size()));
195+
}
196+
++KindsIter;
175197
}
176198

177199
Symbol = Symbol.drop_front(Len + 1u);

0 commit comments

Comments
 (0)