Skip to content

Commit a2189c6

Browse files
[SYCL] Change return of get_native for opencl backend to vector (#4952)
Change return of get_nativesycl::backend::opencl(event) from cl_event to vector<cl_event>
1 parent 3070b95 commit a2189c6

File tree

8 files changed

+100
-4
lines changed

8 files changed

+100
-4
lines changed

sycl/doc/PreprocessorMacros.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ This file describes macros that have effect on SYCL compiler and run-time.
4949
support do not impose any extra overhead. One can check to see if a device has
5050
native support for `assert()` via `aspect::ext_oneapi_native_assert`.
5151

52+
- **SYCL2020_CONFORMANT_APIS**
53+
This macro is used to comply with the SYCL 2020 specification, as some of the current
54+
implementations may be widespread and not conform to it.
55+
Description of what it changes:
56+
1) According to spec, `backend_return_t` for opencl event
57+
should be `std::vector<cl_event>` instead of `cl_event`. Defining this macro
58+
will change the behavior of `sycl::get_native()` function and using types for
59+
next structs: `interop<backend::opencl, event>`, `BackendInput<backend::opencl, event>`,
60+
`BackendReturn<backend::opencl, event>` to be in line with the spec.
61+
5262
## Version macros
5363

5464
- `__LIBSYCL_MAJOR_VERSION` is set to SYCL runtime library major version.

sycl/include/CL/sycl/backend.hpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,46 @@ auto get_native(const SyclObjectT &Obj)
7171
if (Obj.get_backend() != BackendName) {
7272
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
7373
}
74-
7574
return Obj.template get_native<BackendName>();
7675
}
7776

77+
// define SYCL2020_CONFORMANT_APIS to correspond SYCL 2020 spec and return
78+
// vector<cl_event> from get_native instead of just cl_event
79+
#ifdef SYCL2020_CONFORMANT_APIS
80+
template <>
81+
inline backend_return_t<backend::opencl, event>
82+
get_native<backend::opencl, event>(const event &Obj) {
83+
// TODO use SYCL 2020 exception when implemented
84+
if (Obj.get_backend() != backend::opencl) {
85+
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
86+
}
87+
backend_return_t<backend::opencl, event> ReturnValue;
88+
for (auto const &element : Obj.getNativeVector()) {
89+
ReturnValue.push_back(
90+
reinterpret_cast<
91+
typename detail::interop<backend::opencl, event>::value_type>(
92+
element));
93+
}
94+
return ReturnValue;
95+
}
96+
#else
97+
// Specialization for cl_event with deprecation message
98+
template <>
99+
__SYCL_DEPRECATED(
100+
"get_native<backend::opencl, event>, which return type is "
101+
"cl_event is deprecated. According to SYCL 2020 spec, please define "
102+
"SYCL2020_CONFORMANT_APIS and use vector<cl_event> instead.")
103+
inline backend_return_t<backend::opencl, event> get_native<
104+
backend::opencl, event>(const event &Obj) {
105+
// TODO use SYCL 2020 exception when implemented
106+
if (Obj.get_backend() != backend::opencl) {
107+
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
108+
}
109+
return reinterpret_cast<
110+
typename detail::interop<backend::opencl, event>::type>(Obj.getNative());
111+
}
112+
#endif
113+
78114
// Native handle of an accessor should be accessed through interop_handler
79115
template <backend BackendName, typename DataT, int Dimensions,
80116
access::mode AccessMode, access::target AccessTarget,

sycl/include/CL/sycl/detail/backend_traits_opencl.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ template <> struct interop<backend::opencl, device> {
4141
using type = cl_device_id;
4242
};
4343

44-
template <> struct interop<backend::opencl, event> { using type = cl_event; };
45-
4644
template <> struct interop<backend::opencl, queue> {
4745
using type = cl_command_queue;
4846
};
@@ -106,13 +104,28 @@ template <> struct BackendReturn<backend::opencl, device> {
106104
using type = cl_device_id;
107105
};
108106

107+
#ifdef SYCL2020_CONFORMANT_APIS
108+
template <> struct interop<backend::opencl, event> {
109+
using type = std::vector<cl_event>;
110+
using value_type = cl_event;
111+
};
112+
template <> struct BackendInput<backend::opencl, event> {
113+
using type = std::vector<cl_event>;
114+
using value_type = cl_event;
115+
};
116+
template <> struct BackendReturn<backend::opencl, event> {
117+
using type = std::vector<cl_event>;
118+
using value_type = cl_event;
119+
};
120+
#else
121+
template <> struct interop<backend::opencl, event> { using type = cl_event; };
109122
template <> struct BackendInput<backend::opencl, event> {
110123
using type = cl_event;
111124
};
112-
113125
template <> struct BackendReturn<backend::opencl, event> {
114126
using type = cl_event;
115127
};
128+
#endif
116129

