Skip to content

mir-transform: Treat optimize(none) the same as opt-level=0 - #160524

Open
clubby789 wants to merge 1 commit into
rust-lang:mainfrom
clubby789:opt-level-opt-none
Open

mir-transform: Treat optimize(none) the same as opt-level=0#160524
clubby789 wants to merge 1 commit into
rust-lang:mainfrom
clubby789:opt-level-opt-none

Conversation

@clubby789

@clubby789 clubby789 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

View all comments

cc @RalfJung

This is a quick initial sketch of a solution and the API will need some refining. The idea is that optimisations have a general condition under which they're enabled (i.e. some compiler option is enabled, we're not on some incompatible target) and a minimum MIR opt level. When we then decide whether or not to run a pass, we calculate an MIR opt level from, in order:

  • #[optimize(none)] (1 if present)
  • -Zmir-opt-level
  • -Copt-level (2 if opt-level >= 2, 1 otherwise)

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 4, 2026
@clubby789
clubby789 force-pushed the opt-level-opt-none branch from 354d5d9 to e4742bc Compare August 4, 2026 20:17
@clubby789

clubby789 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Typo error caused an ICE in drop elaboration

- optimizations.0 >= min_level
+ optimizations.0 < min_level

which seems a bit surprising

EDIT: Seems like some other opts rely on SimplifyCfg removing dead BBs. Not sure if we need to factor that into the pass handling? Probably fine to ignore in this PR

@rust-log-analyzer

This comment has been minimized.

@clubby789

Copy link
Copy Markdown
Contributor Author

Note that this now fails because

#[optimize(none)]
pub fn const_branch() -> i32 {
    if true { 1 } else { 0 }
}

produces return 1

Comment thread compiler/rustc_mir_transform/src/pass_manager.rs Outdated
Comment thread compiler/rustc_mir_transform/src/inline.rs Outdated
@RalfJung

RalfJung commented Aug 5, 2026

Copy link
Copy Markdown
Member

EDIT: Seems like some other opts rely on SimplifyCfg removing dead BBs. Not sure if we need to factor that into the pass handling? Probably fine to ignore in this PR

What do you mean by "rely" here and why does this become more of a problem with this PR?

@clubby789

Copy link
Copy Markdown
Contributor Author

What do you mean by "rely" here

I didn't look into it deeply, but a few passes seemed to ICE if they ran with dead BBs.

and why does this become more of a problem with this PR?

It doesn't; I just made a typo during implementation which exposed this and I was unsure if that was intentional/known.

@RalfJung

RalfJung commented Aug 7, 2026

Copy link
Copy Markdown
Member

It doesn't; I just made a typo during implementation which exposed this and I was unsure if that was intentional/known.

We'd have to ask the people that wrote the passes, so it'd help if you had a concrete example and backtrace. I presume this can be reproduced with -Zmir-enable-passes already on nightly?

@clubby789

Copy link
Copy Markdown
Contributor Author

The ICE is from a debug assertion; with the typo reverted, and debug-assertions enabled, the ICE is as follows (when building std) rustc-ice-2026-08-10T06_58_41-132370.txt

Minimised to a baseline rustc with debug assertions:

// rustc ice.rs -Zmir-enable-passes=-SimplifyCfg-initial,-SimplifyCfg-promote-consts,-SimplifyCfg-post-analysis
fn mir_drop<T>(_place: T) {
    panic!()
}

fn main() {
    mir_drop(());
}

Not sure if this is a problem at all in practice? If you think it is I can split out a new issue since it's not too relevant to this PR specifically

@RalfJung

RalfJung commented Aug 11, 2026 via email

Copy link
Copy Markdown
Member

@rust-log-analyzer

This comment has been minimized.

@clubby789

Copy link
Copy Markdown
Contributor Author

Refactored around this API:

impl PassPolicy {
    /// Create a [`PassPolicy::Optional`] that is not an optimization,
    /// enabled by default under the given condition.
    pub(crate) fn optional_non_optimization(enabled_by_default: bool) -> Self;

    /// Create a [`PassPolicy::Optional`] optimization enabled at the given MIR optimization level.
    pub(crate) fn optimization(ctx: &PassCtx<'_>, min_mir_opt_level: usize) -> Self;

    /// Add another condition to an optional pass's default enablement.
    pub(crate) fn and_enabled(self, enabled: bool) -> Self;

    /// Add a minimum mir-opt-level to an optional pass.
    /// Will panic if used on a required pass.
    fn with_min_mir_opt_level(self, ctx: &PassCtx<'_>, min_mir_opt_level: usize) -> Self ;

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@RalfJung

Copy link
Copy Markdown
Member

I am not sure that PassPolicy::optimization(ctx, 2) is really more readable than PassPolicy::optimization(ctx.mir_opt_level() >= 2). The meaning of the "2" becomes rather opaque.

@clubby789
clubby789 marked this pull request as ready for review August 18, 2026 00:49
@rustbot

rustbot commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in coverage instrumentation.

cc @Zalathar

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 18, 2026
@rustbot

rustbot commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, mir, mir-opt
  • compiler, mir, mir-opt expanded to 75 candidates
  • Random selection from 16 candidates

@clubby789

Copy link
Copy Markdown
Contributor Author

I dropped the optimization method, since it's generally easier to just have an optional constructor which can take a ctx.mir_opt_level() comparison. I also updated the opt-none test to something that is no longer optimised at level 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants