cubecl-macros 0.11.0-pre.1

Procedural macros for CubeCL
Documentation
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use std::collections::{HashMap, HashSet};

use inflections::case::to_snake_case;
use quote::{ToTokens, format_ident, quote};
use syn::{
    FnArg, GenericArgument, GenericParam, Generics, Ident, Lifetime, Path, Signature, TraitItemFn,
    Type, TypeMacro, TypeParamBound, parse, parse_quote, punctuated::Punctuated, spanned::Spanned,
    token::Mut, visit_mut::VisitMut,
};

use crate::{
    RemoveHelpers,
    parse::{
        helpers::{is_comptime_attr, is_define_attribute},
        kernel::{DefinedGeneric, KernelArgs, expand_kernel_ty},
        statement::parse_pat,
    },
    paths::prelude_type,
    statement::Pattern,
};

#[derive(Clone, Debug)]
pub struct KernelSignature {
    pub name: Ident,
    pub parameters: Vec<KernelParam>,
    pub returns: KernelReturns,
    pub generics: Generics,
    pub receiver_arg: Option<FnArg>,
    pub scope_lifetime: Option<Lifetime>,
}

impl KernelSignature {
    pub fn from_signature(mut sig: Signature, args: &KernelArgs) -> syn::Result<Self> {
        let name = sig.ident;
        let mut generics = sig.generics;
        let num_lifetime_params = generics.lifetimes().count();

        let input_lifetimes: HashSet<Lifetime> = sig
            .inputs
            .iter()
            .flat_map(|arg| match arg {
                FnArg::Receiver(receiver) => type_used_lifetimes(&receiver.ty),
                FnArg::Typed(pat_type) => type_used_lifetimes(&pat_type.ty),
            })
            .collect();
        let output_lifetimes = match &sig.output {
            syn::ReturnType::Default => HashSet::new(),
            syn::ReturnType::Type(_, ty) => type_used_lifetimes(ty),
        };

        // Need a separate lifetime for the scope if the output uses any lifetimes, or if we have
        // explicit input params (because mixing can cause issues with early vs late binding)
        let needs_explicit_lifetimes = !output_lifetimes.is_empty() || num_lifetime_params > 0;
        let has_inferred_lifetimes = input_lifetimes.contains(&parse_quote!('_))
            || output_lifetimes.contains(&parse_quote!('_));

        let mut scope_lifetime = None;

        if needs_explicit_lifetimes {
            let lifetime: Lifetime = parse_quote!('scope);
            generics
                .params
                .insert(num_lifetime_params, parse_quote!(#lifetime));
            scope_lifetime = Some(lifetime);
        }

        if needs_explicit_lifetimes && has_inferred_lifetimes {
            generics.params.insert(0, parse_quote!('infer));
            for input in sig.inputs.iter_mut() {
                match input {
                    FnArg::Receiver(receiver) => {
                        type_patch_inferred_lifetimes(&mut receiver.ty);
                        if let Some((token, lifetime)) = receiver.reference.as_mut() {
                            match lifetime {
                                Some(lifetime) if lifetime.ident == "_" => {
                                    lifetime.ident = Ident::new("infer", lifetime.ident.span());
                                }
                                Some(_) => {}
                                other => {
                                    *other = Some(Lifetime::new("'infer", token.span()));
                                }
                            }
                        }
                    }
                    FnArg::Typed(pat_type) => type_patch_inferred_lifetimes(&mut pat_type.ty),
                }
            }
            match &mut sig.output {
                syn::ReturnType::Default => {}
                syn::ReturnType::Type(_, ty) => type_patch_inferred_lifetimes(ty),
            }
        }

        let returns = match sig.output {
            syn::ReturnType::Default => KernelReturns::ExpandType(parse_quote![()]),
            syn::ReturnType::Type(_, ty) => match *ty.clone() {
                Type::Macro(TypeMacro { mac }) => {
                    if mac.path.is_ident("comptime_type") {
                        let inner_type = parse::<Type>(mac.tokens.into())
                            .expect("Interior of comptime_type macro should be a valid type.");
                        KernelReturns::Plain(inner_type)
                    } else {
                        panic!("Only comptime_type macro supported on return type")
                    }
                }
                _ => KernelReturns::ExpandType(*ty),
            },
        };
        let receiver_arg = sig
            .inputs
            .iter()
            .find(|it| matches!(it, FnArg::Receiver(_)))
            .cloned();
        let sig_params = sig
            .inputs
            .into_iter()
            .map(KernelParam::from_param)
            .collect::<Result<Vec<_>, _>>()?;
        let manually_defined_params = sig_params
            .iter()
            .flat_map(|it| it.defines.iter().map(|it| it.ident()))
            .collect::<Vec<_>>();
        let define_params = generics
            .type_params()
            .filter(|it| !manually_defined_params.contains(&&it.ident))
            .filter(|it| {
                it.attrs.iter().any(is_define_attribute)
                    // define sizes by default on launch functions
                    || (args.is_launch() && it.bounds.to_token_stream().to_string() == "Size")
            })
            .map(|ty_param| {
                let type_ = prelude_type("Type");
                let ident = &ty_param.ident;
                let name = format_ident!("_{}", to_snake_case(&ident.to_string()));
                let is_size = ty_param.bounds.to_token_stream().to_string() == "Size";
                let ty = match is_size {
                    true => quote![usize],
                    false => quote![#type_],
                };
                KernelParam::from_param(parse_quote!(#[define(#ident)] #name: #ty))
            })
            .collect::<Result<Vec<_>, _>>()?;

        let parameters = define_params
            .into_iter()
            .chain(sig_params)
            .collect::<Vec<_>>();

        RemoveHelpers.visit_generics_mut(&mut generics);

        Ok(KernelSignature {
            generics,
            name,
            parameters,
            returns,
            receiver_arg,
            scope_lifetime,
        })
    }

    pub fn from_trait_fn(function: TraitItemFn, args: &KernelArgs) -> syn::Result<Self> {
        Self::from_signature(function.sig, args)
    }

    /// If the type is self, we set the returns type to plain instead of expand
    /// type.
    pub fn plain_self(&mut self) {
        if let Type::Path(pat) = self.returns.ty()
            && pat.path.is_ident("Self")
        {
            self.returns = KernelReturns::Plain(self.returns.ty());
        }

        for param in self.parameters.iter_mut() {
            if let Type::Path(pat) = &param.ty
                && pat.path.is_ident("Self")
            {
                param.normalized_ty = parse_quote!(Self);
            }
        }
    }

    pub fn runtime_params(&self) -> impl Iterator<Item = &KernelParam> {
        self.parameters.iter().filter(|it| !it.is_const)
    }

    pub fn define_mappings(&self) -> HashMap<Ident, (Ident, Option<usize>)> {
        let mut mapping = HashMap::new();
        for param in self.parameters.iter() {
            for define in param.defines.iter() {
                match define {
                    DefinedGeneric::Single(ident) => {
                        mapping.insert(ident.clone(), (param.name.clone(), None));
                    }
                    DefinedGeneric::Multiple(ident, index) => {
                        mapping.insert(ident.clone(), (param.name.clone(), Some(*index)));
                    }
                }
            }
        }
        mapping
    }

    pub fn call_generics(&self) -> Generics {
        let mut generics = self.generics.clone();
        generics.params = generics
            .params
            .into_iter()
            .filter(|param| !matches!(param, GenericParam::Lifetime(..)))
            .collect();
        generics
    }
}

#[derive(Clone, Debug)]
pub enum KernelReturns {
    ExpandType(Type),
    Plain(Type),
}

impl KernelReturns {
    pub fn ty(&self) -> Type {
        match self {
            KernelReturns::ExpandType(ty) => ty.clone(),
            KernelReturns::Plain(ty) => ty.clone(),
        }
    }
}

#[derive(Clone, Debug)]
pub struct KernelParam {
    pub name: Ident,
    pub ty: Type,
    pub normalized_ty: Type,
    pub defines: Vec<DefinedGeneric>,
    pub is_const: bool,
    pub mutability: Option<Mut>,
}

impl KernelParam {
    pub fn from_param(param: FnArg) -> syn::Result<Self> {
        let param = match param {
            FnArg::Typed(param) => param,
            FnArg::Receiver(param) => {
                let normalized_ty = expand_kernel_ty(*param.ty.clone(), false)?;

                let mutability = if param.reference.is_none() {
                    param.mutability
                } else {
                    None
                };

                return Ok(KernelParam {
                    name: Ident::new("self", param.span()),
                    ty: *param.ty,
                    normalized_ty,
                    defines: Vec::new(),
                    is_const: false,
                    mutability,
                });
            }
        };
        let Pattern {
            ident,
            is_ref,
            mutability,
            ..
        } = parse_pat(*param.pat.clone())?;
        let mut is_const = false;
        let mut defines = Vec::new();

        for attr in param.attrs.iter() {
            if is_comptime_attr(attr) {
                is_const = true;
            }
            if is_define_attribute(attr) {
                match attr.parse_args::<Ident>() {
                    Ok(ident) => {
                        defines.push(DefinedGeneric::Single(ident));
                    }
                    Err(_) => {
                        let list = attr.meta.require_list().expect("Wrong syntax.");
                        let tokens = list.tokens.to_string();
                        let names = tokens.split(",");
                        for (i, name) in names.enumerate() {
                            let ident = Ident::new(name.trim(), attr.span());
                            defines.push(DefinedGeneric::Multiple(ident, i));
                        }
                    }
                };
                is_const = true;
            }
        }

        let ty = *param.ty.clone();
        let normalized_ty = expand_kernel_ty(*param.ty, is_const)?;

        let mut_token = if !is_ref { mutability } else { None };

        Ok(Self {
            name: ident,
            ty,
            defines,
            normalized_ty,
            is_const,
            mutability: mut_token,
        })
    }

    /// If the type is self, we set the normalized ty to self as well.
    ///
    /// Useful when the param is used in functions or methods associated to the
    /// expand type.
    pub fn plain_normalized_self(&mut self) {
        fn is_self(ty: &Type) -> bool {
            match ty {
                Type::Path(type_path) if type_path.path.is_ident("Self") => true,
                Type::Ptr(type_ptr) => is_self(&type_ptr.elem),
                Type::Reference(type_reference) => is_self(&type_reference.elem),
                _ => false,
            }
        }
        if is_self(&self.ty) {
            self.normalized_ty = self.ty.clone();
        }
    }
}

fn type_used_lifetimes(ty: &Type) -> HashSet<Lifetime> {
    match ty {
        Type::Array(type_array) => type_used_lifetimes(&type_array.elem),
        Type::Group(type_group) => type_used_lifetimes(&type_group.elem),
        Type::ImplTrait(type_impl_trait) => type_impl_trait
            .bounds
            .iter()
            .flat_map(type_param_bound_used_lifetimes)
            .collect(),
        Type::Paren(type_paren) => type_used_lifetimes(&type_paren.elem),
        Type::Path(type_path) => path_used_lifetimes(&type_path.path),
        Type::Ptr(type_ptr) => type_used_lifetimes(&type_ptr.elem),
        Type::Reference(type_reference) => {
            let lifetime = type_reference
                .lifetime
                .clone()
                .unwrap_or_else(|| Lifetime::new("'_", type_reference.and_token.span()));
            [lifetime].into_iter().collect()
        }
        Type::Slice(type_slice) => type_used_lifetimes(&type_slice.elem),
        Type::TraitObject(type_trait_object) => type_trait_object
            .bounds
            .iter()
            .flat_map(type_param_bound_used_lifetimes)
            .collect(),
        Type::Tuple(type_tuple) => type_tuple
            .elems
            .iter()
            .flat_map(type_used_lifetimes)
            .collect(),
        _ => HashSet::default(),
    }
}

fn type_param_bound_used_lifetimes(bound: &TypeParamBound) -> HashSet<Lifetime> {
    let mut out = HashSet::default();
    match bound {
        TypeParamBound::Trait(trait_bound) => {
            out.extend(path_used_lifetimes(&trait_bound.path));
        }
        TypeParamBound::Lifetime(lifetime) => {
            out.insert(lifetime.clone());
        }
        _ => {}
    }
    out
}

fn path_used_lifetimes(path: &Path) -> HashSet<Lifetime> {
    let mut out = HashSet::default();

    for arg in path.segments.iter().flat_map(|it| match &it.arguments {
        syn::PathArguments::AngleBracketed(args) => args.args.clone(),
        _ => Punctuated::new(),
    }) {
        out.extend(generic_arg_used_lifetimes(&arg));
    }

    out
}

fn generic_arg_used_lifetimes(arg: &GenericArgument) -> HashSet<Lifetime> {
    let mut out = HashSet::new();
    match arg {
        syn::GenericArgument::Lifetime(lifetime) => {
            out.insert(lifetime.clone());
        }
        syn::GenericArgument::Type(ty) => out.extend(type_used_lifetimes(ty)),
        syn::GenericArgument::AssocType(assoc_type) => {
            for arg in assoc_type.generics.iter().flat_map(|it| it.args.iter()) {
                out.extend(generic_arg_used_lifetimes(arg));
            }
            out.extend(type_used_lifetimes(&assoc_type.ty))
        }
        syn::GenericArgument::AssocConst(assoc_const) => {
            for arg in assoc_const.generics.iter().flat_map(|it| it.args.iter()) {
                out.extend(generic_arg_used_lifetimes(arg));
            }
        }
        syn::GenericArgument::Constraint(constraint) => {
            for arg in constraint.generics.iter().flat_map(|it| it.args.iter()) {
                out.extend(generic_arg_used_lifetimes(arg));
            }
            for bound in constraint.bounds.iter() {
                out.extend(type_param_bound_used_lifetimes(bound));
            }
        }
        _ => {}
    }
    out
}

fn type_patch_inferred_lifetimes(ty: &mut Type) {
    match ty {
        Type::Array(type_array) => type_patch_inferred_lifetimes(&mut type_array.elem),
        Type::Group(type_group) => type_patch_inferred_lifetimes(&mut type_group.elem),
        Type::ImplTrait(type_impl_trait) => {
            for bound in type_impl_trait.bounds.iter_mut() {
                type_param_bound_patch_inferred_lifetimes(bound);
            }
        }
        Type::Paren(type_paren) => type_patch_inferred_lifetimes(&mut type_paren.elem),
        Type::Path(type_path) => path_patch_inferred_lifetimes(&mut type_path.path),
        Type::Ptr(type_ptr) => type_patch_inferred_lifetimes(&mut type_ptr.elem),
        Type::Reference(type_reference) => {
            let span = type_reference.and_token.span();
            match &mut type_reference.lifetime {
                lifetime @ None => {
                    *lifetime = Some(Lifetime::new("'infer", span));
                }
                Some(lifetime) if lifetime.ident == "_" => {
                    lifetime.ident = Ident::new("infer", lifetime.ident.span());
                }
                _ => {}
            }
        }
        Type::Slice(type_slice) => type_patch_inferred_lifetimes(&mut type_slice.elem),
        Type::TraitObject(type_trait_object) => {
            for bound in type_trait_object.bounds.iter_mut() {
                type_param_bound_patch_inferred_lifetimes(bound);
            }
        }
        Type::Tuple(type_tuple) => {
            for ty in type_tuple.elems.iter_mut() {
                type_patch_inferred_lifetimes(ty);
            }
        }
        _ => {}
    }
}

fn type_param_bound_patch_inferred_lifetimes(bound: &mut TypeParamBound) {
    match bound {
        TypeParamBound::Trait(trait_bound) => {
            path_patch_inferred_lifetimes(&mut trait_bound.path);
        }
        TypeParamBound::Lifetime(lifetime) if lifetime.ident == "_" => {
            lifetime.ident = Ident::new("infer", lifetime.ident.span());
        }
        _ => {}
    }
}

fn path_patch_inferred_lifetimes(path: &mut Path) {
    for arg in path
        .segments
        .iter_mut()
        .filter_map(|it| match &mut it.arguments {
            syn::PathArguments::AngleBracketed(args) => Some(&mut args.args),
            _ => None,
        })
        .flatten()
    {
        generic_arg_patch_inferred_lifetimes(arg);
    }
}

fn generic_arg_patch_inferred_lifetimes(arg: &mut GenericArgument) {
    match arg {
        syn::GenericArgument::Lifetime(lifetime) if lifetime.ident == "_" => {
            lifetime.ident = Ident::new("infer", lifetime.ident.span());
        }
        syn::GenericArgument::Type(ty) => type_patch_inferred_lifetimes(ty),
        syn::GenericArgument::AssocType(assoc_type) => {
            for arg in assoc_type
                .generics
                .iter_mut()
                .flat_map(|it| it.args.iter_mut())
            {
                generic_arg_patch_inferred_lifetimes(arg);
            }
            type_patch_inferred_lifetimes(&mut assoc_type.ty);
        }
        syn::GenericArgument::AssocConst(assoc_const) => {
            for arg in assoc_const
                .generics
                .iter_mut()
                .flat_map(|it| it.args.iter_mut())
            {
                generic_arg_patch_inferred_lifetimes(arg);
            }
        }
        syn::GenericArgument::Constraint(constraint) => {
            for arg in constraint
                .generics
                .iter_mut()
                .flat_map(|it| it.args.iter_mut())
            {
                generic_arg_patch_inferred_lifetimes(arg);
            }
            for bound in constraint.bounds.iter_mut() {
                type_param_bound_patch_inferred_lifetimes(bound)
            }
        }
        _ => {}
    }
}