Replace libclc-remangler with SYCLBuiltinRemangle pass#21588
Replace libclc-remangler with SYCLBuiltinRemangle pass#21588wenju-he wants to merge 2 commits intointel:syclfrom
Conversation
Remove libclc-remangler tool and replace it with SYCLBuiltinRemanglePass that remangles SPIR-V built-ins in SYCL user device code to match OpenCL C mangling in libspirv. The pass is inverse of what libclc-remangler did. Motivation: - libclc-remangler executable doesn't fit in runtime build. - libclc-remangler can't justify as a standalone tool in llvm/tools. Changes: - Add SYCLBuiltinRemanglePass. It uses Itanium demangler and adapts SPIR type system and SPIR mangler from SPIRV-LLVM-Translator. - Type transformations: - long long -> long (OpenCL has no long long) - long -> int (Windows or 32-bit only) - signed char -> char - char -> unsigned char (only if char is signed on the host) - _Float16 -> half (with a few w/a for native-cpu libdevice) - Address space adjustments if target's default addrspace is private. - libspirv is now linked into user code after the optimization pipeline by -mlink-builtin-bitcode-postopt flag since the linking must be after remangling. In the future, the linking will be moved to LTO. - Changes in _CLC_DEFINE_MIPMAP_BINDLESS_READS_BUILTIN also exposed a bug in libclc-remangler which would remangle _Z30__spirv_ImageSampleExplicitLodImDv4_cDv3_fET0_T_T1_iS4_S4_ (correct) to _Z30__spirv_ImageSampleExplicitLodImDv4_aDv3_fET0_T_T1_iS1_S1_ This bug was hidden since correct symbol was hardcoded in the source. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
| When the SYCL compiler is in device mode and targeting the NVPTX backend, the | ||
| compiler exposes NVPTX builtins supported by clang. | ||
| incompatible libclc built-ins. When building a SYCL application targeting the | ||
| CUDA backend, the SYCLBuiltinRemangle pass remangles SPIR-V builtins in device |
There was a problem hiding this comment.
I'm wondering could we make clang emit calls to these functions using the correct name mangling from the start instead of re-mangling as a pass?
I was thinking a function attribute that allows to switch the name mangling to be OpenCL compatible could theoretically work for this. We would need something similar if we wanted to author libclc sources in C++.
| // RUN: %clang -### -resource-dir %{resource_dir} -fsycl -fsycl-targets=nvptx64-nvidia-cuda -nocudalib -target x86_64-unknown-windows-gnu %s 2>&1 \ | ||
| // RUN: | FileCheck %s --check-prefixes=CHECK-WINDOWS | ||
| // CHECK-WINDOWS: "-cc1"{{.*}} "-fsycl-is-device"{{.*}} "-mlink-builtin-bitcode" "{{.*[\\/]}}remangled-l32-signed_char.libspirv.bc" | ||
| // CHECK-WINDOWS: "-cc1"{{.*}} "-fsycl-is-device"{{.*}} "-mlink-builtin-bitcode" "{{.*[\\/]}}libspirv.bc" "-mlink-builtin-bitcode-postopt" |
There was a problem hiding this comment.
Ordering of -mlink-builtin-bitcode and -mlink-builtin-bitcode-postopt does not match implementation (-mlink-builtin-bitcode-postopt is before -mlink-builtin-bitcode)
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // Remangle SPIR-V built-ins in user SYCL device code match OpenCL C mangling |
There was a problem hiding this comment.
| // Remangle SPIR-V built-ins in user SYCL device code match OpenCL C mangling | |
| // Remangle SPIR-V built-ins in user SYCL device code to match OpenCL C mangling |
| // - _Float16 -> half | ||
| // - Pointer address space adjustments if target's default addrspace is private. | ||
|
|
||
| #ifndef LLVM_SYCL_BUILTIN_REMANGLE_H |
There was a problem hiding this comment.
https://llvm.org/docs/CodingStandards.html#header-guard
| #ifndef LLVM_SYCL_BUILTIN_REMANGLE_H | |
| #ifndef LLVM_SYCLLOWERIR_SYCLBUILTINREMANGLE_H |
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // Remangle SPIR-V built-ins in user SYCL device code match OpenCL C mangling | ||
| // used in libspirv. This enables linking with unmodified libspirv. |
There was a problem hiding this comment.
For my understanding, why do we prefer introducing pass vs. updating libspirv?
There was a problem hiding this comment.
For my understanding, why do we prefer introducing pass vs. updating libspirv?
We can't build libclc-remangler in libclc which is a runtime. The other option is to move libclc-remangler out of libclc, but I don't think there is an appropriate place for it.
libspirv as a runtime library will be built into a static library using cmake add_library. I doubt it a good idea to modify it using opt tool to run a remangler pass and create multiple variants of libspirv. That will complicate the runtime build process and can't create a LTO-friendly library.
| // builtins. This is the INVERSE operation of libclc-remangler: instead of | ||
| // transforming library code to match SYCL types, we transform user code to | ||
| // match OpenCL types. |
There was a problem hiding this comment.
if libclc-remangler is removed from codebase, can we avoid referencing it in comments?
| ATTR_NUM = ATTR_NONE | ||
| }; | ||
|
|
||
| // Reference counting template |
There was a problem hiding this comment.
please add . at the end of comments.
| // Reference counting template | |
| // Reference counting template. |
| namespace { | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // SPIR Type System (adapted from SPIRV-LLVM-Translator Mangler) |
There was a problem hiding this comment.
Questions:
-
Since SPIR-V translator is part of this codebase, is it possible to reuse existing code (llvm-spirv/lib/SPIRV/Mangler/Mangler.cpp, etc...) instead of copying?
-
If copying is necessary for some reason:
- should we provide proper attribution per license requirements? something like this maybe?
// Portions of this code are derived from SPIRV-LLVM Translator
// (https://github.com/KhronosGroup/SPIRV-LLVM-Translator)
// Copyright (c) ???? The Khronos Group Inc.
// Licensed under the University of Illinois Open Source License
- is it possible to keep logical split of functionality across several files similar to what we have in translator mangler, instead of having 1.3K LOC file?
| @@ -0,0 +1,124 @@ | |||
| ; Test for -sycl-remangle-char-is-signed option | |||
| ; | |||
| ; This tests the CharIsSigned behavior (PRIMITIVE_CHAR vs PRIMITIVE_UCHAR) | |||
There was a problem hiding this comment.
| ; This tests the CharIsSigned behavior (PRIMITIVE_CHAR vs PRIMITIVE_UCHAR) | |
| ; Test the CharIsSigned behavior (PRIMITIVE_CHAR vs PRIMITIVE_UCHAR) |
| ; - When true (default): SYCL's char is signed -> OpenCL's char ('c') | ||
| ; - When false: SYCL's char is unsigned -> OpenCL's unsigned char ('h') | ||
| ; | ||
| ; Test with default (CharIsSigned=true): char stays as char |
There was a problem hiding this comment.
| ; Test with default (CharIsSigned=true): char stays as char | |
| ; Test with default (CharIsSigned=true): char stays as char. |
| @@ -0,0 +1,58 @@ | |||
| ; RUN: opt -passes=sycl-builtin-remangle -sycl-remangle-spirv-target -mtriple=spir64-unknown-unknown -S < %s | FileCheck %s | |||
|
|
|||
| ; Test signed char type remangling | |||
There was a problem hiding this comment.
please, update everywhere.
| ; Test signed char type remangling | |
| ; Test signed char type remangling. |
| @@ -0,0 +1,42 @@ | |||
| ; RUN: opt -passes=sycl-builtin-remangle -mtriple=nvptx64-nvidia-cuda -S < %s | FileCheck %s | |||
|
|
|||
| ; Test _Float16/half type remangling with real SPIRV builtins from libspirv.ll | |||
There was a problem hiding this comment.
please, update in text/comments.
| ; Test _Float16/half type remangling with real SPIRV builtins from libspirv.ll | |
| ; Test _Float16/half type remangling with real SPIR-V builtins from libspirv.ll |
Remove libclc-remangler tool and replace it with SYCLBuiltinRemanglePass that remangles SPIR-V built-ins in SYCL user device code to match OpenCL C mangling in libspirv. The pass is inverse of what libclc-remangler did.
Motivation:
Changes: