From 9850cbe313d98bcce64082623e83d4a132f0aba0 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 17 Oct 2021 12:56:11 +0800 Subject: [PATCH] nac3core/codegen: optimize for every function This speeds up compilation and reduces memory usage. --- nac3core/src/codegen/mod.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 04039028..34125efc 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -12,9 +12,10 @@ use inkwell::{ builder::Builder, context::Context, module::Module, + passes::{PassManager, PassManagerBuilder}, types::{BasicType, BasicTypeEnum}, - values::PointerValue, - AddressSpace, + values::{FunctionValue, PointerValue}, + AddressSpace, OptimizationLevel, }; use itertools::Itertools; use parking_lot::{Condvar, Mutex}; @@ -168,17 +169,27 @@ impl WorkerRegistry { let mut builder = context.create_builder(); let mut module = context.create_module(generator.get_name()); + let pass_builder = PassManagerBuilder::create(); + pass_builder.set_optimization_level(OptimizationLevel::Default); + let passes = PassManager::create(&module); + pass_builder.populate_function_pass_manager(&passes); + while let Some(task) = self.receiver.recv().unwrap() { let result = gen_func(&context, generator, self, builder, module, task, top_level_ctx.clone()); builder = result.0; module = result.1; + passes.run_on(&result.2); *self.task_count.lock() -= 1; self.wait_condvar.notify_all(); } - // do whatever... - module.verify().unwrap(); + let result = module.verify(); + if let Err(err) = result { + println!("{}", module.print_to_string().to_str().unwrap()); + println!("{}", err); + panic!() + } f.run(&module); let mut lock = self.task_count.lock(); *lock += 1; @@ -254,8 +265,7 @@ pub fn gen_func<'ctx, G: CodeGenerator + ?Sized>( module: Module<'ctx>, task: CodeGenTask, top_level_ctx: Arc, -) -> (Builder<'ctx>, Module<'ctx>) { - // unwrap_or(0) is for unit tests without using rayon +) -> (Builder<'ctx>, Module<'ctx>, FunctionValue<'ctx>) { let (mut unifier, primitives) = { let (unifier, primitives) = task.unifier; (Unifier::from_shared_unifier(&unifier), primitives) @@ -367,5 +377,5 @@ pub fn gen_func<'ctx, G: CodeGenerator + ?Sized>( let CodeGenContext { builder, module, .. } = code_gen_context; - (builder, module) + (builder, module, fn_val) }