Skip to content

[libspirv] Replace libclc-remangler with SYCLRemangleLibspirvPass#21588

Merged
KornevNikita merged 18 commits intointel:syclfrom
wenju-he:move-libclc-remangler-functionality-into-new-pass
Apr 10, 2026
Merged

[libspirv] Replace libclc-remangler with SYCLRemangleLibspirvPass#21588
KornevNikita merged 18 commits intointel:syclfrom
wenju-he:move-libclc-remangler-functionality-into-new-pass

Conversation

@wenju-he
Copy link
Copy Markdown
Contributor

@wenju-he wenju-he commented Mar 23, 2026

Remove libclc-remangler tool and replace it with SYCLRemangleLibspirvPass
that remangles OpenCL C mangling in libspirv to match with SYCL mangling.
The pass ports the functionalities in the tool into compile pipeline.

Motivation:

  • libclc-remangler executable doesn't fit in runtime build.
  • libclc-remangler can't justify as a standalone tool in llvm/tools.

Functional changes:

  • Add SYCLRemangleLibspirvPass. It uses Itanium demangler and adapts SPIR type system and SPIR mangler from SPIRV-LLVM-Translator.
  • The pass runs at the end of compile pipeline for each .cl file.
    libspirv variants are created without separate remangling step.
  • Drop libclc-remangler tool.

llvm-diff changes to remangled libspirv:

  • _clc functions from CLC library are now not remangled because we use
    a single clc library that defaults to OpenCL C mangling.
  • Some _clc functions from CLC library are no longer inlined for NVPTX.
    Previously, these were called only once and inlined before remangling.
    Now, because remangling can generate two caller variants, the _clc
    function is called twice, causing the inliner to skip them.
  • 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.
  • Fixed 4 __spirv_ImageArrayWrite builtins in nvptx l64.signed_char, e.g.
    _Z23__spirv_ImageArrayWriteIyiiEvT_T0_T1_i -> Z23__spirv_ImageArrayWriteIyiiEvT_T0_iT1;
    _Z23__spirv_ImageArrayWriteImiiEvT_T0_T1_i -> Z23__spirv_ImageArrayWriteImiiEvT_T0_iT1.

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>
@wenju-he wenju-he requested review from a team, Maetveis and cperkinsintel as code owners March 23, 2026 08:48
@wenju-he wenju-he requested a review from bratpiorka March 23, 2026 08:49
Copy link
Copy Markdown
Contributor

@Maetveis Maetveis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libclc changes LGTM. I didn't check the new pass or any of the other changes.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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++.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

This is interesting idea, but I don't know about how to implement the automatic mapping.

We would need something similar if we wanted to author libclc sources in C++.

Implement libspirv in C++ and compile in sycl device mode like libdevice does can indeed avoid the remangling issue. However, one problem is that it will be very difficult to share implementation between libclc OpenCL and libspirv builtin wrappers, since eventually the sharing requires a stable interface which SPV-IR intends to be.

Comment thread clang/test/Driver/sycl-libspirv-toolchain.cpp Outdated
Comment thread llvm/include/llvm/SYCLLowerIR/SYCLBuiltinRemangle.h Outdated
Comment thread llvm/include/llvm/SYCLLowerIR/SYCLBuiltinRemangle.h Outdated
Comment thread llvm/include/llvm/SYCLLowerIR/SYCLBuiltinRemangle.h Outdated
Comment thread llvm/lib/SYCLLowerIR/SYCLBuiltinRemangle.cpp Outdated
Comment thread llvm/lib/SYCLLowerIR/SYCLBuiltinRemangle.cpp Outdated
Comment thread llvm/lib/SYCLLowerIR/SYCLBuiltinRemangle.cpp Outdated
Comment thread llvm/test/SYCLLowerIR/SYCLBuiltinRemangle/char-signedness.ll Outdated
Comment thread llvm/test/SYCLLowerIR/SYCLBuiltinRemangle/char-signedness.ll Outdated
Comment thread llvm/test/SYCLLowerIR/SYCLBuiltinRemangle/char-types.ll Outdated
Comment thread llvm/test/SYCLLowerIR/SYCLBuiltinRemangle/float16-types.ll Outdated
Copy link
Copy Markdown
Contributor

@elizabethandrews elizabethandrews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FE changes just add a pass. FE changes LGTM. I have not reviewed the functionality of the pass.

…RemangleLibspirvPass