117130
template <> struct BackendInput<backend::opencl, queue> {
118131
using type = cl_command_queue;

sycl/include/CL/sycl/event.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,19 @@ class __SYCL_EXPORT event {
141141

142142
pi_native_handle getNative() const;
143143

144+
std::vector<pi_native_handle> getNativeVector() const;
145+
144146
std::shared_ptr<detail::event_impl> impl;
145147

146148
template <class Obj>
147149
friend decltype(Obj::impl) detail::getSyclObjImpl(const Obj &SyclObject);
148150

149151
template <class T>
150152
friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);
153+
154+
template <backend BackendName, class SyclObjectT>
155+
friend auto get_native(const SyclObjectT &Obj)
156+
-> backend_return_t<BackendName, SyclObjectT>;
151157
};
152158

153159
} // namespace sycl

sycl/source/event.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,11 @@ event::event(std::shared_ptr<detail::event_impl> event_impl)
8989
backend event::get_backend() const noexcept { return getImplBackend(impl); }
9090

9191
pi_native_handle event::getNative() const { return impl->getNative(); }
92+
93+
std::vector<pi_native_handle> event::getNativeVector() const {
94+
std::vector<pi_native_handle> ReturnVector = {impl->getNative()};
95+
return ReturnVector;
96+
}
97+
9298
} // namespace sycl
9399
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/test/abi/sycl_symbols_linux.dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4056,6 +4056,7 @@ _ZNK2cl4sycl3ext6oneapi15filter_selector13select_deviceEv
40564056
_ZNK2cl4sycl3ext6oneapi15filter_selector5resetEv
40574057
_ZNK2cl4sycl3ext6oneapi15filter_selectorclERKNS0_6deviceE
40584058
_ZNK2cl4sycl5event11get_backendEv
4059+
_ZNK2cl4sycl5event15getNativeVectorEv
40594060
_ZNK2cl4sycl5event18get_profiling_infoILNS0_4info15event_profilingE4737EEENS3_12param_traitsIS4_XT_EE11return_typeEv
40604061
_ZNK2cl4sycl5event18get_profiling_infoILNS0_4info15event_profilingE4738EEENS3_12param_traitsIS4_XT_EE11return_typeEv
40614062
_ZNK2cl4sycl5event18get_profiling_infoILNS0_4info15event_profilingE4739EEENS3_12param_traitsIS4_XT_EE11return_typeEv

sycl/test/abi/sycl_symbols_windows.dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,7 @@
21622162
?getNativeImpl@kernel@sycl@cl@@AEBA_KXZ
21632163
?getNativeMem@interop_handle@sycl@cl@@AEBA_KPEAVAccessorImplHost@detail@23@@Z
21642164
?getNativeQueue@interop_handle@sycl@cl@@AEBA_KXZ
2165+
?getNativeVector@event@sycl@cl@@AEBA?AV?$vector@_KV?$allocator@_K@std@@@std@@XZ
21652166
?getOSMemSize@OSUtil@detail@sycl@cl@@SA_KXZ
21662167
?getOSModuleHandle@OSUtil@detail@sycl@cl@@SA_JPEBX@Z
21672168
?getOrCreateSampler@sampler_impl@detail@sycl@cl@@QEAAPEAU_pi_sampler@@AEBVcontext@34@@Z
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clangxx -fsycl -fsyntax-only -Xclang -verify -DSYCL2020_CONFORMANT_APIS %s
2+
// expected-no-diagnostics
3+
//
4+
//===----------------------------------------------------------------------===//
5+
// This test checks that sycl::get_native<sycl::backend::opencl>(event) return
6+
// std::vector<cl_event> when backend = opencl, according to:
7+
// SYCL™ 2020 Specification (revision 3)
8+
//===----------------------------------------------------------------------===//
9+
10+
#include <sycl/sycl.hpp>
11+
12+
int main() {
13+
#ifdef SYCL_BACKEND_OPENCL
14+
sycl::queue Queue;
15+
if (Queue.get_backend() == sycl::backend::opencl) {
16+
sycl::event event = Queue.submit([&](sycl::handler &cgh) {
17+
cgh.single_task<class event_kernel>([]() {});
18+
});
19+
std::vector<cl_event> interopEventVec =
20+
sycl::get_native<sycl::backend::opencl>(event);
21+
}
22+
#endif
23+
}

0 commit comments

Comments
 (0)