Skip to content

[libclc] Fix memory fence scope mapping for OpenCL - #170542

Merged
wenju-he merged 1 commit into
llvm:mainfrom
vmustya:libclc-memfence-fix
Dec 4, 2025
Merged

wenju-he merged 1 commit into
llvm:mainfrom
vmustya:libclc-memfence-fix

Conversation

@vmustya

@vmustya vmustya commented Dec 3, 2025

Copy link
Copy Markdown
Contributor

The function __opencl_get_memory_scope incorrectly assumed that the
Clang built-in __MEMORY_SCOPE_* macros defined as bitmasks, while they
are actually defined as distinct integer values. This led to incorrect
mapping of OpenCL memory fence flags to LLVM memory scopes, causing
issues in generated code.

The fix involves updating the __opencl_get_memory_scope function to
return the correct __MEMORY_SCOPE_* values based on the provided
cl_mem_fence_flags. Additionally, the __opencl_get_memory_semantics
and the __opencl_get_memory_scope functions are marked as static
to avoid potential multiple definition issues during linking.

The function `__opencl_get_memory_scope` incorrectly assumed that the
Clang built-in `__MEMORY_SCOPE_*` macros defined as bitmasks, while they
are actually defined as distinct integer values. This led to incorrect
mapping of OpenCL memory fence flags to LLVM memory scopes, causing
issues in generated code.

The fix involves updating the `__opencl_get_memory_scope` function to
return the correct `__MEMORY_SCOPE_*` values based on the provided
`cl_mem_fence_flags`. Additionally, the `__opencl_get_memory_semantics`
and the `__opencl_get_memory_scope` functions are marked as `static`
to avoid potential multiple definition issues during linking.
@llvmbot llvmbot added the libclc libclc OpenCL library label Dec 3, 2025
memory_scope |= __MEMORY_SCOPE_WRKGRP;
return memory_scope;
return __MEMORY_SCOPE_WRKGRP;
return __MEMORY_SCOPE_SINGLE;

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.

not sure if the default value is appropriate. Should we call __builtin_unreachable(); like in __opencl_get_memory_semantics?

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.

According to the OpenCL C spec, the synchronization functions allow the 0 to be passed as a valid argument value. When 0 is passed, the single-thread fence looks reasonable to me, because the implementation shouldn't issue any cross-thread memory synchronization in that case. The __builtin_unreachable() won't work correctly.

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.

thanks @vmustya

@vmustya
vmustya requested a review from wenju-he December 4, 2025 01:09

@wenju-he wenju-he left a comment

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.

LGTM

@wenju-he
wenju-he merged commit b8a5888 into llvm:main Dec 4, 2025
honeygoyal pushed a commit to honeygoyal/llvm-project that referenced this pull request Dec 9, 2025
The function `__opencl_get_memory_scope` incorrectly assumed that the
Clang built-in `__MEMORY_SCOPE_*` macros defined as bitmasks, while they
are actually defined as distinct integer values. This led to incorrect
mapping of OpenCL memory fence flags to LLVM memory scopes, causing
issues in generated code.

The fix involves updating the `__opencl_get_memory_scope` function to
return the correct `__MEMORY_SCOPE_*` values based on the provided
`cl_mem_fence_flags`. Additionally, the `__opencl_get_memory_semantics`
and the `__opencl_get_memory_scope` functions are marked as `static`
to avoid potential multiple definition issues during linking.
karolherbst added a commit to karolherbst/llvm-project that referenced this pull request Jul 2, 2026
The compiled SPIR-V libclc binaries ended up with an empty definition for
__clc_flush_denormal_if_not_supported.

b8a5888 ("[libclc] Fix memory fence scope mapping for OpenCL (llvm#170542)")
also added the static keyword probably due to the same issue.

Fixes: 7f36611 ("[libclc] Remove __attribute__((always_inline)) (llvm#158791)")
karolherbst added a commit to karolherbst/llvm-project that referenced this pull request Jul 2, 2026
The compiled SPIR-V libclc binaries ended up with an empty definition for
__clc_flush_denormal_if_not_supported.

b8a5888 ("[libclc] Fix memory fence scope mapping for OpenCL (llvm#170542)")
also added the static keyword probably due to the same issue.

Fixes: 7f36611 ("[libclc] Remove __attribute__((always_inline)) (llvm#158791)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libclc libclc OpenCL library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants