cairo_lang_lowering/optimizations/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::sync::Arc;

use cairo_lang_semantic::corelib;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_utils::unordered_hash_set::UnorderedHashSet;
use cairo_lang_utils::Intern;

use crate::db::LoweringGroup;
use crate::ids::{FunctionId, FunctionLongId};
use crate::utils::InliningStrategy;

/// The default threshold for inlining small functions. Decided according to sample contracts
/// profiling.
// TODO(Gil): Expose this as a configuration in the project toml.
const DEFAULT_INLINE_SMALL_FUNCTIONS_THRESHOLD: usize = 24;

/// A configuration struct that controls the behavior of the optimization passes.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct OptimizationConfig {
    /// A list of functions that can be moved during the reorder_statements optimization.
    pub moveable_functions: Vec<String>,
    /// The size of functions (in lowering statements) below which they are marked as
    /// `should_inline`.
    pub inline_small_functions_threshold: usize,
    /// Determines whether inlining is disabled.
    pub inlining_strategy: InliningStrategy,
}

impl OptimizationConfig {
    /// Sets the list of moveable functions.
    pub fn with_moveable_functions(mut self, moveable_functions: Vec<String>) -> Self {
        self.moveable_functions = moveable_functions;
        self
    }
    /// Sets the list of moveable functions to a minimal set, useful for testing.
    pub fn with_minimal_movable_functions(self) -> Self {
        self.with_moveable_functions(vec!["felt252_sub".into()])
    }
    /// Sets the threshold for inlining small functions.
    pub fn with_inline_small_functions_threshold(
        mut self,
        inline_small_functions_threshold: usize,
    ) -> Self {
        self.inline_small_functions_threshold = inline_small_functions_threshold;
        self
    }
    /// Sets the `inlining_strategy` flag
    pub fn with_inlining_strategy(mut self, inlining_strategy: InliningStrategy) -> Self {
        self.inlining_strategy = inlining_strategy;
        self
    }
}

impl Default for OptimizationConfig {
    fn default() -> Self {
        Self {
            moveable_functions: vec![],
            inline_small_functions_threshold: DEFAULT_INLINE_SMALL_FUNCTIONS_THRESHOLD,
            inlining_strategy: InliningStrategy::Default,
        }
    }
}

pub fn priv_movable_function_ids(db: &dyn LoweringGroup) -> Arc<UnorderedHashSet<FunctionId>> {
    let semantic_db: &dyn SemanticGroup = db.elongate();
    let libfunc_by_name = |name: &String| {
        let mut path_iter = name.split("::");

        let mut module = db.core_module();

        let mut next = path_iter.next();
        while let Some(path_item) = next {
            next = path_iter.next();
            if next.is_some() {
                module = corelib::get_submodule(semantic_db, module, path_item)
                    .unwrap_or_else(|| panic!("module not found: {}", path_item));
                continue;
            }

            return FunctionLongId::Semantic(corelib::get_function_id(
                semantic_db,
                module,
                path_item.into(),
                vec![],
            ))
            .intern(db);
        }

        panic!("Got empty string as movable_function");
    };

    Arc::new(db.optimization_config().moveable_functions.iter().map(libfunc_by_name).collect())
}