Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cairo-lang-lowering/src/lower/test_data/loop
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ Final lowering:
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin
blk0 (root):
Statements:
(v2: core::RangeCheck, v3: core::gas::GasBuiltin, v4: core::panics::PanicResult::<(test::A, core::felt252, ())>) <- test::foo[58-134]{0, { 0: core::felt252 }, }(v0, v1)
(v2: core::RangeCheck, v3: core::gas::GasBuiltin, v4: core::panics::PanicResult::<(test::A, core::felt252, ())>) <- test::foo[58-134]{0, { 0 }, }(v0, v1)
End:
Match(match_enum(v4) {
PanicResult::Ok(v5) => blk1,
Expand Down
201 changes: 108 additions & 93 deletions crates/cairo-lang-lowering/src/lower/test_data/specialized
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ End:
Parameters:
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v1: core::felt252) <- 1
(v2: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v2: core::felt252) <- 1
(v3: ()) <- test::bar(v0, v2)
End:
Return(v2)
Return(v3)

//! > semantic_diagnostics

Expand Down Expand Up @@ -78,11 +79,12 @@ End:
Parameters:
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v1: core::box::Box::<core::felt252>) <- 2.into_box()
(v2: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v2: core::box::Box::<core::felt252>) <- 2.into_box()
(v3: ()) <- test::bar(v0, v2)
End:
Return(v2)
Return(v3)

//! > semantic_diagnostics

Expand Down Expand Up @@ -125,11 +127,12 @@ End:
Parameters:
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v1: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v2: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v2: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v3: ()) <- test::bar(v0, v2)
End:
Return(v2)
Return(v3)

//! > semantic_diagnostics

Expand Down Expand Up @@ -176,17 +179,18 @@ End:
Parameters:
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v6: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v7: core::felt252) <- 1
(v4: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v6, v7)
(v5: core::felt252) <- 2
(v2: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v4, v5)
(v3: core::felt252) <- 3
(v1: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v2, v3)
(v8: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v7: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v8: core::felt252) <- 1
(v5: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v7, v8)
(v6: core::felt252) <- 2
(v3: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v5, v6)
(v4: core::felt252) <- 3
(v2: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v3, v4)
(v9: ()) <- test::bar(v0, v2)
End:
Return(v8)
Return(v9)

//! > semantic_diagnostics

Expand Down Expand Up @@ -229,11 +233,12 @@ End:
Parameters:
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v1: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v2: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v2: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v3: ()) <- test::bar(v0, v2)
End:
Return(v2)
Return(v3)

//! > semantic_diagnostics

Expand Down Expand Up @@ -278,19 +283,20 @@ End:
Return()

//! > specialized_lowering
Parameters: v6: core::felt252
Parameters: v7: core::felt252
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v1: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v4: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v5: core::array::Array::<core::felt252>, v3: @core::array::Array::<core::felt252>) <- snapshot(v4)
(v2: core::array::Span::<core::felt252>) <- struct_construct(v3)
(v7: core::felt252) <- 0
(v8: core::box::Box::<core::felt252>) <- 1.into_box()
(v9: core::array::Array::<core::felt252>, v10: ()) <- test::bar(v0, v1, v2, v6, v7, v8)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v2: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v5: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v6: core::array::Array::<core::felt252>, v4: @core::array::Array::<core::felt252>) <- snapshot(v5)
(v3: core::array::Span::<core::felt252>) <- struct_construct(v4)
(v8: core::felt252) <- 0
(v9: core::box::Box::<core::felt252>) <- 1.into_box()
(v10: core::array::Array::<core::felt252>, v11: ()) <- test::bar(v0, v2, v3, v7, v8, v9)
End:
Return(v9, v10)
Return(v10, v11)

//! > semantic_diagnostics

Expand Down Expand Up @@ -339,17 +345,18 @@ End:
Return()

//! > specialized_lowering
Parameters: v3: core::felt252, v5: core::felt252, v6: core::felt252
Parameters: v4: core::felt252, v6: core::felt252, v7: core::felt252
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v2: core::felt252) <- 1
(v4: core::felt252) <- 3
(v7: core::felt252) <- 6
(v1: test::S6) <- struct_construct(v2, v3, v4, v5, v6, v7)
(v8: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v3: core::felt252) <- 1
(v5: core::felt252) <- 3
(v8: core::felt252) <- 6
(v2: test::S6) <- struct_construct(v3, v4, v5, v6, v7, v8)
(v9: ()) <- test::bar(v0, v2)
End:
Return(v8)
Return(v9)

//! > semantic_diagnostics

Expand Down Expand Up @@ -405,13 +412,14 @@ End:
Return()

//! > specialized_lowering
Parameters: v1: test::SA1
Parameters: v2: test::SA1
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v2: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v3: ()) <- test::bar(v0, v2)
End:
Return(v2)
Return(v3)

//! > semantic_diagnostics

Expand Down Expand Up @@ -465,13 +473,14 @@ End:
Return()

//! > specialized_lowering
Parameters: v1: test::SA1
Parameters: v2: test::SA1
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v2: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v3: ()) <- test::bar(v0, v2)
End:
Return(v2)
Return(v3)

//! > semantic_diagnostics

Expand Down Expand Up @@ -538,13 +547,14 @@ End:
Return()

//! > specialized_lowering
Parameters: v1: test::SB
Parameters: v2: test::SB
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v2: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v3: ()) <- test::bar(v0, v2)
End:
Return(v2)
Return(v3)

//! > semantic_diagnostics

Expand Down Expand Up @@ -589,15 +599,16 @@ End:
Return()

