[OpenCL] RFC: let Clang generate llvm atomic instructions for OpenCL atomic builtin functions


OpenCL atomic builtin functions are similar to llvm atomic instructions. It seems natural to represent OpenCL atomic builtin functions by LLVM atomic instructions.

Doing this can simplify implementation of OpenCL builtin library and have a unified representation for OpenCL atomic builtin functions.

Any feedback? Thanks.


Hi Sam,

Could you clarify your idea?

At the moment clang doesn’t provide any OpenCL built-in functions – they are provided by OpenCL implementation.

Clang provides C11 atomic built-ins which should produce desired llvm atomic instructions.

This should work for OpenCL v2.0 atomics since they are taken from C11.

Hi Alexey,

To clarify, when I said OpenCL atomic builtin functions, the ‘builtin’ was in the sense of OpenCL spec. In Clang they are just treated as ordinary functions.

Let’s take an example of OpenCL atomic builtin function:

uint y;

volatile __global atomic_uint *x;

atomic_store(x, y);

Currently we clang generates a function call

call spir_func void @_Z12atomic_storePVU3AS1U7_Atomicjj(i32 addrspace(1)* %x, i32 %y)

However, it can simply be represented as an LLVM instruction

store atomic volatile i32 %y, i32 addrspace(1)* %x seq_cst, align 4

So I am wondering why not let Clang just generating the instruction directly, since Clang already did the same thing for some libm functions.


Hi Sam,

Could you implement CL2.0 atomics by calling the C11 ones, i.e. doing something like:

C atomic_exchange( volatile A* obj, C desired ){

return __c11_atomic_exchange(obj, desired);


There is still one issue we have though with an absence of memory_scope in Clang/LLVM.

So for the following declaration, we don’t have anything available in Clang to represent the last parameter:

C atomic_exchange_explicit(volatile A *object, C desired, memory_order order, memory_scope scope);

We also don’t have anything in IR to map it to.

One approach here could be to just ignore it and perhaps call a regular C11 atomic passing all other parameters except memory scope.

Alternatively we could extend the Clang atomic builtins with CL2.0 style builtins for those that require memory scope.

Btw @Alexey, we did add some CL builtins recently i.e for Pipes (see clang@r258782) and will probably need more – enqueue_kernel.