Skip to content

Commit ff49710

Browse files
JackAKirkJackAKirk
andauthored
[SYCL][CUDA][HIP AMD] Correct cuda/hip spirv libclc .bc and module triple. (#7245)
People have been getting confused, see e.g. #6922, due to warnings of mismatching triples in the HIP AMD and CUDA backends during linking. This warning occurs because the bitcode compiled from spirv libclc does not use the same triples as those passed to clang to compile SYCL in the CUDA, "nvptx64-nvidia-cuda", and HIP "amdgcn-amd-amdhsa" backends. However these modules are created only to be linked with clang compiled SYCL code and the modules are always compatible. Therefore in this patch we update the triples of the final remangled spirv modules and update the names of the final remangled .bcs that are built/installed/linked with the clang built .bcs. Fixes #6922 Signed-off-by: JackAKirk <[email protected]> Co-authored-by: JackAKirk <[email protected]>
1 parent 97961a0 commit ff49710

File tree

8 files changed

+33
-17
lines changed

8 files changed

+33
-17
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5261,9 +5261,9 @@ class OffloadingActionBuilder final {
52615261
// Select remangled libclc variant
52625262
std::string LibSpirvTargetName =
52635263
(TC->getAuxTriple()->isOSWindows())
5264-
? "remangled-l32-signed_char.libspirv-nvptx64--nvidiacl."
5264+
? "remangled-l32-signed_char.libspirv-nvptx64-nvidia-cuda."
52655265
"bc"
5266-
: "remangled-l64-signed_char.libspirv-nvptx64--nvidiacl."
5266+
: "remangled-l64-signed_char.libspirv-nvptx64-nvidia-cuda."
52675267
"bc";
52685268

52695269
for (StringRef LibraryPath : LibraryPaths) {

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,8 @@ std::string CudaToolChain::getInputFilename(const InputInfo &Input) const {
731731
// Windows
732732
static const char *getLibSpirvTargetName(const ToolChain &HostTC) {
733733
if (HostTC.getTriple().isOSWindows())
734-
return "remangled-l32-signed_char.libspirv-nvptx64--nvidiacl.bc";
735-
return "remangled-l64-signed_char.libspirv-nvptx64--nvidiacl.bc";
734+
return "remangled-l32-signed_char.libspirv-nvptx64-nvidia-cuda.bc";
735+
return "remangled-l64-signed_char.libspirv-nvptx64-nvidia-cuda.bc";
736736
}
737737

738738
void CudaToolChain::addClangTargetOptions(

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
214214
}
215215

216216
static const char *getLibSpirvTargetName(const ToolChain &HostTC) {
217-
return "remangled-l64-signed_char.libspirv-amdgcn--amdhsa.bc";
217+
return "remangled-l64-signed_char.libspirv-amdgcn-amd-amdhsa.bc";
218218
}
219219

220220
void HIPAMDToolChain::addClangTargetOptions(

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ const char *SYCL::Linker::constructLLVMLinkCommand(
172172
StringRef InputFilename = llvm::sys::path::filename(FileName);
173173
if (this->getToolChain().getTriple().isNVPTX()) {
174174
// Linking SYCL Device libs requires libclc as well as libdevice
175-
if ((InputFilename.find("nvidiacl") != InputFilename.npos ||
175+
if ((InputFilename.find("libspirv") != InputFilename.npos ||
176176
InputFilename.find("libdevice") != InputFilename.npos))
177177
return true;
178178
LibPostfix = ".cubin";
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %clang -### -fsycl -fsycl-targets=nvptx64-nvidia-cuda -nocudalib -target x86_64-unknown-windows-msvc %s 2> %t.win.out
22
// RUN: FileCheck %s --check-prefixes=CHECK-WINDOWS --input-file %t.win.out
3-
// CHECK-WINDOWS: remangled-l32-signed_char.libspirv-nvptx64--nvidiacl.bc
3+
// CHECK-WINDOWS: remangled-l32-signed_char.libspirv-nvptx64-nvidia-cuda.bc
44
//
55
// RUN: %clang -### -fsycl -fsycl-targets=nvptx64-nvidia-cuda -nocudalib -target x86_64-unknown-linux-gnu %s 2> %t.lnx.out
66
// RUN: FileCheck %s --check-prefixes=CHECK-LINUX --input-file %t.lnx.out
7-
// CHECK-LINUX: remangled-l64-signed_char.libspirv-nvptx64--nvidiacl.bc
7+
// CHECK-LINUX: remangled-l64-signed_char.libspirv-nvptx64-nvidia-cuda.bc

clang/test/Driver/sycl-libspirv-invalid.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55
// RUN: -fsycl-targets=nvptx64-nvidia-cuda --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
66
// RUN: -fsycl-libspirv-path=%S/Inputs/SYCL/no-libspirv-exists-here.bc %s 2>&1 \
77
// RUN: | FileCheck --check-prefix=ERR-CUDA %s
8-
// ERR-CUDA: cannot find 'remangled-l64-signed_char.libspirv-nvptx64--nvidiacl.bc';
8+
// ERR-CUDA: cannot find 'remangled-l64-signed_char.libspirv-nvptx64-nvidia-cuda.bc';
99
// ERR-CUDA-SAME: provide path to libspirv library via '-fsycl-libspirv-path', or pass '-fno-sycl-libspirv' to build without linking with libspirv
1010

1111
// RUN: %clangxx -### -std=c++11 -target x86_64-unknown-windows-msvc -fsycl \
1212
// RUN: -fsycl-targets=nvptx64-nvidia-cuda --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
1313
// RUN: -fsycl-libspirv-path=%S/Inputs/SYCL/no-libspirv-exists-here.bc %s 2>&1 \
1414
// RUN: | FileCheck --check-prefix=ERR-CUDA-WIN %s
15-
// ERR-CUDA-WIN: cannot find 'remangled-l32-signed_char.libspirv-nvptx64--nvidiacl.bc';
15+
// ERR-CUDA-WIN: cannot find 'remangled-l32-signed_char.libspirv-nvptx64-nvidia-cuda.bc';
1616
// ERR-CUDA-WIN-SAME: provide path to libspirv library via '-fsycl-libspirv-path', or pass '-fno-sycl-libspirv' to build without linking with libspirv
1717

1818
// RUN: %clangxx -### -std=c++11 -target x86_64-unknown-linux-gnu -fsycl \
1919
// RUN: -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx908 \
2020
// RUN: -fsycl-libspirv-path=%S/Inputs/SYCL/no-libspirv-exists-here.bc %s 2>&1 \
2121
// RUN: | FileCheck --check-prefix=ERR-HIP %s
22-
// ERR-HIP: cannot find 'remangled-l64-signed_char.libspirv-amdgcn--amdhsa.bc';
22+
// ERR-HIP: cannot find 'remangled-l64-signed_char.libspirv-amdgcn-amd-amdhsa.bc';
2323
// ERR-HIP-SAME: provide path to libspirv library via '-fsycl-libspirv-path', or pass '-fno-sycl-libspirv' to build without linking with libspirv
2424

2525
// RUN: %clangxx -### -std=c++11 -target x86_64-unknown-linux-gnu -fsycl \
2626
// RUN: -fsycl-targets=nvptx64-nvidia-cuda --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
2727
// RUN: -fsycl-libspirv-path=%S/Inputs/SYCL/no-libspirv-exists-here.bc -fno-sycl-libspirv %s 2>&1 \
2828
// RUN: | FileCheck --check-prefix=OK-CUDA %s
29-
// OK-CUDA-NOT: cannot find suitable 'remangled-l64-signed_char.libspirv-nvptx64--nvidiacl.bc'
29+
// OK-CUDA-NOT: cannot find suitable 'remangled-l64-signed_char.libspirv-nvptx64-nvidia-cuda.bc'
3030

3131
// RUN: %clangxx -### -std=c++11 -target x86_64-unknown-linux-gnu -fsycl \
3232
// RUN: -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx908 \
3333
// RUN: -fsycl-libspirv-path=%S/Inputs/SYCL/no-libspirv-exists-here.bc -fno-sycl-libspirv %s 2>&1 \
3434
// RUN: | FileCheck --check-prefix=OK-HIP %s
35-
// OK-HIP-NOT: cannot find 'remangled-l64-signed_char.libspirv-amdgcn--amdhsa.bc'
35+
// OK-HIP-NOT: cannot find 'remangled-l64-signed_char.libspirv-amdgcn-amd-amdhsa.bc'

libclc/cmake/modules/AddLibclc.cmake

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,34 @@ macro(add_libclc_builtin_set arch_suffix)
110110
if( LIBCLC_GENERATE_REMANGLED_VARIANTS )
111111
set(long_widths l32 l64)
112112
set(char_signedness signed unsigned)
113+
if( ${obj_suffix} STREQUAL "libspirv-nvptx64--nvidiacl.bc")
114+
set( obj_suffix_mangled "libspirv-nvptx64-nvidia-cuda.bc")
115+
elseif( ${obj_suffix} STREQUAL "libspirv-amdgcn--amdhsa.bc")
116+
set( obj_suffix_mangled "libspirv-amdgcn-amd-amdhsa.bc")
117+
else()
118+
set( obj_suffix_mangled "${obj_suffix}")
119+
endif()
113120
# All permutations of [l32, l64] and [signed, unsigned]
114121
foreach(long_width ${long_widths})
115122
foreach(signedness ${char_signedness})
116123
# Remangle
117124
set( builtins_remangle_path
118-
"${LIBCLC_LIBRARY_OUTPUT_INTDIR}/remangled-${long_width}-${signedness}_char.${obj_suffix}" )
125+
"${LIBCLC_LIBRARY_OUTPUT_INTDIR}/remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}" )
119126
add_custom_command( OUTPUT "${builtins_remangle_path}"
120127
COMMAND libclc-remangler
121128
-o "${builtins_remangle_path}"
122129
--long-width=${long_width}
123130
--char-signedness=${signedness}
124131
"$<TARGET_PROPERTY:prepare-${obj_suffix},TARGET_FILE>"
125132
DEPENDS "${builtins_obj_path}" "prepare-${obj_suffix}" libclc-remangler )
126-
add_custom_target( "remangled-${long_width}-${signedness}_char.${obj_suffix}" ALL
133+
add_custom_target( "remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}" ALL
127134
DEPENDS "${builtins_remangle_path}" )
128-
set_target_properties("remangled-${long_width}-${signedness}_char.${obj_suffix}"
135+
set_target_properties("remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}"
129136
PROPERTIES TARGET_FILE "${builtins_remangle_path}")
130137

131138
# Add dependency to top-level pseudo target to ease making other
132139
# targets dependent on libclc.
133-
add_dependencies(${ARG_PARENT_TARGET} "remangled-${long_width}-${signedness}_char.${obj_suffix}")
140+
add_dependencies(${ARG_PARENT_TARGET} "remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}")
134141

135142
# Keep remangled variants
136143
install(

libclc/utils/libclc-remangler/LibclcRemangler.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,15 @@ int main(int argc, const char **argv) {
653653
std::unique_ptr<llvm::Module> M =
654654
ExitOnErr(parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context));
655655

656+
// This module is built explicitly for linking with any .bc compiled with the
657+
// "nvptx64-nvidia-cuda" (CUDA) or "amdgcn-amd-amdhsa" (HIP AMD) triples.
658+
// Therefore we update the module triple.
659+
if (M.get()->getTargetTriple() == "nvptx64-unknown-nvidiacl") {
660+
M.get()->setTargetTriple("nvptx64-nvidia-cuda");
661+
}
662+
else if (M.get()->getTargetTriple() == "amdgcn-unknown-amdhsa") {
663+
M.get()->setTargetTriple("amdgcn-amd-amdhsa");
664+
}
656665
std::error_code EC;
657666
std::unique_ptr<ToolOutputFile> Out(
658667
new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));

0 commit comments

Comments
 (0)