Macros#10
Open
szymon-zadworny wants to merge 15 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces proc-macros to reduce boilerplate when supplying kernel argument lists, adding both a derive macro for structs and generated KernelArgumentList impls for tuples (up to 16 args), and updates examples to demonstrate the new ergonomics.
Changes:
- Add
#[derive(KernelArgumentList)]and a tuple-impl generator proc-macro in the newoneapi-rs-derivecrate. - Extend
oneapi-rskernel argument support (single-arg list blanket impl;KernelArgumentfor&Buffer/&mut Buffer). - Update/add examples to use tuple args and the new derive macro.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| oneapi-rs/src/kernel.rs | Adds KernelArgumentList<1> blanket impl and wires in derive + tuple macro expansion. |
| oneapi-rs/src/buffer.rs | Refactors raw-arg serialization and adds KernelArgument impls for &Buffer/&mut Buffer. |
| oneapi-rs/examples/kernel_launch.rs | Simplifies example to pass args as a tuple (and switches to f64/double). |
| oneapi-rs/examples/kernel_launch_derive.rs | New example demonstrating #[derive(KernelArgumentList)]. |
| oneapi-rs/Cargo.toml | Adds oneapi-rs-derive dependency. |
| oneapi-rs-derive/src/lib.rs | Implements derive macro and tuple-impl generator proc-macro. |
| oneapi-rs-derive/Cargo.toml | Declares new proc-macro crate and dependencies. |
| Cargo.toml | Adds oneapi-rs-derive to the workspace. |
| Cargo.lock | Locks new dependencies (including oneapi-rs-derive and syn v3). |
Comments suppressed due to low confidence (2)
oneapi-rs-derive/src/lib.rs:30
- To make the expansion more robust (avoids relying on method-resolution details and reduces the chance of conflicts with other
as_raw_argmethods), prefer calling the trait method with a fully-qualified path.
unsafe fn as_raw_arg_list(&self) -> [&[u8]; #argc] {
[ #(unsafe { self.#members.as_raw_arg() }),* ]
}
oneapi-rs-derive/src/lib.rs:61
- This doc comment also references
KernelArgumentList<T>, but the trait uses a const generic (KernelArgumentList<const ARGC: usize>).
/// A macro that generates tuples from 2..N which implement the `KernelArgumentList<T>` trait.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (2)
oneapi-rs-derive/src/lib.rs:77
impl_arg_list_for_tuplesusesunwrap()when parsing the arity. If the macro is invoked with invalid input, it will panic rather than producing a structured compile error at the call site.
pub fn impl_arg_list_for_tuples(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as LitInt);
let argc = input.base10_parse::<usize>().unwrap();
let impls = { 2..=argc }.map(get_single_tuple_impl);
oneapi-rs/examples/kernel_launch_derive.rs:15
KernelArgumentis imported but not used in this example; removing it avoidsunused_importswarnings (which can become build failures under-D warnings).
use oneapi_rs::{
buffer::Buffer,
kernel::{KernelArgument, KernelArgumentList},
queue::Queue,
range::NdRange,
usm::{SharedAllocator, UsmAllocator},
};
Comment on lines
62
to
+71
| /// Types which describe an argument list for a SYCL kernel. | ||
| pub unsafe trait KernelArgumentList<const ARGC: usize> { | ||
| unsafe fn as_raw_arg_list(&self) -> [&[u8]; ARGC]; | ||
| } | ||
|
|
||
| unsafe impl<T: KernelArgument> KernelArgumentList<1> for T { | ||
| unsafe fn as_raw_arg_list(&self) -> [&[u8]; 1] { | ||
| [unsafe { self.as_raw_arg() }] | ||
| } | ||
| } |
Comment on lines
+67
to
+71
| unsafe impl<T: KernelArgument> KernelArgumentList<1> for T { | ||
| unsafe fn as_raw_arg_list(&self) -> [&[u8]; 1] { | ||
| [unsafe { self.as_raw_arg() }] | ||
| } | ||
| } |
Comment on lines
+9
to
+21
| fn find_oneapi() -> Ident { | ||
| let crate_name = crate_name("oneapi_rs").expect("oneapi_rs is present in Cargo.toml"); | ||
| match crate_name { | ||
| FoundCrate::Itself => format_ident!("crate"), | ||
| FoundCrate::Name(name) => format_ident!("{name}"), | ||
| } | ||
| } | ||
|
|
||
| /// Derive macro generating an impl of the `KernelArgumentList` trait for a given struct. | ||
| #[proc_macro_derive(KernelArgumentList)] | ||
| pub fn derive_kernel_argument_list(input: TokenStream) -> TokenStream { | ||
| let mut input = parse_macro_input!(input as DeriveInput); | ||
| let oneapi = find_oneapi(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds:
KernelArgumentListtraitKernelArgumentListfor tuples up to 16 arguments in size