Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
77 changes: 58 additions & 19 deletions crates/lean_compiler/src/a_simplify_lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ pub enum SimpleLine {
LocationReport {
location: SourceLineNumber,
},
Goto {
label: String,
},
}

pub fn simplify_program(mut program: Program) -> SimpleProgram {
Expand Down Expand Up @@ -690,6 +693,11 @@ fn simplify_lines(
location: *location,
});
}
Line::Goto { label } => {
res.push(SimpleLine::Goto {
label: label.clone(),
});
}
}
}

Expand Down Expand Up @@ -898,7 +906,7 @@ pub fn find_variable_usage(lines: &[Line]) -> (BTreeSet<Var>, BTreeSet<Var>) {
on_new_expr(index, &internal_vars, &mut external_vars);
on_new_expr(value, &internal_vars, &mut external_vars);
}
Line::Panic | Line::Break | Line::LocationReport { .. } => {}
Line::Panic | Line::Break | Line::LocationReport { .. } | Line::Goto { .. } => {}
}
}

Expand Down Expand Up @@ -943,6 +951,7 @@ pub fn inline_lines(
args: &BTreeMap<Var, SimpleExpr>,
res: &[Var],
inlining_count: usize,
epilogue_label: Option<String>,
) {
let inline_condition = |condition: &mut Boolean| {
let (Boolean::Equal { left, right } | Boolean::Different { left, right }) = condition;
Expand All @@ -964,7 +973,13 @@ pub fn inline_lines(
Line::Match { value, arms } => {
inline_expr(value, args, inlining_count);
for (_, statements) in arms {
inline_lines(statements, args, res, inlining_count);
inline_lines(
statements,
args,
res,
inlining_count,
epilogue_label.clone(),
);
}
}
Line::Assignment { var, value } => {
Expand All @@ -978,8 +993,20 @@ pub fn inline_lines(
} => {
inline_condition(condition);

inline_lines(then_branch, args, res, inlining_count);
inline_lines(else_branch, args, res, inlining_count);
inline_lines(
then_branch,
args,
res,
inlining_count,
epilogue_label.clone(),
);
inline_lines(
else_branch,
args,
res,
inlining_count,
epilogue_label.clone(),
);
}
Line::FunctionCall {
args: func_args,
Expand All @@ -1002,16 +1029,22 @@ pub fn inline_lines(
for expr in return_data.iter_mut() {
inline_expr(expr, args, inlining_count);
}
lines_to_replace.push((
i,
res.iter()
.zip(return_data)
.map(|(res_var, expr)| Line::Assignment {
var: res_var.clone(),
value: expr.clone(),
})
.collect::<Vec<_>>(),
));

let mut replacement_lines = res
.iter()
.zip(return_data)
.map(|(res_var, expr)| Line::Assignment {
var: res_var.clone(),
value: expr.clone(),
})
.collect::<Vec<_>>();

if let Some(label) = &epilogue_label {
replacement_lines.push(Line::Goto {
label: label.clone(),
});
}
lines_to_replace.push((i, replacement_lines));
}
Line::MAlloc { var, size, .. } => {
inline_expr(size, args, inlining_count);
Expand Down Expand Up @@ -1048,7 +1081,7 @@ pub fn inline_lines(
rev: _,
unroll: _,
} => {
inline_lines(body, args, res, inlining_count);
inline_lines(body, args, res, inlining_count, epilogue_label.clone());
inline_internal_var(iterator);
inline_expr(start, args, inlining_count);
inline_expr(end, args, inlining_count);
Expand All @@ -1062,7 +1095,7 @@ pub fn inline_lines(
inline_expr(index, args, inlining_count);
inline_expr(value, args, inlining_count);
}
Line::Panic | Line::Break | Line::LocationReport { .. } => {}
Line::Panic | Line::Goto { .. } | Line::Break | Line::LocationReport { .. } => {}
}
}
for (i, new_lines) in lines_to_replace.into_iter().rev() {
Expand Down Expand Up @@ -1531,7 +1564,7 @@ fn replace_vars_for_unroll(
Line::CounterHint { var } => {
*var = format!("@unrolled_{unroll_index}_{iterator_value}_{var}");
}
Line::Break | Line::Panic | Line::LocationReport { .. } => {}
Line::Break | Line::Panic | Line::LocationReport { .. } | Line::Goto { .. } => {}
}
}
}
Expand Down Expand Up @@ -1652,12 +1685,14 @@ fn handle_inlined_functions_helper(
.zip(&simplified_args)
.map(|((var, _), expr)| (var.clone(), expr.clone()))
.collect::<BTreeMap<_, _>>();
let epilogue_label = format!("@inline_epilogue_{}", total_inlined_counter.0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This idea of "epilogue_label" looks promising. Currently you always put an epilogue_label when inlining but in some cases, when there is no instructions after the RETURN this is unnecessary (we can call such RETURNs "final"). But detecting it is not trivial. We would somehow need to peek at the following of the code (what comes after the RETURN), to see if it's terminal or not, if that makes sense

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, you don't need to always put the label. But yeah finding where you don't need to is quite difficult. We'll need to have some context of what is final in the remainder of the scope.

let mut func_body = func.body.clone();
inline_lines(
&mut func_body,
&inlined_args,
return_data,
total_inlined_counter.next(),
Some(epilogue_label.clone()),
);
inlined_lines.extend(func_body);

Expand Down Expand Up @@ -1921,7 +1956,8 @@ fn get_function_called(lines: &[Line], function_called: &mut Vec<String>) {
| Line::MAlloc { .. }
| Line::Panic
| Line::Break
| Line::LocationReport { .. } => {}
| Line::LocationReport { .. }
| Line::Goto { .. } => {}
}
}
}
Expand Down Expand Up @@ -2025,7 +2061,7 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
assert!(!map.contains_key(var), "Variable {var} is a constant");
replace_vars_by_const_in_expr(size, map);
}
Line::Panic | Line::Break | Line::LocationReport { .. } => {}
Line::Panic | Line::Break | Line::LocationReport { .. } | Line::Goto { .. } => {}
}
}
}
Expand Down Expand Up @@ -2213,6 +2249,9 @@ impl SimpleLine {
}
Self::Panic => "panic".to_string(),
Self::LocationReport { .. } => Default::default(),
Self::Goto { label } => {
format!("goto_{}", label)
}
};
format!("{spaces}{line_str}")
}
Expand Down
15 changes: 14 additions & 1 deletion crates/lean_compiler/src/b_compile_intermediate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,18 @@ fn compile_lines(
location: *location,
});
}
SimpleLine::Goto { label } => {
let goto_label = Label::Custom(label.clone());
instructions.push(IntermediateInstruction::Jump {
dest: IntermediateValue::label(goto_label.clone()),
updated_fp: None,
});

let remaining =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a GOTO intuitively there should not remain any lines to compile? So we could simply assert lines[i + 1..].is_empty() instead of compiling it? Or maybe am I missing some case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one cannot assert that lines[i + 1..] is empty after a GOTO. There are valid cases where lines exist after a GOTO, and those lines need to be compiled and registered with the epilogue label.

compile_lines(&lines[i + 1..], compiler, final_jump, declared_vars)?;
compiler.bytecode.insert(goto_label, remaining);
return Ok(instructions);
}
}
}

