Skip to content

Commit 79620d8

Browse files
authored
[libclc] Add OpenCL atomic_*_explicit builtins (#168318)
Implement atomic_*_explicit (e.g. atomic_store_explicit) with memory_order plus optional memory_scope. OpenCL memory_order maps 1:1 to Clang (e.g. OpenCL memory_order_relaxed == Clang __ATOMIC_RELAXED), so we pass it unchanged to clc_atomic_* function which forwards to Clang _scoped_atomic* builtins. Other changes: * Add __opencl_get_clang_memory_scope helper in opencl/utils.h (OpenCL scope -> Clang scope). * Correct atomic_compare_exchange return type to bool. * Fix atomic_compare_exchange to return true when value stored in the pointer equals expected value. * Remove volatile from CLC functions so that volatile isn't present in LLVM IR. * Add '-fdeclare-opencl-builtins -finclude-default-header' flag to include declaration of memory_scope. Some constants in libclc are already provided by Clang’s OpenCL header; disable those in OpenCL library build and enable them only for CLC library build.
1 parent 7f01d72 commit 79620d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+364
-337
lines changed

libclc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
477477
)
478478

479479
list( APPEND build_flags
480+
-Xclang -fdeclare-opencl-builtins -Xclang -finclude-default-header
480481
-I${CMAKE_CURRENT_SOURCE_DIR}/opencl/include
481482
)
482483

libclc/clc/include/clc/atomic/atomic_decl.inc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@
1515
#ifdef __CLC_NO_VALUE_ARG
1616
#define __CLC_DECLARE_ATOMIC(ADDRSPACE) \
1717
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION( \
18-
volatile ADDRSPACE __CLC_GENTYPE *Ptr, int MemoryOrder, \
19-
int MemoryScope);
18+
ADDRSPACE __CLC_GENTYPE *Ptr, int MemoryOrder, int MemoryScope);
2019
#elif defined(__CLC_RETURN_VOID)
2120
#define __CLC_DECLARE_ATOMIC(ADDRSPACE) \
2221
_CLC_OVERLOAD _CLC_DECL void __CLC_FUNCTION( \
23-
volatile ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Value, \
24-
int MemoryOrder, int MemoryScope);
22+
ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Value, int MemoryOrder, \
23+
int MemoryScope);
2524
#elif defined(__CLC_COMPARE_EXCHANGE)
2625
#define __CLC_DECLARE_ATOMIC(ADDRSPACE) \
2726
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION( \
28-
volatile ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Comparator, \
27+
ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Comparator, \
2928
__CLC_GENTYPE Value, int MemoryOrderEqual, int MemoryOrderUnequal, \
3029
int MemoryScope);
3130
#else
3231
#define __CLC_DECLARE_ATOMIC(ADDRSPACE) \
3332
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION( \
34-
volatile ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Value, \
35-
int MemoryOrder, int MemoryScope);
33+
ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Value, int MemoryOrder, \
34+
int MemoryScope);
3635
#endif
3736

3837
__CLC_DECLARE_ATOMIC(global)

libclc/clc/include/clc/float/definitions.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#ifndef MAXFLOAT
910
#define MAXFLOAT 0x1.fffffep127f
11+
#endif
12+
#ifndef HUGE_VALF
1013
#define HUGE_VALF __builtin_huge_valf()
14+
#endif
15+
#ifndef INFINITY
1116
#define INFINITY __builtin_inff()
17+
#endif
1218

1319
#define FLT_DIG 6
1420
#define FLT_MANT_DIG 24
@@ -17,33 +23,67 @@
1723
#define FLT_MIN_10_EXP -37
1824
#define FLT_MIN_EXP -125
1925
#define FLT_RADIX 2
26+
#ifndef FLT_MAX
2027
#define FLT_MAX MAXFLOAT
28+
#endif
2129
#define FLT_MIN 0x1.0p-126f
2230
#define FLT_EPSILON 0x1.0p-23f
2331
#define FLT_NAN __builtin_nanf("")
2432

33+
#ifndef FP_ILOGB0
2534
#define FP_ILOGB0 (-2147483647 - 1)
35+
#endif
36+
#ifndef FP_ILOGBNAN
2637
#define FP_ILOGBNAN 2147483647
38+
#endif
2739

