cairo-native 0.9.0-rc.6

A compiler to convert Cairo's IR Sierra code to MLIR and execute it.
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#![cfg(test)]

use cairo_lang_sierra::{
    extensions::{
        branch_align::BranchAlignLibfunc,
        core::CoreType,
        enm::{EnumInitLibfunc, EnumType},
        lib_func::{SierraApChange, SignatureSpecializationContext},
        structure::{StructConstructLibfunc, StructType},
        type_specialization_context::TypeSpecializationContext,
        types::TypeInfo,
        ConcreteType, GenericLibfunc, GenericType, NamedLibfunc, NamedType,
    },
    ids::{
        ConcreteLibfuncId, ConcreteTypeId, FunctionId, GenericLibfuncId, GenericTypeId, UserTypeId,
        VarId,
    },
    program::{
        BranchInfo, BranchTarget, ConcreteLibfuncLongId, DeclaredTypeInfo, Function,
        FunctionSignature, GenericArg, Invocation, LibfuncDeclaration, Param, Program, Statement,
        StatementIdx, TypeDeclaration,
    },
};
use std::collections::HashMap;
use std::{
    cell::{OnceCell, RefCell},
    iter::once,
    marker::PhantomData,
};

#[derive(Debug)]
pub struct SierraGenerator<T>
where
    T: GenericLibfunc,
{
    program: Program,
    phantom: PhantomData<T>,
}

impl<T> Default for SierraGenerator<T>
where
    T: GenericLibfunc,
{
    fn default() -> Self {
        Self {
            program: Program {
                type_declarations: Vec::new(),
                libfunc_declarations: Vec::new(),
                statements: Vec::new(),
                funcs: Vec::new(),
            },
            phantom: PhantomData,
        }
    }
}