Expand Down Expand Up @@ -795,7 +807,8 @@ fn find_internal_vars(lines: &[SimpleLine]) -> BTreeSet<Var> {
| SimpleLine::Print { .. }
| SimpleLine::FunctionRet { .. }
| SimpleLine::Precompile { .. }
| SimpleLine::LocationReport { .. } => {}
| SimpleLine::LocationReport { .. }
| SimpleLine::Goto { .. } => {}
}
}
internal_vars
Expand Down
6 changes: 6 additions & 0 deletions crates/lean_compiler/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ pub enum Line {
LocationReport {
location: SourceLineNumber,
},
Goto {
label: String,
},
}
impl Display for Expression {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand Down Expand Up @@ -561,6 +564,9 @@ impl Line {
}
Self::Break => "break".to_string(),
Self::Panic => "panic".to_string(),
Self::Goto { label } => {
format!("goto_{}", label)
}
};
format!("{spaces}{line_str}")
}
Expand Down
35 changes: 18 additions & 17 deletions crates/lean_compiler/tests/test_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,23 +438,24 @@ fn test_match() {
compile_and_run(program, &[], &[], DEFAULT_NO_VEC_RUNTIME_MEMORY, false);
}

// #[test]
// fn inline_bug_mre() {
// let program = r#"
// fn main() {
// boolean(0);
// return;
// }

// fn boolean(a) inline -> 1 {
// if a == 0 {
// return 0;
// }
// return 1;
// }
// "#;
// compile_and_run(program, &[], &[]);
// }
#[test]
fn inline_bug_mre() {
let program = r#"
fn main() {
x = boolean(0);
return;
}

fn boolean(a) inline -> 1 {
if a == 0 {
return 0;
}
retur3n 1;
}
"#;

compile_and_run(program, &[], &[], DEFAULT_NO_VEC_RUNTIME_MEMORY, false);
}

#[test]
fn test_const_functions_calling_const_functions() {
Expand Down
Loading