Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline recursive calls #1022

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"compiler/vm",
"compiler/vm/fuzz",
]
default-members = ["compiler/cli"]

[workspace.package]
edition = "2021"
Expand Down
3 changes: 2 additions & 1 deletion compiler/frontend/src/mir_optimize/current_expression.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{pure::PurenessInsights, OptimizeMir};
use super::{inlining::InliningState, pure::PurenessInsights, OptimizeMir};
use crate::{
error::CompilerError,
id::IdGenerator,
Expand All @@ -16,6 +16,7 @@ pub struct Context<'a> {
pub visible: &'a mut VisibleExpressions,
pub id_generator: &'a mut IdGenerator<Id>,
pub pureness: &'a mut PurenessInsights,
pub inlining_state: InliningState,
}

pub struct CurrentExpression<'a> {
Expand Down
28 changes: 27 additions & 1 deletion compiler/frontend/src/mir_optimize/inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use crate::{
mir::{Expression, Id},
};
use rustc_hash::FxHashMap;
use std::{collections::hash_map::Entry, num::NonZeroUsize};

const NAME: &str = "Inlining";

Expand Down Expand Up @@ -113,6 +114,16 @@ pub fn inline_calls_with_constant_arguments(
}
}

#[derive(Clone, Debug, Default)]
pub struct InliningState {
recursive_inlining_counts: FxHashMap<Id, NonZeroUsize>,
}
impl InliningState {
/// To avoid infinite recursion, we limit the number of times a function can
/// be inlined into itself in a single module.
const MAX_RECURSION_INLINING_COUNT_IN_MODULE: usize = 1;
}

impl Context<'_> {
fn inline_call(&mut self, expression: &mut CurrentExpression) {
let Expression::Call {
Expand All @@ -126,7 +137,22 @@ impl Context<'_> {
};
if arguments.contains(function) {
// Callee is used as an argument → recursion
return;
match self
.inlining_state
.recursive_inlining_counts
.entry(*function)
{
Entry::Occupied(mut entry) => {
let count = entry.get_mut();
if count.get() >= InliningState::MAX_RECURSION_INLINING_COUNT_IN_MODULE {
return;
}
*count = count.saturating_add(1);
}
Entry::Vacant(entry) => {
entry.insert(NonZeroUsize::new(1).unwrap());
}
}
}

let Expression::Function {
Expand Down
2 changes: 2 additions & 0 deletions compiler/frontend/src/mir_optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

use self::{
current_expression::{Context, CurrentExpression},
inlining::InliningState,
log::OptimizationLogger,
pure::PurenessInsights,
};
Expand Down Expand Up @@ -155,6 +156,7 @@ impl Mir {
visible: &mut VisibleExpressions::none_visible(),
id_generator: &mut self.id_generator,
pureness,
inlining_state: InliningState::default(),
};
context.optimize_body(&mut self.body);
if cfg!(debug_assertions) {
Expand Down
3 changes: 1 addition & 2 deletions packages/Core/result.candy
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ and resultA resultB :=
needs (is resultB)
resultA | flatMap { value -> resultB }

# TODO: find a better name
also result okSideEffect :=
inspect result okSideEffect :=
needs (is result)
needs (function.is1 okSideEffect)
result %
Expand Down
4 changes: 2 additions & 2 deletions packages/FileSystem/file.candy
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ readBytes fileSystemFile path :=
needs (text.is path)

path | fileSystemFile.open
| result.also { file -> needs (function.is0 file) }
| result.inspect { file -> needs (function.is0 file) }
| result.flatMap { file ->
file | fileSystemFile.readToEnd | result.map { bytes -> [file, bytes] }
}
| result.also { [bytes] ->
| result.inspect { [bytes] ->
needs (list.is bytes)
needs (bytes | iterator.fromList | iterator.all { byte -> int.isUnsignedByte byte })
}
Expand Down
Loading