Where is mir-opt-level=2 (or higher) actually applied in rustc?

Hi all,

While reading the rustc 1.90 source, I noticed that mir-opt-level is defined as:

pub fn mir_opt_level(&self) -> usize {
    self.opts.unstable_opts.mir_opt_level
        .unwrap_or_else(|| if self.opts.optimize != OptLevel::No { 2 } else { 1 })
}

and in rustc_mir_transform/src/lib.rs,all passes are registered as:

WithMinOptLevel(1, x)

and within rustc_mir_transform/src/lib.rs, the optimization passes are organized roughly as:

fn o1<T>(x: T) -> WithMinOptLevel<T> {
    WithMinOptLevel(1, x)
}

// Example snippet
pm::run_passes(
    tcx,
    body,
    &[
        &check_alignment::CheckAlignment,
        &check_null::CheckNull,
        ...
        &o1(simplify::SimplifyCfg::Final),
        &strip_debuginfo::StripDebugInfo,
    ],
);

and there are no WithMinOptLevel(2, …) or higher anywhere in the MIR pass pipeline.

Questions:

  1. Where (if anywhere) is mir-opt-level=2 or higher actually used inside rustc?
  2. Does setting -Z mir-opt-level=2 enable any additional MIR passes or change pass behavior internally?

Here is an example:

this doesn't use WithMinOptLevel because it can be enabled independently of -Zmir-opt-level=2 through -Zmir-inline.

and here is another:

This one could use WithMinOptLevel I guess, although WithMinOptLevel is itself implemented in terms of is_enabled, so I think it mostly makes sense to use it when a pass runs multiple times and you only want different opt levels for the different times it runs.

3 Likes

Thanks for the clarification — that makes me understand where O2 or higher optimization level apply. Appreciate your detailed answer!!!