40+
#ifndef M_E_F
2841
#define M_E_F 0x1.5bf0a8p+1f
42+
#endif
43+
#ifndef M_LOG2E_F
2944
#define M_LOG2E_F 0x1.715476p+0f
45+
#endif
46+
#ifndef M_LOG10E_F
3047
#define M_LOG10E_F 0x1.bcb7b2p-2f
48+
#endif
49+
#ifndef M_LN2_F
3150
#define M_LN2_F 0x1.62e430p-1f
51+
#endif
52+
#ifndef M_LN10_F
3253
#define M_LN10_F 0x1.26bb1cp+1f
54+
#endif
55+
#ifndef M_PI_F
3356
#define M_PI_F 0x1.921fb6p+1f
57+
#endif
58+
#ifndef M_PI_2_F
3459
#define M_PI_2_F 0x1.921fb6p+0f
60+
#endif
61+
#ifndef M_PI_4_F
3562
#define M_PI_4_F 0x1.921fb6p-1f
63+
#endif
64+
#ifndef M_1_PI_F
3665
#define M_1_PI_F 0x1.45f306p-2f
66+
#endif
67+
#ifndef M_2_PI_F
3768
#define M_2_PI_F 0x1.45f306p-1f
69+
#endif
70+
#ifndef M_2_SQRTPI_F
3871
#define M_2_SQRTPI_F 0x1.20dd76p+0f
72+
#endif
73+
#ifndef M_SQRT2_F
3974
#define M_SQRT2_F 0x1.6a09e6p+0f
75+
#endif
76+
#ifndef M_SQRT1_2_F
4077
#define M_SQRT1_2_F 0x1.6a09e6p-1f
78+
#endif
4179

4280
#define M_LOG210_F 0x1.a934f0p+1f
4381

4482
#ifdef cl_khr_fp64
4583

84+
#ifndef HUGE_VAL
4685
#define HUGE_VAL __builtin_huge_val()
86+
#endif
4787

4888
#define DBL_DIG 15
4989
#define DBL_MANT_DIG 53
@@ -82,11 +122,19 @@
82122
#define HALF_MIN_EXP -13
83123

84124
#define HALF_RADIX 2
125+
#ifndef HALF_MAX
85126
#define HALF_MAX 0x1.ffcp15h
127+
#endif
128+
#ifndef HALF_MIN
86129
#define HALF_MIN 0x1.0p-14h
130+
#endif
131+
#ifndef HALF_EPSILON
87132
#define HALF_EPSILON 0x1.0p-10h
133+
#endif
88134
#define HALF_NAN __builtin_nanf16("")
89135

136+
#ifndef M_LOG2E_H
90137
#define M_LOG2E_H 0x1.714p+0h
138+
#endif
91139

92140
#endif

libclc/clc/include/clc/integer/definitions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@
1111

1212
#define CHAR_BIT 8
1313
#define INT_MAX 2147483647
14+
#ifndef INT_MIN
1415
#define INT_MIN (-2147483647 - 1)
16+
#endif
1517
#define LONG_MAX 0x7fffffffffffffffL
18+
#ifndef LONG_MIN
1619
#define LONG_MIN (-0x7fffffffffffffffL - 1)
20+
#endif
1721
#define CHAR_MAX SCHAR_MAX
1822
#define CHAR_MIN SCHAR_MIN
1923
#define SCHAR_MAX 127
24+
#ifndef SCHAR_MIN
2025
#define SCHAR_MIN (-127 - 1)
26+
#endif
2127
#define SHRT_MAX 32767
28+
#ifndef SHRT_MIN
2229
#define SHRT_MIN (-32767 - 1)
30+
#endif
2331
#define UCHAR_MAX 255
2432
#define UCHAR_MIN 0
2533
#define USHRT_MAX 65535

