pub struct Optimizer { /* private fields */ }Expand description
The configured optimizer (Tier 2): which passes, how many sweeps, what budget, and whether to validate between passes.
§Examples
use ir_lang::{Linkage, Module, Overflow, Signature, Type};
use opt_lang::{Optimizer, 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 unused = b.add(x, x, Overflow::Wrap)?;
b.ret(&[x])?;
let stats = Optimizer::new()
.passes(&[PassKind::Dce])
.max_iterations(2)
.validate(true)
.run(&mut m)?;
assert_eq!((stats.insts_before(), stats.insts_after()), (1, 0));Implementations§
Source§impl Optimizer
impl Optimizer
Sourcepub fn new() -> Optimizer
pub fn new() -> Optimizer
The default configuration: DEFAULT_PIPELINE,
DEFAULT_MAX_ITERATIONS sweeps, a budget of
DEFAULT_STEPS_PER_UNIT steps per unit of function size, validation
between passes in debug builds only, and compaction of every changed
function.
§Examples
let opt = opt_lang::Optimizer::new();Sourcepub fn passes(self, passes: &[PassKind]) -> Optimizer
pub fn passes(self, passes: &[PassKind]) -> Optimizer
The passes to run, in order, each sweep. An empty list does nothing.
§Examples
use opt_lang::{Optimizer, PassKind};
let cleanup = Optimizer::new().passes(&[PassKind::Dce, PassKind::SimplifyCfg]);Sourcepub fn max_iterations(self, n: usize) -> Optimizer
pub fn max_iterations(self, n: usize) -> Optimizer
The most sweeps per function; the pipeline stops earlier at its fixpoint
(a sweep that changes nothing). 0 runs nothing.
§Examples
use ir_lang::{Linkage, Module, Signature};
let mut m = Module::new("m");
let f = m.declare_function("f", &Signature::new(&[], &[]), Linkage::Export)?;
m.build(f)?.ret(&[])?;
let stats = opt_lang::Optimizer::new().max_iterations(0).run(&mut m)?;
assert_eq!(stats.iterations(), 0);Sourcepub fn budget(self, steps: u64) -> Optimizer
pub fn budget(self, steps: u64) -> Optimizer
A fixed budget of steps per function, instead of the default
size-proportional one.
§Examples
use ir_lang::{Linkage, Module, Signature};
let mut m = Module::new("m");
let f = m.declare_function("f", &Signature::new(&[], &[]), Linkage::Export)?;
m.build(f)?.ret(&[])?;
let stats = opt_lang::Optimizer::new().budget(0).run(&mut m)?;
assert_eq!(stats.budget_exhausted(), 1);Sourcepub fn validate(self, on: bool) -> Optimizer
pub fn validate(self, on: bool) -> Optimizer
Whether to run the ir-lang validator after every pass that changed the function (default: on in debug builds, off in release builds). The input is always validated.
§Examples
let checked = opt_lang::Optimizer::new().validate(true);
assert_ne!(checked, opt_lang::Optimizer::new().validate(false));Sourcepub fn compact(self, on: bool) -> Optimizer
pub fn compact(self, on: bool) -> Optimizer
Whether to compact every changed function at the end (default on): renumber values, blocks, and instructions densely and drop removed entities. With it off, handles stay stable and removed entities remain as tombstones.
§Examples
let opt = opt_lang::Optimizer::new().compact(false);Sourcepub fn run(&self, module: &mut Module) -> Result<Stats, OptError>
pub fn run(&self, module: &mut Module) -> Result<Stats, OptError>
Optimizes every function with a body.
§Errors
OptError::InvalidInput if a function does not validate (functions
before it are already optimized; it and the ones after are untouched);
OptError::InvalidOutput or OptError::Build for a defect in a
pass.
See Optimizer for an example.
Sourcepub fn run_function(
&self,
module: &mut Module,
func: FuncId,
) -> Result<Stats, OptError>
pub fn run_function( &self, module: &mut Module, func: FuncId, ) -> Result<Stats, OptError>
Optimizes one function.
§Errors
OptError::Module if func is unknown or has no body; otherwise as
run.
§Examples
use ir_lang::{Linkage, Module, Signature, Type};
let mut m = Module::new("m");
let f = m.declare_function("f", &Signature::new(&[], &[Type::U8]), Linkage::Export)?;
let mut b = m.build(f)?;
let v = b.iconst(Type::U8, 7)?;
let _dead = b.iconst(Type::U8, 8)?;
b.ret(&[v])?;
let stats = opt_lang::Optimizer::new().run_function(&mut m, f)?;
assert_eq!(stats.insts_after(), 1);