Skip to content

Commit ee7e99f

Browse files
authored
[SYCL] Fix get_native() and add get_backend() accordingly to the specification (#3236)
Signed-off-by: mdimakov <[email protected]>
1 parent 733d5e3 commit ee7e99f

25 files changed

+232
-14
lines changed

sycl/include/CL/sycl/backend.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010

1111
#include <CL/sycl/accessor.hpp>
1212
#include <CL/sycl/backend_types.hpp>
13+
#include <CL/sycl/exception.hpp>
1314

1415
__SYCL_INLINE_NAMESPACE(cl) {
1516
namespace sycl {
1617

1718
template <backend BackendName, class SyclObjectT>
1819
auto get_native(const SyclObjectT &Obj) ->
1920
typename interop<BackendName, SyclObjectT>::type {
21+
// TODO use SYCL 2020 exception when implemented
22+
if (Obj.get_backend() != BackendName)
23+
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
2024
return Obj.template get_native<BackendName>();
2125
}
2226

sycl/include/CL/sycl/backend/opencl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ template <> struct interop<backend::opencl, program> {
3636
using type = cl_program;
3737
};
3838

39+
template <> struct interop<backend::opencl, event> { using type = cl_event; };
40+
3941
template <typename DataT, int Dimensions, access::mode AccessMode>
4042
struct interop<backend::opencl, accessor<DataT, Dimensions, AccessMode,
4143
access::target::global_buffer,

sycl/include/CL/sycl/context.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ class __SYCL_EXPORT context {
194194
/// \return true if this context is a SYCL host context.
195195
bool is_host() const;
196196

197+
/// Returns the backend associated with this context.
198+
///
199+
/// \return the backend associated with this context.
200+
backend get_backend() const noexcept;
201+
197202
/// Gets platform associated with this SYCL context.
198203
///
199204
/// \return a valid instance of SYCL platform.

sycl/include/CL/sycl/device.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ class __SYCL_EXPORT device {
172172
static vector_class<device>
173173
get_devices(info::device_type deviceType = info::device_type::all);
174174

175+
/// Returns the backend associated with this device.
176+
///
177+
/// \return the backend associated with this device.
178+
backend get_backend() const noexcept;
179+
175180
/// Gets the native handle of the SYCL device.
176181
///
177182
/// \return a native handle, the type of which defined by the backend.

sycl/include/CL/sycl/event.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,18 @@ class __SYCL_EXPORT event {
118118
typename info::param_traits<info::event_profiling, param>::return_type
119119
get_profiling_info() const;
120120

121+
/// Returns the backend associated with this platform.
122+
///
123+
/// \return the backend associated with this platform
124+
backend get_backend() const noexcept;
125+
121126
/// Gets the native handle of the SYCL event.
122127
///
123128
/// \return a native handle, the type of which defined by the backend.
124129
template <backend BackendName>
125130
auto get_native() const -> typename interop<BackendName, event>::type {
126-
return static_cast<typename interop<BackendName, event>::type>(getNative());
131+
return reinterpret_cast<typename interop<BackendName, event>::type>(
132+
getNative());
127133
}
128134

129135
private:

sycl/include/CL/sycl/program.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ class __SYCL_EXPORT program {
356356
#endif // __SYCL_DEVICE_ONLY__
357357
}
358358

359+
/// Returns the backend associated with this program.
360+
///
361+
/// \return the backend associated with this program.
362+
backend get_backend() const noexcept;
363+
359364
/// Gets the native handle of the SYCL platform.
360365
///
361366
/// \return a native handle, the type of which defined by the backend.

sycl/include/CL/sycl/queue.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,11 @@ class __SYCL_EXPORT queue {
750750
/// Equivalent to has_property<property::queue::in_order>()
751751
bool is_in_order() const;
752752

753+
/// Returns the backend associated with this queue.
754+
///
755+
/// \return the backend associated with this queue.
756+
backend get_backend() const noexcept;
757+
753758
/// Gets the native handle of the SYCL queue.
754759
///
755760
/// \return a native handle, the type of which defined by the backend.

sycl/source/context.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <CL/sycl/platform.hpp>
1616
#include <CL/sycl/properties/all_properties.hpp>
1717
#include <CL/sycl/stl.hpp>
18+
#include <detail/backend_impl.hpp>
1819
#include <detail/context_impl.hpp>
1920

2021
#include <algorithm>
@@ -117,6 +118,8 @@ cl_context context::get() const { return impl->get(); }
117118

118119
bool context::is_host() const { return impl->is_host(); }
119120

121+
backend context::get_backend() const noexcept { return getImplBackend(impl); }
122+
120123
platform context::get_platform() const {
121124
return impl->get_info<info::context::platform>();
122125
}

sycl/source/detail/backend_impl.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//==------------------ backend_impl.hpp - get impls backend
2+
//-------------------------==//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#pragma once
11+
#include <CL/sycl/backend_types.hpp>
12+
13+
__SYCL_INLINE_NAMESPACE(cl) {
14+
namespace sycl {
15+
namespace detail {
16+
17+
template <class T> backend getImplBackend(const T &Impl) {
18+
backend Result;
19+
if (Impl->is_host())
20+
Result = backend::host;
21+
else
22+
Result = Impl->getPlugin().getBackend();
23+
24+
return Result;
25+
}
26+
27+
} // namespace detail
28+
} // namespace sycl
29+
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/source/detail/context_impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ bool context_impl::hasDevice(
169169

170170
pi_native_handle context_impl::getNative() const {
171171
auto Plugin = getPlugin();
172+
if (Plugin.getBackend() == backend::opencl)
173+
Plugin.call<PiApiKind::piContextRetain>(getHandleRef());
172174
pi_native_handle Handle;
173175
Plugin.call<PiApiKind::piextContextGetNativeHandle>(getHandleRef(), &Handle);
174176
return Handle;

sycl/source/detail/device_impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ vector_class<device> device_impl::create_sub_devices(
209209

210210
pi_native_handle device_impl::getNative() const {
211211
auto Plugin = getPlugin();
212+
if (Plugin.getBackend() == backend::opencl)
213+
Plugin.call<PiApiKind::piDeviceRetain>(getHandleRef());
212214
pi_native_handle Handle;
213215
Plugin.call<PiApiKind::piextDeviceGetNativeHandle>(getHandleRef(), &Handle);
214216
return Handle;

sycl/source/detail/event_impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ void HostProfilingInfo::end() { EndTime = getTimestamp(); }
292292

293293
pi_native_handle event_impl::getNative() const {
294294
auto Plugin = getPlugin();
295+
if (Plugin.getBackend() == backend::opencl)
296+
Plugin.call<PiApiKind::piEventRetain>(getHandleRef());
295297
pi_native_handle Handle;
296298
Plugin.call<PiApiKind::piextEventGetNativeHandle>(getHandleRef(), &Handle);
297299
return Handle;

sycl/source/detail/event_impl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <CL/sycl/detail/pi.hpp>
1414
#include <CL/sycl/info/info_desc.hpp>
1515
#include <CL/sycl/stl.hpp>
16+
#include <detail/plugin.hpp>
1617

1718
#include <atomic>
1819
#include <cassert>

sycl/source/detail/platform_impl.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,6 @@ class platform_impl {
108108
/// \return a vector of all available SYCL platforms.
109109
static vector_class<platform> get_platforms();
110110

111-
// \return the Backend associated with this platform.
112-
backend get_backend() const noexcept {
113-
backend Result;
114-
if (is_host())
115-
Result = backend::host;
116-
else {
117-
Result = getPlugin().getBackend();
118-
}
119-
120-
return Result;
121-
}
122-
123111
// \return the Plugin associated with this platform.
124112
const plugin &getPlugin() const {
125113
assert(!MHostPlatform && "Plugin is not available for Host.");

sycl/source/detail/program_impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ void program_impl::flush_spec_constants(const RTDeviceBinaryImage &Img,
583583

584584
pi_native_handle program_impl::getNative() const {
585585
const auto &Plugin = getPlugin();
586+
if (Plugin.getBackend() == backend::opencl)
587+
Plugin.call<PiApiKind::piProgramRetain>(MProgram);
586588
pi_native_handle Handle;
587589
Plugin.call<PiApiKind::piextProgramGetNativeHandle>(MProgram, &Handle);
588590
return Handle;

sycl/source/detail/queue_impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ void queue_impl::initHostTaskAndEventCallbackThreadPool() {
273273

274274
pi_native_handle queue_impl::getNative() const {
275275
const detail::plugin &Plugin = getPlugin();
276+
if (Plugin.getBackend() == backend::opencl)
277+
Plugin.call<PiApiKind::piQueueRetain>(MQueues[0]);
276278
pi_native_handle Handle{};
277279
Plugin.call<PiApiKind::piextQueueGetNativeHandle>(MQueues[0], &Handle);
278280
return Handle;

sycl/source/device.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <CL/sycl/device.hpp>
1111
#include <CL/sycl/device_selector.hpp>
1212
#include <CL/sycl/info/info_desc.hpp>
13+
#include <detail/backend_impl.hpp>
1314
#include <detail/config.hpp>
1415
#include <detail/device_impl.hpp>
1516
#include <detail/force_device.hpp>
@@ -136,6 +137,8 @@ device::get_info() const {
136137

137138
#undef __SYCL_PARAM_TRAITS_SPEC
138139

140+
backend device::get_backend() const noexcept { return getImplBackend(impl); }
141+
139142
pi_native_handle device::getNative() const { return impl->getNative(); }
140143

141144
bool device::has(aspect Aspect) const { return impl->has(Aspect); }

sycl/source/event.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <CL/sycl/event.hpp>
1313
#include <CL/sycl/info/info_desc.hpp>
1414
#include <CL/sycl/stl.hpp>
15+
#include <detail/backend_impl.hpp>
1516
#include <detail/event_impl.hpp>
1617
#include <detail/scheduler/scheduler.hpp>
1718

@@ -85,6 +86,8 @@ event::event(shared_ptr_class<detail::event_impl> event_impl)
8586

8687
#undef __SYCL_PARAM_TRAITS_SPEC
8788

89+
backend event::get_backend() const noexcept { return getImplBackend(impl); }
90+
8891
pi_native_handle event::getNative() const { return impl->getNative(); }
8992

9093
} // namespace sycl

sycl/source/platform.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <CL/sycl/device_selector.hpp>
1111
#include <CL/sycl/info/info_desc.hpp>
1212
#include <CL/sycl/platform.hpp>
13+
#include <detail/backend_impl.hpp>
1314
#include <detail/force_device.hpp>
1415
#include <detail/platform_impl.hpp>
1516

@@ -43,7 +44,7 @@ vector_class<platform> platform::get_platforms() {
4344
return detail::platform_impl::get_platforms();
4445
}
4546

46-
backend platform::get_backend() const noexcept { return impl->get_backend(); }
47+
backend platform::get_backend() const noexcept { return getImplBackend(impl); }
4748

4849
template <info::platform param>
4950
typename info::param_traits<info::platform, param>::return_type

sycl/source/program.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <CL/sycl/program.hpp>
1010
#include <CL/sycl/properties/all_properties.hpp>
1111
#include <CL/sycl/property_list.hpp>
12+
#include <detail/backend_impl.hpp>
1213
#include <detail/program_impl.hpp>
1314

1415
#include <vector>
@@ -47,6 +48,8 @@ program::program(const context &context, cl_program clProgram)
4748
clRetainProgram(clProgram);
4849
}
4950

51+
backend program::get_backend() const noexcept { return getImplBackend(impl); }
52+
5053
pi_native_handle program::getNative() const { return impl->getNative(); }
5154

5255
program::program(std::shared_ptr<detail::program_impl> impl) : impl(impl) {}

sycl/source/queue.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <CL/sycl/handler.hpp>
1212
#include <CL/sycl/queue.hpp>
1313
#include <CL/sycl/stl.hpp>
14+
#include <detail/backend_impl.hpp>
1415
#include <detail/queue_impl.hpp>
1516

1617
#include <algorithm>
@@ -138,6 +139,8 @@ bool queue::is_in_order() const {
138139
return impl->has_property<property::queue::in_order>();
139140
}
140141

142+
backend queue::get_backend() const noexcept { return getImplBackend(impl); }
143+
141144
pi_native_handle queue::getNative() const { return impl->getNative(); }
142145

143146
} // namespace sycl

sycl/test/abi/sycl_symbols_linux.dump

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3897,6 +3897,7 @@ _ZNK2cl4sycl15interop_handler12GetNativeMemEPNS0_6detail16AccessorImplHostE
38973897
_ZNK2cl4sycl15interop_handler14GetNativeQueueEv
38983898
_ZNK2cl4sycl16default_selectorclERKNS0_6deviceE
38993899
_ZNK2cl4sycl20accelerator_selectorclERKNS0_6deviceE
3900+
_ZNK2cl4sycl5event11get_backendEv
39003901
_ZNK2cl4sycl5event18get_profiling_infoILNS0_4info15event_profilingE4737EEENS3_12param_traitsIS4_XT_EE11return_typeEv
39013902
_ZNK2cl4sycl5event18get_profiling_infoILNS0_4info15event_profilingE4738EEENS3_12param_traitsIS4_XT_EE11return_typeEv
39023903
_ZNK2cl4sycl5event18get_profiling_infoILNS0_4info15event_profilingE4739EEENS3_12param_traitsIS4_XT_EE11return_typeEv
@@ -3908,6 +3909,7 @@ _ZNK2cl4sycl5event9getNativeEv
39083909
_ZNK2cl4sycl5eventeqERKS1_
39093910
_ZNK2cl4sycl5eventneERKS1_
39103911
_ZNK2cl4sycl5queue10get_deviceEv
3912+
_ZNK2cl4sycl5queue11get_backendEv
39113913
_ZNK2cl4sycl5queue11get_contextEv
39123914
_ZNK2cl4sycl5queue11is_in_orderEv
39133915
_ZNK2cl4sycl5queue12get_propertyINS0_8property5queue16enable_profilingEEET_v
@@ -3968,6 +3970,7 @@ _ZNK2cl4sycl6detail19kernel_bundle_plain37get_specialization_constant_raw_valueE
39683970
_ZNK2cl4sycl6detail19kernel_bundle_plain3endEv
39693971
_ZNK2cl4sycl6detail19kernel_bundle_plain5beginEv
39703972
_ZNK2cl4sycl6detail19kernel_bundle_plain5emptyEv
3973+
_ZNK2cl4sycl6device11get_backendEv
39713974
_ZNK2cl4sycl6device12get_platformEv
39723975
_ZNK2cl4sycl6device13has_extensionERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
39733976
_ZNK2cl4sycl6device14is_acceleratorEv
@@ -4103,6 +4106,7 @@ _ZNK2cl4sycl6stream22get_max_statement_sizeEv
41034106
_ZNK2cl4sycl6stream8get_sizeEv
41044107
_ZNK2cl4sycl6streameqERKS1_
41054108
_ZNK2cl4sycl6streamneERKS1_
4109+
_ZNK2cl4sycl7context11get_backendEv
41064110
_ZNK2cl4sycl7context11get_devicesEv
41074111
_ZNK2cl4sycl7context12get_platformEv
41084112
_ZNK2cl4sycl7context12get_propertyINS0_3ext6oneapi8property6buffer22use_pinned_host_memoryEEET_v
@@ -4137,6 +4141,7 @@ _ZNK2cl4sycl7program10get_kernelENSt7__cxx1112basic_stringIcSt11char_traitsIcESa
41374141
_ZNK2cl4sycl7program10get_kernelENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEb
41384142
_ZNK2cl4sycl7program10has_kernelENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
41394143
_ZNK2cl4sycl7program10has_kernelENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEb
4144+
_ZNK2cl4sycl7program11get_backendEv
41404145
_ZNK2cl4sycl7program11get_contextEv
41414146
_ZNK2cl4sycl7program11get_devicesEv
41424147
_ZNK2cl4sycl7program12get_binariesEv

sycl/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ endforeach()
99

1010
include(AddSYCLUnitTest)
1111

12+
add_subdirectory(get_native_interop)
1213
add_subdirectory(misc)
1314
add_subdirectory(pi)
1415
add_subdirectory(kernel-and-program)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_sycl_unittest_with_device(GetNativeTests OBJECT
2+
test_get_native.cpp
3+
)

0 commit comments

Comments
 (0)