Skip to main content

run_pass

Function run_pass 

Source
pub fn run_pass(
    b: &mut Builder<'_>,
    pass: PassKind,
    budget: &mut Budget,
) -> Result<bool, OptError>
Expand description

Runs one pass on the function being edited, for callers that build their own pipeline (Tier 3).

The function is validated first (passes require valid input); after the pass it is validated again in debug builds. Returns whether the pass changed anything. A pass the budget cannot pay for does nothing and returns Ok(false), with Budget::is_exhausted set.

§Errors

OptError::InvalidInput if the function does not validate (nothing is changed); OptError::InvalidOutput or OptError::Build for a defect in a pass.

§Examples

use ir_lang::{Linkage, Module, Overflow, Signature, Type};
use opt_lang::{Budget, PassKind};

let mut m = Module::new("m");
let f = m.declare_function("f", &Signature::new(&[Type::I64], &[Type::I64]), Linkage::Export)?;
let mut b = m.build(f)?;
let x = b.param(0)?;
let a = b.add(x, x, Overflow::Wrap)?;
let again = b.add(x, x, Overflow::Wrap)?; // the same value
let y = b.mul(a, again, Overflow::Wrap)?;
b.ret(&[y])?;

let mut budget = Budget::unlimited();
assert!(opt_lang::run_pass(&mut m.edit(f)?, PassKind::Gvn, &mut budget)?);
assert!(!opt_lang::run_pass(&mut m.edit(f)?, PassKind::Gvn, &mut budget)?);
assert!(m.to_string().contains("mul<overflow=wrap> v1, v1"));