Skip to content

Commit 8d329a0

Browse files
authored
[SYCL] Fix devicelib identification for NVPTX (#15357)
Recent upstream changes removed the workaround that required libraries to be named ".cubin". We also renamed the NVPTX libdevice library when we unified it into a single bytecode library. Unfortunately both of these things broke the driver's brittle logic that determined whether or not an object was a device library. This resulted in us not stripping any symbols from the final object, which is both slower and broke some SYCL CTS tests. This commit more clearly delineates logic between NVPTX and NativeCPU, as the latter target didn't actually link against anything containing 'libdevice' so was too lenient.
1 parent 32cb1b3 commit 8d329a0

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -592,16 +592,15 @@ const char *SYCL::Linker::constructLLVMLinkCommand(
592592
NewLibPostfix = ".new.obj";
593593
std::string FileName = this->getToolChain().getInputFilename(II);
594594
StringRef InputFilename = llvm::sys::path::filename(FileName);
595-
if (IsNVPTX || IsSYCLNativeCPU) {
596-
// Linking SYCL Device libs requires libclc as well as libdevice
597-
if ((InputFilename.find("libspirv") != InputFilename.npos ||
598-
InputFilename.find("libdevice") != InputFilename.npos))
599-
return true;
600-
if (IsNVPTX) {
601-
LibPostfix = ".cubin";
602-
NewLibPostfix = ".new.cubin";
603-
}
604-
}
595+
// NativeCPU links against libclc (libspirv)
596+
if (IsSYCLNativeCPU && InputFilename.contains("libspirv"))
597+
return true;
598+
// NVPTX links against our libclc (libspirv), our libdevice (devicelib),
599+
// and the CUDA libdevice
600+
if (IsNVPTX && (InputFilename.starts_with("devicelib-") ||
601+
InputFilename.contains("libspirv") ||
602+
InputFilename.contains("libdevice")))
603+
return true;
605604
StringRef LibSyclPrefix("libsycl-");
606605
if (!InputFilename.starts_with(LibSyclPrefix) ||
607606
!InputFilename.ends_with(LibPostfix) ||

clang/test/Driver/sycl-nvptx-link.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Check that we correctly determine that the final link command links
2+
// devicelibs together, as far as the driver is concerned. This results in the
3+
// -only-needed flag.
4+
//
5+
// Note we check the names of the various device libraries because that's the
6+
// logic the driver uses.
7+
8+
// Older CUDA versions had versioned libdevice files. We don't support CUDA
9+
// this old in SYCL, but we still test the driver's ability to pick out the
10+
// correctly versioned libdevice. We use Inputs/CUDA_80 which has a full set of
11+
// libdevice files.
12+
// RUN: %clang -### -fsycl -fsycl-targets=nvptx64-nvidia-cuda \
13+
// RUN: -Xsycl-target-backend --cuda-gpu-arch=sm_30 \
14+
// RUN: --sysroot=%S/Inputs/SYCL --cuda-path=%S/Inputs/CUDA_80/usr/local/cuda %s 2>&1 \
15+
// RUN: | FileCheck %s --check-prefixes=CHECK,LIBDEVICE30
16+
// RUN: %clang -### -fsycl -fsycl-targets=nvptx64-nvidia-cuda \
17+
// RUN: -Xsycl-target-backend --cuda-gpu-arch=sm_35 \
18+
// RUN: --sysroot=%S/Inputs/SYCL --cuda-path=%S/Inputs/CUDA_80/usr/local/cuda %s 2>&1 \
19+
// RUN: | FileCheck %s --check-prefixes=CHECK,LIBDEVICE35
20+
// RUN: %clang -### -fsycl -fsycl-targets=nvptx64-nvidia-cuda \
21+
// RUN: -Xsycl-target-backend --cuda-gpu-arch=sm_50 \
22+
// RUN: --sysroot=%S/Inputs/SYCL --cuda-path=%S/Inputs/CUDA_80/usr/local/cuda %s 2>&1 \
23+
// RUN: | FileCheck %s --check-prefixes=CHECK,LIBDEVICE50
24+
25+
// CUDA-9+ uses the same libdevice for all GPU variants
26+
// RUN: %clang -### -fsycl -fsycl-targets=nvptx64-nvidia-cuda \
27+
// RUN: -Xsycl-target-backend --cuda-gpu-arch=sm_35 \
28+
// RUN: --sysroot=%S/Inputs/SYCL --cuda-path=%S/Inputs/CUDA_90/usr/local/cuda %s 2>&1 \
29+
// RUN: | FileCheck %s --check-prefixes=CHECK,LIBDEVICE10
30+
31+
// First link command: ignored
32+
// CHECK: llvm-link
33+
34+
// CHECK: llvm-link
35+
// CHECK-SAME: -only-needed
36+
// CHECK-SAME: devicelib--cuda.bc
37+
// CHECK-SAME: libspirv-nvptx64-nvidia-cuda.bc
38+
// LIBDEVICE10-SAME: libdevice.10.bc
39+
// LIBDEVICE30-SAME: libdevice.compute_30.10.bc
40+
// LIBDEVICE35-SAME: libdevice.compute_35.10.bc
41+
// LIBDEVICE50-SAME: libdevice.compute_50.10.bc

0 commit comments

Comments
 (0)