This repository was archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
Add test run to cranelift-filetests to allow executing CLIF
#890
Merged
bnjbvr
merged 3 commits into
bytecodealliance:master
from
abrown:clif-filetests-test-run
Aug 21, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| use core::mem; | ||
| use cranelift_codegen::binemit::{NullRelocSink, NullStackmapSink, NullTrapSink}; | ||
| use cranelift_codegen::ir::Function; | ||
| use cranelift_codegen::isa::{CallConv, TargetIsa}; | ||
| use cranelift_codegen::{settings, Context}; | ||
| use cranelift_native::builder as host_isa_builder; | ||
| use mmap::{MapOption, MemoryMap}; | ||
| use region; | ||
| use region::Protection; | ||
|
|
||
| /// Run a function on a host | ||
| pub struct FunctionRunner { | ||
| function: Function, | ||
| isa: Box<dyn TargetIsa>, | ||
| } | ||
|
|
||
| impl FunctionRunner { | ||
| /// Build a function runner from a function and the ISA to run on (must be the host machine's ISA) | ||
| pub fn new(function: Function, isa: Box<dyn TargetIsa>) -> Self { | ||
| FunctionRunner { function, isa } | ||
| } | ||
|
|
||
| /// Build a function runner using the host machine's ISA and the passed flags | ||
| pub fn with_host_isa(function: Function, flags: settings::Flags) -> Self { | ||
| let builder = host_isa_builder().expect("Unable to build a TargetIsa for the current host"); | ||
| let isa = builder.finish(flags); | ||
| FunctionRunner::new(function, isa) | ||
| } | ||
|
|
||
| /// Build a function runner using the host machine's ISA and the default flags for this ISA | ||
| pub fn with_default_host_isa(function: Function) -> Self { | ||
| let flags = settings::Flags::new(settings::builder()); | ||
| FunctionRunner::with_host_isa(function, flags) | ||
| } | ||
|
|
||
| /// Compile and execute a single function, expecting a boolean to be returned; a 'true' value is | ||
| /// interpreted as a successful test execution and mapped to Ok whereas a 'false' value is | ||
| /// interpreted as a failed test and mapped to Err. | ||
| pub fn run(&self) -> Result<(), String> { | ||
| let func = self.function.clone(); | ||
| if !(func.signature.params.is_empty() | ||
| && func.signature.returns.len() == 1 | ||
| && func.signature.returns.first().unwrap().value_type.is_bool()) | ||
| { | ||
| return Err(String::from( | ||
| "Functions must have a signature like: () -> boolean", | ||
| )); | ||
| } | ||
|
|
||
| if func.signature.call_conv != self.isa.default_call_conv() | ||
| && func.signature.call_conv != CallConv::Fast | ||
| { | ||
| // ideally we wouldn't have to also check for Fast here but currently there is no way to inform the filetest parser that we would like to use a default other than Fast | ||
| return Err(String::from( | ||
| "Functions only run on the host's default calling convention; remove the specified calling convention in the function signature to use the host's default.", | ||
| )); | ||
| } | ||
|
|
||
| // set up the context | ||
| let mut context = Context::new(); | ||
| context.func = func; | ||
|
|
||
| // compile and encode the result to machine code | ||
| let relocs = &mut NullRelocSink {}; | ||
| let traps = &mut NullTrapSink {}; | ||
| let stackmaps = &mut NullStackmapSink {}; | ||
| let code_info = context | ||
| .compile(self.isa.as_ref()) | ||
| .map_err(|e| e.to_string())?; | ||
| let code_page = MemoryMap::new(code_info.total_size as usize, &[MapOption::MapWritable]) | ||
| .map_err(|e| e.to_string())?; | ||
| let callable_fn: fn() -> bool = unsafe { | ||
| context.emit_to_memory( | ||
| self.isa.as_ref(), | ||
| code_page.data(), | ||
| relocs, | ||
| traps, | ||
| stackmaps, | ||
| ); | ||
| region::protect(code_page.data(), code_page.len(), Protection::ReadExecute) | ||
| .map_err(|e| e.to_string())?; | ||
| mem::transmute(code_page.data()) | ||
| }; | ||
|
|
||
| // execute | ||
| match callable_fn() { | ||
| true => Ok(()), | ||
| false => Err(format!("Failed: {}", context.func.name.to_string())), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
| use cranelift_reader::parse_test; | ||
|
|
||
| #[test] | ||
| fn nop() { | ||
| let code = String::from( | ||
| "function %test() -> b8 system_v { | ||
| ebb0: | ||
| nop | ||
| v1 = bconst.b8 true | ||
| return v1 | ||
| }", | ||
| ); | ||
|
|
||
| // extract function | ||
| let test_file = parse_test(code.as_str(), None, None).unwrap(); | ||
| assert_eq!(1, test_file.functions.len()); | ||
| let function = test_file.functions[0].0.clone(); | ||
|
|
||
| // execute function | ||
| let runner = FunctionRunner::with_default_host_isa(function); | ||
| runner.run().unwrap() // will panic if execution fails | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| //! Test command for running CLIF files and verifying their results | ||
| //! | ||
| //! The `run` test command compiles each function on the host machine and executes it | ||
|
|
||
| use crate::function_runner::FunctionRunner; | ||
| use crate::subtest::{Context, SubTest, SubtestResult}; | ||
| use cranelift_codegen; | ||
| use cranelift_codegen::ir; | ||
| use cranelift_reader::TestCommand; | ||
| use std::borrow::Cow; | ||
|
|
||
| struct TestRun; | ||
|
|
||
| pub fn subtest(parsed: &TestCommand) -> SubtestResult<Box<dyn SubTest>> { | ||
| assert_eq!(parsed.command, "run"); | ||
| if !parsed.options.is_empty() { | ||
| Err(format!("No options allowed on {}", parsed)) | ||
| } else { | ||
| Ok(Box::new(TestRun)) | ||
| } | ||
| } | ||
|
|
||
| impl SubTest for TestRun { | ||
| fn name(&self) -> &'static str { | ||
| "run" | ||
| } | ||
|
|
||
| fn is_mutating(&self) -> bool { | ||
| false | ||
| } | ||
|
|
||
| fn needs_isa(&self) -> bool { | ||
| false | ||
| } | ||
|
|
||
| fn run(&self, func: Cow<ir::Function>, context: &Context) -> SubtestResult<()> { | ||
| for comment in context.details.comments.iter() { | ||
| if comment.text.contains("run") { | ||
| let runner = | ||
| FunctionRunner::with_host_isa(func.clone().into_owned(), context.flags.clone()); | ||
| runner.run()? | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| test run | ||
|
|
||
| function %test_compare_i32() -> b1 { | ||
| ebb0: | ||
| v0 = iconst.i32 42 | ||
| v1 = iconst.i32 42 | ||
| v2 = icmp eq v0, v1 | ||
| return v2 | ||
| } | ||
|
|
||
| ; run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.