pulp_macro/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use syn::punctuated::Punctuated;
use syn::token::{Colon, PathSep};
use syn::{
    ConstParam, FnArg, GenericParam, ItemFn, LifetimeParam, Pat, PatIdent, PatType, Path,
    PathSegment, Type, TypeParam, TypePath,
};

#[proc_macro_attribute]
pub fn with_simd(
    attr: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let attr: TokenStream = attr.into();
    let item: TokenStream = item.into();
    let Ok(syn::Meta::NameValue(attr)) = syn::parse2::<syn::Meta>(attr.clone()) else {
        return quote! {
            ::core::compile_error!("pulp::with_simd expected function name and arch expression");
            #item
        }
        .into();
    };
    let Some(name) = attr.path.get_ident() else {
        return quote! {
            ::core::compile_error!("pulp::with_simd expected function name and arch expression");
            #item
        }
        .into();
    };
    let Ok(item) = syn::parse2::<syn::ItemFn>(item.clone()) else {
        return quote! {
            ::core::compile_error!("pulp::with_simd expected function");
            #item
        }
        .into();
    };

    let ItemFn {
        attrs,
        vis,
        sig,
        block,
    } = item.clone();

    let mut struct_generics = Vec::new();
    let mut struct_field_names = Vec::new();
    let mut struct_field_types = Vec::new();

    let mut first_non_lifetime = usize::MAX;
    for (idx, param) in sig.generics.params.clone().into_pairs().enumerate() {
        let (param, _) = param.into_tuple();
        match &param {
            syn::GenericParam::Lifetime(_) => {}
            _ => {
                if first_non_lifetime == usize::MAX {
                    first_non_lifetime = idx;
                    continue;
                }
            }
        }
    }
    let mut new_fn_sig = sig.clone();
    new_fn_sig.generics.params = new_fn_sig
        .generics
        .params
        .into_iter()
        .enumerate()
        .filter(|(idx, _)| *idx != first_non_lifetime)
        .map(|(_, arg)| arg)
        .collect();
    new_fn_sig.inputs = new_fn_sig
        .inputs
        .into_iter()
        .skip(1)
        .enumerate()
        .map(|(idx, arg)| {
            FnArg::Typed(PatType {
                attrs: Vec::new(),
                pat: Box::new(Pat::Ident(PatIdent {
                    attrs: Vec::new(),
                    by_ref: None,
                    mutability: None,
                    ident: Ident::new(&format!("__{idx}"), Span::call_site()),
                    subpat: None,
                })),
                colon_token: Colon {
                    spans: [Span::call_site()],
                },
                ty: match arg {
                    FnArg::Typed(ty) => ty.ty,
                    FnArg::Receiver(_) => panic!(),
                },
            })
        })
        .collect();
    new_fn_sig.ident = name.clone();
    let mut param_ty = Vec::new();

    for (idx, param) in new_fn_sig.inputs.clone().into_pairs().enumerate() {
        let (param, _) = param.into_tuple();
        let FnArg::Typed(param) = param.clone() else {
            panic!();
        };
        let name = *param.pat;
        let syn::Pat::Ident(name) = name else {
            panic!();
        };

        let anon_ty = Ident::new(&format!("__T{idx}"), Span::call_site());

        struct_field_names.push(name.ident.clone());
        let mut ty = Punctuated::<_, PathSep>::new();
        ty.push_value(PathSegment {
            ident: anon_ty.clone(),
            arguments: syn::PathArguments::None,
        });
        struct_field_types.push(Type::Path(TypePath {
            qself: None,
            path: Path {
                leading_colon: None,
                segments: ty,
            },
        }));
        struct_generics.push(anon_ty);
        param_ty.push(*param.ty);
    }

    let output_ty = match sig.output.clone() {
        syn::ReturnType::Default => quote! { () },
        syn::ReturnType::Type(_, ty) => quote! { #ty },
    };

    let fn_name = sig.ident.clone();

    let arch = attr.value;
    let new_fn_generics = new_fn_sig.generics.clone();
    let params = new_fn_generics.params.clone();
    let generics = params.into_iter().collect::<Vec<_>>();
    let non_lt_generics_names = generics
        .iter()
        .map(|p| match p {
            GenericParam::Type(TypeParam { ident, .. })
            | GenericParam::Const(ConstParam { ident, .. }) => {
                quote! { #ident, }
            }
            _ => quote! {},
        })
        .collect::<Vec<_>>();
    let generics_decl = generics
        .iter()
        .map(|p| match p {
            GenericParam::Lifetime(LifetimeParam {
                lifetime,
                colon_token,
                bounds,
                ..
            }) => {
                quote! { #lifetime #colon_token #bounds }
            }
            GenericParam::Type(TypeParam {
                ident,
                colon_token,
                bounds,
                ..
            }) => {
                quote! { #ident #colon_token #bounds }
            }
            GenericParam::Const(ConstParam {
                const_token,
                ident,
                colon_token,
                ty,
                ..
            }) => {
                quote! { #const_token #ident #colon_token #ty }
            }
        })
        .collect::<Vec<_>>();
    let generics_where_clause = new_fn_generics.where_clause;

    let code = quote! {
        #(#attrs)*
        #vis #new_fn_sig {
            #[allow(non_camel_case_types)]
            struct #name<#(#struct_generics,)*> (#(#struct_field_types,)*);

            impl<#(#generics_decl,)*> ::pulp::WithSimd for #name<
                #(#param_ty,)*
            > #generics_where_clause {
                type Output = #output_ty;

                #[inline(always)]
                fn with_simd<__S: ::pulp::Simd>(self, __simd: __S) -> <Self as ::pulp::WithSimd>::Output {
                    let Self ( #(#struct_field_names,)* ) = self;
                    #[allow(unused_unsafe)]
                    unsafe {
                        #fn_name::<__S,
                        #(#non_lt_generics_names)*
                        >(__simd, #(#struct_field_names,)*)
                    }
                }
            }

            (#arch).dispatch( #name ( #(#struct_field_names,)* ) )
        }

        #(#attrs)*
        #vis #sig #block
    };
    code.into()
}