|
| 1 | +//==- kernel_compiler_context_error.cpp -- kernel_compiler extension tests -==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// REQUIRES: (opencl || level_zero) |
| 10 | + |
| 11 | +// UNSUPPORTED: accelerator |
| 12 | +// UNSUPPORTED-INTENDED: while accelerator is AoT only, this cannot run there. |
| 13 | + |
| 14 | +// RUN: %{build} -o %t.out |
| 15 | +// RUN: %{run-unfiltered-devices} %t.out |
| 16 | + |
| 17 | +#include <sycl/detail/core.hpp> |
| 18 | +#include <sycl/kernel_bundle.hpp> |
| 19 | +#include <sycl/usm.hpp> |
| 20 | + |
| 21 | +namespace syclexp = sycl::ext::oneapi::experimental; |
| 22 | + |
| 23 | +const std::string source = R"""( |
| 24 | + #include <sycl/sycl.hpp> |
| 25 | + namespace syclext = sycl::ext::oneapi; |
| 26 | + namespace syclexp = sycl::ext::oneapi::experimental; |
| 27 | +
|
| 28 | + extern "C" |
| 29 | + SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 30 | + void iota(float start, float *ptr) { |
| 31 | + size_t id = syclext::this_work_item::get_nd_item<1>().get_global_linear_id(); |
| 32 | + ptr[id] = start + static_cast<float>(id); |
| 33 | + } |
| 34 | +)"""; |
| 35 | + |
| 36 | +int main() { |
| 37 | + |
| 38 | + auto has_multiple_compatible_devices = [](sycl::platform platform) -> bool { |
| 39 | + auto devices = platform.get_devices(); |
| 40 | + if (devices.size() < 2) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + for (auto dev : devices) { |
| 44 | + if (!dev.ext_oneapi_can_compile(syclexp::source_language::sycl)) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + } |
| 48 | + return true; |
| 49 | + }; |
| 50 | + |
| 51 | + std::vector<sycl::device> all_devices = [&]() -> std::vector<sycl::device> { |
| 52 | + for (auto platform : sycl::platform::get_platforms()) { |
| 53 | + if (has_multiple_compatible_devices(platform)) { |
| 54 | + return platform.get_devices(); |
| 55 | + } |
| 56 | + } |
| 57 | + return {}; |
| 58 | + }(); |
| 59 | + |
| 60 | + if (all_devices.size() < 2) { |
| 61 | + std::cerr << "Cannot find platform with more than 1 device, skipping" |
| 62 | + << std::endl; |
| 63 | + return 0; |
| 64 | + } |
| 65 | + |
| 66 | + sycl::context single_device_context{all_devices.front()}; |
| 67 | + |
| 68 | + // Create a source kernel bundle with a context that contains only one device. |
| 69 | + sycl::kernel_bundle<sycl::bundle_state::ext_oneapi_source> kb_src = |
| 70 | + syclexp::create_kernel_bundle_from_source( |
| 71 | + single_device_context, syclexp::source_language::sycl, source); |
| 72 | + |
| 73 | + // Compile the kernel. There is no need to use the "registered_names" |
| 74 | + // property because the kernel is declared extern "C". |
| 75 | + try { |
| 76 | + syclexp::build(kb_src, all_devices); |
| 77 | + assert(false && "out-of-context device not detected"); |
| 78 | + } catch (sycl::exception &e) { |
| 79 | + assert(e.code() == sycl::errc::invalid); |
| 80 | + assert(std::string(e.what()).find( |
| 81 | + "device not part of kernel_bundle context") != |
| 82 | + std::string::npos); |
| 83 | + } |
| 84 | + return 0; |
| 85 | +} |
0 commit comments