Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions compiler/noirc_evaluator/src/ssa/opt/inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ impl Ssa {
self
}

/// Inline "simple" functions.
/// A simple function being a function with one or less instructions.
/// Simple functions are still restricted to not inlined if they are recursive or marked with no predicates.
pub(crate) fn inline_simple_functions(mut self: Ssa) -> Ssa {
let should_inline_call = |callee: &Function| {
if let RuntimeType::Acir(_) = callee.runtime() {
Expand All @@ -112,10 +115,32 @@ impl Ssa {
}

// Only inline functions with 0 or 1 instructions
entry_block.instructions().len() <= 1
if entry_block.instructions().len() > 1 {
return false;
}

let instructions = callee.dfg[entry_block_id].instructions();
if instructions.is_empty() {
return true;
}

// Check whether the only instruction is a recursive call, which prevents inlining the callee.
// This special check is done here to avoid performing the entire inline info computation.
// The inline info computation contains extra logic and requires passing over every function.
// which we can avoid in when inlining simple functions.
let only_instruction = callee.dfg[entry_block_id].instructions()[0];
if let Instruction::Call { func, .. } = callee.dfg[only_instruction] {
let Value::Function(func_id) = callee.dfg[func] else {
return true;
};

func_id != callee.id()
} else {
true
}
};

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "simple_infinite_recursive_function"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main() {
// Safety: testing context
unsafe {
foo();
}
}

unconstrained fn foo() {
bar();
}

unconstrained fn bar() {
bar();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "simple_infinite_recursive_lambda"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn main() {
// Safety: testing context
unsafe {
foo(bar);
}
}

unconstrained fn foo<Env>(f: unconstrained fn[Env]()) {
f();
f();
}

unconstrained fn bar() {
bar();
bar();
}
Loading