Skip to content

Commit e9cad6d

Browse files
committed
flow-control: Store VariableId instead of VarUsage.
commit-id:030388f3
1 parent 3768b7a commit e9cad6d

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::lower::context::{
1212
LoweredExpr, LoweringContext, LoweringFlowError, LoweringResult, handle_lowering_flow_error,
1313
};
1414
use crate::lower::lowered_expr_to_block_scope_end;
15-
use crate::{BlockEnd, BlockId, MatchInfo, VarUsage};
15+
use crate::{BlockEnd, BlockId, MatchInfo, VarUsage, VariableId};
1616

1717
mod lower_node;
1818

@@ -74,8 +74,8 @@ struct LowerGraphContext<'db, 'mt, 'a> {
7474
/// A list of sealed blocks for the arms (excluding the arms that do not continue to the
7575
/// callsite).
7676
sealed_blocks: Vec<SealedGotoCallsite<'db>>,
77-
/// A map from [FlowControlVar] to [VarUsage].
78-
vars: UnorderedHashMap<FlowControlVar, VarUsage<'db>>,
77+
/// A map from [FlowControlVar] to [VariableId].
78+
vars: UnorderedHashMap<FlowControlVar, VariableId>,
7979
/// The first node (starting from the root) that does not pass its block builder directly to
8080
/// the child node (see [Self::pass_builder_to_child]).
8181
/// The block builder of this node is the original block builder.
@@ -172,13 +172,21 @@ impl<'mt, 'db, 'a> LowerGraphContext<'db, 'mt, 'a> {
172172
/// This function is called when lowering the node creating the [FlowControlVar].
173173
/// Later, when the [FlowControlVar] is used in another node, [Self::vars] is used to get the
174174
/// [VarUsage].
175-
fn register_var(&mut self, var_id: FlowControlVar, lowered_var: VarUsage<'db>) {
175+
fn register_var(&mut self, var_id: FlowControlVar, lowered_var: VariableId) {
176176
assert!(
177177
self.vars.insert(var_id, lowered_var).is_none(),
178178
"Variable {var_id:?} was already registered.",
179179
);
180180
}
181181

182+
/// Returns the [VarUsage] of the given [FlowControlVar].
183+
///
184+
/// Note that the usage location of [FlowControlVar]s is identical to the location of their
185+
/// definition.
186+
fn var_usage(&self, var: FlowControlVar) -> VarUsage<'db> {
187+
VarUsage { var_id: self.vars[&var], location: var.location(self.graph) }
188+
}
189+
182190
// Finalization functions.
183191

184192
/// Finalizes a block with a match.

crates/cairo-lang-lowering/src/lower/flow_control/lower_graph/lower_node.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn lower_evaluate_expr<'db>(
8787

8888
match lowered_expr.as_var_usage(ctx.ctx, &mut builder) {
8989
Ok(lowered_var) => {
90-
ctx.register_var(var, lowered_var);
90+
ctx.register_var(var, lowered_var.var_id);
9191
ctx.pass_builder_to_child(id, node.next, builder);
9292
Ok(())
9393
}
@@ -116,7 +116,7 @@ fn lower_boolean_if<'db>(
116116
// Finalize the block.
117117
let match_info = MatchInfo::Enum(MatchEnumInfo {
118118
concrete_enum_id: corelib::core_bool_enum(db),
119-
input: ctx.vars[&node.condition_var],
119+
input: ctx.var_usage(node.condition_var),
120120
arms: vec![
121121
MatchArm {
122122
arm_selector: MatchArmSelector::VariantId(corelib::false_variant(db)),
@@ -179,8 +179,7 @@ fn lower_enum_match<'db>(
179179
let var_location = flow_control_var.location(ctx.graph);
180180
// Create a variable for the variant inner value.
181181
let variant_var = ctx.ctx.new_var(VarRequest { ty: var_ty, location: var_location });
182-
let var_usage = VarUsage { var_id: variant_var, location: var_location };
183-
ctx.register_var(*flow_control_var, var_usage);
182+
ctx.register_var(*flow_control_var, variant_var);
184183
MatchArm {
185184
arm_selector: MatchArmSelector::VariantId(*concrete_variant),
186185
block_id: ctx.assign_child_block_id(*variant_node, &builder),
@@ -191,7 +190,7 @@ fn lower_enum_match<'db>(
191190

192191
let match_info = MatchInfo::Enum(MatchEnumInfo {
193192
concrete_enum_id: node.concrete_enum_id,
194-
input: ctx.vars[&node.matched_var],
193+
input: ctx.var_usage(node.matched_var),
195194
arms,
196195
location: match_location,
197196
});
@@ -243,7 +242,7 @@ fn handle_extern_match<'db>(
243242
// (b) `LoweredExpr::Tuple` of `LoweredExpr::AtVariable`,
244243
// we can safely unwrap the `as_var_usage` result.
245244
let var_usage = variant_expr.as_var_usage(ctx.ctx, &mut child_builder).unwrap();
246-
ctx.register_var(*flow_control_var, var_usage);
245+
ctx.register_var(*flow_control_var, var_usage.var_id);
247246

248247
let block_id = ctx.register_child_builder(*variant_node, child_builder);
249248

@@ -290,7 +289,7 @@ fn lower_value_match<'db>(
290289
let match_info = MatchInfo::Value(MatchEnumValue {
291290
num_of_arms: node.nodes.len(),
292291
arms,
293-
input: ctx.vars[&node.matched_var],
292+
input: ctx.var_usage(node.matched_var),
294293
location: ctx.location,
295294
});
296295

@@ -310,7 +309,7 @@ fn lower_equals_literal<'db>(
310309

311310
let literal_stable_ptr = node.stable_ptr.untyped();
312311
let literal_location = ctx.ctx.get_location(literal_stable_ptr);
313-
let input_var = ctx.vars[&node.input];
312+
let input_var = ctx.var_usage(node.input);
314313

315314
// Lower the expression `input_var - literal`.
316315
let is_equal: VarUsage<'db> = if node.literal == BigInt::from(0) {
@@ -383,7 +382,7 @@ fn lower_bind_var<'db>(
383382
mut builder: BlockBuilder<'db>,
384383
) -> Maybe<()> {
385384
let pattern_variable = node.output.get(ctx.graph);
386-
let var_id = ctx.vars[&node.input].var_id;
385+
let var_id = ctx.var_usage(node.input).var_id;
387386

388387
// Override variable location to with the location of the variable in the pattern.
389388
// TODO(lior): Consider using the location of the first instance of the pattern binding instead
@@ -418,11 +417,11 @@ fn lower_deconstruct<'db>(
418417
.collect();
419418

420419
let variable_ids =
421-
generators::StructDestructure { input: ctx.vars[&node.input], var_reqs: var_requests }
420+
generators::StructDestructure { input: ctx.var_usage(node.input), var_reqs: var_requests }
422421
.add(ctx.ctx, &mut builder.statements);
423422

424423
for (var_id, output) in zip_eq(variable_ids, &node.outputs) {
425-
ctx.register_var(*output, VarUsage { var_id, location: output.location(ctx.graph) });
424+
ctx.register_var(*output, var_id);
426425
}
427426

428427
ctx.pass_builder_to_child(id, node.next, builder);
@@ -445,15 +444,15 @@ fn lower_upcast<'db>(
445444

446445
let call_result = generators::Call {
447446
function,
448-
inputs: vec![ctx.vars[&node.input]],
447+
inputs: vec![ctx.var_usage(node.input)],
449448
coupon_input: None,
450449
extra_ret_tys: vec![],
451450
ret_tys: vec![output_ty],
452451
location: node.input.location(ctx.graph),
453452
}
454453
.add(ctx.ctx, &mut builder.statements);
455454

456-
ctx.register_var(node.output, call_result.returns.into_iter().next().unwrap());
455+
ctx.register_var(node.output, call_result.returns.into_iter().next().unwrap().var_id);
457456
ctx.pass_builder_to_child(id, node.next, builder);
458457
Ok(())
459458
}
@@ -480,12 +479,11 @@ fn lower_downcast<'db>(
480479
// Create the output variable.
481480
let output_location = node.output.location(ctx.graph);
482481
let output_var = ctx.ctx.new_var(VarRequest { ty: output_ty, location: output_location });
483-
let var_usage = VarUsage { var_id: output_var, location: output_location };
484-
ctx.register_var(node.output, var_usage);
482+
ctx.register_var(node.output, output_var);
485483

486484
let match_info = MatchInfo::Extern(MatchExternInfo {
487485
function: function_id,
488-
inputs: vec![ctx.vars[&node.input]],
486+
inputs: vec![ctx.var_usage(node.input)],
489487
arms: vec![
490488
MatchArm {
491489
arm_selector: MatchArmSelector::VariantId(corelib::option_some_variant(

0 commit comments

Comments
 (0)