Integrate function inlining into compiler

This commit is contained in:
d0cd 2023-02-10 09:30:28 -08:00
parent ccae285408
commit 31592a9b02
3 changed files with 31 additions and 4 deletions

View File

@ -196,14 +196,33 @@ impl<'a> Compiler<'a> {
}
/// Runs the flattening pass.
pub fn flattening_pass(&mut self, symbol_table: &SymbolTable, assigner: Assigner) -> Result<()> {
self.ast = Flattener::do_pass((std::mem::take(&mut self.ast), symbol_table, assigner))?;
pub fn flattening_pass(&mut self, symbol_table: &SymbolTable, assigner: Assigner) -> Result<Assigner> {
let (ast, assigner) = Flattener::do_pass((std::mem::take(&mut self.ast), symbol_table, assigner))?;
self.ast = ast;
if self.output_options.flattened_ast {
self.write_ast_to_json("flattened_ast.json")?;
}
Ok(())
Ok(assigner)
}
/// Runs the function inlining pass.
pub fn function_inlining_pass(
&mut self,
symbol_table: &SymbolTable,
call_graph: &CallGraph,
assigner: Assigner,
) -> Result<Assigner> {
let (ast, assigner) =
FunctionInliner::do_pass((std::mem::take(&mut self.ast), symbol_table, call_graph, assigner))?;
self.ast = ast;
if self.output_options.inlined_ast {
self.write_ast_to_json("inlined_ast.json")?;
}
Ok(assigner)
}
/// Runs the compiler stages.
@ -220,7 +239,9 @@ impl<'a> Compiler<'a> {
// TODO: Make this pass optional.
let assigner = self.static_single_assignment_pass(&st, assigner)?;
self.flattening_pass(&st, assigner)?;
let assigner = self.flattening_pass(&st, assigner)?;
let _ = self.function_inlining_pass(&st, &call_graph, assigner)?;
Ok((st, struct_graph, call_graph))
}

View File

@ -30,4 +30,6 @@ pub struct OutputOptions {
pub ssa_ast: bool,
/// If enabled writes the AST after flattening.
pub flattened_ast: bool,
/// If enabled writes the AST after inlining.
pub inlined_ast: bool,
}

View File

@ -58,6 +58,8 @@ pub struct BuildOptions {
pub enable_ssa_ast_snapshot: bool,
#[structopt(long, help = "Writes AST snapshot of the flattened AST.")]
pub enable_flattened_ast_snapshot: bool,
#[structopt(long, help = "Writes AST snapshot of the inlined AST.")]
pub enable_inlined_ast_snapshot: bool,
}
impl From<BuildOptions> for OutputOptions {
@ -69,6 +71,7 @@ impl From<BuildOptions> for OutputOptions {
unrolled_ast: options.enable_unrolled_ast_snapshot,
ssa_ast: options.enable_ssa_ast_snapshot,
flattened_ast: options.enable_flattened_ast_snapshot,
inlined_ast: options.enable_inlined_ast_snapshot,
};
if options.enable_all_ast_snapshots {
out_options.initial_input_ast = true;
@ -76,6 +79,7 @@ impl From<BuildOptions> for OutputOptions {
out_options.unrolled_ast = true;
out_options.ssa_ast = true;
out_options.flattened_ast = true;
out_options.inlined_ast = true;
}
out_options