Skip to main content

Optimizer

Struct Optimizer 

Source
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

Source

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();
Source

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]);
Source

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);
Source

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);
Source

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));
Source

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);
Source

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.

Source

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);

Trait Implementations§

Source§

impl Clone for Optimizer

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Optimizer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Optimizer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Optimizer

Source§

impl PartialEq for Optimizer

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Optimizer

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.