Skip to content

Commit 5eeb444

Browse files
committed
Added sub-strategy.
commit-id:34b56463
1 parent b61c1e5 commit 5eeb444

File tree

56 files changed

+38547
-43953
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+38547
-43953
lines changed

crates/cairo-lang-executable/src/compile_test_data/basic

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ ret;
262262
[ap + 0] = [fp + -7] + 5, ap++;
263263
jmp rel 4;
264264
[ap + 0] = [fp + -7] + 2, ap++;
265-
call rel 89;
265+
call rel 87;
266266
[ap + 0] = [ap + -9], ap++;
267267
[ap + 0] = 1, ap++;
268268
[ap + 0] = [ap + -4], ap++;
@@ -271,7 +271,7 @@ ret;
271271
[ap + 0] = [fp + -7] + 4, ap++;
272272
jmp rel 4;
273273
[ap + 0] = [fp + -7] + 1, ap++;
274-
call rel 80;
274+
call rel 78;
275275
[ap + 0] = [ap + -9], ap++;
276276
[ap + 0] = 1, ap++;
277277
[ap + 0] = [ap + -4], ap++;
@@ -280,14 +280,14 @@ ret;
280280
[ap + 0] = [fp + -7] + 3, ap++;
281281
jmp rel 3;
282282
[ap + 0] = [fp + -7], ap++;
283-
call rel 72;
283+
call rel 70;
284284
[ap + 0] = [ap + -9], ap++;
285285
[ap + 0] = 1, ap++;
286286
[ap + 0] = [ap + -4], ap++;
287287
[ap + 0] = [ap + -4], ap++;
288288
ret;
289289
[ap + 0] = 117999715903629884655797335944760714204113152088920212735095598, ap++;
290-
call rel 67;
290+
call rel 65;
291291
ret;
292292
jmp rel 9 if [fp + -3] != 0;
293293
[ap + 0] = [fp + -6], ap++;
@@ -300,24 +300,22 @@ ret;
300300
jmp rel 7 if [ap + -1] != 0, ap++;
301301
[ap + -1] = [ap + 0] + 340282366920938463463374607431768211456, ap++;
302302
[ap + -1] = [[fp + -6] + 0];
303-
jmp rel 25;
303+
jmp rel 23;
304304
[ap + -1] = [[fp + -6] + 0];
305-
[ap + 0] = [fp + -3], ap++;
306-
call rel 52;
307-
jmp rel 10 if [ap + -2] != 0;
308-
[ap + 0] = [fp + -6] + 1, ap++;
309-
[ap + 0] = [fp + -4], ap++;
310-
[ap + 0] = [ap + -8], ap++;
311-
[ap + 0] = [ap + -4], ap++;
312-
call rel -28;
313-
ret;
314-
call rel 52;
305+
jmp rel 11 if [fp + -3] != 0;
306+
call rel 49;
315307
[ap + 0] = [fp + -6] + 1, ap++;
316308
[ap + 0] = 1, ap++;
317309
[ap + 0] = [ap + -4], ap++;
318310
[ap + 0] = [ap + -4], ap++;
319311
ret;
320-
call rel 48;
312+
[ap + 0] = [fp + -6] + 1, ap++;
313+
[ap + 0] = [fp + -4], ap++;
314+
[ap + 0] = [ap + -3], ap++;
315+
[fp + -3] = [ap + 0] + 1, ap++;
316+
call rel -35;
317+
ret;
318+
call rel 36;
321319
[ap + 0] = [fp + -6] + 1, ap++;
322320
[ap + 0] = 1, ap++;
323321
[ap + 0] = [ap + -4], ap++;
@@ -338,18 +336,11 @@ ap += 1;
338336
[ap + 0] = [ap + -1], ap++;
339337
[ap + 0] = [ap + -2] + 1, ap++;
340338
ret;
341-
jmp rel 7 if [fp + -3] != 0;
342-
[ap + 0] = 1, ap++;
343-
[ap + 0] = 340282366920938463463374607431768211455, ap++;
344-
ret;
345-
[ap + 0] = 0, ap++;
346-
[fp + -3] = [ap + 0] + 1, ap++;
347-
ret;
348339
[ap + 0] = 39878429859763533771555484554338820190071, ap++;
349-
call rel -21;
340+
call rel -9;
350341
ret;
351342
[ap + 0] = 39878429859757942499084499860145094553463, ap++;
352-
call rel -26;
343+
call rel -14;
353344
ret;
354345
// footer
355346
ret;

