Skip to content

Commit f8f7057

Browse files
committed
flow_control: Integrate the new code for simple boolean ifs.
commit-id:827e842e
1 parent ceb7386 commit f8f7057

File tree

7 files changed

+46
-39
lines changed

7 files changed

+46
-39
lines changed

crates/cairo-lang-lowering/src/borrow_check/test_data/borrow_check

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -658,19 +658,19 @@ Statements:
658658
(v11: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
659659
() <- test::invalidate(v11{`self.a`})
660660
End:
661-
Goto(blk3, {v3 -> v12, v11 -> v13})
661+
Goto(blk3, {v11 -> v12})
662662

663663
blk2:
664664
Statements:
665665
End:
666-
Goto(blk3, {v3 -> v12, v1 -> v13})
666+
Goto(blk3, {v1 -> v12})
667667

668668
blk3:
669669
Statements:
670-
(v14: ()) <- struct_construct()
671-
(v15: test::MyStruct) <- struct_construct(v13{`self`}, v12{`self`})
670+
(v13: ()) <- struct_construct()
671+
(v14: test::MyStruct) <- struct_construct(v12{`self`}, v3{`self`})
672672
End:
673-
Return(v15, v14)
673+
Return(v14, v13)
674674

675675
//! > ==========================================================================
676676

@@ -724,17 +724,17 @@ Statements:
724724
(v11: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
725725
() <- test::invalidate(v11{`v.a`})
726726
End:
727-
Goto(blk3, {v3 -> v12, v11 -> v13})
727+
Goto(blk3, {v11 -> v12})
728728

729729
blk2:
730730
Statements:
731731
End:
732-
Goto(blk3, {v3 -> v12, v1 -> v13})
732+
Goto(blk3, {v1 -> v12})
733733

734734
blk3:
735735
Statements:
736736
End:
737-
Return(v12)
737+
Return(v3)
738738

739739
//! > ==========================================================================
740740

@@ -836,20 +836,21 @@ Statements:
836836
(v5: core::integer::u32) <- struct_destructure(v0{`a.x`})
837837
(v6: core::integer::u32, v7: @core::integer::u32) <- snapshot(v5{`a.x`})
838838
(v8: ()) <- test::bar(v7{`@a.x`})
839-
(v10: test::A) <- struct_construct(v6{`if true { bar(@a.x); }`})
840839
End:
841-
Goto(blk3, {v10 -> v9})
840+
Goto(blk3, {v6 -> v9})
842841

843842
blk2:
844843
Statements:
844+
(v10: core::integer::u32) <- struct_destructure(v0{`if true { bar(@a.x); }`})
845845
End:
846-
Goto(blk3, {v0 -> v9})
846+
Goto(blk3, {v10 -> v9})
847847

848848
blk3:
849849
Statements:
850850
(v11: ()) <- struct_construct()
851+
(v12: test::A) <- struct_construct(v9{`a`})
851852
End:
852-
Return(v9, v11)
853+
Return(v12, v11)
853854

854855
//! > ==========================================================================
855856

crates/cairo-lang-lowering/src/lower/flow_control/create_graph.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ mod filtered_patterns;
1212
mod patterns;
1313

1414
/// Creates a graph node for [semantic::ExprIf].
15-
#[allow(dead_code)]
1615
pub fn create_graph_expr_if<'db>(
1716
ctx: &LoweringContext<'db, '_>,
1817
expr: &semantic::ExprIf<'db>,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
mod create_graph;
1+
pub mod create_graph;
22
mod graph;
3-
mod lower_graph;
3+
pub mod lower_graph;
44

55
#[cfg(test)]
66
mod graph_test;

crates/cairo-lang-lowering/src/lower/lower_if.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use semantic::{Condition, MatchArmSelector};
77

88
use super::block_builder::{BlockBuilder, SealedBlockBuilder};
99
use super::context::{LoweredExpr, LoweringContext, LoweringFlowError, LoweringResult};
10+
use super::flow_control::create_graph::create_graph_expr_if;
11+
use super::flow_control::lower_graph::lower_graph;
1012
use super::lowered_expr_to_block_scope_end;
1113
use crate::diagnostic::LoweringDiagnosticKind::{self};
1214
use crate::diagnostic::{LoweringDiagnosticsBuilder, MatchDiagnostic, MatchError, MatchKind};
@@ -45,6 +47,11 @@ pub fn lower_expr_if<'db>(
4547
builder: &mut BlockBuilder<'db>,
4648
expr: &semantic::ExprIf<'db>,
4749
) -> LoweringResult<'db, LoweredExpr<'db>> {
50+
if expr.conditions.len() == 1 && matches!(expr.conditions[0], Condition::BoolExpr(_)) {
51+
let graph = create_graph_expr_if(ctx, expr);
52+
return lower_graph(ctx, builder, &graph, ctx.get_location(expr.stable_ptr.untyped()));
53+
}
54+
4855
// Else block is not supported yet for multiple conditions.
4956
if expr.conditions.len() > 1 {
5057
if let Some(else_block) = expr.else_block {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,15 @@ End:
410410

411411
blk1:
412412
Statements:
413-
(v16: core::felt252, v17: @core::felt252) <- snapshot(v3)
413+
(v18: core::felt252, v19: @core::felt252) <- snapshot(v3)
414414
End:
415-
Goto(blk3, {v16 -> v20, v17 -> v21})
415+
Goto(blk3, {v18 -> v20, v19 -> v21})
416416

417417
blk2:
418418
Statements:
419-
(v18: core::felt252, v19: @core::felt252) <- snapshot(v3)
419+
(v16: core::felt252, v17: @core::felt252) <- snapshot(v3)
420420
End:
421-
Goto(blk3, {v18 -> v20, v19 -> v21})
421+
Goto(blk3, {v16 -> v20, v17 -> v21})
422422

423423
blk3:
424424
Statements:

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -845,15 +845,15 @@ End:
845845

846846
blk1:
847847
Statements:
848-
(v5: core::integer::u8) <- 0
848+
(v6: core::integer::u8) <- 0
849849
End:
850-
Goto(blk3, {v5 -> v7})
850+
Goto(blk3, {v6 -> v7})
851851

852852
blk2:
853853
Statements:
854-
(v6: core::integer::u8) <- 0
854+
(v5: core::integer::u8) <- 0
855855
End:
856-
Goto(blk3, {v6 -> v7})
856+
Goto(blk3, {v5 -> v7})
857857

858858
blk3:
859859
Statements:

crates/cairo-lang-lowering/src/optimizations/test_data/gas_redeposit

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ End:
4646

4747
blk1:
4848
Statements:
49-
(v9: core::felt252) <- test::heavy_op1()
49+
(v10: core::felt252) <- test::heavy_op1()
5050
End:
51-
Goto(blk3, {v9 -> v11})
51+
Goto(blk3, {v10 -> v11})
5252

5353
blk2:
5454
Statements:
55-
(v10: core::felt252) <- test::heavy_op2()
55+
(v9: core::felt252) <- test::heavy_op2()
5656
End:
57-
Goto(blk3, {v10 -> v11})
57+
Goto(blk3, {v9 -> v11})
5858

5959
blk3:
6060
Statements:
@@ -77,15 +77,15 @@ End:
7777

7878
blk1:
7979
Statements:
80-
(v9: core::felt252) <- test::heavy_op1()
80+
(v10: core::felt252) <- test::heavy_op1()
8181
End:
82-
Goto(blk3, {v9 -> v11})
82+
Goto(blk3, {v10 -> v11})
8383

8484
blk2:
8585
Statements:
86-
(v10: core::felt252) <- test::heavy_op2()
86+
(v9: core::felt252) <- test::heavy_op2()
8787
End:
88-
Goto(blk3, {v10 -> v11})
88+
Goto(blk3, {v9 -> v11})
8989

9090
blk3:
9191
Statements:
@@ -140,15 +140,15 @@ End:
140140

141141
blk1:
142142
Statements:
143-
(v9: core::felt252) <- test::heavy_op1()
143+
(v10: core::felt252) <- test::heavy_op1()
144144
End:
145-
Goto(blk3, {v9 -> v11})
145+
Goto(blk3, {v10 -> v11})
146146

147147
blk2:
148148
Statements:
149-
(v10: core::felt252) <- test::heavy_op2()
149+
(v9: core::felt252) <- test::heavy_op2()
150150
End:
151-
Goto(blk3, {v10 -> v11})
151+
Goto(blk3, {v9 -> v11})
152152

153153
blk3:
154154
Statements:
@@ -172,16 +172,16 @@ End:
172172
blk1:
173173
Statements:
174174
() <- core::gas::redeposit_gas()
175-
(v9: core::felt252) <- test::heavy_op1()
175+
(v10: core::felt252) <- test::heavy_op1()
176176
End:
177-
Goto(blk3, {v9 -> v11})
177+
Goto(blk3, {v10 -> v11})
178178

179179
blk2:
180180
Statements:
181181
() <- core::gas::redeposit_gas()
182-
(v10: core::felt252) <- test::heavy_op2()
182+
(v9: core::felt252) <- test::heavy_op2()
183183
End:
184-
Goto(blk3, {v10 -> v11})
184+
Goto(blk3, {v9 -> v11})
185185

186186
blk3:
187187
Statements:

0 commit comments

Comments
 (0)