|
| 1 | += sycl_ext_oneapi_spirv_queries |
| 2 | + |
| 3 | +:source-highlighter: coderay |
| 4 | +:coderay-linenums-mode: table |
| 5 | + |
| 6 | +// This section needs to be after the document title. |
| 7 | +:doctype: book |
| 8 | +:toc2: |
| 9 | +:toc: left |
| 10 | +:encoding: utf-8 |
| 11 | +:lang: en |
| 12 | +:dpcpp: pass:[DPC++] |
| 13 | +:endnote: —{nbsp}end{nbsp}note |
| 14 | + |
| 15 | +// Set the default source code type in this document to C++, |
| 16 | +// for syntax highlighting purposes. This is needed because |
| 17 | +// docbook uses c++ and html5 uses cpp. |
| 18 | +:language: {basebackend@docbook:c++:cpp} |
| 19 | + |
| 20 | + |
| 21 | +== Notice |
| 22 | + |
| 23 | +Copyright (C) 2025 Intel Corporation. All rights reserved. |
| 24 | + |
| 25 | +Khronos(R) is a registered trademark and SYCL(TM) and SPIR(TM) are trademarks |
| 26 | +of The Khronos Group Inc. |
| 27 | +OpenCL(TM) is a trademark of Apple Inc. used by permission by Khronos. |
| 28 | + |
| 29 | + |
| 30 | +== Contact |
| 31 | + |
| 32 | +To report problems with this extension, please open a new issue at: |
| 33 | + |
| 34 | +https://github.com/intel/llvm/issues |
| 35 | + |
| 36 | + |
| 37 | +== Dependencies |
| 38 | + |
| 39 | +This extension is written against the SYCL 2020 revision 10 specification. |
| 40 | +All references below to the "core SYCL specification" or to section numbers in |
| 41 | +the SYCL specification refer to that revision. |
| 42 | + |
| 43 | + |
| 44 | +== Status |
| 45 | + |
| 46 | +This is a proposed extension specification, intended to gather community |
| 47 | +feedback. Interfaces defined in this specification may not be implemented yet |
| 48 | +or may be in a preliminary state. The specification itself may also change in |
| 49 | +incompatible ways before it is finalized. *Shipping software products should |
| 50 | +not rely on APIs defined in this specification.* |
| 51 | + |
| 52 | + |
| 53 | +== Overview |
| 54 | + |
| 55 | +This extension adds new queries that allow an application to identify the |
| 56 | +link:https://www.khronos.org/spirv/[SPIR-V] features that are supported by a |
| 57 | +SYCL device. |
| 58 | +These queries may be useful for applications that use the |
| 59 | +link:../experimental/sycl_ext_oneapi_kernel_compiler_spirv.asciidoc[sycl_ext_oneapi_kernel_compiler_spirv] |
| 60 | +extension to ensure that a SYCL device supports the features required by a |
| 61 | +SPIR-V binary module. |
| 62 | + |
| 63 | + |
| 64 | +== Specification |
| 65 | + |
| 66 | +=== Feature test macro |
| 67 | + |
| 68 | +This extension provides a feature-test macro as described in the core SYCL |
| 69 | +specification. |
| 70 | +An implementation supporting this extension must predefine the macro |
| 71 | +`SYCL_EXT_ONEAPI_SPIRV_QUERIES` |
| 72 | +to one of the values defined in the table below. |
| 73 | +Applications can test for the existence of this macro to determine if the |
| 74 | +implementation supports this feature, or applications can test the macro's |
| 75 | +value to determine which of the extension's features the implementation |
| 76 | +supports. |
| 77 | + |
| 78 | +[%header,cols="1,5"] |
| 79 | +|=== |
| 80 | +|Value |
| 81 | +|Description |
| 82 | + |
| 83 | +|1 |
| 84 | +|The APIs of this experimental extension are not versioned, so the |
| 85 | + feature-test macro always has this value. |
| 86 | +|=== |
| 87 | + |
| 88 | +=== Device aspects |
| 89 | + |
| 90 | +This extension adds the following new device aspects. |
| 91 | + |
| 92 | +|==== |
| 93 | +a| |
| 94 | +[frame=all,grid=none] |
| 95 | +!==== |
| 96 | +a! |
| 97 | +[source,c++] |
| 98 | +---- |
| 99 | +namespace sycl { |
| 100 | +
|
| 101 | +enum class aspect : /* unspecified */ { |
| 102 | + ... |
| 103 | + ext_oneapi_spirv, |
| 104 | + ext_oneapi_spirv_queries |
| 105 | +} |
| 106 | +
|
| 107 | +} // namespace sycl |
| 108 | +---- |
| 109 | +!==== |
| 110 | + |
| 111 | +.*aspect::ext_oneapi_spirv* |
| 112 | +Indicates that the device supports compiling and launching kernels written in |
| 113 | +SPIR-V. |
| 114 | + |
| 115 | +.*aspect::ext_oneapi_spirv_queries* |
| 116 | +Indicates that the device supports queries for supported SPIR-V extended |
| 117 | +instruction sets, extensions, and capabilities. |
| 118 | +This aspect will always be supported if the device does not have the |
| 119 | +`aspect::ext_oneapi_spirv` aspect. |
| 120 | +In this case, the queries will indicate that the device does not support any |
| 121 | +SPIR-V extended instruction sets, extensions, or capabilities, because the |
| 122 | +device does not support SPIR-V at all. |
| 123 | +|==== |
| 124 | + |
| 125 | +=== Queries |
| 126 | + |
| 127 | +==== New SPIR-V version type |
| 128 | + |
| 129 | +This extension adds the following type and constant definitions to |
| 130 | +identify a SPIR-V version. |
| 131 | + |
| 132 | +|==== |
| 133 | +a| |
| 134 | +[frame=all,grid=none] |
| 135 | +!==== |
| 136 | +a! |
| 137 | +[source,c++] |
| 138 | +---- |
| 139 | +namespace sycl::ext::oneapi::experimental { |
| 140 | +
|
| 141 | +struct spirv_version { |
| 142 | + unsigned major : 16; |
| 143 | + unsigned minor : 16; |
| 144 | +}; |
| 145 | +
|
| 146 | +inline constexpr spirv_version spirv_1_0 = {1,0}; |
| 147 | +inline constexpr spirv_version spirv_1_1 = {1,1}; |
| 148 | +inline constexpr spirv_version spirv_1_2 = {1,2}; |
| 149 | +inline constexpr spirv_version spirv_1_3 = {1,3}; |
| 150 | +inline constexpr spirv_version spirv_1_4 = {1,4}; |
| 151 | +inline constexpr spirv_version spirv_1_5 = {1,5}; |
| 152 | +inline constexpr spirv_version spirv_1_6 = {1,6}; |
| 153 | +
|
| 154 | +} // namespace ext::oneapi::experimental |
| 155 | +---- |
| 156 | +!==== |
| 157 | + |
| 158 | +The meaning of the `major` and `minor` values are defined by the SPIR-V |
| 159 | +specification. |
| 160 | + |
| 161 | +The constant values (e.g. `spirv_1_0`) are shorthands that identify various |
| 162 | +SPIR-V versions. |
| 163 | +|==== |
| 164 | + |
| 165 | +==== New device information descriptors |
| 166 | + |
| 167 | +This extension adds the following new information descriptors to |
| 168 | +`device::get_info`. |
| 169 | + |
| 170 | +|==== |
| 171 | +a| |
| 172 | +[frame=all,grid=none] |
| 173 | +!==== |
| 174 | +a! |
| 175 | +[source] |
| 176 | +---- |
| 177 | +namespace sycl::ext::oneapi::experimental::info::device { |
| 178 | +struct spirv_versions { |
| 179 | + using return_type = std::vector<sycl::ext::oneapi::experimental::spirv_version>; |
| 180 | +}; |
| 181 | +} // namespace sycl::ext::oneapi::experimental::info::device |
| 182 | +---- |
| 183 | +!==== |
| 184 | + |
| 185 | +_Remarks:_ Template parameter to `device::get_info`. |
| 186 | + |
| 187 | +_Returns:_ The SPIR-V versions that are supported by the device. |
| 188 | +If the device supports `aspect::ext_oneapi_spirv`, this query must return a |
| 189 | +non-empty vector. |
| 190 | +Otherwise, when the device does not support `aspect::ext_oneapi_spirv`, this |
| 191 | +query must return an empty vector. |
| 192 | + |
| 193 | +a| |
| 194 | +[frame=all,grid=none] |
| 195 | +!==== |
| 196 | +a! |
| 197 | +[source] |
| 198 | +---- |
| 199 | +namespace sycl::ext::oneapi::experimental::info::device { |
| 200 | +struct spirv_extended_instruction_sets { |
| 201 | + using return_type = std::vector<std::string>; |
| 202 | +}; |
| 203 | +} // namespace sycl::ext::oneapi::experimental::info::device |
| 204 | +---- |
| 205 | +!==== |
| 206 | + |
| 207 | +_Remarks:_ Template parameter to `device::get_info`. |
| 208 | + |
| 209 | +_Returns:_ The SPIR-V extended instruction sets that are supported by the device. |
| 210 | +SPIR-V extended instruction sets may be described on the SPIR-V registry. |
| 211 | +When the device does not support `aspect::ext_oneapi_spirv`, this query must |
| 212 | +return an empty vector. |
| 213 | + |
| 214 | +_Throws_: A synchronous `exception` with the `errc::feature_not_supported` error |
| 215 | +code if the device does not support `aspect::ext_oneapi_spirv_queries`. |
| 216 | + |
| 217 | +a| |
| 218 | +[frame=all,grid=none] |
| 219 | +!==== |
| 220 | +a! |
| 221 | +[source] |
| 222 | +---- |
| 223 | +namespace sycl::ext::oneapi::experimental::info::device { |
| 224 | +struct spirv_extensions { |
| 225 | + using return_type = std::vector<std::string>; |
| 226 | +}; |
| 227 | +} // namespace sycl::ext::oneapi::experimental::info::device |
| 228 | +---- |
| 229 | +!==== |
| 230 | + |
| 231 | +_Remarks:_ Template parameter to `device::get_info`. |
| 232 | + |
| 233 | +_Returns:_ The SPIR-V extensions that are supported by the device. |
| 234 | +SPIR-V extensions may be described on the SPIR-V registry. |
| 235 | +When the device does not support `aspect::ext_oneapi_spirv`, this query must |
| 236 | +return an empty vector. |
| 237 | + |
| 238 | +_Throws_: A synchronous `exception` with the `errc::feature_not_supported` error |
| 239 | +code if the device does not support `aspect::ext_oneapi_spirv_queries`. |
| 240 | + |
| 241 | +a| |
| 242 | +[frame=all,grid=none] |
| 243 | +!==== |
| 244 | +a! |
| 245 | +[source] |
| 246 | +---- |
| 247 | +namespace sycl::ext::oneapi::experimental::info::device { |
| 248 | +struct spirv_capabilities { |
| 249 | + using return_type = std::vector<uint32_t>; |
| 250 | +}; |
| 251 | +} // namespace sycl::ext::oneapi::experimental::info::device |
| 252 | +---- |
| 253 | +!==== |
| 254 | + |
| 255 | +_Remarks:_ Template parameter to `device::get_info`. |
| 256 | + |
| 257 | +_Returns:_ The SPIR-V capabilities that are supported by the device. |
| 258 | +SPIR-V capabilities are described in the SPIR-V specification. |
| 259 | +Some capabilities may additionally require a specific SPIR-V version or SPIR-V |
| 260 | +extension. |
| 261 | +When the device does not support `aspect::ext_oneapi_spirv`, this query must |
| 262 | +return an empty vector. |
| 263 | + |
| 264 | +_Throws_: A synchronous `exception` with the `errc::feature_not_supported` error |
| 265 | +code if the device does not support `aspect::ext_oneapi_spirv_queries`. |
| 266 | + |
| 267 | +|==== |
| 268 | + |
| 269 | +==== New member functions for the device class |
| 270 | + |
| 271 | +This extension also adds the following member functions to the `device` class, |
| 272 | +which allow the application to query the SPIR-V versions, extended instruction |
| 273 | +sets, extensions, and capabilities that the device supports. |
| 274 | + |
| 275 | +|==== |
| 276 | +a| |
| 277 | +[frame=all,grid=none] |
| 278 | +!==== |
| 279 | +a! |
| 280 | +[source] |
| 281 | +---- |
| 282 | +class device { |
| 283 | + bool ext_oneapi_supports_spirv_version( |
| 284 | + const sycl::ext::oneapi::experimental::spirv_version &version) const; |
| 285 | +}; |
| 286 | +---- |
| 287 | +!==== |
| 288 | + |
| 289 | +_Returns:_ Equivalent to querying the supported SPIR-V versions using |
| 290 | +`get_info<info::device::spirv_versions>()` and then calling `std::find` to check |
| 291 | +if `version` is one of the supported versions. |
| 292 | + |
| 293 | +a| |
| 294 | +[frame=all,grid=none] |
| 295 | +!==== |
| 296 | +a! |
| 297 | +[source] |
| 298 | +---- |
| 299 | +class device { |
| 300 | + bool ext_oneapi_supports_spirv_extended_instruction_set( |
| 301 | + const std::string &name) const; |
| 302 | +}; |
| 303 | +---- |
| 304 | +!==== |
| 305 | + |
| 306 | +_Returns:_ Equivalent to querying the supported SPIR-V extended instruction sets |
| 307 | +using `get_info<info::device::spirv_extended_instruction_sets>()` and then |
| 308 | +calling `std::find` to check if `name` is one of the supported extended |
| 309 | +instruction sets. |
| 310 | + |
| 311 | + |
| 312 | +a| |
| 313 | +[frame=all,grid=none] |
| 314 | +!==== |
| 315 | +a! |
| 316 | +[source] |
| 317 | +---- |
| 318 | +class device { |
| 319 | + bool ext_oneapi_supports_spirv_extension(const std::string &name) const; |
| 320 | +}; |
| 321 | +---- |
| 322 | +!==== |
| 323 | + |
| 324 | +_Returns:_ Equivalent to querying the supported SPIR-V extensions using |
| 325 | +`get_info<info::device::spirv_extensions>()` and then calling `std::find` to |
| 326 | +check if `name` is one of the supported extensions. |
| 327 | + |
| 328 | +a| |
| 329 | +[frame=all,grid=none] |
| 330 | +!==== |
| 331 | +a! |
| 332 | +[source] |
| 333 | +---- |
| 334 | +class device { |
| 335 | + bool ext_oneapi_supports_spirv_capability(uint32_t capability) const; |
| 336 | +}; |
| 337 | +---- |
| 338 | +!==== |
| 339 | + |
| 340 | +_Returns:_ Equivalent to querying the supported SPIR-V capabilities using |
| 341 | +`get_info<info::device::spirv_capabilities>()` and then calling `std::find` to |
| 342 | +check if `capability` is one of the supported capabilities. |
| 343 | + |
| 344 | +|==== |
| 345 | + |
| 346 | + |
| 347 | +== Examples |
| 348 | + |
| 349 | +=== Simple example |
| 350 | + |
| 351 | +The following example shows a simple SYCL program that demonstrates how to query |
| 352 | +whether a SYCL device supports SPIR-V and SPIR-V queries, and if it does, |
| 353 | +whether the SYCL devices supports SPIR-V extended instruction sets, SPIR-V |
| 354 | +extensions, and SPIR-V capabilities. |
| 355 | + |
| 356 | +[source,c++] |
| 357 | +---- |
| 358 | +#include <sycl/sycl.hpp> |
| 359 | +#include <spirv/unified1/spirv.hpp> |
| 360 | +
|
| 361 | +namespace syclex = sycl::ext::oneapi::experimental; |
| 362 | +
|
| 363 | +int main() { |
| 364 | + sycl::queue q; |
| 365 | + sycl::device d = q.get_device(); |
| 366 | +
|
| 367 | + if (d.has(sycl::aspect::ext_oneapi_spirv)) |
| 368 | + std::cout << "Device supports SPIR-V.\n"; |
| 369 | +
|
| 370 | + for (const auto &ver : d.get_info<syclex::info::device::spirv_versions>()) |
| 371 | + std::cout << "Device supports SPIR-V version: " << ver.major << "." << ver.minor << "\n"; |
| 372 | +
|
| 373 | + if (d.ext_oneapi_supports_spirv_version(syclex::spirv_1_0)) |
| 374 | + std::cout << "Device supports SPIR-V 1.0.\n"; |
| 375 | +
|
| 376 | + if (d.has(sycl::aspect::ext_oneapi_spirv_queries)) { |
| 377 | + std::cout << "Device supports SPIR-V queries.\n"; |
| 378 | +
|
| 379 | + for (const auto &name : d.get_info<syclex::info::device::spirv_extended_instruction_sets>()) |
| 380 | + std::cout << "Device supports SPIR-V extended instruction set: " << name << "\n"; |
| 381 | +
|
| 382 | + for (const auto &name : d.get_info<syclex::info::device::spirv_extensions>()) |
| 383 | + std::cout << "Device supports SPIR-V extension: " << name << "\n"; |
| 384 | +
|
| 385 | + for (const auto &cap : d.get_info<syclex::info::device::spirv_capabilities>()) |
| 386 | + std::cout << "Device supports SPIR-V capability with value: " << cap << "\n"; |
| 387 | +
|
| 388 | + if (d.ext_oneapi_supports_spirv_extended_instruction_set("OpenCL.std")) |
| 389 | + std::cout << "Device supports the OpenCL.std SPIR-V extended instruction set.\n"; |
| 390 | +
|
| 391 | + if (d.ext_oneapi_supports_spirv_extension("SPV_KHR_linkonce_odr")) |
| 392 | + std::cout << "Device supports the SPV_KHR_linkonce_odr SPIR-V extension.\n"; |
| 393 | +
|
| 394 | + if (d.ext_oneapi_supports_spirv_capability(spv::CapabilityAddresses)) |
| 395 | + std::cout << "Device supports the Addresses SPIR-V capability.\n"; |
| 396 | + } |
| 397 | +} |
| 398 | +---- |
| 399 | + |
0 commit comments