Skip to content

Commit

Permalink
cranelift: Add some libcalls to test interpret
Browse files Browse the repository at this point in the history
  • Loading branch information
afonso360 committed Oct 21, 2023
1 parent ed68661 commit 00b1325
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cranelift/filetests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ num_cpus = "1.8.0"
target-lexicon = { workspace = true }
thiserror = { workspace = true }
anyhow = { workspace = true }
similar ={ workspace = true }
similar = { workspace = true }
wat.workspace = true
toml = { workspace = true }
serde = { workspace = true }
serde_derive = { workspace = true }
cranelift-wasm.workspace = true
wasmparser.workspace = true
cranelift.workspace = true
smallvec = { workspace = true }
5 changes: 4 additions & 1 deletion cranelift/filetests/filetests/runtests/call_libcall.clif
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
test interpret
test run
target x86_64
; AArch64 Does not have these libcalls
target aarch64
target aarch64 sign_return_address
target aarch64 has_pauth sign_return_address
target s390x
target riscv64
target riscv64 has_c has_zcb
Expand Down
21 changes: 18 additions & 3 deletions cranelift/filetests/src/test_interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
use crate::runone::FileUpdate;
use crate::subtest::SubTest;
use anyhow::Context;
use cranelift_codegen::ir::Function;
use cranelift_codegen::data_value::DataValue;
use cranelift_codegen::ir::{Function, LibCall};
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::settings::Flags;
use cranelift_codegen::{self, ir};
use cranelift_interpreter::environment::FunctionStore;
use cranelift_interpreter::interpreter::{Interpreter, InterpreterState};
use cranelift_interpreter::interpreter::{Interpreter, InterpreterState, LibCallValues};
use cranelift_interpreter::step::ControlFlow;
use cranelift_reader::{parse_run_command, Details, TestCommand, TestFile};
use log::{info, trace};
use smallvec::smallvec;
use std::borrow::Cow;

struct TestInterpret;
Expand Down Expand Up @@ -83,7 +85,20 @@ fn run_test(func_store: &FunctionStore, func: &Function, details: &Details) -> a
.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 state = InterpreterState::default().with_function_store(func_store.clone());
let state = InterpreterState::default()
.with_function_store(func_store.clone())
.with_libcall_handler(|libcall: LibCall, args: LibCallValues| {
use LibCall::*;
Ok(smallvec![match (libcall, &args[..]) {
(CeilF32, [DataValue::F32(a)]) => DataValue::F32(a.ceil()),
(CeilF64, [DataValue::F64(a)]) => DataValue::F64(a.ceil()),
(FloorF32, [DataValue::F32(a)]) => DataValue::F32(a.floor()),
(FloorF64, [DataValue::F64(a)]) => DataValue::F64(a.floor()),
(TruncF32, [DataValue::F32(a)]) => DataValue::F32(a.trunc()),
(TruncF64, [DataValue::F64(a)]) => DataValue::F64(a.trunc()),
_ => unreachable!(),
}])
});

let mut args = Vec::with_capacity(run_args.len());
args.extend_from_slice(run_args);
Expand Down

0 comments on commit 00b1325

Please sign in to comment.