cairo-native 0.8.0

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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! # Debug utilities
//!
//! A collection of utilities to debug values in MLIR in execution.
//!
//! ## Example
//!
//! ```rust,ignore
//! # use cairo_lang_sierra::{
//! #     extensions::{
//! #         core::{CoreLibfunc, CoreType},
//! #         lib_func::SignatureAndTypeConcreteLibfunc,
//! #         GenericType,
//! #         GenericLibfunc,
//! #     },
//! #     program_registry::ProgramRegistry,
//! # };
//! # use cairo_native::{
//! #     error::{
//! #         Error, Result,
//! #     },
//! #     libfuncs::{LibfuncBuilder, LibfuncHelper},
//! #     metadata::{debug_utils::DebugUtils, MetadataStorage},
//! #     types::TypeBuilder,
//! #     utils::ProgramRegistryExt,
//! # };
//! # use melior::{
//! #     dialect::llvm,
//! #     ir::{
//! #         attribute::DenseI64ArrayAttribute,
//! #         r#type::IntegerType,
//! #         Block,
//! #         Location,
//! #     },
//! #     Context,
//! # };
//!
//! pub fn build_array_len<'ctx, 'this>(
//!     context: &'ctx Context,
//!     registry: &ProgramRegistry<CoreType, CoreLibfunc>,
//!     entry: &'this Block<'ctx>,
//!     location: Location<'ctx>,
//!     helper: &LibfuncHelper<'ctx, 'this>,
//!     metadata: &mut MetadataStorage,
//!     info: &SignatureAndTypeConcreteLibfunc,
//! ) -> Result<()>
//! {
//!     let array_val = entry.arg(0)?;
//!     let elem_ty = registry.build_type(context, helper, registry, metadata, &info.ty)?;
//!
//!     #[cfg(feature = "with-debug-utils")]
//!     {
//!         let array_ptr = entry
//!             .append_operation(llvm::extract_value(
//!                 context,
//!                 array_val,
//!                 DenseI64ArrayAttribute::new(context, &[0]),
//!                 elem_ty,
//!                 location,
//!             ))
//!             .result(0)?
//!             .into();
//!
//!         metadata.get_mut::<DebugUtils>()
//!             .ok_or(Error::MissingMetadata)?
//!             .print_pointer(context, helper, entry, array_ptr, location)?;
//!     }
//!
//!     let array_len = entry
//!         .append_operation(llvm::extract_value(
//!             context,
//!             array_val,
//!             DenseI64ArrayAttribute::new(context, &[1]),
//!             IntegerType::new(context, 32).into(),
//!             location,
//!         ))
//!         .result(0)?
//!         .into();
//!
//!     entry.append_operation(helper.br(0, &[array_len], location));
//!     Ok(())
//! }
//! ```

#![cfg(feature = "with-debug-utils")]

use crate::{
    error::{Error, Result},
    utils::get_integer_layout,
};
use melior::{
    dialect::{
        arith,
        llvm::{self},
        ods,
    },
    helpers::{ArithBlockExt, BuiltinBlockExt, LlvmBlockExt},
    ir::{
        attribute::{FlatSymbolRefAttribute, IntegerAttribute, StringAttribute, TypeAttribute},
        operation::OperationBuilder,
        r#type::IntegerType,
        Attribute, Block, BlockLike, Location, Module, Region, Value,
    },
    Context,
};
use num_bigint::BigUint;
use std::{collections::HashSet, ffi::c_void};

#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
enum DebugBinding {
    BreakpointMarker,
    PrintStr,
    PrintI1,
    PrintI8,
    PrintI32,
    PrintI64,
    PrintI128,
    PrintPointer,
    PrintFelt252,
    DumpMemRegion,
}

