@@ -6,8 +6,8 @@ use super::WithSelf;
66use crate :: {
77 error:: { Result , SierraAssertError } ,
88 metadata:: {
9- drop_overrides :: DropOverridesMeta , dup_overrides :: DupOverridesMeta ,
10- realloc_bindings:: ReallocBindingsMeta , MetadataStorage ,
9+ debug_utils :: DebugUtils , drop_overrides :: DropOverridesMeta ,
10+ dup_overrides :: DupOverridesMeta , realloc_bindings:: ReallocBindingsMeta , MetadataStorage ,
1111 } ,
1212 utils:: { get_integer_layout, layout_repeat, ProgramRegistryExt } ,
1313} ;
@@ -22,7 +22,7 @@ use cairo_lang_sierra::{
2222} ;
2323use melior:: {
2424 dialect:: { arith:: CmpiPredicate , func, llvm, scf} ,
25- helpers:: { ArithBlockExt , BuiltinBlockExt , LlvmBlockExt } ,
25+ helpers:: { ArithBlockExt , BuiltinBlockExt , GepIndex , LlvmBlockExt } ,
2626 ir:: { r#type:: IntegerType , Block , BlockLike , Location , Module , Region , Type , Value } ,
2727 Context ,
2828} ;
@@ -305,8 +305,6 @@ pub fn build_circuit_outputs<'ctx>(
305305 metadata : & mut MetadataStorage ,
306306 info : WithSelf < InfoOnlyConcreteType > ,
307307) -> Result < Type < ' ctx > > {
308- let u8_ty = IntegerType :: new ( context, 8 ) . into ( ) ;
309-
310308 DupOverridesMeta :: register_with (
311309 context,
312310 module,
@@ -318,15 +316,42 @@ pub fn build_circuit_outputs<'ctx>(
318316 let region = Region :: new ( ) ;
319317 let value_ty = registry. build_type ( context, module, metadata, info. self_ty ( ) ) ?;
320318 let entry = region. append_block ( Block :: new ( & [ ( value_ty, location) ] ) ) ;
321- let k1 = entry. const_int ( context, location, 1 , 8 ) ?;
319+ let k1 = entry. const_int ( context, location, 1 , 32 ) ?;
322320
323321 let outputs = entry. arg ( 0 ) ?;
324- let ref_count = entry. extract_value ( context, location, outputs, u8_ty, 0 ) ?;
322+ let gates_ptr = entry. extract_value (
323+ context,
324+ location,
325+ outputs,
326+ llvm:: r#type:: pointer ( context, 0 ) ,
327+ 0 ,
328+ ) ?;
329+ let ref_count_ptr = entry. gep (
330+ context,
331+ location,
332+ gates_ptr,
333+ & [ GepIndex :: Const ( 0 ) ] ,
334+ IntegerType :: new ( context, 8 ) . into ( ) ,
335+ ) ?;
336+ let ref_count = entry. load (
337+ context,
338+ location,
339+ ref_count_ptr,
340+ IntegerType :: new ( context, 32 ) . into ( ) ,
341+ ) ?;
325342 let ref_count_inc = entry. addi ( ref_count, k1, location) ?;
326343
327- let output_new = entry. insert_value ( context, location, outputs, ref_count_inc, 0 ) ?;
344+ metadata
345+ . get_mut :: < DebugUtils > ( )
346+ . unwrap ( )
347+ . debug_print ( context, module, & entry, "DUP" , location) ?;
348+ metadata
349+ . get_mut :: < DebugUtils > ( )
350+ . unwrap ( )
351+ . print_i32 ( context, module, & entry, ref_count, location) ?;
328352
329- entry. append_operation ( func:: r#return ( & [ output_new, output_new] , location) ) ;
353+ entry. store ( context, location, ref_count_ptr, ref_count_inc) ?;
354+ entry. append_operation ( func:: r#return ( & [ outputs, outputs] , location) ) ;
330355
331356 Ok ( Some ( region) )
332357 } ,
@@ -342,14 +367,43 @@ pub fn build_circuit_outputs<'ctx>(
342367 let region = Region :: new ( ) ;
343368 let value_ty = registry. build_type ( context, module, metadata, info. self_ty ( ) ) ?;
344369 let entry = region. append_block ( Block :: new ( & [ ( value_ty, location) ] ) ) ;
345- let k1 = entry. const_int ( context, location, 1 , 8 ) ?;
370+ let k1 = entry. const_int ( context, location, 1 , 32 ) ?;
346371
347372 let outputs = entry. arg ( 0 ) ?;
348- let ref_count = entry. extract_value ( context, location, outputs, u8_ty, 0 ) ?;
373+ let gates_ptr = entry. extract_value (
374+ context,
375+ location,
376+ outputs,
377+ llvm:: r#type:: pointer ( context, 0 ) ,
378+ 0 ,
379+ ) ?;
380+
381+ let ref_count_ptr = entry. gep (
382+ context,
383+ location,
384+ gates_ptr,
385+ & [ GepIndex :: Const ( 0 ) ] ,
386+ IntegerType :: new ( context, 8 ) . into ( ) ,
387+ ) ?;
388+ let ref_count = entry. load (
389+ context,
390+ location,
391+ ref_count_ptr,
392+ IntegerType :: new ( context, 32 ) . into ( ) ,
393+ ) ?;
349394
350395 // Check that the reference counting is different from 1. If it is equeal to 1, then it is shared.
351396 let is_shared = entry. cmpi ( context, CmpiPredicate :: Ne , ref_count, k1, location) ?;
352397
398+ metadata
399+ . get_mut :: < DebugUtils > ( )
400+ . unwrap ( )
401+ . debug_print ( context, module, & entry, "DROP" , location) ?;
402+ metadata
403+ . get_mut :: < DebugUtils > ( )
404+ . unwrap ( )
405+ . print_i32 ( context, module, & entry, ref_count, location) ?;
406+
353407 entry. append_operation ( scf:: r#if (
354408 is_shared,
355409 & [ ] ,
@@ -359,8 +413,7 @@ pub fn build_circuit_outputs<'ctx>(
359413 let entry = region. append_block ( Block :: new ( & [ ] ) ) ;
360414 let ref_count_dec = entry. subi ( ref_count, k1, location) ?;
361415
362- entry. insert_value ( context, location, ref_count_dec, outputs, 0 ) ?;
363-
416+ entry. store ( context, location, ref_count_ptr, ref_count_dec) ?;
364417 entry. append_operation ( scf:: r#yield ( & [ ] , location) ) ;
365418
366419 region
@@ -370,14 +423,6 @@ pub fn build_circuit_outputs<'ctx>(
370423 let region = Region :: new ( ) ;
371424 let entry = region. append_block ( Block :: new ( & [ ] ) ) ;
372425
373- let gates_ptr = entry. extract_value (
374- context,
375- location,
376- outputs,
377- llvm:: r#type:: pointer ( context, 0 ) ,
378- 1 ,
379- ) ?;
380-
381426 entry
382427 . append_operation ( ReallocBindingsMeta :: free ( context, gates_ptr, location) ?) ;
383428 entry. append_operation ( scf:: r#yield ( & [ ] , location) ) ;
@@ -396,7 +441,6 @@ pub fn build_circuit_outputs<'ctx>(
396441 Ok ( llvm:: r#type:: r#struct (
397442 context,
398443 & [
399- u8_ty,
400444 llvm:: r#type:: pointer ( context, 0 ) ,
401445 build_u384_struct_type ( context) ,
402446 ] ,
@@ -546,3 +590,11 @@ pub fn build_u384_struct_type(context: &Context) -> Type<'_> {
546590 false ,
547591 )
548592}
593+
594+ pub fn calc_circuit_output_prefix_layout ( ) -> Layout {
595+ let u384_layout = get_integer_layout ( 384 ) ;
596+ get_integer_layout ( 32 )
597+ . align_to ( u384_layout. align ( ) )
598+ . expect ( "layout size rounded up to the next multiple of layout alignment should never be greater than ISIZE::MAX" )
599+ . pad_to_align ( )
600+ }
0 commit comments