Route IdentIds to refcount proc generator in dev backend

This commit is contained in:
Brian Carroll 2021-12-02 09:58:34 +00:00
parent b193483756
commit d0ef1e5b36
5 changed files with 77 additions and 28 deletions

View file

@ -3,7 +3,7 @@ use roc_gen_llvm::llvm::build::module_from_builtins;
#[cfg(feature = "llvm")] #[cfg(feature = "llvm")]
pub use roc_gen_llvm::llvm::build::FunctionIterator; pub use roc_gen_llvm::llvm::build::FunctionIterator;
use roc_load::file::{LoadedModule, MonomorphizedModule}; use roc_load::file::{LoadedModule, MonomorphizedModule};
use roc_module::symbol::{Interns, ModuleId}; use roc_module::symbol::{get_module_ident_ids, Interns, ModuleId};
use roc_mono::ir::OptLevel; use roc_mono::ir::OptLevel;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::Duration; use std::time::Duration;
@ -526,15 +526,26 @@ fn gen_from_mono_module_dev_assembly(
let lazy_literals = true; let lazy_literals = true;
let generate_allocators = false; // provided by the platform let generate_allocators = false; // provided by the platform
let MonomorphizedModule {
module_id,
procedures,
interns,
exposed_to_host,
..
} = loaded;
let mut ident_ids = get_module_ident_ids(&interns.all_ident_ids, &module_id).unwrap().clone();
let env = roc_gen_dev::Env { let env = roc_gen_dev::Env {
arena, arena,
interns: loaded.interns, module_id,
exposed_to_host: loaded.exposed_to_host.keys().copied().collect(), interns,
exposed_to_host: exposed_to_host.keys().copied().collect(),
lazy_literals, lazy_literals,
generate_allocators, generate_allocators,
}; };
let module_object = roc_gen_dev::build_module(&env, target, loaded.procedures); let module_object = roc_gen_dev::build_module(&env, &mut ident_ids, target, procedures);
let module_out = module_object let module_out = module_object
.write() .write()

View file

@ -2,7 +2,8 @@ use crate::{Backend, Env, Relocation};
use bumpalo::collections::Vec; use bumpalo::collections::Vec;
use roc_builtins::bitcode::{FloatWidth, IntWidth}; use roc_builtins::bitcode::{FloatWidth, IntWidth};
use roc_collections::all::{MutMap, MutSet}; use roc_collections::all::{MutMap, MutSet};
use roc_module::symbol::Symbol; use roc_module::symbol::{IdentIds, Symbol};
use roc_mono::gen_refcount::RefcountProcGenerator;
use roc_mono::ir::{BranchInfo, JoinPointId, Literal, Param, SelfRecursive, Stmt}; use roc_mono::ir::{BranchInfo, JoinPointId, Literal, Param, SelfRecursive, Stmt};
use roc_mono::layout::{Builtin, Layout}; use roc_mono::layout::{Builtin, Layout};
use roc_reporting::internal_error; use roc_reporting::internal_error;
@ -226,6 +227,7 @@ pub struct Backend64Bit<
phantom_asm: PhantomData<ASM>, phantom_asm: PhantomData<ASM>,
phantom_cc: PhantomData<CC>, phantom_cc: PhantomData<CC>,
env: &'a Env<'a>, env: &'a Env<'a>,
refcount_proc_gen: RefcountProcGenerator<'a>,
buf: Vec<'a, u8>, buf: Vec<'a, u8>,
relocs: Vec<'a, Relocation>, relocs: Vec<'a, Relocation>,
proc_name: Option<String>, proc_name: Option<String>,
@ -273,6 +275,7 @@ impl<
phantom_asm: PhantomData, phantom_asm: PhantomData,
phantom_cc: PhantomData, phantom_cc: PhantomData,
env, env,
refcount_proc_gen: RefcountProcGenerator::new(env.arena, IntWidth::I64, env.module_id),
proc_name: None, proc_name: None,
is_self_recursive: None, is_self_recursive: None,
buf: bumpalo::vec![in env.arena], buf: bumpalo::vec![in env.arena],
@ -298,6 +301,9 @@ impl<
fn env(&self) -> &'a Env<'a> { fn env(&self) -> &'a Env<'a> {
self.env self.env
} }
fn refcount_proc_gen_mut(&mut self) -> &mut RefcountProcGenerator<'a> {
&mut self.refcount_proc_gen
}
fn reset(&mut self, name: String, is_self_recursive: SelfRecursive) { fn reset(&mut self, name: String, is_self_recursive: SelfRecursive) {
self.proc_name = Some(name); self.proc_name = Some(name);
@ -532,6 +538,7 @@ impl<
fn build_switch( fn build_switch(
&mut self, &mut self,
ident_ids: &mut IdentIds,
cond_symbol: &Symbol, cond_symbol: &Symbol,
_cond_layout: &Layout<'a>, // cond_layout must be a integer due to potential jump table optimizations. _cond_layout: &Layout<'a>, // cond_layout must be a integer due to potential jump table optimizations.
branches: &'a [(u64, BranchInfo<'a>, Stmt<'a>)], branches: &'a [(u64, BranchInfo<'a>, Stmt<'a>)],
@ -554,7 +561,7 @@ impl<
let start_offset = ASM::jne_reg64_imm64_imm32(&mut self.buf, cond_reg, *val, 0); let start_offset = ASM::jne_reg64_imm64_imm32(&mut self.buf, cond_reg, *val, 0);
// Build all statements in this branch. // Build all statements in this branch.
self.build_stmt(stmt, ret_layout); self.build_stmt(ident_ids, stmt, ret_layout);
// Build unconditional jump to the end of this switch. // Build unconditional jump to the end of this switch.
// Since we don't know the offset yet, set it to 0 and overwrite later. // Since we don't know the offset yet, set it to 0 and overwrite later.
@ -578,7 +585,7 @@ impl<
} }
let (branch_info, stmt) = default_branch; let (branch_info, stmt) = default_branch;
if let BranchInfo::None = branch_info { if let BranchInfo::None = branch_info {
self.build_stmt(stmt, ret_layout); self.build_stmt(ident_ids, stmt, ret_layout);
// Update all return jumps to jump past the default case. // Update all return jumps to jump past the default case.
let ret_offset = self.buf.len(); let ret_offset = self.buf.len();
@ -600,6 +607,7 @@ impl<
fn build_join( fn build_join(
&mut self, &mut self,
ident_ids: &mut IdentIds,
id: &JoinPointId, id: &JoinPointId,
parameters: &'a [Param<'a>], parameters: &'a [Param<'a>],
body: &'a Stmt<'a>, body: &'a Stmt<'a>,
@ -638,7 +646,7 @@ impl<
sub_backend.load_args(args.into_bump_slice(), ret_layout); sub_backend.load_args(args.into_bump_slice(), ret_layout);
// Build all statements in body. // Build all statements in body.
sub_backend.build_stmt(body, ret_layout); sub_backend.build_stmt(ident_ids, body, ret_layout);
// Merge the "sub function" into the main function. // Merge the "sub function" into the main function.
let sub_func_offset = self.buf.len() as u64; let sub_func_offset = self.buf.len() as u64;
@ -682,7 +690,7 @@ impl<
); );
// Build remainder of function. // Build remainder of function.
self.build_stmt(remainder, ret_layout) self.build_stmt(ident_ids, remainder, ret_layout)
} }
fn build_jump( fn build_jump(

View file

@ -7,7 +7,8 @@ use roc_builtins::bitcode::{self, FloatWidth, IntWidth};
use roc_collections::all::{MutMap, MutSet}; use roc_collections::all::{MutMap, MutSet};
use roc_module::ident::{ModuleName, TagName}; use roc_module::ident::{ModuleName, TagName};
use roc_module::low_level::LowLevel; use roc_module::low_level::LowLevel;
use roc_module::symbol::{Interns, Symbol}; use roc_module::symbol::{IdentIds, Interns, ModuleId, Symbol};
use roc_mono::gen_refcount::RefcountProcGenerator;
use roc_mono::ir::{ use roc_mono::ir::{
BranchInfo, CallType, Expr, JoinPointId, ListLiteralElement, Literal, Param, Proc, BranchInfo, CallType, Expr, JoinPointId, ListLiteralElement, Literal, Param, Proc,
SelfRecursive, Stmt, SelfRecursive, Stmt,
@ -22,6 +23,7 @@ mod run_roc;
pub struct Env<'a> { pub struct Env<'a> {
pub arena: &'a Bump, pub arena: &'a Bump,
pub module_id: ModuleId,
pub interns: Interns, pub interns: Interns,
pub exposed_to_host: MutSet<Symbol>, pub exposed_to_host: MutSet<Symbol>,
pub lazy_literals: bool, pub lazy_literals: bool,
@ -63,6 +65,8 @@ where
fn env(&self) -> &'a Env<'a>; fn env(&self) -> &'a Env<'a>;
fn refcount_proc_gen_mut(&mut self) -> &mut RefcountProcGenerator<'a>;
/// reset resets any registers or other values that may be occupied at the end of a procedure. /// reset resets any registers or other values that may be occupied at the end of a procedure.
/// It also passes basic procedure information to the builder for setup of the next function. /// It also passes basic procedure information to the builder for setup of the next function.
fn reset(&mut self, name: String, is_self_recursive: SelfRecursive); fn reset(&mut self, name: String, is_self_recursive: SelfRecursive);
@ -81,7 +85,11 @@ where
fn build_wrapped_jmp(&mut self) -> (&'a [u8], u64); fn build_wrapped_jmp(&mut self) -> (&'a [u8], u64);
/// build_proc creates a procedure and outputs it to the wrapped object writer. /// build_proc creates a procedure and outputs it to the wrapped object writer.
fn build_proc(&mut self, proc: Proc<'a>) -> (&'a [u8], &[Relocation]) { fn build_proc(
&mut self,
ident_ids: &mut IdentIds,
proc: Proc<'a>,
) -> (&'a [u8], &[Relocation]) {
let proc_name = LayoutIds::default() let proc_name = LayoutIds::default()
.get(proc.name, &proc.ret_layout) .get(proc.name, &proc.ret_layout)
.to_symbol_string(proc.name, &self.env().interns); .to_symbol_string(proc.name, &self.env().interns);
@ -92,27 +100,37 @@ where
} }
self.scan_ast(&proc.body); self.scan_ast(&proc.body);
self.create_free_map(); self.create_free_map();
self.build_stmt(&proc.body, &proc.ret_layout); self.build_stmt(ident_ids, &proc.body, &proc.ret_layout);
self.finalize() self.finalize()
} }
/// build_stmt builds a statement and outputs at the end of the buffer. /// build_stmt builds a statement and outputs at the end of the buffer.
fn build_stmt(&mut self, stmt: &Stmt<'a>, ret_layout: &Layout<'a>) { fn build_stmt(&mut self, ident_ids: &mut IdentIds, stmt: &Stmt<'a>, ret_layout: &Layout<'a>) {
match stmt { match stmt {
Stmt::Let(sym, expr, layout, following) => { Stmt::Let(sym, expr, layout, following) => {
self.build_expr(sym, expr, layout); self.build_expr(sym, expr, layout);
self.set_layout_map(*sym, layout); self.set_layout_map(*sym, layout);
self.free_symbols(stmt); self.free_symbols(stmt);
self.build_stmt(following, ret_layout); self.build_stmt(ident_ids, following, ret_layout);
} }
Stmt::Ret(sym) => { Stmt::Ret(sym) => {
self.load_literal_symbols(&[*sym]); self.load_literal_symbols(&[*sym]);
self.return_symbol(sym, ret_layout); self.return_symbol(sym, ret_layout);
self.free_symbols(stmt); self.free_symbols(stmt);
} }
Stmt::Refcounting(_modify, following) => { Stmt::Refcounting(modify, following) => {
// TODO: actually deal with refcounting. For hello world, we just skipped it. let sym = modify.get_symbol();
self.build_stmt(following, ret_layout); let layout = self.layout_map().get(&sym).unwrap().clone();
// Expand the Refcounting statement into more detailed IR with a function call
// If this layout requires a new RC proc, we get enough info to create a linker symbol
// for it. Here we don't create linker symbols at this time, but in Wasm backend, we do.
let (_rc_stmt, _new_proc_info) = self
.refcount_proc_gen_mut()
.expand_refcount_stmt(ident_ids, layout, modify, *following);
// TODO: actually use the rc_stmt!! For now just trying to satisfy the borrow checker
self.build_stmt(ident_ids, *following, ret_layout)
} }
Stmt::Switch { Stmt::Switch {
cond_symbol, cond_symbol,
@ -123,6 +141,7 @@ where
} => { } => {
self.load_literal_symbols(&[*cond_symbol]); self.load_literal_symbols(&[*cond_symbol]);
self.build_switch( self.build_switch(
ident_ids,
cond_symbol, cond_symbol,
cond_layout, cond_layout,
branches, branches,
@ -140,7 +159,7 @@ where
for param in parameters.iter() { for param in parameters.iter() {
self.set_layout_map(param.symbol, &param.layout); self.set_layout_map(param.symbol, &param.layout);
} }
self.build_join(id, parameters, body, remainder, ret_layout); self.build_join(ident_ids, id, parameters, body, remainder, ret_layout);
self.free_symbols(stmt); self.free_symbols(stmt);
} }
Stmt::Jump(id, args) => { Stmt::Jump(id, args) => {
@ -164,6 +183,7 @@ where
// build_switch generates a instructions for a switch statement. // build_switch generates a instructions for a switch statement.
fn build_switch( fn build_switch(
&mut self, &mut self,
ident_ids: &mut IdentIds,
cond_symbol: &Symbol, cond_symbol: &Symbol,
cond_layout: &Layout<'a>, cond_layout: &Layout<'a>,
branches: &'a [(u64, BranchInfo<'a>, Stmt<'a>)], branches: &'a [(u64, BranchInfo<'a>, Stmt<'a>)],
@ -174,6 +194,7 @@ where
// build_join generates a instructions for a join statement. // build_join generates a instructions for a join statement.
fn build_join( fn build_join(
&mut self, &mut self,
ident_ids: &mut IdentIds,
id: &JoinPointId, id: &JoinPointId,
parameters: &'a [Param<'a>], parameters: &'a [Param<'a>],
body: &'a Stmt<'a>, body: &'a Stmt<'a>,

View file

@ -8,7 +8,7 @@ use object::{
SymbolFlags, SymbolKind, SymbolScope, SymbolFlags, SymbolKind, SymbolScope,
}; };
use roc_collections::all::MutMap; use roc_collections::all::MutMap;
use roc_module::symbol; use roc_module::symbol::{self, IdentIds};
use roc_mono::ir::{Proc, ProcLayout}; use roc_mono::ir::{Proc, ProcLayout};
use roc_mono::layout::LayoutIds; use roc_mono::layout::LayoutIds;
use roc_reporting::internal_error; use roc_reporting::internal_error;
@ -22,6 +22,7 @@ use target_lexicon::{Architecture as TargetArch, BinaryFormat as TargetBF, Tripl
/// It takes the request to build a module and output the object file for the module. /// It takes the request to build a module and output the object file for the module.
pub fn build_module<'a>( pub fn build_module<'a>(
env: &'a Env, env: &'a Env,
ident_ids: &'a mut IdentIds,
target: &Triple, target: &Triple,
procedures: MutMap<(symbol::Symbol, ProcLayout<'a>), Proc<'a>>, procedures: MutMap<(symbol::Symbol, ProcLayout<'a>), Proc<'a>>,
) -> Object { ) -> Object {
@ -39,6 +40,7 @@ pub fn build_module<'a>(
> = Backend::new(env); > = Backend::new(env);
build_object( build_object(
env, env,
ident_ids,
procedures, procedures,
backend, backend,
Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little), Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little),
@ -57,6 +59,7 @@ pub fn build_module<'a>(
> = Backend::new(env); > = Backend::new(env);
build_object( build_object(
env, env,
ident_ids,
procedures, procedures,
backend, backend,
Object::new( Object::new(
@ -79,6 +82,7 @@ pub fn build_module<'a>(
> = Backend::new(env); > = Backend::new(env);
build_object( build_object(
env, env,
ident_ids,
procedures, procedures,
backend, backend,
Object::new(BinaryFormat::Elf, Architecture::Aarch64, Endianness::Little), Object::new(BinaryFormat::Elf, Architecture::Aarch64, Endianness::Little),
@ -97,6 +101,7 @@ pub fn build_module<'a>(
> = Backend::new(env); > = Backend::new(env);
build_object( build_object(
env, env,
ident_ids,
procedures, procedures,
backend, backend,
Object::new( Object::new(
@ -165,6 +170,7 @@ fn generate_wrapper<'a, B: Backend<'a>>(
fn build_object<'a, B: Backend<'a>>( fn build_object<'a, B: Backend<'a>>(
env: &'a Env, env: &'a Env,
ident_ids: &'a mut IdentIds,
procedures: MutMap<(symbol::Symbol, ProcLayout<'a>), Proc<'a>>, procedures: MutMap<(symbol::Symbol, ProcLayout<'a>), Proc<'a>>,
mut backend: B, mut backend: B,
mut output: Object, mut output: Object,
@ -231,6 +237,7 @@ fn build_object<'a, B: Backend<'a>>(
&mut output, &mut output,
&mut backend, &mut backend,
&mut relocations, &mut relocations,
ident_ids,
data_section, data_section,
fn_name, fn_name,
section_id, section_id,
@ -296,6 +303,7 @@ fn build_proc<'a, B: Backend<'a>>(
output: &mut Object, output: &mut Object,
backend: &mut B, backend: &mut B,
relocations: &mut Vec<'a, (SectionId, object::write::Relocation)>, relocations: &mut Vec<'a, (SectionId, object::write::Relocation)>,
ident_ids: &mut IdentIds,
data_section: SectionId, data_section: SectionId,
fn_name: String, fn_name: String,
section_id: SectionId, section_id: SectionId,
@ -303,7 +311,7 @@ fn build_proc<'a, B: Backend<'a>>(
proc: Proc<'a>, proc: Proc<'a>,
) { ) {
let mut local_data_index = 0; let mut local_data_index = 0;
let (proc_data, relocs) = backend.build_proc(proc); let (proc_data, relocs) = backend.build_proc(ident_ids, proc);
let proc_offset = output.add_symbol_data(proc_id, section_id, proc_data, 16); let proc_offset = output.add_symbol_data(proc_id, section_id, proc_data, 16);
for reloc in relocs { for reloc in relocs {
let elfreloc = match reloc { let elfreloc = match reloc {

View file

@ -3,6 +3,7 @@ use roc_build::link::{link, LinkType};
use roc_builtins::bitcode; use roc_builtins::bitcode;
use roc_can::builtins::builtin_defs_map; use roc_can::builtins::builtin_defs_map;
use roc_collections::all::MutMap; use roc_collections::all::MutMap;
use roc_module::symbol::get_module_ident_ids;
use tempfile::tempdir; use tempfile::tempdir;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -64,18 +65,13 @@ pub fn helper(
use roc_load::file::MonomorphizedModule; use roc_load::file::MonomorphizedModule;
let MonomorphizedModule { let MonomorphizedModule {
procedures: top_procedures, module_id,
procedures,
interns, interns,
exposed_to_host, exposed_to_host,
.. ..
} = loaded; } = loaded;
let mut procedures = MutMap::default();
for (key, proc) in top_procedures {
procedures.insert(key, proc);
}
// You can comment and uncomment this block out to get more useful information // You can comment and uncomment this block out to get more useful information
// while you're working on the dev backend! // while you're working on the dev backend!
{ {
@ -179,8 +175,13 @@ pub fn helper(
assert_eq!(0, 1, "Mistakes were made"); assert_eq!(0, 1, "Mistakes were made");
} }
let mut ident_ids = get_module_ident_ids(&interns.all_ident_ids, &module_id)
.unwrap()
.clone();
let env = roc_gen_dev::Env { let env = roc_gen_dev::Env {
arena, arena,
module_id,
interns, interns,
exposed_to_host: exposed_to_host.keys().copied().collect(), exposed_to_host: exposed_to_host.keys().copied().collect(),
lazy_literals, lazy_literals,
@ -188,7 +189,7 @@ pub fn helper(
}; };
let target = target_lexicon::Triple::host(); let target = target_lexicon::Triple::host();
let module_object = roc_gen_dev::build_module(&env, &target, procedures); let module_object = roc_gen_dev::build_module(&env, &mut ident_ids, &target, procedures);
let module_out = module_object let module_out = module_object
.write() .write()