cranelift-filetest: Add support for Wasm-to-CLIF translation filetests (#5412)

This adds support for `.wat` tests in `cranelift-filetest`. The test runner
translates the WAT to Wasm and then uses `cranelift-wasm` to translate the Wasm
to CLIF.

These tests are always precise output tests. The test expectations can be
updated by running tests with the `CRANELIFT_TEST_BLESS=1` environment variable
set, similar to our compile precise output tests. The test's expected output is
contained in the last comment in the test file.

The tests allow for configuring the kinds of heaps used to implement Wasm linear
memory via TOML in a `;;!` comment at the start of the test.

To get ISA and Cranelift flags parsing available in the filetests crate, I had
to move the `parse_sets_and_triple` helper from the `cranelift-tools` binary
crate to the `cranelift-reader` crate, where I think it logically
fits.

Additionally, I had to make some more bits of `cranelift-wasm`'s dummy
environment `pub` so that I could properly wrap and compose it with the
environment used for the `.wat` tests. I don't think this is a big deal, but if
we eventually want to clean this stuff up, we can probably remove the dummy
environments completely, remove `translate_module`, and fold them into these new
test environments and test runner (since Wasmtime isn't using those things
anyways).
This commit is contained in:
Nick Fitzgerald
2022-12-12 11:31:29 -08:00
committed by GitHub
parent 7adf3cacc5
commit f2e1eaa847
19 changed files with 1069 additions and 104 deletions

View File

@@ -138,13 +138,13 @@ pub struct DummyEnvironment {
pub info: DummyModuleInfo,
/// Function translation.
trans: FuncTranslator,
pub trans: FuncTranslator,
/// Vector of wasm bytecode size for each function.
pub func_bytecode_sizes: Vec<usize>,
/// Instructs to collect debug data during translation.
debug_info: bool,
pub debug_info: bool,
/// Name of the module from the wasm file.
pub module_name: Option<String>,
@@ -153,7 +153,8 @@ pub struct DummyEnvironment {
function_names: SecondaryMap<FuncIndex, String>,
/// Expected reachability data (before/after for each op) to assert. This is used for testing.
expected_reachability: Option<ExpectedReachability>,
#[doc(hidden)]
pub expected_reachability: Option<ExpectedReachability>,
}
impl DummyEnvironment {
@@ -176,7 +177,8 @@ impl DummyEnvironment {
DummyFuncEnvironment::new(&self.info, self.expected_reachability.clone())
}
fn get_func_type(&self, func_index: FuncIndex) -> TypeIndex {
/// Get the type for the function at the given index.
pub fn get_func_type(&self, func_index: FuncIndex) -> TypeIndex {
self.info.functions[func_index].entity
}
@@ -205,6 +207,7 @@ impl DummyEnvironment {
/// The `FuncEnvironment` implementation for use by the `DummyEnvironment`.
pub struct DummyFuncEnvironment<'dummy_environment> {
/// This function environment's module info.
pub mod_info: &'dummy_environment DummyModuleInfo,
/// Expected reachability data (before/after for each op) to assert. This is used for testing.
@@ -212,6 +215,7 @@ pub struct DummyFuncEnvironment<'dummy_environment> {
}
impl<'dummy_environment> DummyFuncEnvironment<'dummy_environment> {
/// Construct a new `DummyFuncEnvironment`.
pub fn new(
mod_info: &'dummy_environment DummyModuleInfo,
expected_reachability: Option<ExpectedReachability>,
@@ -222,9 +226,9 @@ impl<'dummy_environment> DummyFuncEnvironment<'dummy_environment> {
}
}
// Create a signature for `sigidx` amended with a `vmctx` argument after the standard wasm
// arguments.
fn vmctx_sig(&self, sigidx: TypeIndex) -> ir::Signature {
/// Create a signature for `sigidx` amended with a `vmctx` argument after
/// the standard wasm arguments.
pub fn vmctx_sig(&self, sigidx: TypeIndex) -> ir::Signature {
let mut sig = self.mod_info.signatures[sigidx].clone();
sig.params.push(ir::AbiParam::special(
self.pointer_type(),

View File

@@ -4,7 +4,9 @@ mod dummy;
#[macro_use]
mod spec;
pub use crate::environ::dummy::DummyEnvironment;
pub use crate::environ::dummy::{
DummyEnvironment, DummyFuncEnvironment, DummyModuleInfo, ExpectedReachability,
};
pub use crate::environ::spec::{
FuncEnvironment, GlobalVariable, ModuleEnvironment, TargetEnvironment,
};

View File

@@ -57,7 +57,8 @@ mod state;
mod translation_utils;
pub use crate::environ::{
DummyEnvironment, FuncEnvironment, GlobalVariable, ModuleEnvironment, TargetEnvironment,
DummyEnvironment, DummyFuncEnvironment, DummyModuleInfo, ExpectedReachability, FuncEnvironment,
GlobalVariable, ModuleEnvironment, TargetEnvironment,
};
pub use crate::func_translator::FuncTranslator;
pub use crate::module_translator::translate_module;