Skip to content

Commit

Permalink
Enable egraph-based optimization by default. (#5587)
Browse files Browse the repository at this point in the history
This PR follows up on #5382 and #5391, which rebuilt the egraph-based optimization framework to be more performant, by enabling it by default.

Based on performance results in #5382 (my measurements on SpiderMonkey and bjorn3's independent confirmation with cg_clif), it seems that this is reasonable to enable. Now that we have been fuzzing compiler configurations with egraph opts (#5388) for 6 weeks, having fixed a few fuzzbugs that came up (#5409, #5420, #5438) and subsequently received no further reports from OSS-Fuzz, I believe it is stable enough to rely on.

This PR enables `use_egraphs`, and also normalizes its meaning: previously it forced optimization (it basically meant "turn on the egraph optimization machinery"), now it runs egraph opts if the opt level indicates (it means "use egraphs to optimize if we are going to optimize"). The conditionals in the top-level pass driver are a little subtle, but will get simpler once we can remove the non-egraph path (which we plan to do eventually!).

Fixes #5181.
  • Loading branch information
cfallin authored Jan 19, 2023
1 parent 704f5a5 commit 1faff8c
Show file tree
Hide file tree
Showing 20 changed files with 44 additions and 34 deletions.
8 changes: 5 additions & 3 deletions cranelift/codegen/meta/src/shared/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ pub(crate) fn define() -> SettingGroup {
"Enable egraph-based optimization.",
r#"
This enables an optimization phase that converts CLIF to an egraph (equivalence graph)
representation, performs various rewrites, and then converts it back. This can result in
better optimization, but is currently considered experimental.
representation, performs various rewrites, and then converts it back. This should result in
better optimization, but the traditional optimization pass structure is also still
available by setting this to `false`. The `false` setting will eventually be
deprecated and removed.
"#,
false,
true,
);

settings.add_bool(
Expand Down
16 changes: 9 additions & 7 deletions cranelift/codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,20 @@ impl Context {
self.compute_domtree();
self.eliminate_unreachable_code(isa)?;

if isa.flags().use_egraphs() || opt_level != OptLevel::None {
if opt_level != OptLevel::None {
self.dce(isa)?;
}

self.remove_constant_phis(isa)?;

if isa.flags().use_egraphs() {
self.egraph_pass()?;
} else if opt_level != OptLevel::None && isa.flags().enable_alias_analysis() {
for _ in 0..2 {
self.replace_redundant_loads()?;
self.simple_gvn(isa)?;
if opt_level != OptLevel::None {
if isa.flags().use_egraphs() {
self.egraph_pass()?;
} else if isa.flags().enable_alias_analysis() {
for _ in 0..2 {
self.replace_redundant_loads()?;
self.simple_gvn(isa)?;
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions cranelift/codegen/src/machinst/isle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use crate::machinst::{
ABIArg, ABIArgSlot, InputSourceInst, Lower, LowerBackend, RealReg, Reg, RelocDistance, Sig,
VCodeInst, Writable,
};
pub use crate::settings::TlsModel;
pub use crate::settings::{OptLevel, TlsModel};

pub type Unit = ();
pub type ValueSlice = (ValueList, usize);
Expand Down Expand Up @@ -131,7 +131,9 @@ macro_rules! isle_lower_prelude_methods {
// use. This lowers register pressure. (Only do this if we are
// not using egraph-based compilation; the egraph framework
// more efficiently rematerializes constants where needed.)
if !self.backend.flags().use_egraphs() {
if !(self.backend.flags().use_egraphs()
&& self.backend.flags().opt_level() != OptLevel::None)
{
let inputs = self.lower_ctx.get_value_as_source_or_const(val);
if inputs.constant.is_some() {
let insn = match inputs.inst {
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ probestack_strategy = "outline"
regalloc_checker = false
regalloc_verbose_logs = false
enable_alias_analysis = true
use_egraphs = false
use_egraphs = true
enable_verifier = true
is_pic = false
use_colocated_libcalls = false
Expand Down
2 changes: 1 addition & 1 deletion cranelift/filetests/filetests/egraph/algebraic.clif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test optimize
set opt_level=none
set opt_level=speed
set use_egraphs=true
target x86_64

Expand Down
2 changes: 1 addition & 1 deletion cranelift/filetests/filetests/egraph/alias_analysis.clif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test optimize
set opt_level=none
set opt_level=speed
set use_egraphs=true
target x86_64

Expand Down
2 changes: 1 addition & 1 deletion cranelift/filetests/filetests/egraph/basic-gvn.clif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test optimize
set opt_level=none
set opt_level=speed
set use_egraphs=true
target x86_64

Expand Down
2 changes: 1 addition & 1 deletion cranelift/filetests/filetests/egraph/cprop.clif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test optimize
set opt_level=none
set opt_level=speed
set use_egraphs=true
target x86_64

Expand Down
2 changes: 1 addition & 1 deletion cranelift/filetests/filetests/egraph/i128-opts.clif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test optimize
set opt_level=speed_and_size
set opt_level=speed
set use_egraphs=true
target x86_64

Expand Down
2 changes: 1 addition & 1 deletion cranelift/filetests/filetests/egraph/isplit.clif
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test interpret
test run
set opt_level=speed_and_size
set opt_level=speed
set use_egraphs=true
set enable_llvm_abi_extensions=true
target x86_64
Expand Down
2 changes: 1 addition & 1 deletion cranelift/filetests/filetests/egraph/issue-5405.clif
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test interpret
test run
set opt_level=speed_and_size
set opt_level=speed
set use_egraphs=true
target aarch64

Expand Down
1 change: 1 addition & 0 deletions cranelift/filetests/filetests/egraph/issue-5437.clif
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test compile
set opt_level=speed
set use_egraphs=true
target x86_64
target aarch64
Expand Down
2 changes: 1 addition & 1 deletion cranelift/filetests/filetests/egraph/licm.clif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test optimize
set opt_level=none
set opt_level=speed
set use_egraphs=true
target x86_64

Expand Down
2 changes: 1 addition & 1 deletion cranelift/filetests/filetests/egraph/misc.clif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test optimize
set opt_level=none
set opt_level=speed
set use_egraphs=true
target x86_64

Expand Down
1 change: 1 addition & 0 deletions cranelift/filetests/filetests/egraph/multivalue.clif
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test compile precise-output
set opt_level=speed
set use_egraphs=true
target x86_64

Expand Down
1 change: 1 addition & 0 deletions cranelift/filetests/filetests/egraph/not_a_load.clif
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test compile precise-output
set opt_level=speed
set use_egraphs=true
target x86_64

Expand Down
2 changes: 1 addition & 1 deletion cranelift/filetests/filetests/egraph/remat.clif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test optimize
set opt_level=none
set opt_level=speed
set use_egraphs=true
target x86_64

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
;;!
;;! settings = [
;;! "enable_heap_access_spectre_mitigation=true",
;;! "opt_level=speed_and_size"
;;! "opt_level=speed_and_size",
;;! "use_egraphs=false"
;;! ]
;;!
;;! [globals.vmctx]
Expand Down Expand Up @@ -112,4 +113,4 @@
;;
;; block1:
;; @006e return v14, v14
;; }
;; }
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
;;!
;;! settings = [
;;! "enable_heap_access_spectre_mitigation=true",
;;! "opt_level=speed_and_size"
;;! "opt_level=speed_and_size",
;;! "use_egraphs=false"
;;! ]
;;!
;;! [globals.vmctx]
Expand Down
15 changes: 7 additions & 8 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,14 +832,13 @@ impl Config {
/// Configures the Cranelift code generator to use its
/// "egraph"-based mid-end optimizer.
///
/// This optimizer is intended to replace the compiler's more
/// traditional pipeline of optimization passes with a unified
/// code-rewriting system. It is not yet on by default, but it is
/// intended to become the default in a future version. It may
/// result in faster code, at the cost of slightly more
/// compilation-time work.
///
/// The default value for this is `false`.
/// This optimizer has replaced the compiler's more traditional
/// pipeline of optimization passes with a unified code-rewriting
/// system. It is on by default, but the traditional optimization
/// pass structure is still available for now (it is deprecrated and
/// will be removed in a future version).
///
/// The default value for this is `true`.
#[cfg(compiler)]
#[cfg_attr(nightlydoc, doc(cfg(feature = "cranelift")))] // see build.rs
pub fn cranelift_use_egraphs(&mut self, enable: bool) -> &mut Self {
Expand Down

0 comments on commit 1faff8c

Please sign in to comment.