|
| 1 | +//==--- opencl.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: ocloc && (opencl || level_zero) |
| 10 | +// UNSUPPORTED: accelerator |
| 11 | +// UNSUPPORTED-INTENDED: while accelerator is AoT only, this cannot run there. |
| 12 | + |
| 13 | +// -- Test the kernel_compiler with OpenCL source. |
| 14 | +// RUN: %{build} -o %t.out |
| 15 | + |
| 16 | +// -- Test with caching. |
| 17 | +// DEFINE: %{cache_vars} = env SYCL_CACHE_PERSISTENT=1 SYCL_CACHE_TRACE=5 SYCL_CACHE_DIR=%t/cache_dir |
| 18 | +// RUN: %{run-aux} rm -rf %t/cache_dir |
| 19 | +// RUN: %{l0_leak_check} %{cache_vars} %{run} %t.out 2>&1 | FileCheck %s --check-prefixes=CHECK-WRITTEN-TO-CACHE |
| 20 | +// RUN: %{l0_leak_check} %{cache_vars} %{run} %t.out 2>&1 | FileCheck %s --check-prefixes=CHECK-READ-FROM-CACHE |
| 21 | + |
| 22 | +// CHECK-WRITTEN-TO-CACHE: [Persistent Cache]: enabled |
| 23 | +// CHECK-WRITTEN-TO-CACHE-NOT: [kernel_compiler Persistent Cache]: using cached binary |
| 24 | +// CHECK-WRITTEN-TO-CACHE: [kernel_compiler Persistent Cache]: binary has been cached |
| 25 | + |
| 26 | +// CHECK-READ-FROM-CACHE: [Persistent Cache]: enabled |
| 27 | +// CHECK-READ-FROM-CACHE-NOT: [kernel_compiler Persistent Cache]: binary has been cached |
| 28 | +// CHECK-READ-FROM-CACHE: [kernel_compiler Persistent Cache]: using cached binary |
| 29 | + |
| 30 | +#include <sycl/detail/core.hpp> |
| 31 | +#include <sycl/kernel_bundle.hpp> |
| 32 | + |
| 33 | +auto constexpr CLSource = R"===( |
| 34 | +__kernel void my_kernel(__global int *in, __global int *out) { |
| 35 | + size_t i = get_global_id(0); |
| 36 | + out[i] = in[i]*2 + 100; |
| 37 | +} |
| 38 | +__kernel void her_kernel(__global int *in, __global int *out) { |
| 39 | + size_t i = get_global_id(0); |
| 40 | + out[i] = in[i]*5 + 1000; |
| 41 | +} |
| 42 | +)==="; |
| 43 | + |
| 44 | +using namespace sycl; |
| 45 | + |
| 46 | +void testSyclKernel(sycl::queue &Q, sycl::kernel Kernel, int multiplier, |
| 47 | + int added) { |
| 48 | + constexpr int N = 4; |
| 49 | + cl_int InputArray[N] = {0, 1, 2, 3}; |
| 50 | + cl_int OutputArray[N] = {}; |
| 51 | + |
| 52 | + sycl::buffer InputBuf(InputArray, sycl::range<1>(N)); |
| 53 | + sycl::buffer OutputBuf(OutputArray, sycl::range<1>(N)); |
| 54 | + |
| 55 | + Q.submit([&](sycl::handler &CGH) { |
| 56 | + CGH.set_arg(0, InputBuf.get_access<sycl::access::mode::read>(CGH)); |
| 57 | + CGH.set_arg(1, OutputBuf.get_access<sycl::access::mode::write>(CGH)); |
| 58 | + CGH.parallel_for(sycl::range<1>{N}, Kernel); |
| 59 | + }); |
| 60 | + |
| 61 | + sycl::host_accessor Out{OutputBuf}; |
| 62 | + for (int I = 0; I < N; I++) |
| 63 | + assert(Out[I] == ((I * multiplier) + added)); |
| 64 | +} |
| 65 | + |
| 66 | +void test_build_and_run() { |
| 67 | + namespace syclex = sycl::ext::oneapi::experimental; |
| 68 | + using source_kb = sycl::kernel_bundle<sycl::bundle_state::ext_oneapi_source>; |
| 69 | + using exe_kb = sycl::kernel_bundle<sycl::bundle_state::executable>; |
| 70 | + |
| 71 | + // only one device is supported at this time, so we limit the queue and |
| 72 | + // context to that |
| 73 | + sycl::device d{sycl::default_selector_v}; |
| 74 | + sycl::context ctx{d}; |
| 75 | + sycl::queue q{ctx, d}; |
| 76 | + |
| 77 | + bool ok = |
| 78 | + q.get_device().ext_oneapi_can_build(syclex::source_language::opencl); |
| 79 | + if (!ok) { |
| 80 | + std::cout << "Apparently this device does not support OpenCL C source " |
| 81 | + "kernel bundle extension: " |
| 82 | + << q.get_device().get_info<sycl::info::device::name>() |
| 83 | + << std::endl; |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + source_kb kbSrc = syclex::create_kernel_bundle_from_source( |
| 88 | + ctx, syclex::source_language::opencl, CLSource); |
| 89 | + exe_kb kbExe1 = syclex::build(kbSrc); |
| 90 | + |
| 91 | + sycl::kernel my_kernel = kbExe1.ext_oneapi_get_kernel("my_kernel"); |
| 92 | + |
| 93 | + testSyclKernel(q, my_kernel, 2, 100); |
| 94 | +} |
| 95 | + |
| 96 | +int main() { |
| 97 | +#ifndef SYCL_EXT_ONEAPI_KERNEL_COMPILER_OPENCL |
| 98 | + static_assert(false, "KernelCompiler OpenCL feature test macro undefined"); |
| 99 | +#endif |
| 100 | + |
| 101 | +#ifdef SYCL_EXT_ONEAPI_KERNEL_COMPILER |
| 102 | + test_build_and_run(); |
| 103 | +#else |
| 104 | + static_assert(false, "Kernel Compiler feature test macro undefined"); |
| 105 | +#endif |
| 106 | + return 0; |
| 107 | +} |
0 commit comments