Skip to content

Commit

Permalink
feat: inline simple functions
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jan 23, 2025
1 parent 5f6ec4b commit 8c96690
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/noirc_evaluator/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ fn optimize_all(builder: SsaBuilder, options: &SsaEvaluatorOptions) -> Result<Ss
Ok(builder
.run_pass(Ssa::remove_unreachable_functions, "Removing Unreachable Functions (1st)")
.run_pass(Ssa::defunctionalize, "Defunctionalization")
.run_pass(Ssa::inline_simple_functions, "Inlining simple functions")
.run_pass(Ssa::remove_paired_rc, "Removing Paired rc_inc & rc_decs")
.run_pass(
|ssa| ssa.preprocess_functions(options.inliner_aggressiveness),
Expand Down
29 changes: 29 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,35 @@ impl Ssa {
});
self
}

pub(crate) fn inline_simple_functions(mut self: Ssa) -> Ssa {
let callers = compute_callers(&self);
let times_called = compute_times_called(&callers);

let should_inline_call = |function: &Function| {
let entry_block_id = function.entry_block();
let entry_block = &function.dfg[entry_block_id];

// If a function is only called once, inline it
if times_called.get(&function.id()) == Some(&1) {
return true;
}

// Only inline functions with a single block
if entry_block.successors().next().is_some() {
return false;
}

// Only inline functions with 0 or 1 instructions
entry_block.instructions().len() <= 1
};

self.functions = btree_map(self.functions.iter(), |(id, function)| {
(*id, function.inlined(&self, &should_inline_call))
});

self
}
}

impl Function {
Expand Down

0 comments on commit 8c96690

Please sign in to comment.