Skip to content

Commit 0c051a8

Browse files
[SYCL][ESIMD][EMU] lsc_atomic support (#6588)
* [SYCL][ESIMD][EMU] lsc_atomic support - __esimd_lsc_xatomic_slm_0/1/2 - __esimd_lsc_xatomic_bti_0/1/2/ - __esimd_lsc_xatomic_stateless_0/1/2
1 parent 4554eb4 commit 0c051a8

File tree

3 files changed

+565
-24
lines changed

3 files changed

+565
-24
lines changed

sycl/include/sycl/ext/intel/esimd/detail/atomic_intrin.hpp

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,116 @@
1111

1212
#include <sycl/exception.hpp>
1313

14+
namespace __ESIMD_DNS {
15+
1416
// This function implements atomic update of pre-existing variable in the
1517
// absense of C++ 20's atomic_ref.
18+
19+
template <typename Ty> Ty atomic_load(Ty *ptr) {
20+
#ifdef _WIN32
21+
// TODO: Windows will be supported soon
22+
__ESIMD_UNSUPPORTED_ON_HOST;
23+
#else
24+
return __atomic_load(ptr, __ATOMIC_SEQ_CST);
25+
#endif
26+
}
27+
28+
template <typename Ty> Ty atomic_store(Ty *ptr, Ty val) {
29+
#ifdef _WIN32
30+
// TODO: Windows will be supported soon
31+
__ESIMD_UNSUPPORTED_ON_HOST;
32+
#else
33+
__atomic_store(ptr, val, __ATOMIC_SEQ_CST);
34+
#endif
35+
}
36+
1637
template <typename Ty> Ty atomic_add_fetch(Ty *ptr, Ty val) {
1738
#ifdef _WIN32
1839
// TODO: Windows will be supported soon
1940
__ESIMD_UNSUPPORTED_ON_HOST;
2041
#else
21-
return __atomic_add_fetch(ptr, val, __ATOMIC_RELAXED);
42+
return __atomic_add_fetch(ptr, val, __ATOMIC_SEQ_CST);
43+
#endif
44+
}
45+
46+
template <typename Ty> Ty atomic_sub_fetch(Ty *ptr, Ty val) {
47+
#ifdef _WIN32
48+
// TODO: Windows will be supported soon
49+
__ESIMD_UNSUPPORTED_ON_HOST;
50+
#else
51+
return __atomic_sub_fetch(ptr, val, __ATOMIC_SEQ_CST);
52+
#endif
53+
}
54+
55+
template <typename Ty> Ty atomic_and_fetch(Ty *ptr, Ty val) {
56+
#ifdef _WIN32
57+
// TODO: Windows will be supported soon
58+
__ESIMD_UNSUPPORTED_ON_HOST;
59+
#else
60+
return __atomic_and_fetch(ptr, val, __ATOMIC_SEQ_CST);
61+
#endif
62+
}
63+
64+
template <typename Ty> Ty atomic_or_fetch(Ty *ptr, Ty val) {
65+
#ifdef _WIN32
66+
// TODO: Windows will be supported soon
67+
__ESIMD_UNSUPPORTED_ON_HOST;
68+
#else
69+
return __atomic_or_fetch(ptr, val, __ATOMIC_SEQ_CST);
2270
#endif
2371
}
2472

73+
template <typename Ty> Ty atomic_xor_fetch(Ty *ptr, Ty val) {
74+
#ifdef _WIN32
75+
// TODO: Windows will be supported soon
76+
__ESIMD_UNSUPPORTED_ON_HOST;
77+
#else
78+
return __atomic_xor_fetch(ptr, val, __ATOMIC_SEQ_CST);
79+
#endif
80+
}
81+
82+
template <typename Ty> Ty atomic_min(Ty *ptr, Ty val) {
83+
#ifdef _WIN32
84+
// TODO: Windows will be supported soon
85+
__ESIMD_UNSUPPORTED_ON_HOST;
86+
#else
87+
Ty _old, _new;
88+
do {
89+
_old = *ptr;
90+
_new = std::min<Ty>(_old, val);
91+
} while (!__atomic_compare_exchange_n(ptr, &_old, _new, false,
92+
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
93+
return _new;
94+
#endif
95+
}
96+
97+
template <typename Ty> Ty atomic_max(Ty *ptr, Ty val) {
98+
#ifdef _WIN32
99+
// TODO: Windows will be supported soon
100+
__ESIMD_UNSUPPORTED_ON_HOST;
101+
#else
102+
Ty _old, _new;
103+
do {
104+
_old = *ptr;
105+
_new = std::max<Ty>(_old, val);
106+
} while (!__atomic_compare_exchange_n(ptr, &_old, _new, false,
107+
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
108+
return _new;
109+
#endif
110+
}
111+
112+
template <typename Ty> Ty atomic_cmpxchg(Ty *ptr, Ty expected, Ty desired) {
113+
#ifdef _WIN32
114+
// TODO: Windows will be supported soon
115+
__ESIMD_UNSUPPORTED_ON_HOST;
116+
#else
117+
Ty _old = expected;
118+
__atomic_compare_exchange_n(ptr, &_old, desired, false, __ATOMIC_SEQ_CST,
119+
__ATOMIC_SEQ_CST);
120+
return *ptr;
121+
#endif
122+
}
123+
124+
} // namespace __ESIMD_DNS
125+
25126
/// @endcond ESIMD_DETAIL

sycl/include/sycl/ext/intel/esimd/detail/memory_intrin.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ __esimd_svm_atomic1(__ESIMD_DNS::vector_type_t<uint64_t, N> addrs,
506506

507507
switch (Op) {
508508
case __ESIMD_NS::atomic_op::add:
509-
retv[i] = atomic_add_fetch<Ty>(p, src0[i]);
509+
retv[i] = __ESIMD_DNS::atomic_add_fetch<Ty>(p, src0[i]);
510510
break;
511511
default:
512512
__ESIMD_UNSUPPORTED_ON_HOST;
@@ -849,7 +849,7 @@ __esimd_dword_atomic0(__ESIMD_DNS::simd_mask_storage_t<N> pred,
849849

850850
switch (Op) {
851851
case __ESIMD_NS::atomic_op::inc:
852-
retv[i] = atomic_add_fetch<Ty>(p, 1);
852+
retv[i] = __ESIMD_DNS::atomic_add_fetch<Ty>(p, 1);
853853
break;
854854
default:
855855
__ESIMD_UNSUPPORTED_ON_HOST;

0 commit comments

Comments
 (0)