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: a type implement this trait must mirror the representation and alignment of the
55/// corresponding SYCL kernel argument structure.
56pub unsafe trait KernelArgument {
57    /// Converts self to a raw byte representation.
58    ///
59    /// Safety: This function returns a reference to raw bytes. These bytes will be passed to FFI
60    /// functions. The caller must make sure these functions respect Rust's aliasing rules.
61    unsafe fn as_raw_arg(&self) -> &[u8];
62}
63
64unsafe impl<T: Pod> KernelArgument for T {
65    unsafe fn as_raw_arg(&self) -> &[u8] {
66        bytemuck::bytes_of(self)
67    }
68}
69
70/// Types which describe an argument list for a SYCL kernel.
71///
72/// Safety: a type implement this trait must mirror the representation and alignment of each
73/// corresponding SYCL kernel argument inside the returned array.
74pub unsafe trait KernelArgumentList<const ARGC: usize> {
75    /// Converts each struct member to a raw byte representation.
76    ///
77    /// Safety: This function returns references to raw bytes. These bytes will be passed to FFI
78    /// functions. The caller must make sure these functions respect Rust's aliasing rules.
79    unsafe fn as_raw_arg_list(&self) -> [&[u8]; ARGC];
80}
81
82unsafe impl KernelArgumentList<0> for () {
83    unsafe fn as_raw_arg_list(&self) -> [&[u8]; 0] {
84        []
85    }
86}
87
88unsafe impl<T: KernelArgument> KernelArgumentList<1> for T {
89    unsafe fn as_raw_arg_list(&self) -> [&[u8]; 1] {
90        [unsafe { self.as_raw_arg() }]
91    }
92}
93
94pub use sycl_rs_derive::KernelArgumentList;
95
96use sycl_rs_derive::impl_arg_list_for_tuples;
97
98impl_arg_list_for_tuples! {16}