impl<T> SierraGenerator<T>
where
    T: GenericLibfunc,
{
    pub fn build(self, generic_args: impl Into<Vec<GenericArg>>) -> Program {
        match T::supported_ids().as_slice() {
            [generic_id] => self.build_with_generic_id(generic_id.clone(), generic_args.into()),
            _ => panic!("multiple generic ids detected, please use build_with_generic_id directly"),
        }
    }

    pub fn build_with_generic_id(
        mut self,
        generic_id: GenericLibfuncId,
        generic_args: impl Into<Vec<GenericArg>>,
    ) -> Program {
        let generic_args = generic_args.into();

        let libfunc = T::by_id(&generic_id).unwrap();
        let libfunc_signature = libfunc
            .specialize_signature(
                &SierraGeneratorWrapper {
                    generator: RefCell::new(&mut self),
                    type_info_cache: RefCell::new(HashMap::new()),
                },
                &generic_args,
            )
            .unwrap();

        // Push the libfunc declaration.
        let libfunc_id = self
            .push_libfunc_declaration(ConcreteLibfuncLongId {
                generic_id,
                generic_args: generic_args.to_vec(),
            })
            .clone();

        // Generate packed types.
        let num_builtins = libfunc_signature
            .param_signatures
            .iter()
            .take_while(|param_signature| {
                let long_id = &self
                    .program
                    .type_declarations
                    .iter()
                    .find(|type_declaration| type_declaration.id == param_signature.ty)
                    .unwrap()
                    .long_id;

                matches!(
                    long_id.generic_id.0.as_str(),
                    "Bitwise"
                        | "EcOp"
                        | "GasBuiltin"
                        | "BuiltinCosts"
                        | "RangeCheck"
                        | "RangeCheck96"
                        | "Pedersen"
                        | "Poseidon"
                        | "Coupon"
                        | "System"
                        | "SegmentArena"
                        | "AddMod"
                        | "MulMod"
                )
            })
            .count();

        let mut return_types = Vec::with_capacity(libfunc_signature.branch_signatures.len());
        let mut packed_unit_type_id = None;
        for branch_signature in &libfunc_signature.branch_signatures {
            assert!(branch_signature
                .vars
                .iter()
                .zip(libfunc_signature.param_signatures.iter().take(num_builtins))
                .all(|(lhs, rhs)| lhs.ty == rhs.ty));

            return_types.push(match branch_signature.vars.len() - num_builtins {
                0 => match libfunc_signature.branch_signatures.len() {
                    1 => ResultVarType::Empty(None),
                    _ => ResultVarType::Empty(Some(
                        packed_unit_type_id
                            .get_or_insert_with(|| {
                                self.push_type_declaration::<StructType>(&[GenericArg::UserType(
                                    UserTypeId::from_string("Tuple"),
                                )])
                                .clone()
                            })
                            .clone(),
                    )),
                },
                1 => ResultVarType::Single(branch_signature.vars[num_builtins].ty.clone()),
                _ => ResultVarType::Multi(
                    self.push_type_declaration::<StructType>(
                        once(GenericArg::UserType(UserTypeId::from_string("Tuple")))
                            .chain(
                                branch_signature
                                    .vars
                                    .iter()
                                    .skip(num_builtins)
                                    .map(|var_info| GenericArg::Type(var_info.ty.clone())),
                            )
                            .collect::<Vec<_>>(),
                    )
                    .clone(),
                ),
            });
        }

        // Generate switch type.
        let return_type = match return_types.len() {
            1 => match return_types[0].clone() {
                ResultVarType::Empty(ty) => ty.unwrap().clone(),
                ResultVarType::Single(ty) => ty.clone(),
                ResultVarType::Multi(ty) => ty.clone(),
            },
            _ => self
                .push_type_declaration::<EnumType>(
                    once(GenericArg::UserType(UserTypeId::from_string("Tuple")))
                        .chain(return_types.iter().map(|ty| {
                            GenericArg::Type(match ty {
                                ResultVarType::Empty(ty) => ty.clone().unwrap(),
                                ResultVarType::Single(ty) => ty.clone(),
                                ResultVarType::Multi(ty) => ty.clone(),
                            })
                        }))
                        .collect::<Vec<_>>(),
                )
                .clone(),
        };

        // Generate function declaration.
        self.program.funcs.push(Function {
            id: FunctionId::new(0),
            signature: FunctionSignature {
                param_types: libfunc_signature
                    .param_signatures
                    .iter()
                    .map(|param_signature| param_signature.ty.clone())
                    .collect(),
                ret_types: libfunc_signature.param_signatures[..num_builtins]
                    .iter()
                    .map(|param_signature| param_signature.ty.clone())
                    .chain(once(return_type.clone()))
                    .collect(),
            },
            params: libfunc_signature
                .param_signatures
                .iter()
                .enumerate()
                .map(|(idx, param_signature)| Param {
                    id: VarId::new(idx as u64),
                    ty: param_signature.ty.clone(),
                })
                .collect(),
            entry_point: StatementIdx(0),
        });

        // Generate statements.
        let mut libfunc_invocation = Invocation {
            libfunc_id,
            args: libfunc_signature
                .param_signatures
                .iter()
                .enumerate()
                .map(|(idx, _)| VarId::new(idx as u64))
                .collect(),
            branches: Vec::new(),
        };

        let branch_align_libfunc = OnceCell::new();
        let construct_unit_libfunc = packed_unit_type_id.map(|ty| {
            self.push_libfunc_declaration(ConcreteLibfuncLongId {
                generic_id: GenericLibfuncId::from_string(StructConstructLibfunc::STR_ID),
                generic_args: vec![GenericArg::Type(ty)],
            })
            .clone()
        });

        for (branch_index, branch_signature) in
            libfunc_signature.branch_signatures.iter().enumerate()
        {
            if libfunc_signature.branch_signatures.len() > 1 {
                let branch_align_libfunc_id = branch_align_libfunc
                    .get_or_init(|| {
                        self.push_libfunc_declaration(ConcreteLibfuncLongId {
                            generic_id: GenericLibfuncId::from_string(BranchAlignLibfunc::STR_ID),
                            generic_args: Vec::new(),
                        })
                        .clone()
                    })
                    .clone();
                self.program
                    .statements
                    .push(Statement::Invocation(Invocation {
                        libfunc_id: branch_align_libfunc_id,
                        args: Vec::new(),
                        branches: vec![BranchInfo {
                            target: BranchTarget::Fallthrough,
                            results: Vec::new(),
                        }],
                    }));
            }

            let branch_target = match branch_index {
                0 => BranchTarget::Fallthrough,
                _ => {
                    let statement_idx = StatementIdx(self.program.statements.len());
                    BranchTarget::Statement(statement_idx)
                }
            };

            // Maybe pack values.
            match &return_types[branch_index] {
                ResultVarType::Empty(Some(_)) => {
                    self.program
                        .statements
                        .push(Statement::Invocation(Invocation {
                            libfunc_id: construct_unit_libfunc.clone().unwrap(),
                            args: Vec::new(),
                            branches: vec![BranchInfo {
                                target: BranchTarget::Fallthrough,
                                results: vec![VarId::new(num_builtins as u64)],
                            }],
                        }));
                }
                ResultVarType::Multi(type_id) => {
                    let construct_libfunc_id = self
                        .push_libfunc_declaration(ConcreteLibfuncLongId {
                            generic_id: GenericLibfuncId::from_string(
                                StructConstructLibfunc::STR_ID,
                            ),
                            generic_args: vec![GenericArg::Type(type_id.clone())],
                        })
                        .clone();

                    self.program
                        .statements
                        .push(Statement::Invocation(Invocation {
                            libfunc_id: construct_libfunc_id,
                            args: (num_builtins..branch_signature.vars.len())
                                .map(|x| VarId::new(x as u64))
                                .collect(),
                            branches: vec![BranchInfo {
                                target: BranchTarget::Fallthrough,
                                results: vec![VarId::new(num_builtins as u64)],
                            }],
                        }));
                }
                _ => {}
            }

            // Maybe enum values.
            if libfunc_signature.branch_signatures.len() > 1 {
                let enum_libfunc_id = self
                    .push_libfunc_declaration(ConcreteLibfuncLongId {
                        generic_id: GenericLibfuncId::from_string(EnumInitLibfunc::STR_ID),
                        generic_args: vec![
                            GenericArg::Type(return_type.clone()),
                            GenericArg::Value(branch_index.into()),
                        ],
                    })
                    .clone();

                self.program
                    .statements
                    .push(Statement::Invocation(Invocation {
                        libfunc_id: enum_libfunc_id,
                        args: vec![VarId::new(num_builtins as u64)],
                        branches: vec![BranchInfo {
                            target: BranchTarget::Fallthrough,
                            results: vec![VarId::new(num_builtins as u64)],
                        }],
                    }));
            }

            // Return.
            self.program.statements.push(Statement::Return(
                (0..=num_builtins).map(|x| VarId::new(x as u64)).collect(),
            ));

            // Push the branch target.
            libfunc_invocation.branches.push(BranchInfo {
                target: branch_target,
                results: branch_signature
                    .vars
                    .iter()
                    .enumerate()
                    .map(|(idx, _)| VarId::new(idx as u64))
                    .collect(),
            });
        }

        self.program
            .statements
            .insert(0, Statement::Invocation(libfunc_invocation));

        self.program
    }

    pub fn push_type_declaration<U>(
        &mut self,
        generic_args: impl Into<Vec<GenericArg>>,
    ) -> &ConcreteTypeId
    where
        U: NamedType,
    {
        self.push_type_declaration_with_generic_id::<U>(U::ID, generic_args)
    }

    pub fn push_type_declaration_with_generic_id<U>(
        &mut self,
        generic_id: GenericTypeId,
        generic_args: impl Into<Vec<GenericArg>>,
    ) -> &ConcreteTypeId
    where
        U: GenericType,
    {
        let generic_args = generic_args.into();

        let type_info = U::by_id(&generic_id)
            .unwrap()
            .specialize(
                &SierraGeneratorWrapper {
                    generator: RefCell::new(self),
                    type_info_cache: RefCell::new(HashMap::new()),
                },
                &generic_args,
            )
            .unwrap()
            .info()
            .clone();
        let current_index = self
            .program
            .type_declarations
            .iter()
            .enumerate()
            .find_map(|(idx, type_decl)| (type_decl.long_id == type_info.long_id).then_some(idx));

        let current_index = current_index.unwrap_or_else(|| {
            let idx = self.program.type_declarations.len();
            self.program.type_declarations.push(TypeDeclaration {
                id: ConcreteTypeId::new(idx as u64),
                long_id: type_info.long_id,
                declared_type_info: Some(DeclaredTypeInfo {
                    storable: type_info.storable,
                    droppable: type_info.droppable,
                    duplicatable: type_info.duplicatable,
                    zero_sized: type_info.zero_sized,
                }),
            });
            idx
        });
        &self.program.type_declarations[current_index].id
    }

    fn push_libfunc_declaration(&mut self, long_id: ConcreteLibfuncLongId) -> &ConcreteLibfuncId {
        let id = ConcreteLibfuncId::new(self.program.libfunc_declarations.len() as u64);
        self.program
            .libfunc_declarations
            .push(LibfuncDeclaration { id, long_id });

        &self.program.libfunc_declarations.last().unwrap().id
    }
}

