Revert "cranelift: Register all functions in test file for interpreter (#4800)" (#4810)

This reverts commit 500a9f17be.
This commit is contained in:
Chris Fallin
2022-08-29 18:15:11 -07:00
committed by GitHub
parent 955d4e4ba1
commit 2b4b257834
6 changed files with 38 additions and 139 deletions

View File

@@ -3,21 +3,16 @@
//! The `interpret` test command interprets each function on the host machine
//! using [RunCommand](cranelift_reader::RunCommand)s.
use crate::runone::FileUpdate;
use crate::runtest_environment::RuntestEnvironment;
use crate::subtest::SubTest;
use anyhow::{anyhow, Context};
use crate::subtest::{Context, SubTest};
use cranelift_codegen::data_value::DataValue;
use cranelift_codegen::ir::types::I64;
use cranelift_codegen::ir::Function;
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::settings::Flags;
use cranelift_codegen::{self, ir};
use cranelift_interpreter::environment::FunctionStore;
use cranelift_interpreter::interpreter::{HeapInit, Interpreter, InterpreterState};
use cranelift_interpreter::step::ControlFlow;
use cranelift_reader::{parse_run_command, Details, TestCommand, TestFile};
use log::{info, trace};
use cranelift_reader::{parse_run_command, TestCommand};
use log::trace;
use std::borrow::Cow;
struct TestInterpret;
@@ -43,81 +38,43 @@ impl SubTest for TestInterpret {
false
}
/// Runs the entire subtest for a given target, invokes [Self::run] for running
/// individual tests.
fn run_target<'a>(
&self,
testfile: &TestFile,
_: &mut FileUpdate,
_: &'a str,
_: &'a Flags,
_: Option<&'a dyn TargetIsa>,
) -> anyhow::Result<()> {
// We can build the FunctionStore once and reuse it
let mut func_store = FunctionStore::default();
for (func, _) in &testfile.functions {
func_store.add(func.name.to_string(), &func);
fn run(&self, func: Cow<ir::Function>, context: &Context) -> anyhow::Result<()> {
let test_env = RuntestEnvironment::parse(&context.details.comments[..])?;
for comment in context.details.comments.iter() {
if let Some(command) = parse_run_command(comment.text, &func.signature)? {
trace!("Parsed run command: {}", command);
let mut env = FunctionStore::default();
env.add(func.name.to_string(), &func);
command
.run(|func_name, run_args| {
test_env.validate_signature(&func)?;
let mut state = InterpreterState::default().with_function_store(env);
let mut args = Vec::with_capacity(run_args.len());
if test_env.is_active() {
let vmctx_addr = register_heaps(&mut state, &test_env);
args.push(vmctx_addr);
}
args.extend_from_slice(run_args);
// Because we have stored function names with a leading %, we need to re-add it.
let func_name = &format!("%{}", func_name);
match Interpreter::new(state).call_by_name(func_name, &args) {
Ok(ControlFlow::Return(results)) => Ok(results.to_vec()),
Ok(_) => {
panic!("Unexpected returned control flow--this is likely a bug.")
}
Err(t) => Err(format!("unexpected trap: {:?}", t)),
}
})
.map_err(|e| anyhow::anyhow!("{}", e))?;
}
}
for (func, details) in &testfile.functions {
info!("Test: {}({}) interpreter", self.name(), func.name);
let test_env = RuntestEnvironment::parse(&details.comments[..])?;
test_env.validate_signature(&func).map_err(|s| anyhow!(s))?;
run_test(&test_env, &func_store, func, details).context(self.name())?;
}
Ok(())
}
fn run(
&self,
_func: Cow<ir::Function>,
_context: &crate::subtest::Context,
) -> anyhow::Result<()> {
unreachable!()
}
}
fn run_test(
test_env: &RuntestEnvironment,
func_store: &FunctionStore,
func: &Function,
details: &Details,
) -> anyhow::Result<()> {
for comment in details.comments.iter() {
if let Some(command) = parse_run_command(comment.text, &func.signature)? {
trace!("Parsed run command: {}", command);
command
.run(|func_name, run_args| {
// Rebuild the interpreter state on every run to ensure that we don't accidentally depend on
// some leftover state
let mut state =
InterpreterState::default().with_function_store(func_store.clone());
let mut args = Vec::with_capacity(run_args.len());
if test_env.is_active() {
let vmctx_addr = register_heaps(&mut state, test_env);
args.push(vmctx_addr);
}
args.extend_from_slice(run_args);
// Because we have stored function names with a leading %, we need to re-add it.
let func_name = &format!("%{}", func_name);
match Interpreter::new(state).call_by_name(func_name, &args) {
Ok(ControlFlow::Return(results)) => Ok(results.to_vec()),
Ok(e) => {
panic!("Unexpected returned control flow: {:?}", e)
}
Err(t) => Err(format!("unexpected trap: {:?}", t)),
}
})
.map_err(|e| anyhow::anyhow!("{}", e))?;
}
}
Ok(())
}
/// Build a VMContext struct with the layout described in docs/testing.md.