crates/cairo-lang-lowering/src/lower/test_data/loop

Lines changed: 34 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -801,27 +801,28 @@ Final lowering:
801801
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin
802802
blk0 (root):
803803
Statements:
804+
(v2: core::integer::u8) <- 3
805+
(v3: core::RangeCheck, v4: core::gas::GasBuiltin, v5: core::panics::PanicResult::<(core::integer::u8, ())>) <- test::foo[45-203](v0, v1, v2)
804806
End:
805-
Match(match core::gas::withdraw_gas(v0, v1) {
806-
Option::Some(v2, v3) => blk1,
807-
Option::None(v4, v5) => blk2,
807+
Match(match_enum(v5) {
808+
PanicResult::Ok(v6) => blk1,
809+
PanicResult::Err(v7) => blk2,
808810
})
809811

810812
blk1:
811813
Statements:
812-
(v6: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v3)
813-
(v7: ()) <- struct_construct()
814-
(v8: ((),)) <- struct_construct(v7)
815-
(v9: core::panics::PanicResult::<((),)>) <- PanicResult::Ok(v8)
814+
(v8: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v4)
815+
(v9: ()) <- struct_construct()
816+
(v10: ((),)) <- struct_construct(v9)
817+
(v11: core::panics::PanicResult::<((),)>) <- PanicResult::Ok(v10)
816818
End:
817-
Return(v2, v6, v9)
819+
Return(v3, v8, v11)
818820

819821
blk2:
820822
Statements:
821-
(v10: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<375233589013918064796019>()
822-
(v11: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v10)
823+
(v12: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v7)
823824
End:
824-
Return(v4, v5, v11)
825+
Return(v3, v4, v12)
825826

826827

827828
Generated loop lowering for source location:
@@ -891,40 +892,25 @@ Statements:
891892
End:
892893
Match(match core::gas::withdraw_gas(v0, v1) {
893894
Option::Some(v3, v4) => blk1,
894-
Option::None(v5, v6) => blk4,
895+
Option::None(v5, v6) => blk2,
895896
})
896897

897898
blk1:
898899
Statements:
899-
(v7: core::integer::u8) <- 0
900+
(v7: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v4)
901+
(v8: core::integer::u8) <- 0
902+
(v9: ()) <- struct_construct()
903+
(v10: (core::integer::u8, ())) <- struct_construct(v8, v9)
904+
(v11: core::panics::PanicResult::<(core::integer::u8, ())>) <- PanicResult::Ok(v10)
900905
End:
901-
Match(match core::integer::u8_is_zero(v7) {
902-
IsZeroResult::Zero => blk2,
903-
IsZeroResult::NonZero(v8) => blk3,
904-
})
906+
Return(v3, v7, v11)
905907

906908
blk2:
907909
Statements:
908-
(v9: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v4)
909-
(v10: ()) <- struct_construct()
910-
(v11: (core::integer::u8, ())) <- struct_construct(v7, v10)
911-
(v12: core::panics::PanicResult::<(core::integer::u8, ())>) <- PanicResult::Ok(v11)
910+
(v12: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<375233589013918064796019>()
911+
(v13: core::panics::PanicResult::<(core::integer::u8, ())>) <- PanicResult::Err(v12)
912912
End:
913-
Return(v3, v9, v12)
914-
915-
blk3:
916-
Statements:
917-
(v13: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v4)
918-
(v14: core::RangeCheck, v15: core::gas::GasBuiltin, v16: core::panics::PanicResult::<(core::integer::u8, ())>) <- test::foo[45-203](v3, v13, v7)
919-
End:
920-
Return(v14, v15, v16)
921-
922-
blk4:
923-
Statements:
924-
(v17: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<375233589013918064796019>()
925-
(v18: core::panics::PanicResult::<(core::integer::u8, ())>) <- PanicResult::Err(v17)
926-
End:
927-
Return(v5, v6, v18)
913+
Return(v5, v6, v13)
928914

929915
//! > ==========================================================================
930916

@@ -1576,58 +1562,28 @@ Final lowering:
15761562
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin
15771563
blk0 (root):
15781564
Statements:
1565+
(v2: core::integer::u8) <- 0
1566+
(v3: core::RangeCheck, v4: core::gas::GasBuiltin, v5: core::panics::PanicResult::<(core::integer::u8, ())>) <- test::MyImpl::impl_in_trait[54-175](v0, v1, v2)
15791567
End:
1580-
Match(match core::gas::withdraw_gas(v0, v1) {
1581-
Option::Some(v2, v3) => blk1,
1582-
Option::None(v4, v5) => blk6,
1568+
Match(match_enum(v5) {
1569+
PanicResult::Ok(v6) => blk1,
1570+
PanicResult::Err(v7) => blk2,
15831571
})
15841572

15851573
blk1:
15861574
Statements:
1587-
(v6: core::result::Result::<core::integer::u8, core::integer::u8>) <- core::internal::num::u8_inc{0, }()
1575+
(v8: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v4)
1576+
(v9: ()) <- struct_construct()
1577+
(v10: ((),)) <- struct_construct(v9)
1578+
(v11: core::panics::PanicResult::<((),)>) <- PanicResult::Ok(v10)
15881579
End:
1589-
Match(match_enum(v6) {
1590-
Result::Ok(v7) => blk2,
1591-
Result::Err(v8) => blk5,
1592-
})
1580+
Return(v3, v8, v11)
15931581

15941582
blk2:
15951583
Statements:
1596-
(v9: core::RangeCheck, v10: core::gas::GasBuiltin, v11: core::panics::PanicResult::<(core::integer::u8, ())>) <- test::MyImpl::impl_in_trait[54-175](v2, v3, v7)
1597-
End:
1598-
Match(match_enum(v11) {
1599-
PanicResult::Ok(v12) => blk3,
1600-
PanicResult::Err(v13) => blk4,
1601-
})
1602-
1603-
blk3:
1604-
Statements:
1605-
(v14: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v10)
1606-
(v15: ()) <- struct_construct()
1607-
(v16: ((),)) <- struct_construct(v15)
1608-
(v17: core::panics::PanicResult::<((),)>) <- PanicResult::Ok(v16)
1609-
End:
1610-
Return(v9, v14, v17)
1611-
1612-
blk4:
1613-
Statements:
1614-
(v18: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v13)
1615-
End:
1616-
Return(v9, v10, v18)
1617-
1618-
blk5:
1619-
Statements:
1620-
(v19: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<608642104203229548495787928534675319>()
1621-
(v20: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v19)
1622-
End:
1623-
Return(v2, v3, v20)
1624-
1625-
blk6:
1626-
Statements:
1627-
(v21: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<375233589013918064796019>()
1628-
(v22: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v21)
1584+
(v12: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v7)
16291585
End:
1630-
Return(v4, v5, v22)
1586+
Return(v3, v4, v12)
16311587

16321588
//! > lowering_diagnostics
16331589

crates/cairo-lang-lowering/src/lower/test_data/while

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,45 +39,28 @@ Final lowering:
3939
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin, v2: core::felt252
4040
blk0 (root):
4141
Statements:
42+
(v3: core::felt252) <- 5
43+
(v4: core::RangeCheck, v5: core::gas::GasBuiltin, v6: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[51-95](v0, v1, v3)
4244
End:
43-
Match(match core::gas::withdraw_gas(v0, v1) {
44-
Option::Some(v3, v4) => blk1,
45-
Option::None(v5, v6) => blk4,
45+
Match(match_enum(v6) {
46+
PanicResult::Ok(v7) => blk1,
47+
PanicResult::Err(v8) => blk2,
4648
})
4749

4850
blk1:
4951
Statements:
50-
(v7: core::felt252) <- 5
51-
(v8: core::felt252) <- 1
52-
(v9: core::felt252) <- core::felt252_sub(v7, v8)
53-
(v10: core::RangeCheck, v11: core::gas::GasBuiltin, v12: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[51-95](v3, v4, v9)
52+
(v9: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v5)
53+
(v10: core::felt252, v11: ()) <- struct_destructure(v7)
54+
(v12: (core::felt252,)) <- struct_construct(v10)
55+
(v13: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v12)
5456
End:
55-
Match(match_enum(v12) {
56-
PanicResult::Ok(v13) => blk2,
57-
PanicResult::Err(v14) => blk3,
58-
})
57+
Return(v4, v9, v13)
5958

6059
blk2:
6160
Statements:
62-
(v15: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v11)
63-
(v16: core::felt252, v17: ()) <- struct_destructure(v13)
64-
(v18: (core::felt252,)) <- struct_construct(v16)
65-
(v19: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v18)
66-
End:
67-
Return(v10, v15, v19)
68-
69-
blk3:
70-
Statements:
71-
(v20: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v14)
72-
End:
73-
Return(v10, v11, v20)
74-
75-
blk4:
76-
Statements:
77-
(v21: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<375233589013918064796019>()
78-
(v22: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v21)
61+
(v14: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v8)
7962
End:
80-
Return(v5, v6, v22)
63+
Return(v4, v5, v14)
8164

8265

8366
Generated loop lowering for source location:

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

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ pub enum OptimizationPhase {
4343
LowerImplicits,
4444
/// A validation phase that checks the lowering is valid. Used for debugging purposes.
4545
Validate,
46+
/// A phase that iteratively a set of optimizations to the lowering.
47+
/// Stops after a certain number of iterations, or when no more changes are made.
48+
SubStrategy {
49+
/// The id of the optimization strategy to apply.
50+
strategy: OptimizationStrategyId,
51+
/// The number of times to apply the strategy.
52+
iterations: usize,
53+
},
4654
}
4755

4856
impl OptimizationPhase {
@@ -73,6 +81,16 @@ impl OptimizationPhase {
7381
OptimizationPhase::GasRedeposit => gas_redeposit(db, function, lowered),
7482
OptimizationPhase::Validate => validate(lowered)
7583
.unwrap_or_else(|err| panic!("Failed validation: {:?}", err.to_message())),
84+
OptimizationPhase::SubStrategy { strategy, iterations } => {
85+
for _ in 1..iterations {
86+
let before = lowered.clone();
87+
strategy.apply_strategy(db, function, lowered)?;
88+
if *lowered == before {
89+
return Ok(());
90+
}
91+
}
92+
strategy.apply_strategy(db, function, lowered)?
93+
}
7694
}
7795
Ok(())
7896
}
@@ -111,27 +129,29 @@ impl OptimizationStrategyId {
111129
/// Query implementation of [crate::db::LoweringGroup::baseline_optimization_strategy].
112130
pub fn baseline_optimization_strategy(db: &dyn LoweringGroup) -> OptimizationStrategyId {
113131
OptimizationStrategy(vec![
114-
// Must be right before const folding.
115-
OptimizationPhase::ReorganizeBlocks,
116-
// Apply `ConstFolding` before inlining to get better inlining decisions.
117-
OptimizationPhase::ConstFolding,
118-
OptimizationPhase::ApplyInlining,
119-
OptimizationPhase::ReturnOptimization,
120-
OptimizationPhase::ReorganizeBlocks,
121-
OptimizationPhase::ReorderStatements,
122-
OptimizationPhase::BranchInversion,
123-
OptimizationPhase::CancelOps,
124-
// Must be right before const folding.
125-
OptimizationPhase::ReorganizeBlocks,
126-
OptimizationPhase::ConstFolding,
127-
OptimizationPhase::OptimizeMatches,
128-
OptimizationPhase::SplitStructs,
129-
OptimizationPhase::ReorganizeBlocks,
130-
OptimizationPhase::ReorderStatements,
131-
OptimizationPhase::OptimizeMatches,
132-
OptimizationPhase::ReorganizeBlocks,
133-
OptimizationPhase::CancelOps,
134-
OptimizationPhase::ReorganizeBlocks,
132+
OptimizationPhase::SubStrategy {
133+
strategy: OptimizationStrategy(vec![
134+
// Must be right before const folding.
135+
OptimizationPhase::ReorganizeBlocks,
136+
// Apply `ConstFolding` before inlining to get better inlining decisions.
137+
OptimizationPhase::ConstFolding,
138+
OptimizationPhase::ApplyInlining,
139+
OptimizationPhase::ReturnOptimization,
140+
OptimizationPhase::ReorganizeBlocks,
141+
OptimizationPhase::ReorderStatements,
142+
OptimizationPhase::BranchInversion,
143+
OptimizationPhase::CancelOps,
144+
OptimizationPhase::SplitStructs,
145+
OptimizationPhase::ReorganizeBlocks,
146+
OptimizationPhase::ReorderStatements,
147+
OptimizationPhase::OptimizeMatches,
148+
OptimizationPhase::ReorganizeBlocks,
149+
OptimizationPhase::CancelOps,
150+
OptimizationPhase::ReorganizeBlocks,
151+
])
152+
.intern(db),
153+
iterations: 5,
154+
},
135155
OptimizationPhase::DedupBlocks,
136156
// Re-run ReturnOptimization to eliminate harmful merges introduced by DedupBlocks.
137157
OptimizationPhase::ReturnOptimization,

0 commit comments

Comments
 (0)