cairo_lang_lowering/optimizations/
config.rsuse std::sync::Arc;
use cairo_lang_semantic::corelib;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_utils::Intern;
use cairo_lang_utils::unordered_hash_set::UnorderedHashSet;
use crate::db::LoweringGroup;
use crate::ids::{FunctionId, FunctionLongId};
use crate::utils::InliningStrategy;
const DEFAULT_INLINE_SMALL_FUNCTIONS_THRESHOLD: usize = 24;
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct OptimizationConfig {
pub moveable_functions: Vec<String>,
pub inline_small_functions_threshold: usize,
pub inlining_strategy: InliningStrategy,
pub skip_const_folding: bool,
}
impl OptimizationConfig {
pub fn with_moveable_functions(mut self, moveable_functions: Vec<String>) -> Self {
self.moveable_functions = moveable_functions;
self
}
pub fn with_minimal_movable_functions(self) -> Self {
self.with_moveable_functions(vec!["felt252_sub".into()])
}
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
}
pub fn with_inlining_strategy(mut self, inlining_strategy: InliningStrategy) -> Self {
self.inlining_strategy = inlining_strategy;
self
}
pub fn with_skip_const_folding(mut self, skip_const_folding: bool) -> Self {
self.skip_const_folding = skip_const_folding;
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,
skip_const_folding: false,
}
}
}
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())
}