Skip to content

Commit 77747a8

Browse files
committed
Minor refactoring for const-folding.
commit-id:be8b16ce
1 parent 5838f4e commit 77747a8

File tree

1 file changed

+27
-44
lines changed

1 file changed

+27
-44
lines changed

crates/cairo-lang-lowering/src/optimizations/const_folding.rs

+27-44
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ mod test;
44

55
use std::sync::Arc;
66

7-
use cairo_lang_defs::ids::{ExternFunctionId, ModuleId};
7+
use cairo_lang_defs::ids::{ExternFunctionId, FreeFunctionId};
88
use cairo_lang_semantic::helper::ModuleHelper;
99
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};
1111
use cairo_lang_semantic::items::imp::ImplLookupContext;
1212
use cairo_lang_semantic::{GenericArgumentId, MatchArmSelector, TypeId, corelib};
1313
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
@@ -69,7 +69,7 @@ pub fn const_folding(
6969
// Skipping const-folding for `panic_with_const_felt252` - to avoid replacing a call to
7070
// `panic_with_felt252` with `panic_with_const_felt252` and causing accidental recursion.
7171
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)
7373
{
7474
return;
7575
}
@@ -282,11 +282,8 @@ impl ConstFoldingContext<'_> {
282282
if stmt.function == self.panic_with_felt252 {
283283
let val = self.as_const(stmt.inputs[0].var_id)?;
284284
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))])
290287
.lowered(db);
291288
return None;
292289
}
@@ -334,14 +331,9 @@ impl ConstFoldingContext<'_> {
334331
let input_var = stmt.inputs[0].var_id;
335332
if let Some(ConstValue::Int(val, ty)) = self.as_const(input_var) {
336333
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);
345337
}
346338
None
347339
} else if id == self.into_box {
@@ -575,9 +567,8 @@ impl ConstFoldingContext<'_> {
575567
let unused_arr_output0 = self.variables.alloc(self.variables[arr].clone());
576568
let unused_arr_output1 = self.variables.alloc(self.variables[arr].clone());
577569
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);
581572
success.var_ids.insert(0, unused_arr_output0);
582573
failure.var_ids.insert(0, unused_arr_output1);
583574
}
@@ -667,18 +658,18 @@ pub struct ConstFoldingLibfuncInfo {
667658
bounded_int_sub: ExternFunctionId,
668659
/// The `bounded_int_constrain` libfunc.
669660
bounded_int_constrain: ExternFunctionId,
670-
/// The array module.
671-
array_module: ModuleId,
672661
/// The `array_get` libfunc.
673662
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,
676665
/// The `storage_base_address_from_felt252` libfunc.
677666
storage_base_address_from_felt252: ExternFunctionId,
667+
/// The `storage_base_address_const` libfunc.
668+
storage_base_address_const: GenericFunctionId,
678669
/// The `core::panic_with_felt252` function.
679670
panic_with_felt252: FunctionId,
680671
/// The `core::panic_with_const_felt252` function.
681-
panic_with_const_felt252: GenericFunctionWithBodyId,
672+
panic_with_const_felt252: FreeFunctionId,
682673
/// Type ranges.
683674
type_value_ranges: OrderedHashMap<TypeId, TypeInfo>,
684675
/// The info used for semantic const calculation.
@@ -687,17 +678,12 @@ pub struct ConstFoldingLibfuncInfo {
687678
impl ConstFoldingLibfuncInfo {
688679
fn new(db: &dyn LoweringGroup) -> Self {
689680
let core = ModuleHelper::core(db);
690-
let felt_sub = core.extern_function_id("felt252_sub");
691681
let box_module = core.submodule("box");
692-
let into_box = box_module.extern_function_id("into_box");
693682
let integer_module = core.submodule("integer");
694683
let bounded_int_module = core.submodule("internal").submodule("bounded_int");
695684
let array_module = core.submodule("array");
696-
let array_get = array_module.extern_function_id("array_get");
697685
let starknet_module = core.submodule("starknet");
698686
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");
701687
let nz_fns = OrderedHashSet::<_>::from_iter(chain!(
702688
[
703689
core.extern_function_id("felt252_is_zero"),
@@ -737,9 +723,6 @@ impl ConstFoldingLibfuncInfo {
737723
[bounded_int_module.extern_function_id("bounded_int_div_rem")],
738724
utypes.map(|ty| integer_module.extern_function_id(format!("{ty}_safe_divmod"))),
739725
));
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");
743726
let type_value_ranges = OrderedHashMap::from_iter(
744727
[
745728
("u8", BigInt::ZERO, u8::MAX.into(), false),
@@ -770,8 +753,8 @@ impl ConstFoldingLibfuncInfo {
770753
),
771754
);
772755
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"),
775758
nz_fns,
776759
eq_fns,
777760
uadd_fns,
@@ -781,17 +764,17 @@ impl ConstFoldingLibfuncInfo {
781764
isub_fns,
782765
wide_mul_fns,
783766
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"),
791776
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"),
795778
type_value_ranges,
796779
const_calculation_info: db.const_calc_info(),
797780
}

0 commit comments

Comments
 (0)