cranelift_filetests/
test_legalizer.rs

1//! Test command for checking the IR legalizer.
2//!
3//! The `test legalizer` test command runs each function through `legalize_function()` and sends
4//! the result to filecheck.
5
6use crate::subtest::{run_filecheck, Context, SubTest};
7use cranelift_codegen::ir::Function;
8use cranelift_reader::TestCommand;
9use std::borrow::Cow;
10
11struct TestLegalizer;
12
13pub fn subtest(parsed: &TestCommand) -> anyhow::Result<Box<dyn SubTest>> {
14    assert_eq!(parsed.command, "legalizer");
15    if !parsed.options.is_empty() {
16        anyhow::bail!("No options allowed on {}", parsed);
17    }
18    Ok(Box::new(TestLegalizer))
19}
20
21impl SubTest for TestLegalizer {
22    fn name(&self) -> &'static str {
23        "legalizer"
24    }
25
26    fn is_mutating(&self) -> bool {
27        true
28    }
29
30    fn needs_isa(&self) -> bool {
31        true
32    }
33
34    fn run(&self, func: Cow<Function>, context: &Context) -> anyhow::Result<()> {
35        let mut comp_ctx = cranelift_codegen::Context::for_function(func.into_owned());
36        let isa = context.isa.expect("legalizer needs an ISA");
37
38        comp_ctx.compute_cfg();
39        comp_ctx
40            .legalize(isa)
41            .map_err(|e| crate::pretty_anyhow_error(&comp_ctx.func, e))?;
42
43        let text = comp_ctx.func.display().to_string();
44        run_filecheck(&text, context)
45    }
46}