Files
wasmtime/cranelift/filetests/src/test_wasm/config.rs
Alex Crichton 8bb183f16e 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>
2023-03-07 15:52:41 +00:00

227 lines
6.3 KiB
Rust

//! Configuration of `.wat` tests.
//!
//! The config is the leading `;;!` comments in the WAT. It is in TOML.
use anyhow::{bail, ensure, Result};
use cranelift_codegen::ir;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TestConfig {
#[serde(default)]
pub debug_info: bool,
#[serde(default)]
pub target: String,
#[serde(default)]
pub compile: bool,
#[serde(default)]
pub optimize: bool,
#[serde(default)]
pub settings: Vec<String>,
#[serde(default)]
pub globals: BTreeMap<String, TestGlobal>,
#[serde(default)]
pub heaps: Vec<TestHeap>,
#[serde(default)]
pub relaxed_simd_deterministic: bool,
}
impl TestConfig {
pub fn validate(&self) -> Result<()> {
if self.compile || self.optimize {
ensure!(
!(self.compile && self.optimize),
"The `compile` and `optimize` options are mutually exclusive."
);
}
for global in self.globals.values() {
ensure!(
global.vmctx || global.load.is_some(),
"global must be either `vmctx` or a `load`"
);
ensure!(
!(global.vmctx && global.load.is_some()),
"global cannot be both a `vmctx` and a `load`"
);
if let Some(load) = &global.load {
ensure!(
self.globals.contains_key(&load.base),
"global's load base must be another global"
);
}
}
for heap in &self.heaps {
ensure!(
self.globals.contains_key(&heap.base),
"heap base must be a declared global"
);
match heap.style.kind.as_str() {
"static" => match &heap.style.bound {
toml::value::Value::Integer(x) => {
ensure!(*x >= 0, "static heap bound cannot be negative")
}
_ => bail!("static heap bounds must be integers"),
},
"dynamic" => match &heap.style.bound {
toml::value::Value::String(g) => {
ensure!(
self.globals.contains_key(g),
"dynamic heap bound must be a declared global"
)
}
_ => bail!("dynamic heap bounds must be strings"),
},
other => {
bail!(
"heap style must be 'static' or 'dynamic', found '{}'",
other
)
}
}
}
Ok(())
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TestGlobal {
#[serde(rename = "type")]
pub type_: String,
#[serde(default)]
pub vmctx: bool,
#[serde(default)]
pub load: Option<TestGlobalLoad>,
}
impl TestGlobal {
pub fn to_ir(
&self,
name_to_ir_global: &BTreeMap<String, ir::GlobalValue>,
) -> ir::GlobalValueData {
if self.vmctx {
ir::GlobalValueData::VMContext
} else if let Some(load) = &self.load {
ir::GlobalValueData::Load {
base: name_to_ir_global[&load.base],
offset: i32::try_from(load.offset).unwrap().into(),
global_type: match self.type_.as_str() {
"i32" => ir::types::I32,
"i64" => ir::types::I64,
other => panic!("test globals cannot be of type '{other}'"),
},
readonly: load.readonly,
}
} else {
unreachable!()
}
}
pub fn dependencies<'a>(&'a self) -> impl Iterator<Item = &'a str> + 'a {
let mut deps = None;
if let Some(load) = &self.load {
deps = Some(load.base.as_str());
}
deps.into_iter()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TestGlobalLoad {
pub base: String,
#[serde(default)]
pub offset: u32,
#[serde(default)]
pub readonly: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TestHeap {
pub base: String,
#[serde(default)]
pub min_size: u64,
#[serde(default)]
pub offset_guard_size: u64,
pub style: TestHeapStyle,
pub index_type: String,
}
impl TestHeap {
pub fn to_ir(
&self,
name_to_ir_global: &BTreeMap<String, ir::GlobalValue>,
) -> cranelift_wasm::HeapData {
cranelift_wasm::HeapData {
base: name_to_ir_global[&self.base],
min_size: self.min_size.into(),
offset_guard_size: self.offset_guard_size.into(),
style: self.style.to_ir(name_to_ir_global),
index_type: match self.index_type.as_str() {
"i32" => ir::types::I32,
"i64" => ir::types::I64,
other => panic!("heap indices may only be i32 or i64, found '{other}'"),
},
}
}
pub fn dependencies<'a>(&'a self) -> impl Iterator<Item = &'a str> + 'a {
let mut deps = vec![self.base.as_str()];
if self.style.kind == "dynamic" {
deps.push(match &self.style.bound {
toml::Value::String(g) => g.as_str(),
_ => unreachable!(),
});
}
deps.into_iter()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TestHeapStyle {
pub kind: String,
pub bound: toml::value::Value,
}
impl TestHeapStyle {
pub fn to_ir(
&self,
name_to_ir_global: &BTreeMap<String, ir::GlobalValue>,
) -> cranelift_wasm::HeapStyle {
match self.kind.as_str() {
"static" => cranelift_wasm::HeapStyle::Static {
bound: match &self.bound {
toml::Value::Integer(x) => u64::try_from(*x).unwrap().into(),
_ => unreachable!(),
},
},
"dynamic" => cranelift_wasm::HeapStyle::Dynamic {
bound_gv: match &self.bound {
toml::Value::String(g) => name_to_ir_global[g],
_ => unreachable!(),
},
},
_ => unreachable!(),
}
}
}