impl DebugBinding {
    const fn symbol(self) -> &'static str {
        match self {
            DebugBinding::BreakpointMarker => "cairo_native__debug__breakpoint_marker_impl",
            DebugBinding::PrintStr => "cairo_native__debug__print_str_impl",
            DebugBinding::PrintI1 => "cairo_native__debug__print_i1_impl",
            DebugBinding::PrintI8 => "cairo_native__debug__print_i8_impl",
            DebugBinding::PrintI32 => "cairo_native__debug__print_i32_impl",
            DebugBinding::PrintI64 => "cairo_native__debug__print_i64_impl",
            DebugBinding::PrintI128 => "cairo_native__debug__print_i128_impl",
            DebugBinding::PrintPointer => "cairo_native__debug__print_pointer_impl",
            DebugBinding::PrintFelt252 => "cairo_native__debug__print_felt252_impl",
            DebugBinding::DumpMemRegion => "cairo_native__debug__dump_mem_region_impl",
        }
    }
    const fn function_ptr(self) -> *const () {
        match self {
            DebugBinding::BreakpointMarker => breakpoint_marker_impl as *const (),
            DebugBinding::PrintStr => print_str_impl as *const (),
            DebugBinding::PrintI1 => print_i1_impl as *const (),
            DebugBinding::PrintI8 => print_i8_impl as *const (),
            DebugBinding::PrintI32 => print_i32_impl as *const (),
            DebugBinding::PrintI64 => print_i64_impl as *const (),
            DebugBinding::PrintI128 => print_i128_impl as *const (),
            DebugBinding::PrintPointer => print_pointer_impl as *const (),
            DebugBinding::PrintFelt252 => print_felt252_impl as *const (),
            DebugBinding::DumpMemRegion => dump_mem_region_impl as *const (),
        }
    }
}

#[derive(Debug, Default)]
pub struct DebugUtils {
    active_map: HashSet<DebugBinding>,
}

impl DebugUtils {
    /// Register the global for the given binding, if not yet registered, and return
    /// a pointer to the stored function.
    ///
    /// For the function to be available, `setup_runtime` must be called before running the module
    fn build_function<'c, 'a>(
        &mut self,
        context: &'c Context,
        module: &Module,
        block: &'a Block<'c>,
        location: Location<'c>,
        binding: DebugBinding,
    ) -> Result<Value<'c, 'a>> {
        if self.active_map.insert(binding) {
            module.body().append_operation(
                ods::llvm::mlir_global(
                    context,
                    Region::new(),
                    TypeAttribute::new(llvm::r#type::pointer(context, 0)),
                    StringAttribute::new(context, binding.symbol()),
                    Attribute::parse(context, "#llvm.linkage<weak>")
                        .ok_or(Error::ParseAttributeError)?,
                    location,
                )
                .into(),
            );
        }

        let global_address = block.append_op_result(
            ods::llvm::mlir_addressof(
                context,
                llvm::r#type::pointer(context, 0),
                FlatSymbolRefAttribute::new(context, binding.symbol()),
                location,
            )
            .into(),
        )?;

        Ok(block.load(
            context,
            location,
            global_address,
            llvm::r#type::pointer(context, 0),
        )?)
    }

    pub fn breakpoint_marker(
        &mut self,
        context: &Context,
        module: &Module,
        block: &Block,
        location: Location,
    ) -> Result<()> {
        let function = self.build_function(
            context,
            module,
            block,
            location,
            DebugBinding::BreakpointMarker,
        )?;

        block.append_operation(
            OperationBuilder::new("llvm.call", location)
                .add_operands(&[function])
                .build()?,
        );

        Ok(())
    }

    pub fn debug_breakpoint_trap(&self, block: &Block, location: Location) -> Result<()> {
        block.append_operation(OperationBuilder::new("llvm.intr.debugtrap", location).build()?);
        Ok(())
    }

    /// Prints the given &str.
    pub fn debug_print(
        &mut self,
        context: &Context,
        module: &Module,
        block: &Block,
        message: &str,
        location: Location,
    ) -> Result<()> {
        let function =
            self.build_function(context, module, block, location, DebugBinding::PrintStr)?;

        let ty = llvm::r#type::array(
            IntegerType::new(context, 8).into(),
            message
                .len()
                .try_into()
                .map_err(|_| Error::IntegerConversion)?,
        );

        let ptr = block.alloca1(context, location, ty, get_integer_layout(8).align())?;

        let msg = block
            .append_operation(
                ods::llvm::mlir_constant(
                    context,
                    llvm::r#type::array(
                        IntegerType::new(context, 8).into(),
                        message
                            .len()
                            .try_into()
                            .map_err(|_| Error::IntegerConversion)?,
                    ),
                    StringAttribute::new(context, message).into(),
                    location,
                )
                .into(),
            )
            .result(0)?
            .into();
        block.append_operation(ods::llvm::store(context, msg, ptr, location).into());
        let len = block
            .append_operation(arith::constant(
                context,
                IntegerAttribute::new(
                    IntegerType::new(context, 64).into(),
                    message
                        .len()
                        .try_into()
                        .map_err(|_| Error::IntegerConversion)?,
                )
                .into(),
                location,
            ))
            .result(0)?
            .into();

