Skip to content

Commit b9aa33e

Browse files
authored
[SYCL] Implement get_backend_info() (#12906)
Implementing the get_backend_info() functions for our SYCL implementation based on SYCL 2020 spec. (Link here: https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html you may search for "get_backend_info()" there for the spec for these functions) There're six groups of variations for this function, namely `sycl::platform::get_backend_info()`, `sycl::context::get_backend_info()`, `sycl::device::get_backend_info()`, `sycl::queue::get_backend_info()`, `sycl::eventv::get_backend_info()`, and `sycl::kernel::get_backend_info()` One known concern: it seems that sycl::platform, sycl::context and sycl::kernel may have multiple associated device, but according to the spec the return type for `sycl::xxx::get_backend_info<info::device::version>()` should be std::string (i.e. a single device version) so I'm just returning the version of the first associated device in the list. Is this OK? --------- Signed-off-by: Hu, Peisen <[email protected]>
1 parent d4045be commit b9aa33e

29 files changed

+572
-0
lines changed

sycl/include/sycl/context.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ class __SYCL_EXPORT context : public detail::OwnerLessBase<context> {
175175
template <typename Param>
176176
typename detail::is_context_info_desc<Param>::return_type get_info() const;
177177

178+
/// Queries this SYCL context for SYCL backend-specific information.
179+
///
180+
/// The return type depends on information being queried.
181+
template <typename Param>
182+
typename detail::is_backend_info_desc<Param>::return_type
183+
get_backend_info() const;
184+
178185
context(const context &rhs) = default;
179186

180187
context(context &&rhs) = default;

sycl/include/sycl/detail/info_desc_helpers.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ template <typename T> struct is_event_profiling_info_desc : std::false_type {};
3939
// workaround, we use return_type alias from is_*info_desc that doesn't run into
4040
// the same problem.
4141
// TODO remove once this gcc/clang discrepancy is resolved
42+
43+
template <typename T> struct is_backend_info_desc : std::false_type {};
44+
// Similar approach to limit valid get_backend_info template argument
45+
4246
#define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, PiCode) \
4347
template <> struct PiInfoCode<info::DescType::Desc> { \
4448
static constexpr pi_##DescType##_info value = PiCode; \
@@ -124,6 +128,13 @@ struct IsSubGroupInfo<info::kernel_device_specific::compile_sub_group_size>
124128
#include <sycl/info/ext_intel_device_traits.def>
125129
#include <sycl/info/ext_oneapi_device_traits.def>
126130
#undef __SYCL_PARAM_TRAITS_SPEC
131+
#define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, PiCode) \
132+
template <> \
133+
struct is_backend_info_desc<info::DescType::Desc> : std::true_type { \
134+
using return_type = info::DescType::Desc::return_type; \
135+
};
136+
#include <sycl/info/sycl_backend_traits.def>
137+
#undef __SYCL_PARAM_TRAITS_SPEC
127138

128139
} // namespace detail
129140
} // namespace _V1

sycl/include/sycl/device.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ class __SYCL_EXPORT device : public detail::OwnerLessBase<device> {
233233
get_info() const;
234234
#endif
235235

236+
/// Queries this SYCL device for SYCL backend-specific information.
237+
///
238+
/// The return type depends on information being queried.
239+
template <typename Param>
240+
typename detail::is_backend_info_desc<Param>::return_type
241+
get_backend_info() const;
242+
236243
/// Check SYCL extension support by device
237244
///
238245
/// \param extension_name is a name of queried extension.

sycl/include/sycl/event.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ class __SYCL_EXPORT event : public detail::OwnerLessBase<event> {
115115
template <typename Param>
116116
typename detail::is_event_info_desc<Param>::return_type get_info() const;
117117

118+
/// Queries this SYCL event for SYCL backend-specific information.
119+
///
120+
/// \return depends on information being queried.
121+
template <typename Param>
122+
typename detail::is_backend_info_desc<Param>::return_type
123+
get_backend_info() const;
124+
118125
/// Queries this SYCL event for profiling information.
119126
///
120127
/// If the requested info is not available when this member function is called
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__SYCL_PARAM_TRAITS_SPEC(platform, version, std::string, PI_PLATFORM_INFO_VERSION)
2+
__SYCL_PARAM_TRAITS_SPEC(device, version, std::string, PI_DEVICE_INFO_VERSION)
3+
__SYCL_PARAM_TRAITS_SPEC(device, backend_version, std::string, PI_DEVICE_INFO_BACKEND_VERSION)

sycl/include/sycl/kernel.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ class __SYCL_EXPORT kernel : public detail::OwnerLessBase<kernel> {
150150
typename detail::is_kernel_info_desc<Param>::return_type get_info() const;
151151
#endif
152152

153+
/// Queries the kernel object for SYCL backend-specific information.
154+
///
155+
/// The return type depends on information being queried.
156+
template <typename Param>
157+
typename detail::is_backend_info_desc<Param>::return_type
158+
get_backend_info() const;
159+
153160
/// Query device-specific information from the kernel object using the
154161
/// info::kernel_device_specific descriptor.
155162
///

sycl/include/sycl/platform.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ class __SYCL_EXPORT platform : public detail::OwnerLessBase<platform> {
201201
typename detail::is_platform_info_desc<Param>::return_type>
202202
get_info() const;
203203
#endif
204+
205+
/// Queries this SYCL platform for SYCL backend-specific info.
206+
///
207+
/// The return type depends on information being queried.
208+
template <typename Param>
209+
typename detail::is_backend_info_desc<Param>::return_type
210+
get_backend_info() const;
211+
204212
/// Returns all available SYCL platforms in the system.
205213
///
206214
/// The resulting vector always contains a single SYCL host platform instance.

sycl/include/sycl/queue.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,13 @@ class __SYCL_EXPORT queue : public detail::OwnerLessBase<queue> {
331331
template <typename Param>
332332
typename detail::is_queue_info_desc<Param>::return_type get_info() const;
333333

334+
/// Queries SYCL queue for SYCL backend-specific information.
335+
///
336+
/// The return type depends on information being queried.
337+
template <typename Param>
338+
typename detail::is_backend_info_desc<Param>::return_type
339+
get_backend_info() const;
340+
334341
private:
335342
// A shorthand for `get_device().has()' which is expected to be a bit quicker
336343
// than the long version

sycl/source/context.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ context::get_info() const {
103103

104104
#undef __SYCL_PARAM_TRAITS_SPEC
105105

106+
template <typename Param>
107+
typename detail::is_backend_info_desc<Param>::return_type
108+
context::get_backend_info() const {
109+
return impl->get_backend_info<Param>();
110+
}
111+
112+
#define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, Picode) \
113+
template __SYCL_EXPORT ReturnT \
114+
context::get_backend_info<info::DescType::Desc>() const;
115+
116+
#include <sycl/info/sycl_backend_traits.def>
117+
118+
#undef __SYCL_PARAM_TRAITS_SPEC
119+
106120
#define __SYCL_PARAM_TRAITS_SPEC(param_type) \
107121
template <> \
108122
__SYCL_EXPORT bool context::has_property<param_type>() const noexcept { \

sycl/source/detail/context_impl.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,51 @@ context_impl::get_info<info::context::atomic_fence_scope_capabilities>() const {
254254
return CapabilityList;
255255
}
256256

257+
template <>
258+
typename info::platform::version::return_type
259+
context_impl::get_backend_info<info::platform::version>() const {
260+
if (getBackend() != backend::opencl) {
261+
throw sycl::exception(errc::backend_mismatch,
262+
"the info::platform::version info descriptor can "
263+
"only be queried with an OpenCL backend");
264+
}
265+
return MDevices[0].get_platform().get_info<info::platform::version>();
266+
}
267+
268+
device select_device(DSelectorInvocableType DeviceSelectorInvocable,
269+
std::vector<device> &Devices);
270+
271+
template <>
272+
typename info::device::version::return_type
273+
context_impl::get_backend_info<info::device::version>() const {
274+
if (getBackend() != backend::opencl) {
275+
throw sycl::exception(errc::backend_mismatch,
276+
"the info::device::version info descriptor can only "
277+
"be queried with an OpenCL backend");
278+
}
279+
auto Devices = get_info<info::context::devices>();
280+
if (Devices.empty()) {
281+
return "No available device";
282+
}
283+
// Use default selector to pick a device.
284+
return select_device(default_selector_v, Devices)
285+
.get_info<info::device::version>();
286+
}
287+
288+
template <>
289+
typename info::device::backend_version::return_type
290+
context_impl::get_backend_info<info::device::backend_version>() const {
291+
if (getBackend() != backend::ext_oneapi_level_zero) {
292+
throw sycl::exception(errc::backend_mismatch,
293+
"the info::device::backend_version info descriptor "
294+
"can only be queried with a Level Zero backend");
295+
}
296+
return "";
297+
// Currently The Level Zero backend does not define the value of this
298+
// information descriptor and implementations are encouraged to return the
299+
// empty string as per specification.
300+
}
301+
257302
sycl::detail::pi::PiContext &context_impl::getHandleRef() { return MContext; }
258303
const sycl::detail::pi::PiContext &context_impl::getHandleRef() const {
259304
return MContext;

sycl/source/detail/context_impl.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ class context_impl {
118118
/// The return type depends on information being queried.
119119
template <typename Param> typename Param::return_type get_info() const;
120120

121+
/// Queries SYCL queue for SYCL backend-specific information.
122+
///
123+
/// The return type depends on information being queried.
124+
template <typename Param>
125+
typename Param::return_type get_backend_info() const;
126+
121127
/// Gets the underlying context object (if any) without reference count
122128
/// modification.
123129
///

sycl/source/detail/device_impl.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,42 @@ typename Param::return_type device_impl::get_info() const {
143143
#include <sycl/info/ext_oneapi_device_traits.def>
144144
#undef __SYCL_PARAM_TRAITS_SPEC
145145

146+
template <>
147+
typename info::platform::version::return_type
148+
device_impl::get_backend_info<info::platform::version>() const {
149+
if (getBackend() != backend::opencl) {
150+
throw sycl::exception(errc::backend_mismatch,
151+
"the info::platform::version info descriptor can "
152+
"only be queried with an OpenCL backend");
153+
}
154+
return get_platform().get_info<info::platform::version>();
155+
}
156+
157+
template <>
158+
typename info::device::version::return_type
159+
device_impl::get_backend_info<info::device::version>() const {
160+
if (getBackend() != backend::opencl) {
161+
throw sycl::exception(errc::backend_mismatch,
162+
"the info::device::version info descriptor can only "
163+
"be queried with an OpenCL backend");
164+
}
165+
return get_info<info::device::version>();
166+
}
167+
168+
template <>
169+
typename info::device::backend_version::return_type
170+
device_impl::get_backend_info<info::device::backend_version>() const {
171+
if (getBackend() != backend::ext_oneapi_level_zero) {
172+
throw sycl::exception(errc::backend_mismatch,
173+
"the info::device::backend_version info descriptor "
174+
"can only be queried with a Level Zero backend");
175+
}
176+
return "";
177+
// Currently The Level Zero backend does not define the value of this
178+
// information descriptor and implementations are encouraged to return the
179+
// empty string as per specification.
180+
}
181+
146182
bool device_impl::has_extension(const std::string &ExtensionName) const {
147183
if (MIsHostDevice)
148184
// TODO: implement extension management for host device;

sycl/source/detail/device_impl.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ class device_impl {
201201
/// \return device info of type described in Table 4.20.
202202
template <typename Param> typename Param::return_type get_info() const;
203203

204+
/// Queries SYCL queue for SYCL backend-specific information.
205+
///
206+
/// The return type depends on information being queried.
207+
template <typename Param>
208+
typename Param::return_type get_backend_info() const;
209+
204210
/// Check if affinity partitioning by specified domain is supported by
205211
/// device
206212
///

sycl/source/detail/event_impl.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,61 @@ event_impl::get_info<info::event::command_execution_status>() {
421421
: info::event_command_status::complete;
422422
}
423423

424+
template <>
425+
typename info::platform::version::return_type
426+
event_impl::get_backend_info<info::platform::version>() const {
427+
if (!MIsContextInitialized) {
428+
return "Context not initialized, no backend info available";
429+
}
430+
if (MContext->getBackend() != backend::opencl) {
431+
throw sycl::exception(errc::backend_mismatch,
432+
"the info::platform::version info descriptor can "
433+
"only be queried with an OpenCL backend");
434+
}
435+
if (QueueImplPtr Queue = MQueue.lock()) {
436+
return Queue->getDeviceImplPtr()
437+
->get_platform()
438+
.get_info<info::platform::version>();
439+
}
440+
return ""; // If the queue has been released, no platform will be associated
441+
// so return empty string
442+
}
443+
444+
template <>
445+
typename info::device::version::return_type
446+
event_impl::get_backend_info<info::device::version>() const {
447+
if (!MIsContextInitialized) {
448+
return "Context not initialized, no backend info available";
449+
}
450+
if (MContext->getBackend() != backend::opencl) {
451+
throw sycl::exception(errc::backend_mismatch,
452+
"the info::device::version info descriptor can only "
453+
"be queried with an OpenCL backend");
454+
}
455+
if (QueueImplPtr Queue = MQueue.lock()) {
456+
return Queue->getDeviceImplPtr()->get_info<info::device::version>();
457+
}
458+
return ""; // If the queue has been released, no device will be associated so
459+
// return empty string
460+
}
461+
462+
template <>
463+
typename info::device::backend_version::return_type
464+
event_impl::get_backend_info<info::device::backend_version>() const {
465+
if (!MIsContextInitialized) {
466+
return "Context not initialized, no backend info available";
467+
}
468+
if (MContext->getBackend() != backend::ext_oneapi_level_zero) {
469+
throw sycl::exception(errc::backend_mismatch,
470+
"the info::device::backend_version info descriptor "
471+
"can only be queried with a Level Zero backend");
472+
}
473+
return "";
474+
// Currently The Level Zero backend does not define the value of this
475+
// information descriptor and implementations are encouraged to return the
476+
// empty string as per specification.
477+
}
478+
424479
void HostProfilingInfo::start() { StartTime = getTimestamp(); }
425480

426481
void HostProfilingInfo::end() { EndTime = getTimestamp(); }

sycl/source/detail/event_impl.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ class event_impl {
110110
/// \return depends on the information being requested.
111111
template <typename Param> typename Param::return_type get_info();
112112

113+
/// Queries this SYCL event for SYCL backend-specific information.
114+
///
115+
/// \return depends on information being queried.
116+
template <typename Param>
117+
typename Param::return_type get_backend_info() const;
118+
113119
~event_impl();
114120

115121
/// Waits for the event with respect to device type.

sycl/source/detail/kernel_impl.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,52 @@ void kernel_impl::checkIfValidForNumArgsInfoQuery() const {
122122
"interoperability function or to query a device built-in kernel");
123123
}
124124

125+
template <>
126+
typename info::platform::version::return_type
127+
kernel_impl::get_backend_info<info::platform::version>() const {
128+
if (MContext->getBackend() != backend::opencl) {
129+
throw sycl::exception(errc::backend_mismatch,
130+
"the info::platform::version info descriptor can "
131+
"only be queried with an OpenCL backend");
132+
}
133+
auto Devices = MKernelBundleImpl->get_devices();
134+
return Devices[0].get_platform().get_info<info::platform::version>();
135+
}
136+
137+
device select_device(DSelectorInvocableType DeviceSelectorInvocable,
138+
std::vector<device> &Devices);
139+
140+
template <>
141+
typename info::device::version::return_type
142+
kernel_impl::get_backend_info<info::device::version>() const {
143+
if (MContext->getBackend() != backend::opencl) {
144+
throw sycl::exception(errc::backend_mismatch,
145+
"the info::device::version info descriptor can only "
146+
"be queried with an OpenCL backend");
147+
}
148+
auto Devices = MKernelBundleImpl->get_devices();
149+
if (Devices.empty()) {
150+
return "No available device";
151+
}
152+
// Use default selector to pick a device.
153+
return select_device(default_selector_v, Devices)
154+
.get_info<info::device::version>();
155+
}
156+
157+
template <>
158+
typename info::device::backend_version::return_type
159+
kernel_impl::get_backend_info<info::device::backend_version>() const {
160+
if (MContext->getBackend() != backend::ext_oneapi_level_zero) {
161+
throw sycl::exception(errc::backend_mismatch,
162+
"the info::device::backend_version info descriptor "
163+
"can only be queried with a Level Zero backend");
164+
}
165+
return "";
166+
// Currently The Level Zero backend does not define the value of this
167+
// information descriptor and implementations are encouraged to return the
168+
// empty string as per specification.
169+
}
170+
125171
} // namespace detail
126172
} // namespace _V1
127173
} // namespace sycl

sycl/source/detail/kernel_impl.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ class kernel_impl {
125125
/// \return depends on information being queried.
126126
template <typename Param> typename Param::return_type get_info() const;
127127

128+
/// Queries the kernel object for SYCL backend-specific information.
129+
///
130+
/// \return depends on information being queried.
131+
template <typename Param>
132+
typename Param::return_type get_backend_info() const;
133+
128134
/// Query device-specific information from a kernel object using the
129135
/// info::kernel_device_specific descriptor.
130136
///

0 commit comments

Comments
 (0)