Skip to content

Commit dafcfed

Browse files
authoredMar 5, 2025··
Added ability to control inlining level through optimization config. (#7401)
1 parent c427052 commit dafcfed

File tree

3 files changed

+22
-34
lines changed

3 files changed

+22
-34
lines changed
 

‎crates/cairo-lang-lowering/src/inline/mod.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,35 @@ pub fn priv_should_inline(
6767
let config = db.function_declaration_inline_config(
6868
function_id.function_with_body_id(db).base_semantic_function(db),
6969
)?;
70-
71-
Ok(match db.optimization_config().inlining_strategy {
72-
InliningStrategy::Default => match config {
73-
InlineConfiguration::Never(_) => false,
74-
InlineConfiguration::Should(_) => true,
75-
InlineConfiguration::Always(_) => true,
76-
InlineConfiguration::None => should_inline_lowered(db, function_id)?,
77-
},
78-
InliningStrategy::Avoid => matches!(config, InlineConfiguration::Always(_)),
70+
Ok(match (db.optimization_config().inlining_strategy, config) {
71+
(_, InlineConfiguration::Always(_)) => true,
72+
(InliningStrategy::Avoid, _) | (_, InlineConfiguration::Never(_)) => false,
73+
(_, InlineConfiguration::Should(_)) => true,
74+
(InliningStrategy::Default, InlineConfiguration::None) => {
75+
/// The default threshold for inlining small functions. Decided according to sample
76+
/// contracts profiling.
77+
const DEFAULT_INLINE_SMALL_FUNCTIONS_THRESHOLD: usize = 24;
78+
should_inline_lowered(db, function_id, DEFAULT_INLINE_SMALL_FUNCTIONS_THRESHOLD)?
79+
}
80+
(InliningStrategy::InlineSmallFunctions(threshold), InlineConfiguration::None) => {
81+
should_inline_lowered(db, function_id, threshold)?
82+
}
7983
})
8084
}
8185

8286
// A heuristic to decide if a function without an inline attribute should be inlined.
8387
fn should_inline_lowered(
8488
db: &dyn LoweringGroup,
8589
function_id: ConcreteFunctionWithBodyId,
90+
inline_small_functions_threshold: usize,
8691
) -> Maybe<bool> {
8792
let lowered = db.inlined_function_with_body_lowered(function_id)?;
8893
// The inline heuristics optimization flag only applies to non-trivial small functions.
8994
// Functions which contains only a call or a literal are always inlined.
9095

9196
let weight_of_blocks = ApproxCasmInlineWeight::new(db, &lowered).lowered_weight(&lowered);
9297

93-
if weight_of_blocks < inline_small_functions_threshold(db).into_or_panic() {
98+
if weight_of_blocks < inline_small_functions_threshold.into_or_panic() {
9499
return Ok(true);
95100
}
96101

@@ -99,7 +104,7 @@ fn should_inline_lowered(
99104
// Functions which contains only a call or a literal are always inlined.
100105
let num_of_statements: usize =
101106
lowered.blocks.iter().map(|(_, block)| block.statements.len()).sum();
102-
if num_of_statements < inline_small_functions_threshold(db) {
107+
if num_of_statements < inline_small_functions_threshold {
103108
return Ok(true);
104109
}
105110

@@ -369,9 +374,3 @@ pub fn apply_inlining(
369374
}
370375
Ok(())
371376
}
372-
373-
/// Returns the threshold, in number of lowering statements, below which a function is marked as
374-
/// `should_inline`.
375-
fn inline_small_functions_threshold(db: &dyn LoweringGroup) -> usize {
376-
db.optimization_config().inline_small_functions_threshold
377-
}

‎crates/cairo-lang-lowering/src/optimizations/config.rs

-17
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,11 @@ use crate::db::LoweringGroup;
99
use crate::ids::{FunctionId, FunctionLongId};
1010
use crate::utils::InliningStrategy;
1111

12-
/// The default threshold for inlining small functions. Decided according to sample contracts
13-
/// profiling.
14-
// TODO(Gil): Expose this as a configuration in the project toml.
15-
const DEFAULT_INLINE_SMALL_FUNCTIONS_THRESHOLD: usize = 24;
16-
1712
/// A configuration struct that controls the behavior of the optimization passes.
1813
#[derive(Debug, Eq, PartialEq, Clone)]
1914
pub struct OptimizationConfig {
2015
/// A list of functions that can be moved during the reorder_statements optimization.
2116
pub moveable_functions: Vec<String>,
22-
/// The size of functions (in lowering statements) below which they are marked as
23-
/// `should_inline`.
24-
pub inline_small_functions_threshold: usize,
2517
/// Determines whether inlining is disabled.
2618
pub inlining_strategy: InliningStrategy,
2719
/// Should const folding be skipped.
@@ -38,14 +30,6 @@ impl OptimizationConfig {
3830
pub fn with_minimal_movable_functions(self) -> Self {
3931
self.with_moveable_functions(vec!["felt252_sub".into()])
4032
}
41-
/// Sets the threshold for inlining small functions.
42-
pub fn with_inline_small_functions_threshold(
43-
mut self,
44-
inline_small_functions_threshold: usize,
45-
) -> Self {
46-
self.inline_small_functions_threshold = inline_small_functions_threshold;
47-
self
48-
}
4933
/// Sets the `inlining_strategy` flag.
5034
pub fn with_inlining_strategy(mut self, inlining_strategy: InliningStrategy) -> Self {
5135
self.inlining_strategy = inlining_strategy;
@@ -62,7 +46,6 @@ impl Default for OptimizationConfig {
6246
fn default() -> Self {
6347
Self {
6448
moveable_functions: vec![],
65-
inline_small_functions_threshold: DEFAULT_INLINE_SMALL_FUNCTIONS_THRESHOLD,
6649
inlining_strategy: InliningStrategy::Default,
6750
skip_const_folding: false,
6851
}

‎crates/cairo-lang-lowering/src/utils.rs

+6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ use crate::{
1212
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
1313
pub enum InliningStrategy {
1414
/// Do not override inlining strategy.
15+
///
16+
/// Note: equivalent to `InlineSmallFunctions(DEFAULT_INLINE_SMALL_FUNCTIONS_THRESHOLD)`.
1517
#[default]
1618
Default,
19+
/// Should inline small functions up to the given weight.
20+
///
21+
/// Note: the weight exact definition is subject to change.
22+
InlineSmallFunctions(usize),
1723
/// Inline only in the case of an `inline(always)` annotation.
1824
Avoid,
1925
}

0 commit comments

Comments
 (0)
Please sign in to comment.