Path: blob/main/cranelift/filetests/src/test_optimize.rs
1691 views
//! Test command for testing the optimization phases.1//!2//! The `optimize` test command runs each function through the3//! optimization passes, but not lowering or regalloc. The output for4//! filecheck purposes is the resulting CLIF.5//!6//! Some legalization may be ISA-specific, so this requires an ISA7//! (for now).89use crate::subtest::{Context, SubTest, check_precise_output, run_filecheck};10use anyhow::Result;11use cranelift_codegen::ir;12use cranelift_control::ControlPlane;13use cranelift_reader::{TestCommand, TestOption};14use std::borrow::Cow;1516struct TestOptimize {17/// Flag indicating that the text expectation, comments after the function,18/// must be a precise 100% match on the compiled output of the function.19/// This test assertion is also automatically-update-able to allow tweaking20/// the code generator and easily updating all affected tests.21precise_output: bool,22}2324pub fn subtest(parsed: &TestCommand) -> Result<Box<dyn SubTest>> {25assert_eq!(parsed.command, "optimize");26let mut test = TestOptimize {27precise_output: false,28};29for option in parsed.options.iter() {30match option {31TestOption::Flag("precise-output") => test.precise_output = true,32_ => anyhow::bail!("unknown option on {}", parsed),33}34}35Ok(Box::new(test))36}3738impl SubTest for TestOptimize {39fn name(&self) -> &'static str {40"optimize"41}4243fn is_mutating(&self) -> bool {44true45}4647fn needs_isa(&self) -> bool {48true49}5051fn run(&self, func: Cow<ir::Function>, context: &Context) -> Result<()> {52let isa = context.isa.expect("optimize needs an ISA");53let mut comp_ctx = cranelift_codegen::Context::for_function(func.into_owned());5455comp_ctx56.optimize(isa, &mut ControlPlane::default())57.map_err(|e| crate::pretty_anyhow_error(&comp_ctx.func, e))?;5859let clif = format!("{:?}", comp_ctx.func);6061if self.precise_output {62let actual: Vec<_> = clif.lines().collect();63check_precise_output(&actual, context)64} else {65run_filecheck(&clif, context)66}67}68}697071