oneapi_rs/
kernel.rs

1//
2// Copyright (C) 2026 Intel Corporation
3//
4// Under the MIT License or the Apache License v2.0.
5// See LICENSE-MIT and LICENSE-APACHE for license information.
6// SPDX-License-Identifier: MIT OR Apache-2.0
7//
8
9use bytemuck::Pod;
10use oneapi_rs_sys::{kernel_bundle::ffi, types};
11
12/// A kernel bundle which stores loaded SYCL source code.
13pub struct SourceKernelBundle(pub(crate) cxx::UniquePtr<types::ffi::SourceKernelBundle>);
14
15impl From<cxx::UniquePtr<types::ffi::SourceKernelBundle>> for SourceKernelBundle {
16    fn from(value: cxx::UniquePtr<types::ffi::SourceKernelBundle>) -> Self {
17        Self(value)
18    }
19}
20
21impl SourceKernelBundle {
22    pub fn build(&mut self) -> ExecutableKernelBundle {
23        ffi::build(&mut self.0).into()
24    }
25}
26
27/// A kernel bundle which stores compiled SYCL kernels.
28pub struct ExecutableKernelBundle(pub(crate) cxx::UniquePtr<types::ffi::ExecutableKernelBundle>);
29
30impl From<cxx::UniquePtr<types::ffi::ExecutableKernelBundle>> for ExecutableKernelBundle {
31    fn from(value: cxx::UniquePtr<types::ffi::ExecutableKernelBundle>) -> Self {
32        Self(value)
33    }
34}
35
36impl ExecutableKernelBundle {
37    pub fn get_kernel(&mut self, name: &str) -> Kernel {
38        ffi::get_kernel(&mut self.0, name).into()
39    }
40}
41
42/// An executable SYCL kernel.
43pub struct Kernel(pub(crate) cxx::UniquePtr<types::ffi::Kernel>);
44
45impl From<cxx::UniquePtr<types::ffi::Kernel>> for Kernel {
46    fn from(value: cxx::UniquePtr<types::ffi::Kernel>) -> Self {
47        Self(value)
48    }
49}
50
51/// Types which can be passed as SYCL kernel arguments.
52pub unsafe trait KernelArgument {
53    unsafe fn as_raw_arg(&self) -> &[u8];
54}
55
56unsafe impl<T: Pod> KernelArgument for T {
57    unsafe fn as_raw_arg(&self) -> &[u8] {
58        bytemuck::bytes_of(self)
59    }
60}
61
62/// Types which describe an argument list for a SYCL kernel.
63pub unsafe trait KernelArgumentList<const ARGC: usize> {
64    unsafe fn as_raw_arg_list(&self) -> [&[u8]; ARGC];
65}
66
67unsafe impl KernelArgumentList<0> for () {
68    unsafe fn as_raw_arg_list(&self) -> [&[u8]; 0] {
69        []
70    }
71}
72
73unsafe impl<T: KernelArgument> KernelArgumentList<1> for T {
74    unsafe fn as_raw_arg_list(&self) -> [&[u8]; 1] {
75        [unsafe { self.as_raw_arg() }]
76    }
77}
78
79pub use oneapi_rs_derive::KernelArgumentList;
80
81use oneapi_rs_derive::impl_arg_list_for_tuples;
82
83impl_arg_list_for_tuples! {16}