Skip to content

Commit

Permalink
Enable match optimization with remapping when there is no future merg…
Browse files Browse the repository at this point in the history
…e. (#6337)
  • Loading branch information
liorgold2 authored Sep 5, 2024
1 parent 1360ba2 commit 004d2b4
Show file tree
Hide file tree
Showing 40 changed files with 62,522 additions and 66,298 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,7 @@ impl<'a> Analyzer<'a> for MatchOptimizerContext {
candidate.match_variable = var_usage.var_id;

if remapping.len() > 1 {
// TODO(lior): Remove the `|| true` below to enable to optimization in this case.
#[allow(clippy::overly_complex_bool_expr)]
if candidate.future_merge || candidate.additional_remappings.is_some() || true {
if candidate.future_merge || candidate.additional_remappings.is_some() {
// TODO(ilya): Support multiple remappings with future merges.

// Revoke the candidate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -903,21 +903,18 @@ End:
blk1:
Statements:
(v6: core::integer::u32) <- 0
(v7: test::Color) <- Color::Red(v2)
End:
Goto(blk5, {v6 -> v8, v7 -> v9})
Goto(blk6, {v2 -> v12, v6 -> v20})

blk2:
Statements:
(v10: test::Color) <- Color::Green(v3)
End:
Goto(blk5, {v0 -> v8, v10 -> v9})
Goto(blk7, {v3 -> v13, v0 -> v22})

blk3:
Statements:
(v11: test::Color) <- Color::Green(v4)
End:
Goto(blk5, {v0 -> v8, v11 -> v9})
Goto(blk7, {v4 -> v13, v0 -> v22})

blk4:
Statements:
Expand All @@ -928,8 +925,8 @@ blk5:
Statements:
End:
Match(match_enum(v9) {
Color::Red(v12) => blk6,
Color::Green(v13) => blk7,
Color::Red(v21) => blk10,
Color::Green(v23) => blk11,
Color::Blue(v14) => blk8,
Color::White(v15) => blk9,
})
Expand All @@ -938,13 +935,13 @@ blk6:
Statements:
(v16: test::Color) <- Color::Red(v12)
End:
Return(v8, v16)
Return(v20, v16)

blk7:
Statements:
(v17: test::Color) <- Color::Green(v13)
End:
Return(v8, v17)
Return(v22, v17)

blk8:
Statements:
Expand All @@ -958,6 +955,16 @@ Statements:
End:
Return(v8, v19)

blk10:
Statements:
End:
Goto(blk6, {v21 -> v12, v8 -> v20})

blk11:
Statements:
End:
Goto(blk7, {v23 -> v13, v8 -> v22})

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

//! > ref argument remapping with merge.
Expand Down
70 changes: 6 additions & 64 deletions crates/cairo-lang-lowering/src/test_data/extern
Original file line number Diff line number Diff line change
Expand Up @@ -80,47 +80,18 @@ End:

blk1:
Statements:
(v9: test::MyEnum) <- MyEnum::A(v3)
End:
Goto(blk4, {v2 -> v10, v9 -> v11})
Return(v2)

blk2:
Statements:
(v12: (core::felt252,)) <- struct_construct(v5)
(v13: test::MyEnum) <- MyEnum::B(v12)
End:
Goto(blk4, {v4 -> v10, v13 -> v11})
Return(v4)

blk3:
Statements:
(v14: (core::felt252, core::felt252)) <- struct_construct(v7, v8)
(v15: test::MyEnum) <- MyEnum::C(v14)
End:
Goto(blk4, {v6 -> v10, v15 -> v11})

blk4:
Statements:
End:
Match(match_enum(v11) {
MyEnum::A(v16) => blk5,
MyEnum::B(v17) => blk6,
MyEnum::C(v18) => blk7,
})

blk5:
Statements:
End:
Return(v10)

blk6:
Statements:
End:
Return(v10)

blk7:
Statements:
End:
Return(v10)
Return(v6)

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

Expand Down Expand Up @@ -281,47 +252,18 @@ End:

blk1:
Statements:
(v13: test::MyEnum) <- MyEnum::A(v5)
End:
Goto(blk4, {v3 -> v14, v4 -> v15, v13 -> v16})
Return(v3, v4)

blk2:
Statements:
(v17: (core::felt252,)) <- struct_construct(v8)
(v18: test::MyEnum) <- MyEnum::B(v17)
End:
Goto(blk4, {v6 -> v14, v7 -> v15, v18 -> v16})
Return(v6, v7)

blk3:
Statements:
(v19: (core::felt252, core::felt252)) <- struct_construct(v11, v12)
(v20: test::MyEnum) <- MyEnum::C(v19)
End:
Goto(blk4, {v9 -> v14, v10 -> v15, v20 -> v16})

blk4:
Statements:
End:
Match(match_enum(v16) {
MyEnum::A(v21) => blk5,
MyEnum::B(v22) => blk6,
MyEnum::C(v23) => blk7,
})

blk5:
Statements:
End:
Return(v14, v15)

blk6:
Statements:
End:
Return(v14, v15)

blk7:
Statements:
End:
Return(v14, v15)
Return(v9, v10)

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

Expand Down
699 changes: 338 additions & 361 deletions crates/cairo-lang-runner/src/profiling_test_data/major_test_cases

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,16 @@ serialize_array_felt_helper
array_pop_front<felt252>([0]) { fallthrough([1], [2]) label_test::serialize_array_felt_helper::0([3]) }
branch_align() -> ()
unbox<felt252>([2]) -> ([4])
enum_init<core::option::Option::<core::felt252>, 0>([4]) -> ([5])
PushValues([1]: Array<felt252>, [5]: core::option::Option::<core::felt252>) -> ([6], [7])
jump() { label_test::serialize_array_felt_helper::2() }
array_append<felt252>([5], [4]) -> ([6])
PushValues([6]: Array<felt252>, [1]: Array<felt252>) -> ([6], [1])
function_call<user@test::serialize_array_felt_helper>([6], [1]) -> ([7])
PushValues([7]: Array<felt252>) -> ([7])
return([7])
label_test::serialize_array_felt_helper::0:
branch_align() -> ()
struct_construct<Unit>() -> ([8])
enum_init<core::option::Option::<core::felt252>, 1>([8]) -> ([9])
PushValues([3]: Array<felt252>, [9]: core::option::Option::<core::felt252>) -> ([6], [7])
label_test::serialize_array_felt_helper::2:
enum_match<core::option::Option::<core::felt252>>([7]) { fallthrough([10]) label_test::serialize_array_felt_helper::3([11]) }
branch_align() -> ()
array_append<felt252>([12], [10]) -> ([13])
PushValues([13]: Array<felt252>, [6]: Array<felt252>) -> ([13], [6])
function_call<user@test::serialize_array_felt_helper>([13], [6]) -> ([14])
PushValues([14]: Array<felt252>) -> ([14])
return([14])
label_test::serialize_array_felt_helper::3:
branch_align() -> ()
drop<Unit>([11]) -> ()
drop<Array<felt252>>([6]) -> ()
PushValues([12]: Array<felt252>) -> ([12])
return([12])
label_test::serialize_array_felt_helper::4:
drop<Array<felt252>>([3]) -> ()
PushValues([5]: Array<felt252>) -> ([5])
return([5])
label_test::serialize_array_felt_helper::1:

//! > lowering_flat
Expand All @@ -67,33 +54,12 @@ End:
blk1:
Statements:
(v5: core::felt252) <- core::box::unbox::<core::felt252>(v3)
(v6: core::option::Option::<core::felt252>) <- Option::Some(v5)
(v6: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v0, v5)
(v7: core::array::Array::<core::felt252>) <- test::serialize_array_felt_helper(v6, v2)
End:
Goto(blk3, {v2 -> v7, v6 -> v8})
Return(v7)

blk2:
Statements:
(v9: ()) <- struct_construct()
(v10: core::option::Option::<core::felt252>) <- Option::None(v9)
End:
Goto(blk3, {v4 -> v7, v10 -> v8})

blk3:
Statements:
End:
Match(match_enum(v8) {
Option::Some(v11) => blk4,
Option::None(v12) => blk5,
})

blk4:
Statements:
(v13: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v0, v11)
(v14: core::array::Array::<core::felt252>) <- test::serialize_array_felt_helper(v13, v7)
End:
Return(v14)

blk5:
Statements:
End:
Return(v0)
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn test_contract_libfuncs_coverage(name: &str) {

/// Tests that compiled_class_hash() returns the correct hash, by comparing it to hard-coded
/// constant that was computed by other implementations.
#[test_case("account__account", "4b552d087e9633fbecf2185d144fafca55e6581502c0fc93953c143757dc8bf")]
#[test_case("account__account", "1663b22c467591b6288c2e063fbad4cda6285ebe4861df9aa3d5bab3f479eb6")]
fn test_compiled_class_hash(name: &str, expected_hash: &str) {
let compiled_json_path =
get_example_file_path(format!("{name}.compiled_contract_class.json").as_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ fn test_validate_gas_cost() {
let entry_point_gas_usage = post_serialization_gas - post_call_gas;
assert!(
call_building_gas_usage == 3250
&& serialization_gas_usage == 54450
&& entry_point_gas_usage == 144700,
&& serialization_gas_usage == 42670
&& entry_point_gas_usage == 144000,
"Unexpected gas_usage:
call_building: `{call_building_gas_usage}`.
serialization: `{serialization_gas_usage}`.
Expand Down
Loading

0 comments on commit 004d2b4

Please sign in to comment.