libclc/clc/lib/generic/atomic/clc_atomic_compare_exchange.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#define __CLC_DEFINE_ATOMIC(ADDRSPACE) \
2727
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_atomic_compare_exchange( \
28-
volatile ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Comparator, \
28+
ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Comparator, \
2929
__CLC_GENTYPE Value, int MemoryOrderEqual, int MemoryOrderUnequal, \
3030
int MemoryScope) { \
3131
__CLC_U_GENTYPE Comp = __CLC_AS_U_GENTYPE(Comparator); \
@@ -39,7 +39,7 @@
3939

4040
#define __CLC_DEFINE_ATOMIC(ADDRSPACE) \
4141
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_atomic_compare_exchange( \
42-
volatile ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Comparator, \
42+
ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Comparator, \
4343
__CLC_GENTYPE Value, int MemoryOrderEqual, int MemoryOrderUnequal, \
4444
int MemoryScope) { \
4545
__scoped_atomic_compare_exchange_n(Ptr, &Comparator, Value, false, \

libclc/clc/lib/generic/atomic/clc_atomic_def.inc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,30 @@
3636
#ifdef __CLC_NO_VALUE_ARG
3737
#define __CLC_DEFINE_ATOMIC(ADDRSPACE) \
3838
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNCTION( \
39-
volatile ADDRSPACE __CLC_GENTYPE *Ptr, int MemoryOrder, \
40-
int MemoryScope) { \
39+
ADDRSPACE __CLC_GENTYPE *Ptr, int MemoryOrder, int MemoryScope) { \
4140
return __CLC_AS_RETTYPE(__CLC_IMPL_FUNCTION( \
4241
(ADDRSPACE __CLC_CASTTYPE *)Ptr, MemoryOrder, MemoryScope)); \
4342
}
4443
#elif defined(__CLC_INC_DEC)
4544
#define __CLC_DEFINE_ATOMIC(ADDRSPACE) \
4645
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNCTION( \
47-
volatile ADDRSPACE __CLC_GENTYPE *Ptr, int MemoryOrder, \
48-
int MemoryScope) { \
46+
ADDRSPACE __CLC_GENTYPE *Ptr, int MemoryOrder, int MemoryScope) { \
4947
return __CLC_IMPL_FUNCTION(Ptr, (__CLC_U_GENTYPE)(-1), MemoryOrder, \
5048
MemoryScope); \
5149
}
5250
#elif defined(__CLC_RETURN_VOID)
5351
#define __CLC_DEFINE_ATOMIC(ADDRSPACE) \
5452
_CLC_OVERLOAD _CLC_DEF void __CLC_FUNCTION( \
55-
volatile ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Value, \
56-
int MemoryOrder, int MemoryScope) { \
53+
ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Value, int MemoryOrder, \
54+
int MemoryScope) { \
5755
__CLC_IMPL_FUNCTION((ADDRSPACE __CLC_CASTTYPE *)Ptr, \
5856
__CLC_AS_CASTTYPE(Value), MemoryOrder, MemoryScope); \
5957
}
6058
#else
6159
#define __CLC_DEFINE_ATOMIC(ADDRSPACE) \
6260
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNCTION( \
63-
volatile ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Value, \
64-
int MemoryOrder, int MemoryScope) { \
61+
ADDRSPACE __CLC_GENTYPE *Ptr, __CLC_GENTYPE Value, int MemoryOrder, \
62+
int MemoryScope) { \
6563
return __CLC_AS_RETTYPE(__CLC_IMPL_FUNCTION( \
6664
(ADDRSPACE __CLC_CASTTYPE *)Ptr, __CLC_AS_CASTTYPE(Value), \
6765
MemoryOrder, MemoryScope)); \

libclc/cmake/modules/AddLibclc.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ function(compile_to_bc)
2929
if( NOT ${FILE_EXT} STREQUAL ".ll" )
3030
# Pass '-c' when not running the preprocessor
3131
set( PP_OPTS -c )
32+
set( EXTRA_OPTS ${ARG_EXTRA_OPTS} )
3233
else()
3334
set( PP_OPTS -E;-P )
3435
set( TMP_SUFFIX .tmp )
36+
string( REPLACE "-Xclang;-fdeclare-opencl-builtins;-Xclang;-finclude-default-header"
37+
"" EXTRA_OPTS "${ARG_EXTRA_OPTS}"
38+
)
3539
endif()
3640

3741
set( TARGET_ARG )
@@ -48,7 +52,7 @@ function(compile_to_bc)
4852
COMMAND ${clang_exe}
4953
${TARGET_ARG}
5054
${PP_OPTS}
51-
${ARG_EXTRA_OPTS}
55+
${EXTRA_OPTS}
5256
-MD -MF ${ARG_OUTPUT}.d -MT ${ARG_OUTPUT}${TMP_SUFFIX}
5357
# LLVM 13 enables standard includes by default - we don't want
5458
# those when pre-processing IR. We disable it unconditionally.

libclc/opencl/include/clc/opencl/as_type.h

Lines changed: 0 additions & 92 deletions
This file was deleted.

libclc/opencl/include/clc/opencl/atomic/atomic_compare_exchange_strong.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef __CLC_OPENCL_ATOMIC_ATOMIC_COMPARE_EXCHANGE_STRONG_H__
1010
#define __CLC_OPENCL_ATOMIC_ATOMIC_COMPARE_EXCHANGE_STRONG_H__
1111

12+
#include <clc/opencl/opencl-base.h>
13+
1214
#define __CLC_FUNCTION atomic_compare_exchange_strong
1315
#define __CLC_COMPARE_EXCHANGE
1416

libclc/opencl/include/clc/opencl/atomic/atomic_compare_exchange_weak.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef __CLC_OPENCL_ATOMIC_ATOMIC_COMPARE_EXCHANGE_WEAK_H__
1010
#define __CLC_OPENCL_ATOMIC_ATOMIC_COMPARE_EXCHANGE_WEAK_H__
1111

12+
#include <clc/opencl/opencl-base.h>
13+
1214
#define __CLC_FUNCTION atomic_compare_exchange_weak
1315
#define __CLC_COMPARE_EXCHANGE
1416

0 commit comments

Comments
 (0)