Skip to main content

sycl_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 crate::Result;
10use bytemuck::Pod;
11use sycl_rs_sys::{kernel_bundle::ffi, types};
12
13/// A kernel bundle which stores loaded SYCL source code.
14pub struct SourceKernelBundle(pub(crate) cxx::UniquePtr<types::ffi::SourceKernelBundle>);
15
16impl From<cxx::UniquePtr<types::ffi::SourceKernelBundle>> for SourceKernelBundle {
17    fn from(value: cxx::UniquePtr<types::ffi::SourceKernelBundle>) -> Self {
18        Self(value)
19    }
20}
21
22impl SourceKernelBundle {
23    pub fn build(&mut self) -> Result<ExecutableKernelBundle> {
24        ffi::build(&mut self.0).map(Into::into)
25    }
26}
27
28/// A kernel bundle which stores compiled SYCL kernels.
29pub struct ExecutableKernelBundle(pub(crate) cxx::UniquePtr<types::ffi::ExecutableKernelBundle>);
30
31impl From<cxx::UniquePtr<types::ffi::ExecutableKernelBundle>> for ExecutableKernelBundle {
32    fn from(value: cxx::UniquePtr<types::ffi::ExecutableKernelBundle>) -> Self {
33        Self(value)
34    }
35}
36
37impl ExecutableKernelBundle {
38    pub fn get_kernel(&mut self, name: &str) -> Result<Kernel> {
39        ffi::get_kernel(&mut self.0, name).map(Into::into)
40    }
41}
42
43/// An executable SYCL kernel.
44pub struct Kernel(pub(crate) cxx::UniquePtr<types::ffi::Kernel>);
45
46impl From<cxx::UniquePtr<types::ffi::Kernel>> for Kernel {
47    fn from(value: cxx::UniquePtr<types::ffi::Kernel>) -> Self {
48        Self(value)
49    }
50}
51
52/// Types which can be passed as SYCL kernel arguments.
53///
54/// # Safety
55///
56/// A type implementing this trait must mirror the representation and alignment of the
57/// corresponding SYCL kernel argument structure.
58pub unsafe trait KernelArgument {
59    /// Converts self to a raw byte representation.
60    ///
61    /// # Safety
62    ///
63    /// This function returns a reference to raw bytes. These bytes will be passed to FFI
64    /// functions. The caller must make sure these functions respect Rust's aliasing rules.
65    unsafe fn as_raw_arg(&self) -> &[u8];
66}
67
68unsafe impl<T: Pod> KernelArgument for T {
69    unsafe fn as_raw_arg(&self) -> &[u8] {
70        bytemuck::bytes_of(self)
71    }
72}
73
74/// Types which describe an argument list for a SYCL kernel.
75///
76/// # Safety
77///
78/// A type implementing this trait must mirror the representation and alignment of each
79/// corresponding SYCL kernel argument inside the returned array.
80pub unsafe trait KernelArgumentList<const ARGC: usize> {
81    /// Converts each struct member to a raw byte representation.
82    ///
83    /// # Safety
84    ///
85    /// This function returns references to raw bytes. These bytes will be passed to FFI
86    /// functions. The caller must make sure these functions respect Rust's aliasing rules.
87    unsafe fn as_raw_arg_list(&self) -> [&[u8]; ARGC];
88}
89
90unsafe impl KernelArgumentList<0> for () {
91    unsafe fn as_raw_arg_list(&self) -> [&[u8]; 0] {
92        []
93    }
94}
95
96unsafe impl<T: KernelArgument> KernelArgumentList<1> for T {
97    unsafe fn as_raw_arg_list(&self) -> [&[u8]; 1] {
98        [unsafe { self.as_raw_arg() }]
99    }
100}
101
102pub use sycl_rs_derive::KernelArgumentList;
103
104use sycl_rs_derive::impl_arg_list_for_tuples;
105
106impl_arg_list_for_tuples! {16}