Skip to content

Commit 44c44ba

Browse files
nnethercoteLegNeato
authored andcommitted
cuda_std_macros: remove KernelHints.
The `#[kernel]` proc macro has some code to allow "kernel hints" arguments like this: ``` #[kernel(block_dim = 1d, grid_dim = 1d)] ``` This dates back to the very first rust-cuda commit. However... - The parsing code is broken. It requires an identifier after the `=`, and then checks that identifier has one of the following values: `1d`, `1D`, `2d`, `2D`, `3d`, `3D`. But those aren't valid identifiers because they start with a digit. So you can't actually write any kernel hints that the compiler will accept. - Even if the parsing succeeded, the hints are ignored, thanks to this: ``` let _ = parse_macro_input!(input as KernelHints); ``` - Unsurprisingly, there are no uses of these hints in the code. This commit just removes the (broken) support for them.
1 parent 92217f0 commit 44c44ba

File tree

1 file changed

+8
-80
lines changed
  • crates/cuda_std_macros/src

1 file changed

+8
-80
lines changed

crates/cuda_std_macros/src/lib.rs

Lines changed: 8 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
use proc_macro::TokenStream;
2-
use proc_macro2::Span;
32
use quote::{ToTokens, quote_spanned};
43
use syn::{
5-
Error, FnArg, Ident, ItemFn, ReturnType, Stmt, Token, parse::Parse, parse_macro_input,
6-
parse_quote, punctuated::Punctuated, spanned::Spanned,
4+
FnArg, Ident, ItemFn, ReturnType, Stmt, parse_macro_input, parse_quote, spanned::Spanned,
75
};
86

97
/// Registers a function as a gpu kernel.
108
///
119
/// This attribute must always be placed on gpu kernel functions.
1210
///
1311
/// This attribute does a couple of things:
14-
/// - Tells `rustc_codegen_nvvm` to mark this as a gpu kernel and to not remove it from the ptx file.
12+
/// - Tells `rustc_codegen_nvvm` to mark this as a gpu kernel and to not remove it from the ptx
13+
/// file.
1514
/// - Marks the function as `no_mangle`.
1615
/// - Errors if the function is not unsafe.
1716
/// - Makes sure function parameters are all [`Copy`].
1817
/// - Makes sure the function doesn't return anything.
1918
///
20-
/// Note that this does not cfg the function for nvptx(64), that is explicit so that rust analyzer is able to
21-
/// offer intellisense by default.
19+
/// Note that this does not cfg the function for nvptx(64), that is explicit so that rust analyzer
20+
/// is able to offer intellisense by default.
2221
#[proc_macro_attribute]
2322
pub fn kernel(input: proc_macro::TokenStream, item: proc_macro::TokenStream) -> TokenStream {
2423
let cloned = input.clone();
25-
let _ = parse_macro_input!(input as KernelHints);
2624
let input = parse_macro_input!(cloned as proc_macro2::TokenStream);
2725
let mut item = parse_macro_input!(item as ItemFn);
2826
let no_mangle = parse_quote!(#[unsafe(no_mangle)]);
2927
item.attrs.push(no_mangle);
3028
let internal = parse_quote!(#[cfg_attr(target_arch="nvptx64", nvvm_internal::kernel(#input))]);
3129
item.attrs.push(internal);
3230

33-
// used to guarantee some things about how params are passed in the codegen.
31+
// Used to guarantee some things about how params are passed in the codegen.
3432
item.sig.abi = Some(parse_quote!(extern "C"));
3533

3634
let check_fn = parse_quote! {
@@ -71,80 +69,10 @@ pub fn kernel(input: proc_macro::TokenStream, item: proc_macro::TokenStream) ->
7169
item.to_token_stream().into()
7270
}
7371

74-
#[derive(Debug, Clone, Copy, PartialEq)]
75-
enum Dimension {
76-
Dim1,
77-
Dim2,
78-
Dim3,
79-
}
80-
81-
impl Parse for Dimension {
82-
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
83-
let val = Ident::parse(input)?;
84-
let val = val.to_string();
85-
match val.as_str() {
86-
"1d" | "1D" => Ok(Self::Dim1),
87-
"2d" | "2D" => Ok(Self::Dim2),
88-
"3d" | "3D" => Ok(Self::Dim3),
89-
_ => Err(syn::Error::new(Span::call_site(), "Invalid dimension")),
90-
}
91-
}
92-
}
93-
94-
enum KernelHint {
95-
GridDim(Dimension),
96-
BlockDim(Dimension),
97-
}
98-
99-
impl Parse for KernelHint {
100-
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
101-
let name = Ident::parse(input)?;
102-
let key = name.to_string();
103-
<Token![=]>::parse(input)?;
104-
match key.as_str() {
105-
"grid_dim" => {
106-
let dim = Dimension::parse(input)?;
107-
Ok(Self::GridDim(dim))
108-
}
109-
"block_dim" => {
110-
let dim = Dimension::parse(input)?;
111-
Ok(Self::BlockDim(dim))
112-
}
113-
_ => Err(Error::new(Span::call_site(), "Unrecognized option")),
114-
}
115-
}
116-
}
117-
118-
#[derive(Debug, Default, Clone, PartialEq)]
119-
struct KernelHints {
120-
grid_dim: Option<Dimension>,
121-
block_dim: Option<Dimension>,
122-
}
123-
124-
impl Parse for KernelHints {
125-
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
126-
let iter = Punctuated::<KernelHint, Token![,]>::parse_terminated(input)?;
127-
let hints = iter
128-
.into_pairs()
129-
.map(|x| x.into_value())
130-
.collect::<Vec<_>>();
131-
132-
let mut out = KernelHints::default();
133-
134-
for hint in hints {
135-
match hint {
136-
KernelHint::GridDim(dim) => out.grid_dim = Some(dim),
137-
KernelHint::BlockDim(dim) => out.block_dim = Some(dim),
138-
}
139-
}
140-
141-
Ok(out)
142-
}
143-
}
144-
14572
// derived from rust-gpu's gpu_only
14673

147-
/// Creates a cpu version of the function which panics and cfg-gates the function for only nvptx/nvptx64.
74+
/// Creates a cpu version of the function which panics and cfg-gates the function for only
75+
/// nvptx/nvptx64.
14876
#[proc_macro_attribute]
14977
pub fn gpu_only(_attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -> TokenStream {
15078
let syn::ItemFn {

0 commit comments

Comments
 (0)