From ea713023e8460c3630b07815ca942f028e8e96bf Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Thu, 16 Apr 2020 20:26:08 -0400 Subject: [PATCH] Add explicit OptLevel to add_passes --- compiler/gen/src/llvm/build.rs | 49 ++++++++++++++++++------------ compiler/gen/tests/helpers/eval.rs | 23 +++++++++++--- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/compiler/gen/src/llvm/build.rs b/compiler/gen/src/llvm/build.rs index 6060065c74..6a57b21537 100644 --- a/compiler/gen/src/llvm/build.rs +++ b/compiler/gen/src/llvm/build.rs @@ -31,6 +31,11 @@ const PRINT_FN_VERIFICATION_OUTPUT: bool = false; // TODO: experiment with different internal calling conventions, e.g. "fast" const DEFAULT_CALLING_CONVENTION: u32 = 0; +pub enum OptLevel { + Normal, + Optimize, +} + type Scope<'a, 'ctx> = ImMap, PointerValue<'ctx>)>; pub struct Env<'a, 'ctx, 'env> { @@ -56,7 +61,7 @@ pub fn module_from_builtins<'ctx>(ctx: &'ctx Context, module_name: &str) -> Modu .unwrap_or_else(|err| panic!("Unable to import builtins bitcode. LLVM error: {:?}", err)) } -pub fn add_passes(fpm: &PassManager>) { +pub fn add_passes(fpm: &PassManager>, opt_level: OptLevel) { // tail-call elimination is always on fpm.add_instruction_combining_pass(); fpm.add_tail_call_elimination_pass(); @@ -64,28 +69,32 @@ pub fn add_passes(fpm: &PassManager>) { let pmb = PassManagerBuilder::create(); // Enable more optimizations when running cargo test --release - if cfg!(debug_assertions) { - pmb.set_optimization_level(OptimizationLevel::None); - } else { - // Default is O2, Aggressive is O3 - // - // See https://llvm.org/doxygen/CodeGen_8h_source.html - pmb.set_optimization_level(OptimizationLevel::Aggressive); + match opt_level { + OptLevel::Normal => { + pmb.set_optimization_level(OptimizationLevel::None); + } + OptLevel::Optimize => { + // Default is O2, Aggressive is O3 + // + // See https://llvm.org/doxygen/CodeGen_8h_source.html + pmb.set_optimization_level(OptimizationLevel::Aggressive); - // TODO figure out how enabling these individually differs from - // the broad "aggressive optimizations" setting. + // TODO figure out how enabling these individually differs from + // the broad "aggressive optimizations" setting. - // fpm.add_reassociate_pass(); - // fpm.add_basic_alias_analysis_pass(); - // fpm.add_promote_memory_to_register_pass(); - // fpm.add_cfg_simplification_pass(); - // fpm.add_gvn_pass(); - // TODO figure out why enabling any of these (even alone) causes LLVM to segfault - // fpm.add_strip_dead_prototypes_pass(); - // fpm.add_dead_arg_elimination_pass(); - // fpm.add_function_inlining_pass(); - // pmb.set_inliner_with_threshold(4); + // fpm.add_reassociate_pass(); + // fpm.add_basic_alias_analysis_pass(); + // fpm.add_promote_memory_to_register_pass(); + // fpm.add_cfg_simplification_pass(); + // fpm.add_gvn_pass(); + // TODO figure out why enabling any of these (even alone) causes LLVM to segfault + // fpm.add_strip_dead_prototypes_pass(); + // fpm.add_dead_arg_elimination_pass(); + // fpm.add_function_inlining_pass(); + // pmb.set_inliner_with_threshold(4); + } } + pmb.populate_function_pass_manager(&fpm); } diff --git a/compiler/gen/tests/helpers/eval.rs b/compiler/gen/tests/helpers/eval.rs index 972e8c70bf..ae08bac5d9 100644 --- a/compiler/gen/tests/helpers/eval.rs +++ b/compiler/gen/tests/helpers/eval.rs @@ -12,9 +12,14 @@ macro_rules! assert_llvm_evals_to { let context = Context::create(); let module = roc_gen::llvm::build::module_from_builtins(&context, "app"); let builder = context.create_builder(); - let fpm = inkwell::passes::PassManager::create(&module); + let opt_level = if(cfg!(debug_assertions)) { + roc_gen::llvm::build::OptLevel::Normal + } else { + roc_gen::llvm::build::OptLevel::Optimize + }; + let fpm = PassManager::create(&module); - roc_gen::llvm::build::add_passes(&fpm); + roc_gen::llvm::build::add_passes(&fpm, opt_level); fpm.initialize(); @@ -147,9 +152,14 @@ macro_rules! assert_opt_evals_to { let context = Context::create(); let module = roc_gen::llvm::build::module_from_builtins(&context, "app"); let builder = context.create_builder(); + let opt_level = if(cfg!(debug_assertions)) { + roc_gen::llvm::build::OptLevel::Normal + } else { + roc_gen::llvm::build::OptLevel::Optimize + }; let fpm = PassManager::create(&module); - roc_gen::llvm::build::add_passes(&fpm); + roc_gen::llvm::build::add_passes(&fpm, opt_level); fpm.initialize(); @@ -278,9 +288,14 @@ macro_rules! emit_expr { let context = Context::create(); let module = context.create_module("app"); let builder = context.create_builder(); + let opt_level = if(cfg!(debug_assertions)) { + roc_gen::llvm::build::OptLevel::Normal + } else { + roc_gen::llvm::build::OptLevel::Optimize + }; let fpm = PassManager::create(&module); - roc_gen::llvm::build::add_passes(&fpm); + roc_gen::llvm::build::add_passes(&fpm, opt_level); fpm.initialize();