Not inlined __clc_ functions are not remangled because we only have a single clc library that has OpenCL C mangling.
@wenju-he wenju-he changed the title Replace libclc-remangler with SYCLBuiltinRemangle pass [libspirv] Replace libclc-remangler with SYCLRemangleLibspirvPass Mar 30, 2026
@wenju-he wenju-he requested a review from YuriPlyakhin March 31, 2026 05:19
Comment thread llvm/lib/SYCLLowerIR/SYCLRemangleLibspirv.cpp Outdated
Copy link
Copy Markdown
Contributor

@YuriPlyakhin YuriPlyakhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 comments left to address for files owned by me.

@wenju-he wenju-he requested a review from YuriPlyakhin April 1, 2026 03:06
Copy link
Copy Markdown
Contributor

@YuriPlyakhin YuriPlyakhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one comment needs to be addressed. #21588 (comment)

Comment thread llvm/lib/SYCLLowerIR/MangleUtils.cpp Outdated
wenju-he and others added 2 commits April 1, 2026 17:19
Co-authored-by: Yury Plyakhin <yury.plyakhin@intel.com>
@wenju-he wenju-he requested a review from YuriPlyakhin April 2, 2026 01:41
Copy link
Copy Markdown
Contributor

@YuriPlyakhin YuriPlyakhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dpcpp-tools owned files LGTM

@wenju-he
Copy link
Copy Markdown
Contributor Author

wenju-he commented Apr 3, 2026

@intel/dpcpp-clang-driver-reviewers @intel/dpcpp-nativecpu-reviewers please review, thanks

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes the standalone libclc-remangler tool and replaces its functionality with a new LLVM pass (SYCLRemangleLibspirvPass) that remangles libspirv builtins within the compile pipeline, while updating build logic and tests to consume the new libspirv variant naming.

Changes:

  • Introduces SYCLRemangleLibspirvPass plus new SPIR mangling utilities to implement remangling inside LLVM.
  • Updates libclc build to generate libspirv.l{32,64}.{signed,unsigned}_char.bc variants via the new pass and removes the libclc-remangler tool and related CMake/test wiring.
  • Updates SYCL/Clang driver logic, docs, and tests to reference the new libspirv variant filenames and ensure the pass can be enabled in the pipeline.

Reviewed changes

