Implement the relaxed SIMD proposal (#5892)

* Initial support for the Relaxed SIMD proposal

This commit adds initial scaffolding and support for the Relaxed SIMD
proposal for WebAssembly. Codegen support is supported on the x64 and
AArch64 backends on this time.

The purpose of this commit is to get all the boilerplate out of the way
in terms of plumbing through a new feature, adding tests, etc. The tests
are copied from the upstream repository at this time while the
WebAssembly/testsuite repository hasn't been updated.

A summary of changes made in this commit are:

* Lowerings for all relaxed simd opcodes have been added, currently all
  exhibiting deterministic behavior. This means that few lowerings are
  optimal on the x86 backend, but on the AArch64 backend, for example,
  all lowerings should be optimal.

* Support is added to codegen to, eventually, conditionally generate
  different code based on input codegen flags. This is intended to
  enable codegen to more efficient instructions on x86 by default, for
  example, while still allowing embedders to force
  architecture-independent semantics and behavior. One good example of
  this is the `f32x4.relaxed_fmadd` instruction which when deterministic
  forces the `fma` instruction, but otherwise if the backend doesn't
  have support for `fma` then intermediate operations are performed
  instead.

* Lowerings of `iadd_pairwise` for `i16x8` and `i32x4` were added to the
  x86 backend as they're now exercised by the deterministic lowerings of
  relaxed simd instructions.

* Sample codegen tests for added for x86 and aarch64 for some relaxed
  simd instructions.

* Wasmtime embedder support for the relaxed-simd proposal and forcing
  determinism have been added to `Config` and the CLI.

* Support has been added to the `*.wast` runtime execution for the
  `(either ...)` matcher used in the relaxed-simd proposal.

* Tests for relaxed-simd are run both with a default `Engine` as well as
  a "force deterministic" `Engine` to test both configurations.

* All tests from the upstream repository were copied into Wasmtime.
  These tests should be deleted when WebAssembly/testsuite is updated.

* x64: Add x86-specific lowerings for relaxed simd

This commit builds on the prior commit and adds an array of `x86_*`
instructions to Cranelift which have semantics that match their
corresponding x86 equivalents. Translation for relaxed simd is then
additionally updated to conditionally generate different CLIF for
relaxed simd instructions depending on whether the target is x86 or not.
This means that for AArch64 no changes are made but for x86 most relaxed
instructions now lower to some x86-equivalent with slightly different
semantics than the "deterministic" lowering.

* Add libcall support for fma to Wasmtime

This will be required to implement the `f32x4.relaxed_madd` instruction
(and others) when an x86 host doesn't specify the `has_fma` feature.

* Ignore relaxed-simd tests on s390x and riscv64

* Enable relaxed-simd tests on s390x

* Update cranelift/codegen/meta/src/shared/instructions.rs

Co-authored-by: Andrew Brown <andrew.brown@intel.com>

* Add a FIXME from review

* Add notes about deterministic semantics

* Don't default `has_native_fma` to `true`

* Review comments and rebase fixes

---------

Co-authored-by: Andrew Brown <andrew.brown@intel.com>
This commit is contained in:
Alex Crichton
2023-03-07 09:52:41 -06:00
committed by GitHub
parent e2dcb19099
commit 8bb183f16e
34 changed files with 1727 additions and 37 deletions

View File

@@ -682,6 +682,56 @@ impl Config {
self
}
/// Configures whether the WebAssembly Relaxed SIMD proposal will be
/// enabled for compilation.
///
/// The [WebAssembly Relaxed SIMD proposal][proposal] is not, at the time of
/// this writing, at stage 4. The relaxed SIMD proposal adds new
/// instructions to WebAssembly which, for some specific inputs, are allowed
/// to produce different results on different hosts. More-or-less this
/// proposal enables exposing platform-specific semantics of SIMD
/// instructions in a controlled fashion to a WebAssembly program. From an
/// embedder's perspective this means that WebAssembly programs may execute
/// differently depending on whether the host is x86_64 or AArch64, for
/// example.
///
/// By default Wasmtime lowers relaxed SIMD instructions to the fastest
/// lowering for the platform it's running on. This means that, by default,
/// some relaxed SIMD instructions may have different results for the same
/// inputs across x86_64 and AArch64. This behavior can be disabled through
/// the [`Config::relaxed_simd_deterministic`] option which will force
/// deterministic behavior across all platforms, as classified by the
/// specification, at the cost of performance.
///
/// This is `false` by default.
///
/// [proposal]: https://github.com/webassembly/relaxed-simd
pub fn wasm_relaxed_simd(&mut self, enable: bool) -> &mut Self {
self.features.relaxed_simd = enable;
self
}
/// This option can be used to control the behavior of the [relaxed SIMD
/// proposal's][proposal] instructions.
///
/// The relaxed SIMD proposal introduces instructions that are allowed to
/// have different behavior on different architectures, primarily to afford
/// an efficient implementation on all architectures. This means, however,
/// that the same module may execute differently on one host than another,
/// which typically is not otherwise the case. This option is provided to
/// force Wasmtime to generate deterministic code for all relaxed simd
/// instructions, at the cost of performance, for all architectures. When
/// this option is enabled then the deterministic behavior of all
/// instructions in the relaxed SIMD proposal is selected.
///
/// This is `false` by default.
///
/// [proposal]: https://github.com/webassembly/relaxed-simd
pub fn relaxed_simd_deterministic(&mut self, enable: bool) -> &mut Self {
self.tunables.relaxed_simd_deterministic = enable;
self
}
/// Configures whether the [WebAssembly bulk memory operations
/// proposal][proposal] will be enabled for compilation.
///
@@ -1560,6 +1610,10 @@ impl Config {
}
}
if self.features.relaxed_simd && !self.features.simd {
bail!("cannot disable the simd proposal but enable the relaxed simd proposal");
}
// Apply compiler settings and flags
for (k, v) in self.compiler_config.settings.iter() {
compiler.set(k, v)?;
@@ -1608,6 +1662,7 @@ impl fmt::Debug for Config {
.field("wasm_reference_types", &self.features.reference_types)
.field("wasm_bulk_memory", &self.features.bulk_memory)
.field("wasm_simd", &self.features.simd)
.field("wasm_relaxed_simd", &self.features.relaxed_simd)
.field("wasm_multi_value", &self.features.multi_value)
.field(
"static_memory_maximum_size",

View File

@@ -309,6 +309,7 @@ impl Metadata {
epoch_interruption,
static_memory_bound_is_maximum,
guard_before_linear_memory,
relaxed_simd_deterministic,
// This doesn't affect compilation, it's just a runtime setting.
dynamic_memory_growth_reserve: _,
@@ -364,6 +365,11 @@ impl Metadata {
other.guard_before_linear_memory,
"guard before linear memory",
)?;
Self::check_bool(
relaxed_simd_deterministic,
other.relaxed_simd_deterministic,
"relaxed simd deterministic semantics",
)?;
Ok(())
}