Skip to content

Commit ae2abcf

Browse files
committed
flow-control: Store VariableId instead of VarUsage.
commit-id:030388f3
1 parent d156ea3 commit ae2abcf

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
@@ -88,7 +88,7 @@ fn lower_evaluate_expr<'db>(
8888

8989
match lowered_expr.as_var_usage(ctx.ctx, &mut builder) {
9090
Ok(lowered_var) => {
91-
ctx.register_var(var, lowered_var);
91+
ctx.register_var(var, lowered_var.var_id);
9292
ctx.pass_builder_to_child(id, node.next, builder);
9393
Ok(())
9494
}
@@ -117,7 +117,7 @@ fn lower_boolean_if<'db>(
117117
// Finalize the block.
118118
let match_info = MatchInfo::Enum(MatchEnumInfo {
119119
concrete_enum_id: corelib::core_bool_enum(db),
120-
input: ctx.vars[&node.condition_var],
120+
input: ctx.var_usage(node.condition_var),
121121
arms: vec![
122122
MatchArm {
123123
arm_selector: MatchArmSelector::VariantId(corelib::false_variant(db)),
@@ -180,8 +180,7 @@ fn lower_enum_match<'db>(
180180
let var_location = flow_control_var.location(ctx.graph);
181181
// Create a variable for the variant inner value.
182182
let variant_var = ctx.ctx.new_var(VarRequest { ty: var_ty, location: var_location });
183-
let var_usage = VarUsage { var_id: variant_var, location: var_location };
184-
ctx.register_var(*flow_control_var, var_usage);
183+
ctx.register_var(*flow_control_var, variant_var);
185184
MatchArm {
186185
arm_selector: MatchArmSelector::VariantId(*concrete_variant),
187186
block_id: ctx.assign_child_block_id(*variant_node, &builder),
@@ -192,7 +191,7 @@ fn lower_enum_match<'db>(
192191

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

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

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

@@ -311,7 +310,7 @@ fn lower_equals_literal<'db>(
311310

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

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

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

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

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

429428
ctx.pass_builder_to_child(id, node.next, builder);
@@ -446,15 +445,15 @@ fn lower_upcast<'db>(
446445

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

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

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

0 commit comments

Comments
 (0)