struct SierraGeneratorWrapper<'a, T>
where
    T: GenericLibfunc,
{
    generator: RefCell<&'a mut SierraGenerator<T>>,
    type_info_cache: RefCell<HashMap<ConcreteTypeId, Box<TypeInfo>>>,
}

impl<T> SignatureSpecializationContext for SierraGeneratorWrapper<'_, T>
where
    T: GenericLibfunc,
{
    fn try_get_concrete_type(
        &self,
        id: GenericTypeId,
        generic_args: &[GenericArg],
    ) -> Option<ConcreteTypeId> {
        Some(
            self.generator
                .borrow_mut()
                .push_type_declaration_with_generic_id::<CoreType>(id, generic_args)
                .clone(),
        )
    }

    fn try_get_function_signature(&self, _function_id: &FunctionId) -> Option<FunctionSignature> {
        todo!()
    }

    fn try_get_function_ap_change(&self, _function_id: &FunctionId) -> Option<SierraApChange> {
        todo!()
    }
}

impl<T> TypeSpecializationContext for SierraGeneratorWrapper<'_, T>
where
    T: GenericLibfunc,
{
    fn try_get_type_info<'a>(&'a self, id: &ConcreteTypeId) -> Option<&'a TypeInfo> {
        // Check if already cached
        if self.type_info_cache.borrow().contains_key(id) {
            // SAFETY: The TypeInfo is heap-allocated inside a Box, so its address is
            // stable even if the HashMap rehashes. We never remove entries from the cache.
            let cache = self.type_info_cache.borrow();
            let ptr: *const TypeInfo = &**cache.get(id).unwrap();
            return Some(unsafe { &*ptr });
        }

        // Find and cache the type info
        let type_info = self
            .generator
            .borrow()
            .program
            .type_declarations
            .iter()
            .find_map(|type_declaration| {
                (type_declaration.id == *id).then(|| {
                    let declared_type_info = type_declaration.declared_type_info.as_ref().unwrap();
                    TypeInfo {
                        long_id: type_declaration.long_id.clone(),
                        storable: declared_type_info.storable,
                        droppable: declared_type_info.droppable,
                        duplicatable: declared_type_info.duplicatable,
                        zero_sized: declared_type_info.zero_sized,
                    }
                })
            })?;

        self.type_info_cache
            .borrow_mut()
            .insert(id.clone(), Box::new(type_info));

        // SAFETY: The TypeInfo is heap-allocated inside a Box, so its address is
        // stable even if the HashMap rehashes. We never remove entries from the cache.
        let cache = self.type_info_cache.borrow();
        let ptr: *const TypeInfo = &**cache.get(id).unwrap();
        Some(unsafe { &*ptr })
    }
}

