Skip to content

Commit 27d5e2f

Browse files
authored
flow-control: More cleanup. (#8434)
1 parent 3c926fd commit 27d5e2f

File tree

4 files changed

+50
-68
lines changed

4 files changed

+50
-68
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use cairo_lang_defs::ids::NamedLanguageElementId;
22
use cairo_lang_diagnostics::Maybe;
3+
use cairo_lang_filesystem::db::FilesGroup;
4+
use cairo_lang_filesystem::flag::Flag;
5+
use cairo_lang_filesystem::ids::{FlagId, FlagLongId};
36
use cairo_lang_semantic::corelib::{CorelibSemantic, validate_literal};
47
use cairo_lang_semantic::items::enm::SemanticEnumEx;
58
use cairo_lang_semantic::items::structure::StructSemantic;
@@ -24,7 +27,6 @@ use crate::diagnostic::{LoweringDiagnosticKind, MatchDiagnostic, MatchError};
2427
use crate::ids::LocationId;
2528
use crate::lower::context::LoweringContext;
2629
use crate::lower::flow_control::graph::{Downcast, EqualsLiteral, Upcast, ValueMatch};
27-
use crate::lower::lower_match::numeric_match_optimization_threshold;
2830

2931
/// A callback that gets a [FilteredPatterns] and constructs a node that continues the pattern
3032
/// matching restricted to the filtered patterns.
@@ -667,6 +669,29 @@ fn optimized_value_match_size<'db>(
667669
if n_arms >= numeric_match_optimization_threshold(ctx, is_small_type) { i } else { 0 }
668670
}
669671

672+
/// Returns the threshold for the number of arms for optimizing numeric match expressions, by using
673+
/// a jump table instead of an if-else construct.
674+
/// `is_small_type` means the matched type has < 2**128 possible values.
675+
pub fn numeric_match_optimization_threshold<'db>(
676+
ctx: &LoweringContext<'db, '_>,
677+
is_small_type: bool,
678+
) -> usize {
679+
// For felt252 the number of steps with if-else is 2 * min(n, number_of_arms) + 2 and 11~13 for
680+
// jump table for small_types the number of steps with if-else is 2 * min(n, number_of_arms) + 4
681+
// and 9~12 for jump table.
682+
let default_threshold = if is_small_type { 8 } else { 10 };
683+
ctx.db
684+
.get_flag(FlagId::new(
685+
ctx.db,
686+
FlagLongId("numeric_match_optimization_min_arms_threshold".into()),
687+
))
688+
.map(|flag| match *flag {
689+
Flag::NumericMatchOptimizationMinArmsThreshold(threshold) => threshold,
690+
_ => panic!("Wrong type flag `{flag:?}`."),
691+
})
692+
.unwrap_or(default_threshold)
693+
}
694+
670695
/// Returns `true` if the pattern accepts any value (`_` or a variable name).
671696
fn pattern_is_any<'a, 'db>(pattern: &'a semantic::Pattern<'db>) -> bool {
672697
match pattern {

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

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 0 additions & 45 deletions
This file was deleted.

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use cairo_lang_utils::unordered_hash_map::{Entry, UnorderedHashMap};
2525
use cairo_lang_utils::{Intern, extract_matches, try_extract_matches};
2626
use context::handle_lowering_flow_error;
2727
use defs::ids::TopLevelLanguageElementId;
28-
use flow_control::create_graph::create_graph_expr_while_let;
28+
use flow_control::create_graph::{
29+
create_graph_expr_if, create_graph_expr_match, create_graph_expr_while_let,
30+
};
2931
use flow_control::lower_graph::lower_graph;
3032
use itertools::{Itertools, chain, izip, zip_eq};
3133
use num_bigint::{BigInt, Sign};
@@ -50,8 +52,6 @@ use self::context::{
5052
};
5153
use self::external::{extern_facade_expr, extern_facade_return_tys};
5254
use self::logical_op::lower_logical_op;
53-
use self::lower_if::lower_expr_if;
54-
use self::lower_match::lower_expr_match;
5555
use crate::blocks::Blocks;
5656
use crate::diagnostic::LoweringDiagnosticKind::{self, *};
5757
use crate::diagnostic::LoweringDiagnosticsBuilder;
@@ -71,9 +71,7 @@ mod external;
7171
mod flow_control;
7272
pub mod generators;
7373
mod logical_op;
74-
mod lower_if;
7574
mod lower_let_else;
76-
mod lower_match;
7775
pub mod refs;
7876

7977
#[cfg(test)]
@@ -1204,6 +1202,27 @@ fn lower_expr_desnap<'db>(
12041202
))
12051203
}
12061204

1205+
/// Lowers an expression of type [semantic::ExprIf].
1206+
fn lower_expr_if<'db>(
1207+
ctx: &mut LoweringContext<'db, '_>,
1208+
builder: &mut BlockBuilder<'db>,
1209+
expr: &semantic::ExprIf<'db>,
1210+
) -> LoweringResult<'db, LoweredExpr<'db>> {
1211+
let graph = create_graph_expr_if(ctx, expr);
1212+
lower_graph(ctx, builder, &graph, ctx.get_location(expr.stable_ptr.untyped()))
1213+
}
1214+
1215+
/// Lowers an expression of type [semantic::ExprMatch].
1216+
fn lower_expr_match<'db>(
1217+
ctx: &mut LoweringContext<'db, '_>,
1218+
expr: &semantic::ExprMatch<'db>,
1219+
builder: &mut BlockBuilder<'db>,
1220+
) -> LoweringResult<'db, LoweredExpr<'db>> {
1221+
log::trace!("Lowering a match expression: {:?}", expr.debug(&ctx.expr_formatter));
1222+
let graph = create_graph_expr_match(ctx, expr);
1223+
lower_graph(ctx, builder, &graph, ctx.get_location(expr.stable_ptr.untyped()))
1224+
}
1225+
12071226
/// Lowers an expression of type [semantic::ExprFunctionCall].
12081227
fn lower_expr_function_call<'db>(
12091228
ctx: &mut LoweringContext<'db, '_>,

0 commit comments

Comments
 (0)