Skip to content

Conversation

Maetveis
Copy link
Contributor

There is no need to remangle functions with local (internal/private) linkage, they won't be called from outside libclc.
Skipping them avoids issues with compiler added suffixes like ".0". We could fix remangling to handle those suffixes, but skipping them is simpler, and we don't expect suffixes on externally visible functions anyway.

Skipping over internal functions could also improve performance when remangling large modules with many internal functions.

This fixes an issue we see when building libclc for a downstream target, where we build libspirv from smaller bitcode files, the error there was:

[11/22] Generating ../../lib/clc/remangled-l32-signed_char.libspirv-<target-triple>.bc
FAILED: lib/clc/remangled-l32-signed_char.libspirv-<target-triple>.bc llvm/lib/clc/remangled-l32-signed_char.libspirv-<target-triple>.bc
cd llvm/tools/libclc && cmake -E make_directory llvm/./lib/clc && llvm/bin/libclc-remangler -o llvm/./lib/clc/remangled-l32-signed_char.libspirv-<target-triple>.bc --triple=<target-triple> --long-width=l32 --char-signedness=signed --input-ir=llvm/./lib/clc/libspirv-<target-triple>.bc llvm/./lib/clc/libclc_dummy_in.cc
Unhandled type: __clc_copysign(float, float)
Unhandled type.
UNREACHABLE executed at llvm/libclc/utils/libclc-remangler/LibclcRemangler.cpp:590!

due to a function named @_Z14__clc_copysignff.9 with internal linkage. The function was likely created by llvm-link when merging multiple copies of copysign into the resulting module.

There is no need to remangle functions with local (internal/private) linkage,
they won't be called from outside libclc.
Skipping them avoids issues with compiler added suffixes like ".0".
We could fix remangling to handle those suffixes, but skipping them is
simpler, and we don't expect suffixes on externally visible functions
anyway.

Skipping over internal functions could also improve performance
when remangling large modules with many internal functions.

This fixes an issue we see when building libclc for a downstream
target, the error was:
```plaintext
[11/22] Generating ../../lib/clc/remangled-l32-signed_char.libspirv-<target-triple>.bc
FAILED: lib/clc/remangled-l32-signed_char.libspirv-<target-triple>.bc llvm/lib/clc/remangled-l32-signed_char.libspirv-<target-triple>.bc
cd llvm/tools/libclc && cmake -E make_directory llvm/./lib/clc && llvm/bin/libclc-remangler -o llvm/./lib/clc/remangled-l32-signed_char.libspirv-<target-triple>.bc --triple=<target-triple> --long-width=l32 --char-signedness=signed --input-ir=llvm/./lib/clc/libspirv-<target-triple>.bc llvm/./lib/clc/libclc_dummy_in.cc
Unhandled type: __clc_copysign(float, float)
Unhandled type.
UNREACHABLE executed at llvm/libclc/utils/libclc-remangler/LibclcRemangler.cpp:590!
```
due to a function named `@_Z14__clc_copysignff.9` with internal linkage.
}

bool remangleFunction(Function &Func, llvm::Module *M) {
if (Func.hasLocalLinkage())
Copy link
Contributor

@wenju-he wenju-he Sep 22, 2025

Choose a reason for hiding this comment

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

IIUC remangling is handling function parameter type and address space, but is unrelated to function linkage type.
I think the remangling process could be refined to skip functions like __clc_copysign whose parameter types don't need remangling.

Copy link
Contributor Author

@Maetveis Maetveis Sep 22, 2025

Choose a reason for hiding this comment

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

IIUC remangling is handling function parameter type and address space, but is unrelated to function linkage type.

Isn't the purpose of remangling that code compiled in SYCL (C++) language mode be able to call into libspirv functions which are compiled as OpenCL C, as there are differences between OpenCL and C++ with regards to e.g. long long size and char signedness?

If there's a function in libspirv that is internal linkage user code (the code we're linking libspirv into at user code compile time) shouldn't be calling it by definition. If it isn't being called by user code there can be no issues with C++ code looking for a different mangled name than what libspirv would provide.

I think the remangling process could be refined to skip functions like __clc_copysign whose parameter types don't need remangling.

The same problem could occur with functions that have types that need renaming.

Copy link
Contributor

Choose a reason for hiding this comment

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

If there's a function in libspirv that is internal linkage user code (the code we're linking libspirv into at user code compile time) shouldn't be calling it by definition. If it isn't being called by user code there can be no issues with C++ code looking for a different mangled name than what libspirv would provide.

Right. It seems you're correct.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe @Maetveis is right. The point of remangling is to give C++ code the opportunity to link against the OpenCL-C-based libclc, without there being signedness/size issues. Since functions with internal linkage shouldn't interface with C++ it should be fine to skip them during remangling.

Copy link
Contributor

@wenju-he wenju-he left a comment

Choose a reason for hiding this comment

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

LGTM. @steffenlarsen please double confirm, thanks.

Copy link
Contributor

@steffenlarsen steffenlarsen left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@intel/llvm-gatekeepers please consider merging

@steffenlarsen steffenlarsen merged commit 9b938b1 into intel:sycl Sep 22, 2025
30 of 31 checks passed
@Maetveis Maetveis deleted the libclc_remangler_skip_internal_functions branch September 22, 2025 16:42
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.

3 participants