Copilot reviewed 42 out of 46 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
sycl/test/check_device_code/native_cpu/sycl-native-cpu-libclc-windows.cpp Updates FileCheck expectations to the new libspirv variant filename for Windows.
sycl/test/check_device_code/native_cpu/sycl-native-cpu-libclc-linux.cpp Updates FileCheck expectations to the new libspirv variant filename for Linux.
sycl/doc/design/CompilerAndRuntimeDesign.md Renames documented libspirv remangled artifacts to the new naming scheme.
sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp Switches JIT-side libspirv variant selection to new filenames.
llvm/test/SYCLLowerIR/SYCLRemangleLibspirv/template-types.ll Adds coverage for template-arg remangling behavior.
llvm/test/SYCLLowerIR/SYCLRemangleLibspirv/qualifiers-and-structs.ll Adds coverage for qualifier preservation and struct-like names.
llvm/test/SYCLLowerIR/SYCLRemangleLibspirv/pointer-addrspace.ll Adds coverage for pointer address space remangling behavior.
llvm/test/SYCLLowerIR/SYCLRemangleLibspirv/long-types.ll Adds coverage for long/ulong remangling across width modes.
llvm/test/SYCLLowerIR/SYCLRemangleLibspirv/float16-types.ll Adds coverage for half/_Float16 remangling.
llvm/test/SYCLLowerIR/SYCLRemangleLibspirv/filter-non-spirv.ll Adds coverage ensuring non-__spirv_ functions are not remangled.
llvm/test/SYCLLowerIR/SYCLRemangleLibspirv/char-unsigned.ll Adds coverage for char remangling when target char is unsigned.
llvm/test/SYCLLowerIR/SYCLRemangleLibspirv/char-signed.ll Adds coverage for char remangling when target char is signed.
llvm/test/SYCLLowerIR/SYCLRemangleLibspirv/bidirectional-collision.ll Adds coverage for rename/clone collision handling (.tmp resolution).
llvm/lib/SYCLLowerIR/SYCLRemangleLibspirv.cpp Implements the new LLVM module pass with remangling + cloning logic.
llvm/lib/SYCLLowerIR/MangleUtils.cpp Adds SPIR mangling utilities used by the pass.
llvm/lib/SYCLLowerIR/CMakeLists.txt Registers new sources in the SYCLLowerIR LLVM component.
llvm/lib/Passes/PassRegistry.def Registers the new pass name for the new pass manager.
llvm/lib/Passes/PassBuilder.cpp Adds include hook for building pipelines that reference the pass.
llvm/include/llvm/SYCLLowerIR/SYCLRemangleLibspirv.h Adds the public pass header.
llvm/include/llvm/SYCLLowerIR/MangleUtils.h Adds SPIR type system declarations used by the mangler.
libclc/utils/libclc-remangler/LibclcRemangler.cpp Deletes the standalone remangler tool implementation.
libclc/utils/libclc-remangler/CMakeLists.txt Removes build definition for the deleted remangler tool.
libclc/utils/CMakeLists.txt Removes subdirectory that built the remangler tool.
libclc/test/CMakeLists.txt Removes remangler-test target plumbing.
libclc/libspirv/lib/ptx-nvidiacl/images/image.cl Adjusts builtin macro arguments to avoid prior remangler bug exposure.
libclc/cmake/modules/AddLibclc.cmake Removes remangle option/logic and moves per-arch objdir creation into function.
libclc/CMakeLists.txt Generates libspirv variants via new pass; removes remangler tool integration and option.
clang/test/Driver/sycl-oneapi-gpu-nvidia.cpp Updates driver tests to use new libspirv variant path.
clang/test/Driver/sycl-offload-static-lib-2-old-model.cpp Updates driver tests to use new libspirv variant path.
clang/test/Driver/sycl-offload-old-model.cpp Updates driver tests to use new libspirv variant path.
clang/test/Driver/sycl-offload-nvptx.cpp Updates driver tests to rely on resource-dir and expect new libspirv variant selection.
clang/test/Driver/sycl-nvptx-link.cpp Updates link-stage checks to match new libspirv variant filename.
clang/test/Driver/sycl-libspirv-toolchain.cpp Updates toolchain discovery tests + error messages for new libspirv filenames.
clang/test/Driver/sycl-libspirv-invalid.cpp Updates invalid-path error expectations to new libspirv filenames.
clang/test/Driver/sycl-device-obj-asm.cpp Updates explicit libspirv input path used in warning tests.
clang/test/Driver/sycl-cuda-rdc.cpp Updates CUDA RDC driver test to use new libspirv variant filename.
clang/test/CodeGen/sycl-remangle-libspirv-pipeline.cl Adds a pipeline-print test ensuring the clang flag enables the pass.
clang/lib/Driver/ToolChains/SYCL.cpp Updates default libspirv basename selection to new naming scheme.
clang/lib/CodeGen/BackendUtil.cpp Wires CodeGen option to add the pass at optimizer-last EP callback.
clang/include/clang/Options/Options.td Adds -fsycl-remangle-libspirv CC1 option to enable the pass.
clang/include/clang/Basic/CodeGenOptions.def Adds SYCLRemangleLibspirv codegen option flag.
buildbot/configure.py Removes obsolete LIBCLC_GENERATE_REMANGLED_VARIANTS buildbot plumbing.
Comments suppressed due to low confidence (2)

llvm/test/SYCLLowerIR/SYCLRemangleLibspirv/char-signed.ll:1

  • This RUN line doesn't actually run the new pass, so the test won't validate remangling behavior (and may fail due to unused/unknown options depending on how opt is built). Update the invocation to include the pass pipeline (e.g., -passes=sycl-remangle-libspirv) consistent with the other tests in this directory.
    llvm/lib/SYCLLowerIR/SYCLRemangleLibspirv.cpp:1
  • The remangled-name validity check is enforced only via assert, meaning release builds won't guard against producing malformed names if an unexpected demangler/mangler edge case occurs. Consider turning this into a runtime check (e.g., return false when isValidRemangledBuiltinName fails) so the pass remains safe in non-assert builds.

Comment thread llvm/include/llvm/SYCLLowerIR/MangleUtils.h
Comment thread clang/test/Driver/sycl-device-obj-asm.cpp Outdated
Comment thread clang/test/Driver/sycl-device-obj-asm.cpp Outdated
Comment thread clang/lib/Driver/ToolChains/SYCL.cpp
@wenju-he
Copy link
Copy Markdown
Contributor Author

wenju-he commented Apr 7, 2026

@bratpiorka @intel/dpcpp-nativecpu-reviewers please review, thanks

@wenju-he
Copy link
Copy Markdown
Contributor Author

@intel/llvm-gatekeepers please merge, thanks

@KornevNikita KornevNikita merged commit 563401d into intel:sycl Apr 10, 2026
34 checks passed
@wenju-he wenju-he deleted the move-libclc-remangler-functionality-into-new-pass branch April 10, 2026 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.