Skip to content

Commit

Permalink
cranelift-wasm: Delete expected-reachability test support (#8155)
Browse files Browse the repository at this point in the history
After #8147 this code isn't used anywhere.
  • Loading branch information
jameysharp authored Mar 16, 2024
1 parent 593f18b commit 7baeafb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 94 deletions.
93 changes: 4 additions & 89 deletions cranelift/wasm/src/environ/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use crate::environ::{FuncEnvironment, GlobalVariable, ModuleEnvironment, TargetEnvironment};
use crate::func_translator::FuncTranslator;
use crate::state::FuncTranslationState;
use crate::{
DataIndex, DefinedFuncIndex, ElemIndex, FuncIndex, Global, GlobalIndex, GlobalInit, Heap,
HeapData, HeapStyle, Memory, MemoryIndex, Table, TableIndex, TableSize, TypeConvert, TypeIndex,
Expand All @@ -24,7 +23,7 @@ use cranelift_frontend::FunctionBuilder;
use std::boxed::Box;
use std::string::String;
use std::vec::Vec;
use wasmparser::{FuncValidator, FunctionBody, Operator, ValidatorResources, WasmFeatures};
use wasmparser::{FuncValidator, FunctionBody, ValidatorResources, WasmFeatures};

/// A collection of names under which a given entity is exported.
pub struct Exportable<T> {
Expand Down Expand Up @@ -105,31 +104,6 @@ impl DummyModuleInfo {
}
}

/// State for tracking and checking reachability at each operator. Used for unit testing with the
/// `DummyEnvironment`.
#[derive(Clone)]
pub struct ExpectedReachability {
/// Before- and after-reachability
reachability: Vec<(bool, bool)>,
before_idx: usize,
after_idx: usize,
}

impl ExpectedReachability {
fn check_before(&mut self, reachable: bool) {
assert_eq!(reachable, self.reachability[self.before_idx].0);
self.before_idx += 1;
}
fn check_after(&mut self, reachable: bool) {
assert_eq!(reachable, self.reachability[self.after_idx].1);
self.after_idx += 1;
}
fn check_end(&self) {
assert_eq!(self.before_idx, self.reachability.len());
assert_eq!(self.after_idx, self.reachability.len());
}
}

/// This `ModuleEnvironment` implementation is a "naïve" one, doing essentially nothing and
/// emitting placeholders when forced to. Don't try to execute code translated for this
/// environment, essentially here for translation debug purposes.
Expand All @@ -148,10 +122,6 @@ pub struct DummyEnvironment {

/// Function names.
function_names: SecondaryMap<FuncIndex, String>,

/// Expected reachability data (before/after for each op) to assert. This is used for testing.
#[doc(hidden)]
pub expected_reachability: Option<ExpectedReachability>,
}

impl DummyEnvironment {
Expand All @@ -163,14 +133,13 @@ impl DummyEnvironment {
func_bytecode_sizes: Vec::new(),
module_name: None,
function_names: SecondaryMap::new(),
expected_reachability: None,
}
}

/// Return a `DummyFuncEnvironment` for translating functions within this
/// `DummyEnvironment`.
pub fn func_env(&self) -> DummyFuncEnvironment {
DummyFuncEnvironment::new(&self.info, self.expected_reachability.clone())
DummyFuncEnvironment::new(&self.info)
}

/// Get the type for the function at the given index.
Expand All @@ -188,27 +157,13 @@ impl DummyEnvironment {
pub fn get_func_name(&self, func_index: FuncIndex) -> Option<&str> {
self.function_names.get(func_index).map(String::as_ref)
}

/// Test reachability bits before and after every opcode during translation, as provided by the
/// `FuncTranslationState`. This is generally used only for unit tests. This is applied to
/// every function in the module (so is likely only useful for test modules with one function).
pub fn test_expected_reachability(&mut self, reachability: Vec<(bool, bool)>) {
self.expected_reachability = Some(ExpectedReachability {
reachability,
before_idx: 0,
after_idx: 0,
});
}
}

/// 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.
expected_reachability: Option<ExpectedReachability>,

/// Heaps we have created to implement Wasm linear memories.
pub heaps: PrimaryMap<Heap, HeapData>,

Expand All @@ -218,13 +173,9 @@ 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>,
) -> Self {
pub fn new(mod_info: &'dummy_environment DummyModuleInfo) -> Self {
Self {
mod_info,
expected_reachability,
heaps: Default::default(),
tables: Default::default(),
}
Expand Down Expand Up @@ -390,41 +341,6 @@ impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environ
}))
}

fn before_translate_operator(
&mut self,
_op: &Operator,
_builder: &mut FunctionBuilder,
state: &FuncTranslationState,
) -> WasmResult<()> {
if let Some(ref mut r) = &mut self.expected_reachability {
r.check_before(state.reachable());
}
Ok(())
}

fn after_translate_operator(
&mut self,
_op: &Operator,
_builder: &mut FunctionBuilder,
state: &FuncTranslationState,
) -> WasmResult<()> {
if let Some(ref mut r) = &mut self.expected_reachability {
r.check_after(state.reachable());
}
Ok(())
}

fn after_translate_function(
&mut self,
_builder: &mut FunctionBuilder,
_state: &FuncTranslationState,
) -> WasmResult<()> {
if let Some(ref mut r) = &mut self.expected_reachability {
r.check_end();
}
Ok(())
}

fn translate_call_indirect(
&mut self,
builder: &mut FunctionBuilder,
Expand Down Expand Up @@ -937,8 +853,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
self.func_bytecode_sizes
.push(body.get_binary_reader().bytes_remaining());
let func = {
let mut func_environ =
DummyFuncEnvironment::new(&self.info, self.expected_reachability.clone());
let mut func_environ = DummyFuncEnvironment::new(&self.info);
let func_index =
FuncIndex::new(self.get_num_func_imports() + self.info.function_bodies.len());

Expand Down
4 changes: 1 addition & 3 deletions cranelift/wasm/src/environ/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ mod dummy;
#[macro_use]
mod spec;

pub use crate::environ::dummy::{
DummyEnvironment, DummyFuncEnvironment, DummyModuleInfo, ExpectedReachability,
};
pub use crate::environ::dummy::{DummyEnvironment, DummyFuncEnvironment, DummyModuleInfo};
pub use crate::environ::spec::{
FuncEnvironment, GlobalVariable, ModuleEnvironment, TargetEnvironment,
};
4 changes: 2 additions & 2 deletions cranelift/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ mod table;
mod translation_utils;

pub use crate::environ::{
DummyEnvironment, DummyFuncEnvironment, DummyModuleInfo, ExpectedReachability, FuncEnvironment,
GlobalVariable, ModuleEnvironment, TargetEnvironment,
DummyEnvironment, DummyFuncEnvironment, DummyModuleInfo, FuncEnvironment, GlobalVariable,
ModuleEnvironment, TargetEnvironment,
};
pub use crate::func_translator::FuncTranslator;
pub use crate::heap::{Heap, HeapData, HeapStyle};
Expand Down

0 comments on commit 7baeafb

Please sign in to comment.