Skip to content

[SYCL] Change return of get_native for opencl backend to vector #4952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions sycl/doc/PreprocessorMacros.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ This file describes macros that have effect on SYCL compiler and run-time.
support do not impose any extra overhead. One can check to see if a device has
native support for `assert()` via `aspect::ext_oneapi_native_assert`.

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

## Version macros

- `__LIBSYCL_MAJOR_VERSION` is set to SYCL runtime library major version.
Expand Down
38 changes: 37 additions & 1 deletion sycl/include/CL/sycl/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,46 @@ auto get_native(const SyclObjectT &Obj)
if (Obj.get_backend() != BackendName) {
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
}

return Obj.template get_native<BackendName>();
}

// define SYCL2020_CONFORMANT_APIS to correspond SYCL 2020 spec and return
// vector<cl_event> from get_native instead of just cl_event
#ifdef SYCL2020_CONFORMANT_APIS
template <>
inline backend_return_t<backend::opencl, event>
get_native<backend::opencl, event>(const event &Obj) {
// TODO use SYCL 2020 exception when implemented
if (Obj.get_backend() != backend::opencl) {
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
}
backend_return_t<backend::opencl, event> ReturnValue;
for (auto const &element : Obj.getNativeVector()) {
ReturnValue.push_back(
reinterpret_cast<
typename detail::interop<backend::opencl, event>::value_type>(
element));
}
return ReturnValue;
}
#else
// Specialization for cl_event with deprecation message
template <>
__SYCL_DEPRECATED(
"get_native<backend::opencl, event>, which return type is "
"cl_event is deprecated. According to SYCL 2020 spec, please define "
"SYCL2020_CONFORMANT_APIS and use vector<cl_event> instead.")
inline backend_return_t<backend::opencl, event> get_native<
backend::opencl, event>(const event &Obj) {
// TODO use SYCL 2020 exception when implemented
if (Obj.get_backend() != backend::opencl) {
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
}
return reinterpret_cast<
typename detail::interop<backend::opencl, event>::type>(Obj.getNative());
}
#endif

// Native handle of an accessor should be accessed through interop_handler
template <backend BackendName, typename DataT, int Dimensions,
access::mode AccessMode, access::target AccessTarget,
Expand Down
19 changes: 16 additions & 3 deletions sycl/include/CL/sycl/detail/backend_traits_opencl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ template <> struct interop<backend::opencl, device> {
using type = cl_device_id;
};

template <> struct interop<backend::opencl, event> { using type = cl_event; };

template <> struct interop<backend::opencl, queue> {
using type = cl_command_queue;
};
Expand Down Expand Up @@ -106,13 +104,28 @@ template <> struct BackendReturn<backend::opencl, device> {
using type = cl_device_id;
};

#ifdef SYCL2020_CONFORMANT_APIS
template <> struct interop<backend::opencl, event> {
using type = std::vector<cl_event>;
using value_type = cl_event;
};
template <> struct BackendInput<backend::opencl, event> {
using type = std::vector<cl_event>;
using value_type = cl_event;
};
template <> struct BackendReturn<backend::opencl, event> {
using type = std::vector<cl_event>;
using value_type = cl_event;
};
#else
template <> struct interop<backend::opencl, event> { using type = cl_event; };
template <> struct BackendInput<backend::opencl, event> {
using type = cl_event;
};

template <> struct BackendReturn<backend::opencl, event> {
using type = cl_event;
};
#endif

template <> struct BackendInput<backend::opencl, queue> {
using type = cl_command_queue;
Expand Down
6 changes: 6 additions & 0 deletions sycl/include/CL/sycl/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,19 @@ class __SYCL_EXPORT event {

pi_native_handle getNative() const;

std::vector<pi_native_handle> getNativeVector() const;

std::shared_ptr<detail::event_impl> impl;

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

template <class T>
friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);

template <backend BackendName, class SyclObjectT>
friend auto get_native(const SyclObjectT &Obj)
-> backend_return_t<BackendName, SyclObjectT>;
};

} // namespace sycl
Expand Down
6 changes: 6 additions & 0 deletions sycl/source/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,11 @@ event::event(std::shared_ptr<detail::event_impl> event_impl)
backend event::get_backend() const noexcept { return getImplBackend(impl); }

pi_native_handle event::getNative() const { return impl->getNative(); }

std::vector<pi_native_handle> event::getNativeVector() const {
std::vector<pi_native_handle> ReturnVector = {impl->getNative()};
return ReturnVector;
}

} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
1 change: 1 addition & 0 deletions sycl/test/abi/sycl_symbols_linux.dump
Original file line number Diff line number Diff line change
Expand Up @@ -4055,6 +4055,7 @@ _ZNK2cl4sycl3ext6oneapi15filter_selector13select_deviceEv
_ZNK2cl4sycl3ext6oneapi15filter_selector5resetEv
_ZNK2cl4sycl3ext6oneapi15filter_selectorclERKNS0_6deviceE
_ZNK2cl4sycl5event11get_backendEv
_ZNK2cl4sycl5event15getNativeVectorEv
_ZNK2cl4sycl5event18get_profiling_infoILNS0_4info15event_profilingE4737EEENS3_12param_traitsIS4_XT_EE11return_typeEv
_ZNK2cl4sycl5event18get_profiling_infoILNS0_4info15event_profilingE4738EEENS3_12param_traitsIS4_XT_EE11return_typeEv
_ZNK2cl4sycl5event18get_profiling_infoILNS0_4info15event_profilingE4739EEENS3_12param_traitsIS4_XT_EE11return_typeEv
Expand Down
1 change: 1 addition & 0 deletions sycl/test/abi/sycl_symbols_windows.dump
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,7 @@
?getNativeImpl@kernel@sycl@cl@@AEBA_KXZ
?getNativeMem@interop_handle@sycl@cl@@AEBA_KPEAVAccessorImplHost@detail@23@@Z
?getNativeQueue@interop_handle@sycl@cl@@AEBA_KXZ
?getNativeVector@event@sycl@cl@@AEBA?AV?$vector@_KV?$allocator@_K@std@@@std@@XZ
?getOSMemSize@OSUtil@detail@sycl@cl@@SA_KXZ
?getOSModuleHandle@OSUtil@detail@sycl@cl@@SA_JPEBX@Z
?getOrCreateSampler@sampler_impl@detail@sycl@cl@@QEAAPEAU_pi_sampler@@AEBVcontext@34@@Z
Expand Down
23 changes: 23 additions & 0 deletions sycl/test/regression/check_vector_of_opencl_event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %clangxx -fsycl -fsyntax-only -Xclang -verify -DSYCL2020_CONFORMANT_APIS %s
// expected-no-diagnostics
//
//===----------------------------------------------------------------------===//
// This test checks that sycl::get_native<sycl::backend::opencl>(event) return
// std::vector<cl_event> when backend = opencl, according to:
// SYCL™ 2020 Specification (revision 3)
//===----------------------------------------------------------------------===//

#include <sycl/sycl.hpp>

int main() {
#ifdef SYCL_BACKEND_OPENCL
sycl::queue Queue;
if (Queue.get_backend() == sycl::backend::opencl) {
sycl::event event = Queue.submit([&](sycl::handler &cgh) {
cgh.single_task<class event_kernel>([]() {});
});
std::vector<cl_event> interopEventVec =
sycl::get_native<sycl::backend::opencl>(event);
}
#endif
}