Skip to content

Commit

Permalink
Made non-copy variable not be forwarded in const folding.
Browse files Browse the repository at this point in the history
Fixes #6321.

commit-id:e27aec6c
  • Loading branch information
orizi committed Sep 1, 2024
1 parent db5e280 commit 7567292
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 4 deletions.
16 changes: 12 additions & 4 deletions crates/cairo-lang-lowering/src/optimizations/const_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ enum VarInfo {
/// The variable is a snapshot of another variable.
Snapshot(Box<VarInfo>),
/// The variable is a struct of other variables.
Struct(Vec<VarInfo>),
/// `None` values represent variables that are not tracked.
Struct(Vec<Option<VarInfo>>),
}

/// Performs constant folding on the lowered program.
Expand Down Expand Up @@ -101,14 +102,19 @@ pub fn const_folding(db: &dyn LoweringGroup, lowered: &mut FlatLowered) {
let mut contains_info = false;
for input in inputs.iter() {
let Some(info) = ctx.var_info.get(&input.var_id) else {
all_args.push(VarInfo::Var(*input));
all_args.push(
lowered.variables[input.var_id]
.copyable
.is_ok()
.then(|| VarInfo::Var(*input)),
);
continue;
};
contains_info = true;
if let VarInfo::Const(value) = info {
const_args.push(value.clone());
}
all_args.push(info.clone());
all_args.push(Some(info.clone()));
}
if const_args.len() == inputs.len() {
let value = ConstValue::Struct(const_args, lowered.variables[*output].ty);
Expand Down Expand Up @@ -141,7 +147,9 @@ pub fn const_folding(db: &dyn LoweringGroup, lowered: &mut FlatLowered) {
}
VarInfo::Struct(members) => {
for (output, member) in zip_eq(outputs, members.clone()) {
ctx.var_info.insert(*output, wrap_with_snapshots(member));
if let Some(member) = member {
ctx.var_info.insert(*output, wrap_with_snapshots(member));
}
}
}
_ => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2946,3 +2946,101 @@ End:
Return(v6)

//! > lowering_diagnostics

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

//! > Construct with undroppable.

//! > test_runner_name
test_match_optimizer

//! > function
fn foo() -> bool {
let x: (felt252, Felt252Dict<felt252>) = (0, Default::<Felt252Dict>::default());
let (l, _) = @x;
*l == 0
}

//! > function_name
foo

//! > module_code

//! > semantic_diagnostics

//! > before
Parameters:
blk0 (root):
Statements:
(v0: core::felt252) <- 0
(v1: core::dict::Felt252Dict::<core::felt252>) <- core::dict::felt252_dict_new::<core::felt252>()
(v2: (core::felt252, core::dict::Felt252Dict::<core::felt252>)) <- struct_construct(v0, v1)
(v3: (core::felt252, core::dict::Felt252Dict::<core::felt252>), v4: @(core::felt252, core::dict::Felt252Dict::<core::felt252>)) <- snapshot(v2)
(v5: core::felt252, v6: core::dict::Felt252Dict::<core::felt252>) <- struct_destructure(v3)
(v7: core::dict::SquashedFelt252Dict::<core::felt252>) <- core::dict::Felt252DictImpl::<core::felt252, core::Felt252Felt252DictValue>::squash(v6)
(v9: @core::felt252, v10: @core::dict::Felt252Dict::<core::felt252>) <- struct_destructure(v4)
(v11: core::felt252) <- desnap(v9)
(v14: core::felt252) <- 0
(v19: core::felt252) <- core::felt252_sub(v11, v14)
End:
Match(match core::felt252_is_zero(v19) {
IsZeroResult::Zero => blk1,
IsZeroResult::NonZero(v20) => blk2,
})

blk1:
Statements:
(v21: ()) <- struct_construct()
(v22: core::bool) <- bool::True(v21)
End:
Goto(blk3, {v22 -> v23})

blk2:
Statements:
(v24: ()) <- struct_construct()
(v25: core::bool) <- bool::False(v24)
End:
Goto(blk3, {v25 -> v23})

blk3:
Statements:
End:
Return(v23)

//! > after
Parameters:
blk0 (root):
Statements:
(v0: core::felt252) <- 0
(v1: core::dict::Felt252Dict::<core::felt252>) <- core::dict::felt252_dict_new::<core::felt252>()
(v2: (core::felt252, core::dict::Felt252Dict::<core::felt252>)) <- struct_construct(v0, v1)
(v3: (core::felt252, core::dict::Felt252Dict::<core::felt252>), v4: @(core::felt252, core::dict::Felt252Dict::<core::felt252>)) <- snapshot(v2)
(v5: core::felt252, v6: core::dict::Felt252Dict::<core::felt252>) <- struct_destructure(v3)
(v7: core::dict::SquashedFelt252Dict::<core::felt252>) <- core::dict::Felt252DictImpl::<core::felt252, core::Felt252Felt252DictValue>::squash(v6)
(v9: @core::felt252, v10: @core::dict::Felt252Dict::<core::felt252>) <- struct_destructure(v4)
(v11: core::felt252) <- desnap(v9)
(v14: core::felt252) <- 0
(v19: core::felt252) <- core::felt252_sub(v11, v14)
End:
Goto(blk1, {})

blk1:
Statements:
(v21: ()) <- struct_construct()
(v22: core::bool) <- bool::True(v21)
End:
Goto(blk3, {v22 -> v23})

blk2:
Statements:
(v24: ()) <- struct_construct()
(v25: core::bool) <- bool::False(v24)
End:
Goto(blk3, {v25 -> v23})

blk3:
Statements:
End:
Return(v23)

//! > lowering_diagnostics

0 comments on commit 7567292

Please sign in to comment.