Merge pull request #726 from rtfeldman/llvm-dce

DCE zig builtins
This commit is contained in:
Richard Feldman 2020-11-22 22:13:10 -05:00 committed by GitHub
commit 5eee74f209
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 2 deletions

View file

@ -98,9 +98,12 @@ pub fn build_file(
let compilation_end = compilation_start.elapsed().unwrap();
let size = std::fs::metadata(&app_o_file).unwrap().len();
println!(
"Finished compilation and code gen in {} ms\n",
compilation_end.as_millis()
"Finished compilation and code gen in {} ms\n\nProduced a app.o file of size {:?}\n",
compilation_end.as_millis(),
size,
);
let cwd = app_o_file.parent().unwrap();

View file

@ -2,6 +2,7 @@ use crate::repl::eval;
use bumpalo::Bump;
use inkwell::context::Context;
use roc_build::link::module_to_dylib;
use roc_build::program::FunctionIterator;
use roc_collections::all::{MutMap, MutSet};
use roc_fmt::annotation::Formattable;
use roc_fmt::annotation::{Newlines, Parens};
@ -111,6 +112,15 @@ pub fn gen_and_eval(src: &[u8], target: Triple, opt_level: OptLevel) -> Result<R
let module = arena.alloc(roc_gen::llvm::build::module_from_builtins(&context, "app"));
let builder = context.create_builder();
// mark our zig-defined builtins as internal
use inkwell::module::Linkage;
for function in FunctionIterator::from_module(module) {
let name = function.get_name().to_str().unwrap();
if name.starts_with("roc_builtins") {
function.set_linkage(Linkage::Internal);
}
}
debug_assert_eq!(exposed_to_host.len(), 1);
let (main_fn_symbol, main_fn_var) = exposed_to_host.iter().next().unwrap();
let main_fn_symbol = *main_fn_symbol;

View file

@ -2,6 +2,7 @@ use crate::target;
use bumpalo::Bump;
use inkwell::context::Context;
use inkwell::targets::{CodeModel, FileType, RelocMode};
use inkwell::values::FunctionValue;
use roc_gen::llvm::build::{build_proc, build_proc_header, module_from_builtins, OptLevel, Scope};
use roc_load::file::MonomorphizedModule;
use roc_mono::layout::LayoutIds;
@ -70,6 +71,15 @@ pub fn gen_from_mono_module(
// strip Zig debug stuff
// module.strip_debug_info();
// mark our zig-defined builtins as internal
use inkwell::module::Linkage;
for function in FunctionIterator::from_module(module) {
let name = function.get_name().to_str().unwrap();
if name.starts_with("roc_builtins") {
function.set_linkage(Linkage::Internal);
}
}
let builder = context.create_builder();
let (dibuilder, compile_unit) = roc_gen::llvm::build::Env::new_debug_info(module);
let (mpm, fpm) = roc_gen::llvm::build::construct_optimization_passes(module, opt_level);
@ -159,3 +169,30 @@ pub fn gen_from_mono_module(
.write_to_file(&env.module, FileType::Object, &app_o_file)
.expect("Writing .o file failed");
}
pub struct FunctionIterator<'ctx> {
next: Option<FunctionValue<'ctx>>,
}
impl<'ctx> FunctionIterator<'ctx> {
pub fn from_module(module: &inkwell::module::Module<'ctx>) -> Self {
Self {
next: module.get_first_function(),
}
}
}
impl<'ctx> Iterator for FunctionIterator<'ctx> {
type Item = FunctionValue<'ctx>;
fn next(&mut self) -> Option<Self::Item> {
match self.next {
Some(function) => {
self.next = function.get_next_function();
Some(function)
}
None => None,
}
}
}

View file

@ -386,6 +386,9 @@ pub fn construct_optimization_passes<'a>(
fpm.add_instruction_combining_pass();
fpm.add_tail_call_elimination_pass();
// remove unused global values (e.g. those defined by zig, but unused in user code)
mpm.add_global_dce_pass();
let pmb = PassManagerBuilder::create();
match opt_level {
OptLevel::Normal => {

View file

@ -1,5 +1,6 @@
use libloading::Library;
use roc_build::link::module_to_dylib;
use roc_build::program::FunctionIterator;
use roc_collections::all::{MutMap, MutSet};
fn promote_expr_to_module(src: &str) -> String {
@ -154,6 +155,15 @@ pub fn helper<'a>(
let (dibuilder, compile_unit) = roc_gen::llvm::build::Env::new_debug_info(module);
// mark our zig-defined builtins as internal
use inkwell::module::Linkage;
for function in FunctionIterator::from_module(module) {
let name = function.get_name().to_str().unwrap();
if name.starts_with("roc_builtins") {
function.set_linkage(Linkage::Internal);
}
}
// Compile and add all the Procs before adding main
let env = roc_gen::llvm::build::Env {
arena: &arena,