From 65ef26b9898deccfec945ff5a4b799c0854986c1 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Fri, 17 Apr 2020 15:25:49 +0200 Subject: [PATCH] Add a setting to choose a register allocator algorithm to use with MachBackend; --- cranelift/codegen/meta/src/shared/settings.rs | 30 +++++++++++++ cranelift/codegen/src/machinst/compile.rs | 9 +++- cranelift/codegen/src/settings.rs | 44 ++++++++++--------- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/cranelift/codegen/meta/src/shared/settings.rs b/cranelift/codegen/meta/src/shared/settings.rs index fd6063e852..becfbd2451 100644 --- a/cranelift/codegen/meta/src/shared/settings.rs +++ b/cranelift/codegen/meta/src/shared/settings.rs @@ -3,6 +3,36 @@ use crate::cdsl::settings::{SettingGroup, SettingGroupBuilder}; pub(crate) fn define() -> SettingGroup { let mut settings = SettingGroupBuilder::new("shared"); + settings.add_enum( + "regalloc", + r#"Register allocator to use with the MachInst backend. + + This selects the register allocator as an option among those offered by the `regalloc.rs` + crate. Please report register allocation bugs to the maintainers of this crate whenever + possible. + + Note: this only applies to target that use the MachInst backend. As of 2020-04-17, this + means the x86_64 backend doesn't use this yet. + + Possible values: + + - `backtracking` is a greedy, backtracking register allocator as implemented in + Spidermonkey's optimizing tier IonMonkey. It may take more time to allocate registers, but + it should generate better code in general, resulting in better throughput of generated + code. + - `backtracking_checked` is the backtracking allocator with additional self checks that may + take some time to run, and thus these checks are disabled by default. + - `experimental_linear_scan` is an experimental linear scan allocator. It may take less + time to allocate registers, but generated code's quality may be inferior. As of + 2020-04-17, it is still experimental and it should not be used in production settings. + "#, + vec![ + "backtracking", + "backtracking_checked", + "experimental_linear_scan", + ], + ); + settings.add_enum( "opt_level", r#" diff --git a/cranelift/codegen/src/machinst/compile.rs b/cranelift/codegen/src/machinst/compile.rs index 2fdaa7292c..42374aea61 100644 --- a/cranelift/codegen/src/machinst/compile.rs +++ b/cranelift/codegen/src/machinst/compile.rs @@ -2,6 +2,7 @@ use crate::ir::Function; use crate::machinst::*; +use crate::settings; use crate::timing; use log::debug; @@ -25,8 +26,12 @@ where debug!("vcode from lowering: \n{}", vcode.show_rru(Some(universe))); // Perform register allocation. - // TODO: select register allocation algorithm from flags. - let algorithm = RegAllocAlgorithm::Backtracking; + let algorithm = match vcode.flags().regalloc() { + settings::Regalloc::Backtracking => RegAllocAlgorithm::Backtracking, + settings::Regalloc::BacktrackingChecked => RegAllocAlgorithm::BacktrackingChecked, + settings::Regalloc::ExperimentalLinearScan => RegAllocAlgorithm::LinearScan, + }; + let result = { let _tt = timing::regalloc(); allocate_registers( diff --git a/cranelift/codegen/src/settings.rs b/cranelift/codegen/src/settings.rs index 57b9c18f89..bd6d7bc360 100644 --- a/cranelift/codegen/src/settings.rs +++ b/cranelift/codegen/src/settings.rs @@ -377,27 +377,29 @@ mod tests { let f = Flags::new(b); assert_eq!( f.to_string(), - "[shared]\n\ - opt_level = \"none\"\n\ - tls_model = \"none\"\n\ - libcall_call_conv = \"isa_default\"\n\ - baldrdash_prologue_words = 0\n\ - probestack_size_log2 = 12\n\ - enable_verifier = true\n\ - is_pic = false\n\ - use_colocated_libcalls = false\n\ - avoid_div_traps = false\n\ - enable_float = true\n\ - enable_nan_canonicalization = false\n\ - enable_pinned_reg = false\n\ - use_pinned_reg_as_heap_base = false\n\ - enable_simd = false\n\ - enable_atomics = true\n\ - enable_safepoints = false\n\ - emit_all_ones_funcaddrs = false\n\ - enable_probestack = true\n\ - probestack_func_adjusts_sp = false\n\ - enable_jump_tables = true\n" + r#"[shared] +regalloc = "backtracking" +opt_level = "none" +tls_model = "none" +libcall_call_conv = "isa_default" +baldrdash_prologue_words = 0 +probestack_size_log2 = 12 +enable_verifier = true +is_pic = false +use_colocated_libcalls = false +avoid_div_traps = false +enable_float = true +enable_nan_canonicalization = false +enable_pinned_reg = false +use_pinned_reg_as_heap_base = false +enable_simd = false +enable_atomics = true +enable_safepoints = false +emit_all_ones_funcaddrs = false +enable_probestack = true +probestack_func_adjusts_sp = false +enable_jump_tables = true +"# ); assert_eq!(f.opt_level(), super::OptLevel::None); assert_eq!(f.enable_simd(), false);