Skip to content

Commit f38cf01

Browse files
authored
[libclc] Use CLC atomic functions for legacy OpenCL atom/atomic builtins (#168325)
Main changes: * OpenCL legacy atom/atomic builtins now call CLC atomic functions (which use Clang __scoped_atomic_*), replacing previous Clang __sync_* functions. * Change memory order from seq_cst to relaxed; keep device scope (spec permits broader than workgroup). LLVM IR for _Z8atom_decPU3AS1Vi in amdgcn--amdhsa.bc: Before: %2 = atomicrmw volatile sub ptr subrspace(1) %0, i32 1 syncscope("agent") seq_cst After: %2 = atomicrmw volatile sub ptr subrspace(1) %0, i32 1 syncscope("agent") monotonic * Also adds OpenCL 1.0 atom_* variants without volatile on the pointer. They are added for backward compatibility.
1 parent f7f4135 commit f38cf01

26 files changed

+190
-265
lines changed

libclc/opencl/include/clc/opencl/atomic/atom_decl_int32.inc

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

9-
#include <clc/clcfunc.h>
10-
#include <clc/clctypes.h>
11-
129
#define __CLC_DECLARE_ATOM(ADDRSPACE, TYPE) \
1310
_CLC_OVERLOAD _CLC_DECL TYPE __CLC_FUNCTION(volatile ADDRSPACE TYPE *, TYPE);
1411

libclc/opencl/include/clc/opencl/atomic/atom_decl_int64.inc

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

9-
#include <clc/clcfunc.h>
10-
#include <clc/clctypes.h>
11-
129
#define __CLC_DECLARE_ATOM(ADDRSPACE, TYPE) \
1310
_CLC_OVERLOAD _CLC_DECL TYPE __CLC_FUNCTION(volatile ADDRSPACE TYPE *, TYPE);
1411

libclc/opencl/lib/amdgcn/SOURCES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cl_khr_int64_extended_atomics/minmax_helpers.ll
21
mem_fence/fence.cl
32
synchronization/barrier.cl
43
workitem/get_global_offset.cl

libclc/opencl/lib/amdgcn/cl_khr_int64_extended_atomics/minmax_helpers.ll

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

libclc/opencl/lib/generic/atomic/atom_add.cl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,35 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/atomic/clc_atomic_fetch_add.h>
910
#include <clc/opencl/atomic/atom_add.h>
10-
#include <clc/opencl/atomic/atomic_add.h>
11+
12+
// Non-volatile overloads are for backward compatibility with OpenCL 1.0.
13+
14+
#define __CLC_IMPL(AS, TYPE) \
15+
_CLC_OVERLOAD _CLC_DEF TYPE atom_add(volatile AS TYPE *p, TYPE val) { \
16+
return __clc_atomic_fetch_add(p, val, __ATOMIC_RELAXED, \
17+
__MEMORY_SCOPE_DEVICE); \
18+
} \
19+
_CLC_OVERLOAD _CLC_DEF TYPE atom_add(AS TYPE *p, TYPE val) { \
20+
return atom_add((volatile AS TYPE *)p, val); \
21+
}
1122

1223
#ifdef cl_khr_global_int32_base_atomics
13-
#define __CLC_ATOMIC_OP add
14-
#define __CLC_ATOMIC_ADDRESS_SPACE global
15-
#include "atom_int32_binary.inc"
24+
__CLC_IMPL(global, int)
25+
__CLC_IMPL(global, unsigned int)
1626
#endif // cl_khr_global_int32_base_atomics
1727

1828
#ifdef cl_khr_local_int32_base_atomics
19-
#define __CLC_ATOMIC_OP add
20-
#define __CLC_ATOMIC_ADDRESS_SPACE local
21-
#include "atom_int32_binary.inc"
29+
__CLC_IMPL(local, int)
30+
__CLC_IMPL(local, unsigned int)
2231
#endif // cl_khr_local_int32_base_atomics
2332

2433
#ifdef cl_khr_int64_base_atomics
2534

26-
#define __CLC_IMPL(AS, TYPE) \
27-
_CLC_OVERLOAD _CLC_DEF TYPE atom_add(volatile AS TYPE *p, TYPE val) { \
28-
return __sync_fetch_and_add_8(p, val); \
29-
}
30-
3135
__CLC_IMPL(global, long)
3236
__CLC_IMPL(global, unsigned long)
3337
__CLC_IMPL(local, long)
3438
__CLC_IMPL(local, unsigned long)
35-
#undef __CLC_IMPL
3639

3740
#endif // cl_khr_int64_base_atomics

libclc/opencl/lib/generic/atomic/atom_and.cl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,35 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/atomic/clc_atomic_fetch_and.h>
910
#include <clc/opencl/atomic/atom_and.h>
10-
#include <clc/opencl/atomic/atomic_and.h>
11+
12+
// Non-volatile overloads are for backward compatibility with OpenCL 1.0.
13+
14+
#define __CLC_IMPL(AS, TYPE) \
15+
_CLC_OVERLOAD _CLC_DEF TYPE atom_and(volatile AS TYPE *p, TYPE val) { \
16+
return __clc_atomic_fetch_and(p, val, __ATOMIC_RELAXED, \
17+
__MEMORY_SCOPE_DEVICE); \
18+
} \
19+
_CLC_OVERLOAD _CLC_DEF TYPE atom_and(AS TYPE *p, TYPE val) { \
20+
return atom_and((volatile AS TYPE *)p, val); \
21+
}
1122

1223
#ifdef cl_khr_global_int32_extended_atomics
13-
#define __CLC_ATOMIC_OP and
14-
#define __CLC_ATOMIC_ADDRESS_SPACE global
15-
#include "atom_int32_binary.inc"
24+
__CLC_IMPL(global, int)
25+
__CLC_IMPL(global, unsigned int)
1626
#endif // cl_khr_global_int32_extended_atomics
1727

1828
#ifdef cl_khr_local_int32_extended_atomics
19-
#define __CLC_ATOMIC_OP and
20-
#define __CLC_ATOMIC_ADDRESS_SPACE local
21-
#include "atom_int32_binary.inc"
29+
__CLC_IMPL(local, int)
30+
__CLC_IMPL(local, unsigned int)
2231
#endif // cl_khr_local_int32_extended_atomics
2332

2433
#ifdef cl_khr_int64_extended_atomics
2534

26-
#define __CLC_IMPL(AS, TYPE) \
27-
_CLC_OVERLOAD _CLC_DEF TYPE atom_and(volatile AS TYPE *p, TYPE val) { \
28-
return __sync_fetch_and_and_8(p, val); \
29-
}
30-
3135
__CLC_IMPL(global, long)
3236
__CLC_IMPL(global, unsigned long)
3337
__CLC_IMPL(local, long)
3438
__CLC_IMPL(local, unsigned long)
35-
#undef __CLC_IMPL
3639

3740
#endif // cl_khr_int64_extended_atomics

libclc/opencl/lib/generic/atomic/atom_cmpxchg.cl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/atomic/clc_atomic_compare_exchange.h>
910
#include <clc/opencl/atomic/atom_cmpxchg.h>
10-
#include <clc/opencl/atomic/atomic_cmpxchg.h>
11+
12+
// Non-volatile overloads are for backward compatibility with OpenCL 1.0.
1113

1214
#define __CLC_IMPL(AS, TYPE) \
1315
_CLC_OVERLOAD _CLC_DEF TYPE atom_cmpxchg(volatile AS TYPE *p, TYPE cmp, \
1416
TYPE val) { \
15-
return atomic_cmpxchg(p, cmp, val); \
17+
return __clc_atomic_compare_exchange(p, cmp, val, __ATOMIC_RELAXED, \
18+
__ATOMIC_RELAXED, \
19+
__MEMORY_SCOPE_DEVICE); \
20+
} \
21+
_CLC_OVERLOAD _CLC_DEF TYPE atom_cmpxchg(AS TYPE *p, TYPE cmp, TYPE val) { \
22+
return atom_cmpxchg((volatile AS TYPE *)p, cmp, val); \
1623
}
1724

1825
#ifdef cl_khr_global_int32_base_atomics
@@ -24,20 +31,11 @@ __CLC_IMPL(local, int)
2431
__CLC_IMPL(local, unsigned int)
2532
#endif // cl_khr_local_int32_base_atomics
2633

27-
#undef __CLC_IMPL
28-
2934
#ifdef cl_khr_int64_base_atomics
3035

31-
#define __CLC_IMPL(AS, TYPE) \
32-
_CLC_OVERLOAD _CLC_DEF TYPE atom_cmpxchg(volatile AS TYPE *p, TYPE cmp, \
33-
TYPE val) { \
34-
return __sync_val_compare_and_swap_8(p, cmp, val); \
35-
}
36-
3736
__CLC_IMPL(global, long)
3837
__CLC_IMPL(global, unsigned long)
3938
__CLC_IMPL(local, long)
4039
__CLC_IMPL(local, unsigned long)
41-
#undef __CLC_IMPL
4240

4341
#endif // cl_khr_int64_base_atomics

libclc/opencl/lib/generic/atomic/atom_dec.cl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/atomic/clc_atomic_dec.h>
910
#include <clc/opencl/atomic/atom_dec.h>
10-
#include <clc/opencl/atomic/atom_sub.h>
11-
#include <clc/opencl/atomic/atomic_dec.h>
11+
12+
// Non-volatile overloads are for backward compatibility with OpenCL 1.0.
1213

1314
#define __CLC_IMPL(AS, TYPE) \
1415
_CLC_OVERLOAD _CLC_DEF TYPE atom_dec(volatile AS TYPE *p) { \
15-
return atomic_dec(p); \
16+
return __clc_atomic_dec(p, __ATOMIC_RELAXED, __MEMORY_SCOPE_DEVICE); \
17+
} \
18+
_CLC_OVERLOAD _CLC_DEF TYPE atom_dec(AS TYPE *p) { \
19+
return atom_dec((volatile AS TYPE *)p); \
1620
}
1721

1822
#ifdef cl_khr_global_int32_base_atomics
@@ -24,19 +28,11 @@ __CLC_IMPL(local, int)
2428
__CLC_IMPL(local, unsigned int)
2529
#endif // cl_khr_local_int32_base_atomics
2630

27-
#undef __CLC_IMPL
28-
2931
#ifdef cl_khr_int64_base_atomics
3032

31-
#define __CLC_IMPL(AS, TYPE) \
32-
_CLC_OVERLOAD _CLC_DEF TYPE atom_dec(volatile AS TYPE *p) { \
33-
return atom_sub(p, (TYPE)1); \
34-
}
35-
3633
__CLC_IMPL(global, long)
3734
__CLC_IMPL(global, unsigned long)
3835
__CLC_IMPL(local, long)
3936
__CLC_IMPL(local, unsigned long)
40-
#undef __CLC_IMPL
4137

4238
#endif // cl_khr_int64_base_atomics

libclc/opencl/lib/generic/atomic/atom_inc.cl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <clc/opencl/atomic/atom_add.h>
9+
#include <clc/atomic/clc_atomic_inc.h>
1010
#include <clc/opencl/atomic/atom_inc.h>
11-
#include <clc/opencl/atomic/atomic_inc.h>
11+
12+
// Non-volatile overloads are for backward compatibility with OpenCL 1.0.
1213

1314
#define __CLC_IMPL(AS, TYPE) \
1415
_CLC_OVERLOAD _CLC_DEF TYPE atom_inc(volatile AS TYPE *p) { \
15-
return atomic_inc(p); \
16+
return __clc_atomic_inc(p, __ATOMIC_RELAXED, __MEMORY_SCOPE_DEVICE); \
17+
} \
18+
_CLC_OVERLOAD _CLC_DEF TYPE atom_inc(AS TYPE *p) { \
19+
return atom_inc((volatile AS TYPE *)p); \
1620
}
1721

1822
#ifdef cl_khr_global_int32_base_atomics
@@ -24,19 +28,11 @@ __CLC_IMPL(local, int)
2428
__CLC_IMPL(local, unsigned int)
2529
#endif // cl_khr_local_int32_base_atomics
2630

27-
#undef __CLC_IMPL
28-
2931
#ifdef cl_khr_int64_base_atomics
3032

31-
#define __CLC_IMPL(AS, TYPE) \
32-
_CLC_OVERLOAD _CLC_DEF TYPE atom_inc(volatile AS TYPE *p) { \
33-
return atom_add(p, (TYPE)1); \
34-
}
35-
3633
__CLC_IMPL(global, long)
3734
__CLC_IMPL(global, unsigned long)
3835
__CLC_IMPL(local, long)
3936
__CLC_IMPL(local, unsigned long)
40-
#undef __CLC_IMPL
4137

4238
#endif // cl_khr_int64_base_atomics

libclc/opencl/lib/generic/atomic/atom_int32_binary.inc

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

0 commit comments

Comments
 (0)