//! > specialized_lowering
Parameters: v2: core::felt252
Parameters: v3: core::felt252
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v3: core::felt252) <- 5
(v1: test::S) <- struct_construct(v2, v3)
(v4: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v4: core::felt252) <- 5
(v2: test::S) <- struct_construct(v3, v4)
(v5: ()) <- test::bar(v0, v2)
End:
Return(v4)
Return(v5)

//! > semantic_diagnostics

Expand Down Expand Up @@ -660,23 +671,24 @@ End:
Return(v12)

//! > specialized_lowering
Parameters: v9: core::felt252, v12: core::felt252, v11: core::felt252
Parameters: v10: core::felt252, v13: core::felt252, v12: core::felt252
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v4: core::array::Array::<test::Outer>) <- core::array::array_new::<test::Outer>()
(v8: core::felt252) <- 1
(v6: test::Inner) <- struct_construct(v8, v9)
(v7: core::felt252) <- 3
(v5: test::Outer) <- struct_construct(v6, v7)
(v2: core::array::Array::<test::Outer>) <- core::array::array_append::<test::Outer>(v4, v5)
(v13: core::felt252) <- 5
(v10: test::Inner) <- struct_construct(v12, v13)
(v3: test::Outer) <- struct_construct(v10, v11)
(v1: core::array::Array::<test::Outer>) <- core::array::array_append::<test::Outer>(v2, v3)
(v14: ()) <- test::bar(v0, v1)
End:
Return(v14)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v5: core::array::Array::<test::Outer>) <- core::array::array_new::<test::Outer>()
(v9: core::felt252) <- 1
(v7: test::Inner) <- struct_construct(v9, v10)
(v8: core::felt252) <- 3
(v6: test::Outer) <- struct_construct(v7, v8)
(v3: core::array::Array::<test::Outer>) <- core::array::array_append::<test::Outer>(v5, v6)
(v14: core::felt252) <- 5
(v11: test::Inner) <- struct_construct(v13, v14)
(v4: test::Outer) <- struct_construct(v11, v12)
(v2: core::array::Array::<test::Outer>) <- core::array::array_append::<test::Outer>(v3, v4)
(v15: ()) <- test::bar(v0, v2)
End:
Return(v15)

//! > semantic_diagnostics

Expand Down Expand Up @@ -725,14 +737,15 @@ End:
Return()

//! > specialized_lowering
Parameters: v2: core::felt252
Parameters: v3: core::felt252
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v1: test::E) <- E::A(v2)
(v3: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v2: test::E) <- E::A(v3)
(v4: ()) <- test::bar(v0, v2)
End:
Return(v3)
Return(v4)

//! > ==========================================================================

Expand Down Expand Up @@ -783,16 +796,17 @@ End:
Return()

//! > specialized_lowering
Parameters: v4: core::felt252
Parameters: v5: core::felt252
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v3: core::felt252) <- 1
(v2: test::S) <- struct_construct(v3, v4)
(v1: test::E) <- E::A(v2)
(v5: ()) <- test::bar(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v4: core::felt252) <- 1
(v3: test::S) <- struct_construct(v4, v5)
(v2: test::E) <- E::A(v3)
(v6: ()) <- test::bar(v0, v2)
End:
Return(v5)
Return(v6)

//! > ==========================================================================

Expand Down Expand Up @@ -860,8 +874,9 @@ End:
Parameters:
blk0 (root):
Statements:
(v0: core::bool) <- bool::False({})
(v1: core::felt252) <- 100000
(v2: core::felt252) <- test::bar1(v0, v1)
(v1: ()) <- struct_construct()
(v0: core::bool) <- bool::False(v1)
(v2: core::felt252) <- 100000
(v3: core::felt252) <- test::bar1(v0, v2)
End:
Return(v2)
Return(v3)
31 changes: 30 additions & 1 deletion crates/cairo-lang-lowering/src/optimizations/const_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,35 @@ use crate::{
StatementStructDestructure, VarRemapping, VarUsage, Variable, VariableArena, VariableId,
};

/// Converts a const value to a specialization arg.
/// For struct and enum const values, recursively converts to SpecializationArg::Struct/Enum.
fn const_to_specialization_arg<'db>(
db: &'db dyn Database,
value: ConstValueId<'db>,
boxed: bool,
) -> SpecializationArg<'db> {
match value.long(db) {
ConstValue::Struct(members, ty) => {
// Only convert to SpecializationArg::Struct if the type is actually a concrete struct,
// not a closure or fixed size array.
if matches!(ty.long(db), TypeLongId::Concrete(ConcreteTypeId::Struct(_))) {
let args = members
.iter()
.map(|member| const_to_specialization_arg(db, *member, false))
.collect();
SpecializationArg::Struct(args)
} else {
SpecializationArg::Const { value, boxed }
}
}
ConstValue::Enum(variant, payload) => SpecializationArg::Enum {
variant: *variant,
payload: Box::new(const_to_specialization_arg(db, *payload, false)),
},
_ => SpecializationArg::Const { value, boxed },
}
}

/// Keeps track of equivalent values that variables might be replaced with.
/// Note: We don't keep track of types as we assume the usage is always correct.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -1310,7 +1339,7 @@ impl<'db, 'mt> ConstFoldingContext<'db, 'mt> {
}

match var_info {
VarInfo::Const(value) => Some(SpecializationArg::Const { value, boxed: false }),
VarInfo::Const(value) => Some(const_to_specialization_arg(self.db, value, false)),
VarInfo::Box(info) => try_extract_matches!(info.as_ref(), VarInfo::Const)
.map(|value| SpecializationArg::Const { value: *value, boxed: true }),
VarInfo::Snapshot(info) => {
Expand Down
Loading