#[derive(Clone)]
enum ResultVarType {
    Empty(Option<ConcreteTypeId>),
    Single(ConcreteTypeId),
    Multi(ConcreteTypeId),
}

#[cfg(test)]
mod test {
    use super::*;
    use cairo_lang_sierra::extensions::{
        array::ArrayNewLibfunc,
        bounded_int::BoundedIntIsZeroLibfunc,
        bytes31::Bytes31FromFelt252Trait,
        int::{
            signed::{Sint8Traits, SintDiffLibfunc},
            unsigned::{Uint32Type, Uint64Traits, Uint8Type},
            unsigned128::U128GuaranteeMulLibfunc,
            IntConstLibfunc,
        },
        try_from_felt252::TryFromFelt252Libfunc,
    };

    #[test]
    fn sierra_generator() {
        let program = SierraGenerator::<IntConstLibfunc<Uint64Traits>>::default()
            .build(&[GenericArg::Value(0.into())]);
        println!("{program}");
    }

    #[test]
    fn sierra_generator_multiret() {
        let program = SierraGenerator::<U128GuaranteeMulLibfunc>::default().build(&[]);
        println!("{program}");
    }

    #[test]
    fn sierra_generator_multibranch() {
        let program = SierraGenerator::<SintDiffLibfunc<Sint8Traits>>::default().build(&[]);
        println!("{program}");
    }

    #[test]
    fn sierra_generator_template() {
        let program = {
            let mut generator = SierraGenerator::<ArrayNewLibfunc>::default();

            let u8_type = generator.push_type_declaration::<Uint8Type>(&[]).clone();

            generator.build(&[GenericArg::Type(u8_type)])
        };
        println!("{program}");
    }

    #[test]
    fn sierra_generator_type_info() {
        let program = {
            let mut generator = SierraGenerator::<BoundedIntIsZeroLibfunc>::default();

            let u32_type = generator.push_type_declaration::<Uint32Type>(&[]).clone();

            generator.build(&[GenericArg::Type(u32_type)])
        };
        println!("{program}");
    }

    #[test]
    fn sierra_generator_branch_align() {
        let program =
            SierraGenerator::<TryFromFelt252Libfunc<Bytes31FromFelt252Trait>>::default().build(&[]);
        println!("{program}");
    }

    #[test]
    fn sierra_generator_type_generation() {
        let mut generator =
            SierraGenerator::<cairo_lang_sierra::extensions::array::ArrayGetLibfunc>::default();
        let u32_ty = generator.push_type_declaration::<Uint32Type>(&[]).clone();
        let array_ty = generator
            .push_type_declaration::<cairo_lang_sierra::extensions::array::ArrayType>(&[
                GenericArg::Type(u32_ty),
            ])
            .clone();

        let program = generator.build(&[GenericArg::Type(array_ty)]);
        println!("{program}");
    }
}