Skip to content

Replace libclc-remangler with SYCLBuiltinRemangle pass#21588

Open
wenju-he wants to merge 2 commits intointel:syclfrom
wenju-he:move-libclc-remangler-functionality-into-new-pass
Open

Replace libclc-remangler with SYCLBuiltinRemangle pass#21588
wenju-he wants to merge 2 commits intointel:syclfrom
wenju-he:move-libclc-remangler-functionality-into-new-pass

Conversation

@wenju-he
Copy link
Contributor

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.

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

// 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"
Copy link
Contributor

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

Suggested change
// 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
Copy link
Contributor

Choose a reason for hiding this comment

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

https://llvm.org/docs/CodingStandards.html#header-guard

Suggested change
#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.
Copy link
Contributor

Choose a reason for hiding this comment

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

For my understanding, why do we prefer introducing pass vs. updating libspirv?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment on lines +11 to +13
// 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

if libclc-remangler is removed from codebase, can we avoid referencing it in comments?

ATTR_NUM = ATTR_NONE
};

// Reference counting template
Copy link
Contributor

Choose a reason for hiding this comment

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

please add . at the end of comments.

Suggested change
// Reference counting template
// Reference counting template.

namespace {

//===----------------------------------------------------------------------===//
// SPIR Type System (adapted from SPIRV-LLVM-Translator Mangler)
Copy link
Contributor

Choose a reason for hiding this comment

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

Questions:

  1. 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?

  2. 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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
; 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
; 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
Copy link
Contributor

Choose a reason for hiding this comment

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

please, update everywhere.

Suggested change
; 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
Copy link
Contributor

Choose a reason for hiding this comment

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

please, update in text/comments.

Suggested change
; 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

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.

4 participants