        block.append_operation(
            OperationBuilder::new("llvm.call", location)
                .add_operands(&[function])
                .add_operands(&[ptr, len])
                .build()?,
        );

        Ok(())
    }

    pub fn print_pointer(
        &mut self,
        context: &Context,
        module: &Module,
        block: &Block,
        value: Value,
        location: Location,
    ) -> Result<()> {
        let function =
            self.build_function(context, module, block, location, DebugBinding::PrintPointer)?;

        block.append_operation(
            OperationBuilder::new("llvm.call", location)
                .add_operands(&[function])
                .add_operands(&[value])
                .build()?,
        );

        Ok(())
    }

    pub fn print_i1(
        &mut self,
        context: &Context,
        module: &Module,
        block: &Block,
        value: Value,
        location: Location,
    ) -> Result<()> {
        let function =
            self.build_function(context, module, block, location, DebugBinding::PrintI1)?;

        block.append_operation(
            OperationBuilder::new("llvm.call", location)
                .add_operands(&[function])
                .add_operands(&[value])
                .build()?,
        );

        Ok(())
    }

    pub fn print_felt252(
        &mut self,
        context: &Context,
        module: &Module,
        block: &Block,
        value: Value,
        location: Location,
    ) -> Result<()> {
        let function =
            self.build_function(context, module, block, location, DebugBinding::PrintFelt252)?;

        let k64 = block
            .append_operation(arith::constant(
                context,
                IntegerAttribute::new(IntegerType::new(context, 252).into(), 64).into(),
                location,
            ))
            .result(0)?
            .into();

        let l0 = block
            .append_operation(arith::trunci(
                value,
                IntegerType::new(context, 64).into(),
                location,
            ))
            .result(0)?
            .into();
        let value = block
            .append_operation(arith::shrui(value, k64, location))
            .result(0)?
            .into();
        let l1 = block
            .append_operation(arith::trunci(
                value,
                IntegerType::new(context, 64).into(),
                location,
            ))
            .result(0)?
            .into();
        let value = block
            .append_operation(arith::shrui(value, k64, location))
            .result(0)?
            .into();
        let l2 = block
            .append_operation(arith::trunci(
                value,
                IntegerType::new(context, 64).into(),
                location,
            ))
            .result(0)?
            .into();
        let value = block
            .append_operation(arith::shrui(value, k64, location))
            .result(0)?
            .into();
        let l3 = block
            .append_operation(arith::trunci(
                value,
                IntegerType::new(context, 64).into(),
                location,
            ))
            .result(0)?
            .into();

        block.append_operation(
            OperationBuilder::new("llvm.call", location)
                .add_operands(&[function])
                .add_operands(&[l0, l1, l2, l3])
                .build()?,
        );

        Ok(())
    }

    pub fn print_i8(
        &mut self,
        context: &Context,
        module: &Module,
        block: &Block,
        value: Value,
        location: Location,
    ) -> Result<()> {
        let function =
            self.build_function(context, module, block, location, DebugBinding::PrintI8)?;

        block.append_operation(
            OperationBuilder::new("llvm.call", location)
                .add_operands(&[function])
                .add_operands(&[value])
                .build()?,
        );

        Ok(())
    }

    pub fn print_i32(
        &mut self,
        context: &Context,
        module: &Module,
        block: &Block,
        value: Value,
        location: Location,
    ) -> Result<()> {
        let function =
            self.build_function(context, module, block, location, DebugBinding::PrintI32)?;

        block.append_operation(
            OperationBuilder::new("llvm.call", location)
                .add_operands(&[function])
                .add_operands(&[value])
                .build()?,
        );

        Ok(())
    }

    pub fn print_i64(
        &mut self,
        context: &Context,
        module: &Module,
        block: &Block,
        value: Value,
        location: Location,
    ) -> Result<()> {
        let function =
            self.build_function(context, module, block, location, DebugBinding::PrintI64)?;

        block.append_operation(
            OperationBuilder::new("llvm.call", location)
                .add_operands(&[function])
                .add_operands(&[value])
                .build()?,
        );

        Ok(())
    }

    pub fn print_i128(
        &mut self,
        context: &Context,
        module: &Module,
        block: &Block,
        value: Value,
        location: Location,
    ) -> Result<()> {
        let function =
            self.build_function(context, module, block, location, DebugBinding::PrintI128)?;

        let i64_ty = IntegerType::new(context, 64).into();
        let k64 = block
            .append_operation(arith::constant(
                context,
                IntegerAttribute::new(IntegerType::new(context, 128).into(), 64).into(),
                location,
            ))
            .result(0)?
            .into();

        let value_lo = block
            .append_operation(arith::trunci(value, i64_ty, location))
            .result(0)?
            .into();
        let value_hi = block
            .append_operation(arith::shrui(value, k64, location))
            .result(0)?
            .into();
        let value_hi = block
            .append_operation(arith::trunci(value_hi, i64_ty, location))
            .result(0)?
            .into();

        block.append_operation(
            OperationBuilder::new("llvm.call", location)
                .add_operands(&[function])
                .add_operands(&[value_lo, value_hi])
                .build()?,
        );

        Ok(())
    }

    /// Dump a memory region at runtime.
    ///
    /// Requires the pointer (at runtime) and its length in bytes (at compile-time).
    pub fn dump_mem(
        &mut self,
        context: &Context,
        module: &Module,
        block: &Block,
        ptr: Value,
        len: usize,
        location: Location,
    ) -> Result<()> {
        let function = self.build_function(
            context,
            module,
            block,
            location,
            DebugBinding::DumpMemRegion,
        )?;

        let len = block.const_int(context, location, len, 64)?;

        block.append_operation(
            OperationBuilder::new("llvm.call", location)
                .add_operands(&[function])
                .add_operands(&[ptr, len])
                .build()?,
        );

        Ok(())
    }
}

