cairo_lang_lowering/optimizations/
config.rs

1use cairo_lang_defs::ids::ExternFunctionId;
2use cairo_lang_semantic::helper::ModuleHelper;
3use cairo_lang_utils::unordered_hash_set::UnorderedHashSet;
4use salsa::Database;
5
6use crate::db::LoweringGroup;
7use crate::utils::InliningStrategy;
8
9/// A configuration struct that controls the behavior of the optimization passes.
10#[derive(Debug, Eq, PartialEq, Clone)]
11pub struct OptimizationConfig {
12    /// A list of functions that can be moved during the reorder_statements optimization.
13    pub moveable_functions: Vec<String>,
14    /// Determines whether inlining is disabled.
15    pub inlining_strategy: InliningStrategy,
16    /// Should const folding be skipped.
17    pub skip_const_folding: bool,
18}
19
20impl OptimizationConfig {
21    /// Sets the list of moveable functions.
22    pub fn with_moveable_functions(mut self, moveable_functions: Vec<String>) -> Self {
23        self.moveable_functions = moveable_functions;
24        self
25    }
26    /// Sets the list of moveable functions to a minimal set, useful for testing.
27    pub fn with_minimal_movable_functions(self) -> Self {
28        self.with_moveable_functions(vec!["felt252_sub".into()])
29    }
30    /// Sets the `inlining_strategy` flag.
31    pub fn with_inlining_strategy(mut self, inlining_strategy: InliningStrategy) -> Self {
32        self.inlining_strategy = inlining_strategy;
33        self
34    }
35    /// Sets the `skip_const_folding` flag.
36    pub fn with_skip_const_folding(mut self, skip_const_folding: bool) -> Self {
37        self.skip_const_folding = skip_const_folding;
38        self
39    }
40}
41
42impl Default for OptimizationConfig {
43    fn default() -> Self {
44        Self {
45            moveable_functions: vec![],
46            inlining_strategy: InliningStrategy::Default,
47            skip_const_folding: false,
48        }
49    }
50}
51
52#[salsa::tracked(returns(ref))]
53pub fn priv_movable_function_ids<'db>(
54    db: &'db dyn Database,
55) -> UnorderedHashSet<ExternFunctionId<'db>> {
56    db.optimization_config()
57        .moveable_functions
58        .iter()
59        .map(|name: &String| {
60            let mut path_iter = name.split("::");
61
62            let mut module = ModuleHelper::core(db);
63
64            let mut next = path_iter.next();
65            while let Some(path_item) = next {
66                next = path_iter.next();
67                if next.is_some() {
68                    module = module.submodule(path_item);
69                    continue;
70                }
71                return module.extern_function_id(path_item);
72            }
73
74            panic!("Got empty string as movable_function");
75        })
76        .collect()
77}