Add early-stage optimization crate (#556)

* Add simple constant folding and folding tests
This commit is contained in:
Lachlan Sneff
2018-11-07 18:59:29 -05:00
committed by Dan Gohman
parent bdcc06eb15
commit 3409af7c07
22 changed files with 745 additions and 21 deletions

View File

@@ -24,6 +24,7 @@
)]
extern crate cranelift_codegen;
extern crate cranelift_preopt;
extern crate cranelift_reader;
extern crate file_per_thread_logger;
extern crate filecheck;
@@ -56,6 +57,7 @@ mod test_print_cfg;
mod test_regalloc;
mod test_shrink;
mod test_simple_gvn;
mod test_simple_preopt;
mod test_verifier;
/// The result of running the test in a file.
@@ -127,12 +129,13 @@ fn new_subtest(parsed: &TestCommand) -> subtest::SubtestResult<Box<subtest::SubT
"legalizer" => test_legalizer::subtest(parsed),
"licm" => test_licm::subtest(parsed),
"postopt" => test_postopt::subtest(parsed),
"preopt" => test_preopt::subtest(parsed),
"simple_preopt" => test_simple_preopt::subtest(parsed),
"print-cfg" => test_print_cfg::subtest(parsed),
"regalloc" => test_regalloc::subtest(parsed),
"shrink" => test_shrink::subtest(parsed),
"simple-gvn" => test_simple_gvn::subtest(parsed),
"verifier" => test_verifier::subtest(parsed),
"preopt" => test_preopt::subtest(parsed),
_ => Err(format!("unknown test command '{}'", parsed.command)),
}
}

View File

@@ -1,10 +1,14 @@
//! Test command for testing the preopt pass.
//! Test command for testing the constant folding pass.
//!
//! The `dce` test command runs each function through the constant folding pass after ensuring
//! that all instructions are legal for the target.
//!
//! The resulting function is sent to `filecheck`.
use cranelift_codegen;
use cranelift_codegen::ir::Function;
use cranelift_codegen::print_errors::pretty_error;
use cranelift_preopt::optimize;
use cranelift_reader::TestCommand;
use std::borrow::Cow;
use subtest::{run_filecheck, Context, SubTest, SubtestResult};
@@ -29,16 +33,18 @@ impl SubTest for TestPreopt {
true
}
fn run(&self, func: Cow<Function>, context: &Context) -> SubtestResult<()> {
let mut comp_ctx = cranelift_codegen::Context::for_function(func.into_owned());
let isa = context.isa.expect("preopt needs an ISA");
fn needs_isa(&self) -> bool {
true
}
comp_ctx.flowgraph();
comp_ctx
.preopt(isa)
fn run(&self, func: Cow<Function>, context: &Context) -> SubtestResult<()> {
let isa = context.isa.expect("compile needs an ISA");
let mut comp_ctx = cranelift_codegen::Context::for_function(func.into_owned());
optimize(&mut comp_ctx, isa)
.map_err(|e| pretty_error(&comp_ctx.func, context.isa, Into::into(e)))?;
let text = &comp_ctx.func.display(isa).to_string();
let text = comp_ctx.func.display(context.isa).to_string();
run_filecheck(&text, context)
}
}

View File

@@ -0,0 +1,44 @@
//! Test command for testing the preopt pass.
//!
//! The resulting function is sent to `filecheck`.
use cranelift_codegen;
use cranelift_codegen::ir::Function;
use cranelift_codegen::print_errors::pretty_error;
use cranelift_reader::TestCommand;
use std::borrow::Cow;
use subtest::{run_filecheck, Context, SubTest, SubtestResult};
struct TestSimplePreopt;
pub fn subtest(parsed: &TestCommand) -> SubtestResult<Box<SubTest>> {
assert_eq!(parsed.command, "simple_preopt");
if !parsed.options.is_empty() {
Err(format!("No options allowed on {}", parsed))
} else {
Ok(Box::new(TestSimplePreopt))
}
}
impl SubTest for TestSimplePreopt {
fn name(&self) -> &'static str {
"simple_preopt"
}
fn is_mutating(&self) -> bool {
true
}
fn run(&self, func: Cow<Function>, context: &Context) -> SubtestResult<()> {
let mut comp_ctx = cranelift_codegen::Context::for_function(func.into_owned());
let isa = context.isa.expect("preopt needs an ISA");
comp_ctx.flowgraph();
comp_ctx
.preopt(isa)
.map_err(|e| pretty_error(&comp_ctx.func, context.isa, Into::into(e)))?;
let text = &comp_ctx.func.display(isa).to_string();
run_filecheck(&text, context)
}
}