Skip to content

Commit 8947ba0

Browse files
authored
[libclc] Add atomic_init, atomic_flag_clear and atomic_flag_test_and_set (#168329)
1 parent 25dee65 commit 8947ba0

File tree

16 files changed

+541
-0
lines changed

16 files changed

+541
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_ATOMIC_CLC_ATOMIC_FLAG_CLEAR_H__
10+
#define __CLC_ATOMIC_CLC_ATOMIC_FLAG_CLEAR_H__
11+
12+
#include <clc/internal/clc.h>
13+
14+
#define __CLC_DECLARE_ATOMIC_FLAG_CLEAR(ADDRSPACE) \
15+
_CLC_OVERLOAD _CLC_DECL void __clc_atomic_flag_clear( \
16+
ADDRSPACE int *Ptr, int MemoryOrder, int MemoryScope);
17+
18+
__CLC_DECLARE_ATOMIC_FLAG_CLEAR(global)
19+
__CLC_DECLARE_ATOMIC_FLAG_CLEAR(local)
20+
#if _CLC_GENERIC_AS_SUPPORTED
21+
__CLC_DECLARE_ATOMIC_FLAG_CLEAR()
22+
#endif
23+
24+
#endif // __CLC_ATOMIC_CLC_ATOMIC_FLAG_CLEAR_H__
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_ATOMIC_CLC_ATOMIC_FLAG_TEST_AND_SET_H__
10+
#define __CLC_ATOMIC_CLC_ATOMIC_FLAG_TEST_AND_SET_H__
11+
12+
#include <clc/internal/clc.h>
13+
14+
#define __CLC_DECLARE_ATOMIC_FLAG_TEST_AND_SET(ADDRSPACE) \
15+
_CLC_OVERLOAD _CLC_DECL bool __clc_atomic_flag_test_and_set( \
16+
ADDRSPACE int *Ptr, int MemoryOrder, int MemoryScope);
17+
18+
__CLC_DECLARE_ATOMIC_FLAG_TEST_AND_SET(global)
19+
__CLC_DECLARE_ATOMIC_FLAG_TEST_AND_SET(local)
20+
#if _CLC_GENERIC_AS_SUPPORTED
21+
__CLC_DECLARE_ATOMIC_FLAG_TEST_AND_SET()
22+
#endif
23+
24+
#endif // __CLC_ATOMIC_CLC_ATOMIC_FLAG_TEST_AND_SET_H__

libclc/clc/lib/generic/SOURCES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ atomic/clc_atomic_fetch_min.cl
99
atomic/clc_atomic_fetch_or.cl
1010
atomic/clc_atomic_fetch_sub.cl
1111
atomic/clc_atomic_fetch_xor.cl
12+
atomic/clc_atomic_flag_clear.cl
13+
atomic/clc_atomic_flag_test_and_set.cl
1214
atomic/clc_atomic_inc.cl
1315
atomic/clc_atomic_load.cl
1416
atomic/clc_atomic_store.cl
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/atomic/clc_atomic_flag_clear.h>
10+
#include <clc/atomic/clc_atomic_store.h>
11+
12+
#define __CLC_ATOMIC_FLAG_FALSE 0
13+
14+
#define __CLC_DEFINE_ATOMIC_FLAG_CLEAR(ADDRSPACE) \
15+
_CLC_OVERLOAD _CLC_DEF void __clc_atomic_flag_clear( \
16+
ADDRSPACE int *Ptr, int MemoryOrder, int MemoryScope) { \
17+
__clc_atomic_store(Ptr, __CLC_ATOMIC_FLAG_FALSE, MemoryOrder, \
18+
MemoryScope); \
19+
}
20+
21+
__CLC_DEFINE_ATOMIC_FLAG_CLEAR(global)
22+
__CLC_DEFINE_ATOMIC_FLAG_CLEAR(local)
23+
#if _CLC_GENERIC_AS_SUPPORTED
24+
__CLC_DEFINE_ATOMIC_FLAG_CLEAR()
25+
#endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/atomic/clc_atomic_exchange.h>
10+
#include <clc/atomic/clc_atomic_flag_test_and_set.h>
11+
12+
#define __CLC_ATOMIC_FLAG_TRUE 1
13+
14+
#define __CLC_DEFINE_ATOMIC_FLAG_TEST_AND_SET(ADDRSPACE) \
15+
_CLC_OVERLOAD _CLC_DEF bool __clc_atomic_flag_test_and_set( \
16+
ADDRSPACE int *Ptr, int MemoryOrder, int MemoryScope) { \
17+
return (bool)__clc_atomic_exchange(Ptr, __CLC_ATOMIC_FLAG_TRUE, \
18+
MemoryOrder, MemoryScope); \
19+
}
20+
21+
__CLC_DEFINE_ATOMIC_FLAG_TEST_AND_SET(global)
22+
__CLC_DEFINE_ATOMIC_FLAG_TEST_AND_SET(local)
23+
#if _CLC_GENERIC_AS_SUPPORTED
24+
__CLC_DEFINE_ATOMIC_FLAG_TEST_AND_SET()
25+
#endif
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_OPENCL_ATOMIC_ATOMIC_FLAG_CLEAR_H__
10+
#define __CLC_OPENCL_ATOMIC_ATOMIC_FLAG_CLEAR_H__
11+
12+
#include <clc/opencl/opencl-base.h>
13+
#include <clc/opencl/types.h>
14+
15+
#if defined(__opencl_c_atomic_order_seq_cst) && \
16+
defined(__opencl_c_atomic_scope_device)
17+
_CLC_OVERLOAD _CLC_DECL void atomic_flag_clear(volatile __global atomic_flag *);
18+
_CLC_OVERLOAD _CLC_DECL void atomic_flag_clear(volatile __local atomic_flag *);
19+
#if defined(__opencl_c_generic_address_space)
20+
_CLC_OVERLOAD _CLC_DECL void atomic_flag_clear(volatile atomic_flag *);
21+
#endif // defined(__opencl_c_generic_address_space)
22+
#endif
23+
24+
#if defined(__opencl_c_atomic_scope_device)
25+
_CLC_OVERLOAD _CLC_DECL void
26+
atomic_flag_clear_explicit(volatile __global atomic_flag *, memory_order);
27+
_CLC_OVERLOAD _CLC_DECL void
28+
atomic_flag_clear_explicit(volatile __local atomic_flag *, memory_order);
29+
#if defined(__opencl_c_generic_address_space)
30+
_CLC_OVERLOAD _CLC_DECL void atomic_flag_clear_explicit(volatile atomic_flag *,
31+
memory_order);
32+
#endif // defined(__opencl_c_generic_address_space)
33+
#endif
34+
35+
_CLC_OVERLOAD _CLC_DECL void
36+
atomic_flag_clear_explicit(volatile __global atomic_flag *, memory_order,
37+
memory_scope);
38+
_CLC_OVERLOAD _CLC_DECL void
39+
atomic_flag_clear_explicit(volatile __local atomic_flag *, memory_order,
40+
memory_scope);
41+
#if defined(__opencl_c_generic_address_space)
42+
_CLC_OVERLOAD _CLC_DECL void
43+
atomic_flag_clear_explicit(volatile atomic_flag *, memory_order, memory_scope);
44+
#endif // defined(__opencl_c_generic_address_space)
45+
46+
#endif // __CLC_OPENCL_ATOMIC_ATOMIC_FLAG_CLEAR_H__
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_OPENCL_ATOMIC_ATOMIC_FLAG_TEST_AND_SET_H__
10+
#define __CLC_OPENCL_ATOMIC_ATOMIC_FLAG_TEST_AND_SET_H__
11+
12+
#include <clc/opencl/opencl-base.h>
13+
#include <clc/opencl/types.h>
14+
15+
#if defined(__opencl_c_atomic_order_seq_cst) && \
16+
defined(__opencl_c_atomic_scope_device)
17+
_CLC_OVERLOAD _CLC_DECL bool
18+
atomic_flag_test_and_set(volatile __global atomic_flag *);
19+
_CLC_OVERLOAD _CLC_DECL bool
20+
atomic_flag_test_and_set(volatile __local atomic_flag *);
21+
#if defined(__opencl_c_generic_address_space)
22+
_CLC_OVERLOAD _CLC_DECL bool atomic_flag_test_and_set(volatile atomic_flag *);
23+
#endif // defined(__opencl_c_generic_address_space)
24+
#endif
25+
26+
#if defined(__opencl_c_atomic_scope_device)
27+
_CLC_OVERLOAD _CLC_DECL bool
28+
atomic_flag_test_and_set_explicit(volatile __global atomic_flag *,
29+
memory_order);
30+
_CLC_OVERLOAD _CLC_DECL bool
31+
atomic_flag_test_and_set_explicit(volatile __local atomic_flag *, memory_order);
32+
#if defined(__opencl_c_generic_address_space)
33+
_CLC_OVERLOAD _CLC_DECL bool
34+
atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order);
35+
#endif // defined(__opencl_c_generic_address_space)
36+
#endif
37+
38+
_CLC_OVERLOAD _CLC_DECL bool
39+
atomic_flag_test_and_set_explicit(volatile __global atomic_flag *, memory_order,
40+
memory_scope);
41+
_CLC_OVERLOAD _CLC_DECL bool
42+
atomic_flag_test_and_set_explicit(volatile __local atomic_flag *, memory_order,
43+
memory_scope);
44+
#if defined(__opencl_c_generic_address_space)
45+
_CLC_OVERLOAD _CLC_DECL bool
46+
atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order,
47+
memory_scope);
48+
#endif // defined(__opencl_c_generic_address_space)
49+
50+
#endif // __CLC_OPENCL_ATOMIC_ATOMIC_FLAG_TEST_AND_SET_H__
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_OPENCL_ATOMIC_ATOMIC_INIT_H__
10+
#define __CLC_OPENCL_ATOMIC_ATOMIC_INIT_H__
11+
12+
#include <clc/opencl/opencl-base.h>
13+
14+
#define __CLC_ATOMIC_GENTYPE __CLC_XCONCAT(atomic_, __CLC_GENTYPE)
15+
16+
#define __CLC_BODY <clc/opencl/atomic/atomic_init.inc>
17+
#include <clc/integer/gentype.inc>
18+
19+
#define __CLC_BODY <clc/opencl/atomic/atomic_init.inc>
20+
#include <clc/math/gentype.inc>
21+
22+
#undef __CLC_ATOMIC_GENTYPE
23+
24+
#endif // __CLC_OPENCL_ATOMIC_ATOMIC_INIT_H__
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifdef __CLC_SCALAR
10+
11+
#if defined(__opencl_c_fp64) && (defined(cl_khr_int64_base_atomics) && \
12+
defined(cl_khr_int64_extended_atomics))
13+
#define __CLC_HAVE_64_ATOMIC
14+
#endif
15+
#if defined(__CLC_FPSIZE) && \
16+
(__CLC_FPSIZE < 64 || defined(__CLC_HAVE_64_ATOMIC))
17+
#define __CLC_HAVE_FP_ATOMIC
18+
#endif
19+
#if defined(__CLC_GENSIZE) && \
20+
((__CLC_GENSIZE == 32) || \
21+
(__CLC_GENSIZE == 64 && defined(__CLC_HAVE_64_ATOMIC)))
22+
#define __CLC_HAVE_INT_ATOMIC
23+
#endif
24+
#if defined(__CLC_HAVE_FP_ATOMIC) || defined(__CLC_HAVE_INT_ATOMIC)
25+
26+
#define __CLC_DECL_ATOMIC(ADDRSPACE) \
27+
_CLC_OVERLOAD _CLC_DECL void atomic_init( \
28+
volatile ADDRSPACE __CLC_ATOMIC_GENTYPE *Ptr, __CLC_GENTYPE Value);
29+
30+
__CLC_DECL_ATOMIC(global)
31+
__CLC_DECL_ATOMIC(local)
32+
#if _CLC_GENERIC_AS_SUPPORTED
33+
__CLC_DECL_ATOMIC()
34+
#endif
35+
36+
#undef __CLC_DECL_ATOMIC
37+
38+
#endif // __CLC_HAVE_FP_ATOMIC || __CLC_HAVE_INT_ATOMIC
39+
40+
#undef __CLC_HAVE_INT_ATOMIC
41+
#undef __CLC_HAVE_FP_ATOMIC
42+
#undef __CLC_HAVE_64_ATOMIC
43+
44+
#endif // __CLC_SCALAR
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_OPENCL_TYPES_H__
10+
#define __CLC_OPENCL_TYPES_H__
11+
12+
// Copied from clang/lib/Headers/opencl-c-base.h
13+
14+
typedef enum memory_scope {
15+
memory_scope_work_item = __OPENCL_MEMORY_SCOPE_WORK_ITEM,
16+
memory_scope_work_group = __OPENCL_MEMORY_SCOPE_WORK_GROUP,
17+
memory_scope_device = __OPENCL_MEMORY_SCOPE_DEVICE,
18+
#if defined(__opencl_c_atomic_scope_all_devices)
19+
memory_scope_all_svm_devices = __OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES,
20+
#if (__OPENCL_C_VERSION__ >= CL_VERSION_3_0 || __OPENCL_CPP_VERSION__ >= 202100)
21+
memory_scope_all_devices = memory_scope_all_svm_devices,
22+
#endif // (__OPENCL_C_VERSION__ >= CL_VERSION_3_0 || __OPENCL_CPP_VERSION__ >=
23+
// 202100)
24+
#endif // defined(__opencl_c_atomic_scope_all_devices)
25+
/**
26+
* Subgroups have different requirements on forward progress, so just test
27+
* all the relevant macros.
28+
* CL 3.0 sub-groups "they are not guaranteed to make independent forward
29+
* progress" KHR subgroups "Subgroups within a workgroup are independent, make
30+
* forward progress with respect to each other"
31+
*/
32+
#if defined(cl_intel_subgroups) || defined(cl_khr_subgroups) || \
33+
defined(__opencl_c_subgroups)
34+
memory_scope_sub_group = __OPENCL_MEMORY_SCOPE_SUB_GROUP
35+
#endif
36+
} memory_scope;
37+
38+
typedef enum memory_order {
39+
memory_order_relaxed = __ATOMIC_RELAXED,
40+
memory_order_acquire = __ATOMIC_ACQUIRE,
41+
memory_order_release = __ATOMIC_RELEASE,
42+
memory_order_acq_rel = __ATOMIC_ACQ_REL,
43+
#if defined(__opencl_c_atomic_order_seq_cst)
44+
memory_order_seq_cst = __ATOMIC_SEQ_CST
45+
#endif
46+
} memory_order;
47+
48+
#endif // __CLC_OPENCL_TYPES_H__

0 commit comments

Comments
 (0)