-
Notifications
You must be signed in to change notification settings - Fork 2
Macros #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
szymon-zadworny
wants to merge
16
commits into
oneapi-src:main
Choose a base branch
from
szymon-zadworny:macros
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Macros #10
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0e26964
Add a new proc-macro crate
szymon-zadworny 976e91b
Add basic macro structure
szymon-zadworny e006ece
Add basic derive macro implementation for structs
szymon-zadworny aaad0f8
Add derive macro to main crate
szymon-zadworny bf885a0
Add tuple argument list support
szymon-zadworny 910cbf3
Add trait bounds to derive macro
szymon-zadworny 171372d
Use full module paths inside macros
szymon-zadworny a08a478
Rename derive macro function
szymon-zadworny 2242309
Make tuple impl generation range inclusive
szymon-zadworny 99918fb
Add blanket KernelArgumentList impl for single arguments
szymon-zadworny 8373c70
cargo fmt
szymon-zadworny da96fd4
Add error message to derive macro
szymon-zadworny d853e11
Add macro documentation
szymon-zadworny 757b0b4
Add derive macro example
szymon-zadworny 06db8bb
Resolve oneapi_rs crate name at macro-expansion time
szymon-zadworny 2d3f0bd
Add support for zero-sized argument lists
szymon-zadworny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| [workspace] | ||
| resolver = "3" | ||
| members = ["oneapi-rs","oneapi-rs-sys"] | ||
| members = ["oneapi-rs", "oneapi-rs-derive","oneapi-rs-sys"] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [package] | ||
| name = "oneapi-rs-derive" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [lib] | ||
| proc-macro = true | ||
|
|
||
| [dependencies] | ||
| proc-macro-crate = "3.5.0" | ||
| proc-macro2 = "1.0.107" | ||
| quote = "1.0.47" | ||
| syn = "3.0.3" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| use proc_macro::TokenStream; | ||
| use proc_macro_crate::{FoundCrate, crate_name}; | ||
| use quote::{format_ident, quote}; | ||
| use syn::{ | ||
| Data, DataStruct, DeriveInput, Error, Field, Ident, LitInt, WhereClause, parse_macro_input, | ||
| parse_quote, | ||
| }; | ||
|
|
||
| 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(); | ||
|
|
||
| let Data::Struct(data) = &input.data else { | ||
| return Error::new_spanned(input, "This derive macro only works on structs.") | ||
| .into_compile_error() | ||
| .into(); | ||
| }; | ||
| expand_where_clause(input.generics.make_where_clause(), data, &oneapi); | ||
| let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); | ||
|
|
||
| let ident = input.ident; | ||
| let argc = data.fields.len(); | ||
| let members = data.fields.members(); | ||
|
|
||
| let expanded = quote! { | ||
| unsafe impl #impl_generics #oneapi::kernel::KernelArgumentList<#argc> | ||
| for #ident #ty_generics #where_clause { | ||
| unsafe fn as_raw_arg_list(&self) -> [&[u8]; #argc] { | ||
| [ #(unsafe { self.#members.as_raw_arg() }),* ] | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| TokenStream::from(expanded) | ||
| } | ||
|
|
||
| fn expand_where_clause(where_clause: &mut WhereClause, data: &DataStruct, oneapi: &Ident) { | ||
| for Field { ty, .. } in &data.fields { | ||
| where_clause | ||
| .predicates | ||
| .push(parse_quote!(#ty: #oneapi::kernel::KernelArgument)); | ||
| } | ||
| } | ||
|
|
||
| fn get_single_tuple_impl(argc: usize) -> proc_macro2::TokenStream { | ||
| let iter = { 0..argc }.map(syn::Index::from); | ||
| let types = { 0..argc } | ||
| .map(|i| format_ident!("T{i}")) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| quote! { | ||
| unsafe impl<#(#types),*> crate::kernel::KernelArgumentList<#argc> for (#(#types),*) | ||
| where #(#types: crate::kernel::KernelArgument),* { | ||
| unsafe fn as_raw_arg_list(&self) -> [&[u8]; #argc] { | ||
| [ #(unsafe { self.#iter.as_raw_arg() }),* ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A macro that generates tuples from 2..N which implement the `KernelArgumentList` trait. | ||
| #[proc_macro] | ||
| 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(); | ||
|
|
||
|
szymon-zadworny marked this conversation as resolved.
|
||
| let impls = { 2..=argc }.map(get_single_tuple_impl); | ||
|
|
||
| let expanded = quote! { | ||
| #(#impls)* | ||
| }; | ||
|
|
||
| TokenStream::from(expanded) | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.