pub fn setup_runtime(find_symbol_ptr: impl Fn(&str) -> Option<*mut c_void>) {
    for binding in [
        DebugBinding::BreakpointMarker,
        DebugBinding::PrintStr,
        DebugBinding::PrintI1,
        DebugBinding::PrintI8,
        DebugBinding::PrintI32,
        DebugBinding::PrintI64,
        DebugBinding::PrintI128,
        DebugBinding::PrintPointer,
        DebugBinding::PrintFelt252,
        DebugBinding::DumpMemRegion,
    ] {
        if let Some(global) = find_symbol_ptr(binding.symbol()) {
            let global = global.cast::<*const ()>();
            unsafe { *global = binding.function_ptr() };
        }
    }
}

extern "C" fn breakpoint_marker_impl() {
    println!("[DEBUG] Breakpoint marker.");
}

extern "C" fn print_str_impl(message: *const std::ffi::c_char, len: u64) {
    // llvm constant strings are not zero terminated
    let slice = unsafe { std::slice::from_raw_parts(message as *const u8, len as usize) };
    let message = std::str::from_utf8(slice);

    if let Ok(message) = message {
        println!("[DEBUG] {}", message);
    } else {
        println!("[DEBUG] {:?}", message);
    }
}

extern "C" fn print_i1_impl(value: bool) {
    println!("[DEBUG] {value}");
}

extern "C" fn print_i8_impl(value: u8) {
    println!("[DEBUG] {value}");
}

extern "C" fn print_i32_impl(value: u32) {
    println!("[DEBUG] {value}");
}

extern "C" fn print_i64_impl(value: u64) {
    println!("[DEBUG] {value}");
}

extern "C" fn print_i128_impl(value_lo: u64, value_hi: u64) {
    let value = ((value_hi as u128) << 64) | value_lo as u128;
    println!("[DEBUG] {value}");
}

extern "C" fn print_pointer_impl(value: *const ()) {
    println!("[DEBUG] {value:018x?}");
}

unsafe extern "C" fn dump_mem_region_impl(ptr: *const (), len: u64) {
    println!("[DEBUG] Memory dump at {ptr:?}:");
    for chunk in (0..len).step_by(8) {
        print!("  {:?}:", ptr.byte_add(chunk as usize));
        for offset in chunk..chunk + 8 {
            print!(" {:02x}", ptr.byte_add(offset as usize).cast::<u8>().read());
        }
        println!();
    }
}

extern "C" fn print_felt252_impl(l0: u64, l1: u64, l2: u64, l3: u64) {
    println!(
        "[DEBUG] {}",
        BigUint::from_bytes_le(
            &l0.to_le_bytes()
                .into_iter()
                .chain(l1.to_le_bytes())
                .chain(l2.to_le_bytes())
                .chain(l3.to_le_bytes())
                .collect::<Vec<_>>(),
        ),
    );
}