@@ -4,10 +4,10 @@ mod test;
4
4
5
5
use std:: sync:: Arc ;
6
6
7
- use cairo_lang_defs:: ids:: { ExternFunctionId , ModuleId } ;
7
+ use cairo_lang_defs:: ids:: { ExternFunctionId , FreeFunctionId } ;
8
8
use cairo_lang_semantic:: helper:: ModuleHelper ;
9
9
use cairo_lang_semantic:: items:: constant:: { ConstCalcInfo , ConstValue } ;
10
- use cairo_lang_semantic:: items:: functions:: GenericFunctionWithBodyId ;
10
+ use cairo_lang_semantic:: items:: functions:: { GenericFunctionId , GenericFunctionWithBodyId } ;
11
11
use cairo_lang_semantic:: items:: imp:: ImplLookupContext ;
12
12
use cairo_lang_semantic:: { GenericArgumentId , MatchArmSelector , TypeId , corelib} ;
13
13
use cairo_lang_utils:: ordered_hash_map:: OrderedHashMap ;
@@ -69,7 +69,7 @@ pub fn const_folding(
69
69
// Skipping const-folding for `panic_with_const_felt252` - to avoid replacing a call to
70
70
// `panic_with_felt252` with `panic_with_const_felt252` and causing accidental recursion.
71
71
if function_id. base_semantic_function ( db) . generic_function ( db)
72
- == libfunc_info. panic_with_const_felt252
72
+ == GenericFunctionWithBodyId :: Free ( libfunc_info. panic_with_const_felt252 )
73
73
{
74
74
return ;
75
75
}
@@ -282,11 +282,8 @@ impl ConstFoldingContext<'_> {
282
282
if stmt. function == self . panic_with_felt252 {
283
283
let val = self . as_const ( stmt. inputs [ 0 ] . var_id ) ?;
284
284
stmt. inputs . clear ( ) ;
285
- stmt. function = ModuleHelper :: core ( db)
286
- . function_id (
287
- "panic_with_const_felt252" ,
288
- vec ! [ GenericArgumentId :: Constant ( val. clone( ) . intern( db) ) ] ,
289
- )
285
+ stmt. function = GenericFunctionId :: Free ( self . panic_with_const_felt252 )
286
+ . concretize ( db, vec ! [ GenericArgumentId :: Constant ( val. clone( ) . intern( db) ) ] )
290
287
. lowered ( db) ;
291
288
return None ;
292
289
}
@@ -334,14 +331,9 @@ impl ConstFoldingContext<'_> {
334
331
let input_var = stmt. inputs [ 0 ] . var_id ;
335
332
if let Some ( ConstValue :: Int ( val, ty) ) = self . as_const ( input_var) {
336
333
stmt. inputs . clear ( ) ;
337
- stmt. function = ModuleHelper { db, id : self . storage_access_module }
338
- . function_id (
339
- "storage_base_address_const" ,
340
- vec ! [ GenericArgumentId :: Constant (
341
- ConstValue :: Int ( val. clone( ) , * ty) . intern( db) ,
342
- ) ] ,
343
- )
344
- . lowered ( db) ;
334
+ let arg = GenericArgumentId :: Constant ( ConstValue :: Int ( val. clone ( ) , * ty) . intern ( db) ) ;
335
+ stmt. function =
336
+ self . storage_base_address_const . concretize ( db, vec ! [ arg] ) . lowered ( db) ;
345
337
}
346
338
None
347
339
} else if id == self . into_box {
@@ -575,9 +567,8 @@ impl ConstFoldingContext<'_> {
575
567
let unused_arr_output0 = self . variables . alloc ( self . variables [ arr] . clone ( ) ) ;
576
568
let unused_arr_output1 = self . variables . alloc ( self . variables [ arr] . clone ( ) ) ;
577
569
info. inputs . truncate ( 1 ) ;
578
- info. function = ModuleHelper { db, id : self . array_module }
579
- . function_id ( "array_snapshot_pop_front" , generic_args)
580
- . lowered ( db) ;
570
+ info. function =
571
+ self . array_snapshot_pop_front . concretize ( db, generic_args) . lowered ( db) ;
581
572
success. var_ids . insert ( 0 , unused_arr_output0) ;
582
573
failure. var_ids . insert ( 0 , unused_arr_output1) ;
583
574
}
@@ -667,18 +658,18 @@ pub struct ConstFoldingLibfuncInfo {
667
658
bounded_int_sub : ExternFunctionId ,
668
659
/// The `bounded_int_constrain` libfunc.
669
660
bounded_int_constrain : ExternFunctionId ,
670
- /// The array module.
671
- array_module : ModuleId ,
672
661
/// The `array_get` libfunc.
673
662
array_get : ExternFunctionId ,
674
- /// The storage access module .
675
- storage_access_module : ModuleId ,
663
+ /// The `array_snapshot_pop_front` libfunc .
664
+ array_snapshot_pop_front : GenericFunctionId ,
676
665
/// The `storage_base_address_from_felt252` libfunc.
677
666
storage_base_address_from_felt252 : ExternFunctionId ,
667
+ /// The `storage_base_address_const` libfunc.
668
+ storage_base_address_const : GenericFunctionId ,
678
669
/// The `core::panic_with_felt252` function.
679
670
panic_with_felt252 : FunctionId ,
680
671
/// The `core::panic_with_const_felt252` function.
681
- panic_with_const_felt252 : GenericFunctionWithBodyId ,
672
+ panic_with_const_felt252 : FreeFunctionId ,
682
673
/// Type ranges.
683
674
type_value_ranges : OrderedHashMap < TypeId , TypeInfo > ,
684
675
/// The info used for semantic const calculation.
@@ -687,17 +678,12 @@ pub struct ConstFoldingLibfuncInfo {
687
678
impl ConstFoldingLibfuncInfo {
688
679
fn new ( db : & dyn LoweringGroup ) -> Self {
689
680
let core = ModuleHelper :: core ( db) ;
690
- let felt_sub = core. extern_function_id ( "felt252_sub" ) ;
691
681
let box_module = core. submodule ( "box" ) ;
692
- let into_box = box_module. extern_function_id ( "into_box" ) ;
693
682
let integer_module = core. submodule ( "integer" ) ;
694
683
let bounded_int_module = core. submodule ( "internal" ) . submodule ( "bounded_int" ) ;
695
684
let array_module = core. submodule ( "array" ) ;
696
- let array_get = array_module. extern_function_id ( "array_get" ) ;
697
685
let starknet_module = core. submodule ( "starknet" ) ;
698
686
let storage_access_module = starknet_module. submodule ( "storage_access" ) ;
699
- let storage_base_address_from_felt252 =
700
- storage_access_module. extern_function_id ( "storage_base_address_from_felt252" ) ;
701
687
let nz_fns = OrderedHashSet :: < _ > :: from_iter ( chain ! (
702
688
[
703
689
core. extern_function_id( "felt252_is_zero" ) ,
@@ -737,9 +723,6 @@ impl ConstFoldingLibfuncInfo {
737
723
[ bounded_int_module. extern_function_id( "bounded_int_div_rem" ) ] ,
738
724
utypes. map( |ty| integer_module. extern_function_id( format!( "{ty}_safe_divmod" ) ) ) ,
739
725
) ) ;
740
- let bounded_int_add = bounded_int_module. extern_function_id ( "bounded_int_add" ) ;
741
- let bounded_int_sub = bounded_int_module. extern_function_id ( "bounded_int_sub" ) ;
742
- let bounded_int_constrain = bounded_int_module. extern_function_id ( "bounded_int_constrain" ) ;
743
726
let type_value_ranges = OrderedHashMap :: from_iter (
744
727
[
745
728
( "u8" , BigInt :: ZERO , u8:: MAX . into ( ) , false ) ,
@@ -770,8 +753,8 @@ impl ConstFoldingLibfuncInfo {
770
753
) ,
771
754
) ;
772
755
Self {
773
- felt_sub,
774
- into_box,
756
+ felt_sub : core . extern_function_id ( "felt252_sub" ) ,
757
+ into_box : box_module . extern_function_id ( "into_box" ) ,
775
758
nz_fns,
776
759
eq_fns,
777
760
uadd_fns,
@@ -781,17 +764,17 @@ impl ConstFoldingLibfuncInfo {
781
764
isub_fns,
782
765
wide_mul_fns,
783
766
div_rem_fns,
784
- bounded_int_add,
785
- bounded_int_sub,
786
- bounded_int_constrain,
787
- array_module : array_module. id ,
788
- array_get,
789
- storage_access_module : storage_access_module. id ,
790
- storage_base_address_from_felt252,
767
+ bounded_int_add : bounded_int_module. extern_function_id ( "bounded_int_add" ) ,
768
+ bounded_int_sub : bounded_int_module. extern_function_id ( "bounded_int_sub" ) ,
769
+ bounded_int_constrain : bounded_int_module. extern_function_id ( "bounded_int_constrain" ) ,
770
+ array_get : array_module. extern_function_id ( "array_get" ) ,
771
+ array_snapshot_pop_front : array_module. generic_function_id ( "array_snapshot_pop_front" ) ,
772
+ storage_base_address_from_felt252 : storage_access_module
773
+ . extern_function_id ( "storage_base_address_from_felt252" ) ,
774
+ storage_base_address_const : storage_access_module
775
+ . generic_function_id ( "storage_base_address_const" ) ,
791
776
panic_with_felt252 : core. function_id ( "panic_with_felt252" , vec ! [ ] ) . lowered ( db) ,
792
- panic_with_const_felt252 : GenericFunctionWithBodyId :: Free (
793
- core. free_function_id ( "panic_with_const_felt252" ) ,
794
- ) ,
777
+ panic_with_const_felt252 : core. free_function_id ( "panic_with_const_felt252" ) ,
795
778
type_value_ranges,
796
779
const_calculation_info : db. const_calc_info ( ) ,
797
780
}
0 commit comments