cairo_lang_lowering/graph_algorithms/
feedback_set.rs

1use cairo_lang_diagnostics::Maybe;
2use cairo_lang_filesystem::flag::Flag;
3use cairo_lang_filesystem::ids::FlagId;
4use cairo_lang_utils::graph_algos::feedback_set::calc_feedback_set;
5use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
6
7use super::concrete_function_node::ConcreteFunctionWithBodyNode;
8use crate::DependencyType;
9use crate::db::{ConcreteSCCRepresentative, LoweringGroup};
10use crate::ids::ConcreteFunctionWithBodyId;
11
12/// Query implementation of [crate::db::LoweringGroup::function_with_body_feedback_set].
13pub fn function_with_body_feedback_set(
14    db: &dyn LoweringGroup,
15    function: ConcreteFunctionWithBodyId,
16) -> Maybe<OrderedHashSet<ConcreteFunctionWithBodyId>> {
17    let r = db.concrete_function_with_body_scc_representative(function, DependencyType::Cost);
18    db.priv_function_with_body_feedback_set_of_representative(r)
19}
20
21/// Returns the value of the `add_withdraw_gas` flag, or `true` if the flag is not set.
22pub fn flag_add_withdraw_gas(db: &dyn LoweringGroup) -> bool {
23    db.get_flag(FlagId::new(db.upcast(), "add_withdraw_gas"))
24        .map(|flag| *flag == Flag::AddWithdrawGas(true))
25        .unwrap_or(true)
26}
27
28/// Query implementation of [crate::db::LoweringGroup::needs_withdraw_gas].
29pub fn needs_withdraw_gas(
30    db: &dyn LoweringGroup,
31    function: ConcreteFunctionWithBodyId,
32) -> Maybe<bool> {
33    Ok(flag_add_withdraw_gas(db)
34        && db.function_with_body_feedback_set(function)?.contains(&function))
35}
36
37/// Query implementation of
38/// [crate::db::LoweringGroup::priv_function_with_body_feedback_set_of_representative].
39pub fn priv_function_with_body_feedback_set_of_representative(
40    db: &dyn LoweringGroup,
41    function: ConcreteSCCRepresentative,
42) -> Maybe<OrderedHashSet<ConcreteFunctionWithBodyId>> {
43    Ok(calc_feedback_set(
44        ConcreteFunctionWithBodyNode {
45            function_id: function.0,
46            db,
47            dependency_type: DependencyType::Cost,
48        }
49        .into(),
50    ))
51}