* 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>
200 lines
6.3 KiB
Rust
200 lines
6.3 KiB
Rust
//! Build program to generate a program which runs all the testsuites.
|
|
//!
|
|
//! By generating a separate `#[test]` test for each file, we allow cargo test
|
|
//! to automatically run the files in parallel.
|
|
|
|
use anyhow::Context;
|
|
use std::env;
|
|
use std::fmt::Write;
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
|
|
let out_dir = PathBuf::from(
|
|
env::var_os("OUT_DIR").expect("The OUT_DIR environment variable must be set"),
|
|
);
|
|
let mut out = String::new();
|
|
|
|
for strategy in &["Cranelift"] {
|
|
writeln!(out, "#[cfg(test)]")?;
|
|
writeln!(out, "#[allow(non_snake_case)]")?;
|
|
writeln!(out, "mod {} {{", strategy)?;
|
|
|
|
with_test_module(&mut out, "misc", |out| {
|
|
test_directory(out, "tests/misc_testsuite", strategy)?;
|
|
test_directory_module(out, "tests/misc_testsuite/multi-memory", strategy)?;
|
|
test_directory_module(out, "tests/misc_testsuite/simd", strategy)?;
|
|
test_directory_module(out, "tests/misc_testsuite/threads", strategy)?;
|
|
test_directory_module(out, "tests/misc_testsuite/memory64", strategy)?;
|
|
test_directory_module(out, "tests/misc_testsuite/component-model", strategy)?;
|
|
|
|
// NB: these are copied from upstream and updated to wasmtime's
|
|
// current version of `wast`. This local copy should go away when
|
|
// all of Wasmtime's tooling is updated and the upstream
|
|
// `testsuite` module is additionally updated.
|
|
test_directory_module(out, "tests/misc_testsuite/relaxed-simd", strategy)?;
|
|
Ok(())
|
|
})?;
|
|
|
|
with_test_module(&mut out, "spec", |out| {
|
|
let spec_tests = test_directory(out, "tests/spec_testsuite", strategy)?;
|
|
// Skip running spec_testsuite tests if the submodule isn't checked
|
|
// out.
|
|
if spec_tests > 0 {
|
|
test_directory_module(out, "tests/spec_testsuite/proposals/memory64", strategy)?;
|
|
test_directory_module(
|
|
out,
|
|
"tests/spec_testsuite/proposals/multi-memory",
|
|
strategy,
|
|
)?;
|
|
test_directory_module(out, "tests/spec_testsuite/proposals/threads", strategy)?;
|
|
} else {
|
|
println!(
|
|
"cargo:warning=The spec testsuite is disabled. To enable, run `git submodule \
|
|
update --remote`."
|
|
);
|
|
}
|
|
Ok(())
|
|
})?;
|
|
|
|
writeln!(out, "}}")?;
|
|
}
|
|
|
|
// Write out our auto-generated tests and opportunistically format them with
|
|
// `rustfmt` if it's installed.
|
|
let output = out_dir.join("wast_testsuite_tests.rs");
|
|
fs::write(&output, out)?;
|
|
drop(Command::new("rustfmt").arg(&output).status());
|
|
Ok(())
|
|
}
|
|
|
|
fn test_directory_module(
|
|
out: &mut String,
|
|
path: impl AsRef<Path>,
|
|
strategy: &str,
|
|
) -> anyhow::Result<usize> {
|
|
let path = path.as_ref();
|
|
let testsuite = &extract_name(path);
|
|
with_test_module(out, testsuite, |out| test_directory(out, path, strategy))
|
|
}
|
|
|
|
fn test_directory(
|
|
out: &mut String,
|
|
path: impl AsRef<Path>,
|
|
strategy: &str,
|
|
) -> anyhow::Result<usize> {
|
|
let path = path.as_ref();
|
|
let mut dir_entries: Vec<_> = path
|
|
.read_dir()
|
|
.context(format!("failed to read {:?}", path))?
|
|
.map(|r| r.expect("reading testsuite directory entry"))
|
|
.filter_map(|dir_entry| {
|
|
let p = dir_entry.path();
|
|
let ext = p.extension()?;
|
|
// Only look at wast files.
|
|
if ext != "wast" {
|
|
return None;
|
|
}
|
|
// Ignore files starting with `.`, which could be editor temporary files
|
|
if p.file_stem()?.to_str()?.starts_with('.') {
|
|
return None;
|
|
}
|
|
Some(p)
|
|
})
|
|
.collect();
|
|
|
|
dir_entries.sort();
|
|
|
|
let testsuite = &extract_name(path);
|
|
for entry in dir_entries.iter() {
|
|
write_testsuite_tests(out, entry, testsuite, strategy, false)?;
|
|
write_testsuite_tests(out, entry, testsuite, strategy, true)?;
|
|
}
|
|
|
|
Ok(dir_entries.len())
|
|
}
|
|
|
|
/// Extract a valid Rust identifier from the stem of a path.
|
|
fn extract_name(path: impl AsRef<Path>) -> String {
|
|
path.as_ref()
|
|
.file_stem()
|
|
.expect("filename should have a stem")
|
|
.to_str()
|
|
.expect("filename should be representable as a string")
|
|
.replace(['-', '/'], "_")
|
|
}
|
|
|
|
fn with_test_module<T>(
|
|
out: &mut String,
|
|
testsuite: &str,
|
|
f: impl FnOnce(&mut String) -> anyhow::Result<T>,
|
|
) -> anyhow::Result<T> {
|
|
out.push_str("mod ");
|
|
out.push_str(testsuite);
|
|
out.push_str(" {\n");
|
|
|
|
let result = f(out)?;
|
|
|
|
out.push_str("}\n");
|
|
Ok(result)
|
|
}
|
|
|
|
fn write_testsuite_tests(
|
|
out: &mut String,
|
|
path: impl AsRef<Path>,
|
|
testsuite: &str,
|
|
strategy: &str,
|
|
pooling: bool,
|
|
) -> anyhow::Result<()> {
|
|
let path = path.as_ref();
|
|
let testname = extract_name(path);
|
|
|
|
writeln!(out, "#[test]")?;
|
|
// Ignore when using QEMU for running tests (limited memory).
|
|
if ignore(testsuite, &testname, strategy) {
|
|
writeln!(out, "#[ignore]")?;
|
|
}
|
|
|
|
writeln!(
|
|
out,
|
|
"fn r#{}{}() {{",
|
|
&testname,
|
|
if pooling { "_pooling" } else { "" }
|
|
)?;
|
|
writeln!(out, " let _ = env_logger::try_init();")?;
|
|
writeln!(
|
|
out,
|
|
" crate::wast::run_wast(r#\"{}\"#, crate::wast::Strategy::{}, {}).unwrap();",
|
|
path.display(),
|
|
strategy,
|
|
pooling,
|
|
)?;
|
|
writeln!(out, "}}")?;
|
|
writeln!(out)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Ignore tests that aren't supported yet.
|
|
fn ignore(testsuite: &str, testname: &str, strategy: &str) -> bool {
|
|
assert_eq!(strategy, "Cranelift");
|
|
match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
|
|
"s390x" => {
|
|
// FIXME: These tests fail under qemu due to a qemu bug.
|
|
testname == "simd_f32x4_pmin_pmax" || testname == "simd_f64x2_pmin_pmax"
|
|
}
|
|
|
|
// Currently the simd wasm proposal is not implemented in the riscv64
|
|
// backend so skip all tests which could use simd.
|
|
"riscv64" => {
|
|
testsuite.contains("simd")
|
|
|| testname.contains("simd")
|
|
|| testname.contains("memory_multi")
|
|
}
|
|
|
|
_ => false,
|
|
}
|
|
}
|