From 25bc12ec82c842b86a67b330dbaccb86ae49978b Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Tue, 18 Oct 2022 10:22:37 -0700 Subject: [PATCH] Add egraphs option to Wasmtime config, and add it to fuzzing config generation. (#5067) * Add egraphs option to Wasmtime config, and add it to fuzzing config generation. This PR adds a wrapper method for Cranelift's `use_egraphs` setting to Wasmtime's `Config`, named `cranelift_use_egraphs` analogously to its existing `cranelift_opt_level`. Eventually this should become a no-op as egraph-based optimization becomes the default, but until then it makes sense to expose this as another kind of optimization option. This PR then adds the option to the `Arbitrary`-based config generation for fuzzing, so compilation with egraphs will be fuzzed (on its own and against other configurations and oracles). * Don't use `NamedTempFile` on Windows It looks like this prevents mmap-ing since the named temporary file holds a `File` open which conflicts with the rights we're trying to open the file for mmap-ing. Instead use a temporary directory to try to fix this issue. Co-authored-by: Alex Crichton --- crates/fuzzing/src/generators/config.rs | 9 ++++++--- crates/wasmtime/src/config.rs | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/fuzzing/src/generators/config.rs b/crates/fuzzing/src/generators/config.rs index 4b94f278b3..1029cd4b96 100644 --- a/crates/fuzzing/src/generators/config.rs +++ b/crates/fuzzing/src/generators/config.rs @@ -163,6 +163,7 @@ impl Config { .native_unwind_info(self.wasmtime.native_unwind_info) .cranelift_nan_canonicalization(self.wasmtime.canonicalize_nans) .cranelift_opt_level(self.wasmtime.opt_level.to_wasmtime()) + .cranelift_use_egraphs(self.wasmtime.use_egraphs) .consume_fuel(self.wasmtime.consume_fuel) .epoch_interruption(self.wasmtime.epoch_interruption) .memory_init_cow(self.wasmtime.memory_init_cow) @@ -303,9 +304,10 @@ impl Config { // Don't propagate these errors to prevent them from accidentally being // interpreted as invalid wasm, these should never fail on a // well-behaved host system. - let file = tempfile::NamedTempFile::new().unwrap(); - std::fs::write(file.path(), module.serialize().unwrap()).unwrap(); - unsafe { Ok(Module::deserialize_file(engine, file.path()).unwrap()) } + let dir = tempfile::TempDir::new().unwrap(); + let file = dir.path().join("module.wasm"); + std::fs::write(&file, module.serialize().unwrap()).unwrap(); + unsafe { Ok(Module::deserialize_file(engine, &file).unwrap()) } } } @@ -369,6 +371,7 @@ impl<'a> Arbitrary<'a> for Config { #[derive(Arbitrary, Clone, Debug, Eq, Hash, PartialEq)] pub struct WasmtimeConfig { opt_level: OptLevel, + use_egraphs: bool, debug_info: bool, canonicalize_nans: bool, interruptable: bool, diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rs index 54073aa834..e5f3fde341 100644 --- a/crates/wasmtime/src/config.rs +++ b/crates/wasmtime/src/config.rs @@ -853,6 +853,27 @@ impl Config { self } + /// 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`. + #[cfg(compiler)] + #[cfg_attr(nightlydoc, doc(cfg(feature = "cranelift")))] // see build.rs + pub fn cranelift_use_egraphs(&mut self, enable: bool) -> &mut Self { + let val = if enable { "true" } else { "false" }; + self.compiler_config + .settings + .insert("use_egraphs".to_string(), val.to_string()); + self + } + /// Configures whether Cranelift should perform a NaN-canonicalization pass. /// /// When Cranelift is used as a code generation backend this will configure