Skip to content

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

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

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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

---- [mir-opt] tests/mir-opt/optimize_none.rs#NO-OPT stdout ----
------FileCheck stdout------------------------------

------FileCheck stderr------------------------------
/checkout/tests/mir-opt/optimize_none.rs:18:12: error: CHECK: expected string not found in input
 // CHECK: [[BOOL:_[0-9]+]] = const true;
           ^
/checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.NO-OPT/optimize_none.mir:19:17: note: scanning from here
fn const_branch() -> i32 {
                ^
/checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.NO-OPT/optimize_none.mir:23:2: note: possible intended match here
 _0 = const 1_i32;
 ^

Input file: /checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.NO-OPT/optimize_none.mir
Check file: /checkout/tests/mir-opt/optimize_none.rs

-dump-input=help explains the following input dump.

Input was:
<<<<<<
            1: // WARNING: This output format is intended for human consumers only 
            2: // and is subject to change without notice. Knock yourself out. 
            3: // HINT: See also -Z dump-mir for MIR at specific points during compilation. 
            4: fn add_noopt() -> i32 { 
            5:  let mut _0: i32; 
            6:  let mut _1: (i32, bool); 
            7:  
            8:  bb0: { 
            9:  _1 = AddWithOverflow(const 1_i32, const 2_i32); 
           10:  assert(!move (_1.1: bool), "attempt to compute `{} + {}`, which would overflow", const 1_i32, const 2_i32) -> [success: bb1, unwind continue]; 
           11:  } 
           12:  
           13:  bb1: { 
           14:  _0 = move (_1.0: i32); 
           15:  return; 
           16:  } 
           17: } 
           18:  
           19: fn const_branch() -> i32 { 
check:18'0                     X~~~~~~~~~~ error: no match found
           20:  let mut _0: i32; 
check:18'0     ~~~~~~~~~~~~~~~~~~
           21:  
check:18'0     ~
           22:  bb0: { 
check:18'0     ~~~~~~~~
           23:  _0 = const 1_i32; 
check:18'0     ~~~~~~~~~~~~~~~~~~~
check:18'1      ?                  possible intended match
           24:  return; 
check:18'0     ~~~~~~~~~
           25:  } 
check:18'0     ~~~
           26: } 
check:18'0     ~~
           27:  
check:18'0     ~
           28: fn main() -> () { 
check:18'0     ~~~~~~~~~~~~~~~~~~
           29:  let mut _0: (); 
---
>>>>>>

------------------------------------------

error in revision `NO-OPT`: verification with 'FileCheck' failed
status: exit status: 1
command: "/usr/lib/llvm-21/bin/FileCheck" "--input-file" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.NO-OPT/optimize_none.mir" "/checkout/tests/mir-opt/optimize_none.rs" "--check-prefix=CHECK" "--check-prefix" "NO-OPT" "--allow-unused-prefixes" "--dump-input-context" "100"
stdout: none
--- stderr -------------------------------
/checkout/tests/mir-opt/optimize_none.rs:18:12: error: CHECK: expected string not found in input
 // CHECK: [[BOOL:_[0-9]+]] = const true;
           ^
/checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.NO-OPT/optimize_none.mir:19:17: note: scanning from here
fn const_branch() -> i32 {
                ^
/checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.NO-OPT/optimize_none.mir:23:2: note: possible intended match here
 _0 = const 1_i32;
 ^

Input file: /checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.NO-OPT/optimize_none.mir
Check file: /checkout/tests/mir-opt/optimize_none.rs

-dump-input=help explains the following input dump.

Input was:
<<<<<<
            1: // WARNING: This output format is intended for human consumers only 
            2: // and is subject to change without notice. Knock yourself out. 
            3: // HINT: See also -Z dump-mir for MIR at specific points during compilation. 
            4: fn add_noopt() -> i32 { 
            5:  let mut _0: i32; 
            6:  let mut _1: (i32, bool); 
            7:  
            8:  bb0: { 
            9:  _1 = AddWithOverflow(const 1_i32, const 2_i32); 
           10:  assert(!move (_1.1: bool), "attempt to compute `{} + {}`, which would overflow", const 1_i32, const 2_i32) -> [success: bb1, unwind continue]; 
           11:  } 
           12:  
           13:  bb1: { 
           14:  _0 = move (_1.0: i32); 
           15:  return; 
           16:  } 
           17: } 
           18:  
           19: fn const_branch() -> i32 { 
check:18'0                     X~~~~~~~~~~ error: no match found
           20:  let mut _0: i32; 
check:18'0     ~~~~~~~~~~~~~~~~~~
           21:  
check:18'0     ~
           22:  bb0: { 
check:18'0     ~~~~~~~~
           23:  _0 = const 1_i32; 
check:18'0     ~~~~~~~~~~~~~~~~~~~
check:18'1      ?                  possible intended match
           24:  return; 
check:18'0     ~~~~~~~~~
           25:  } 
check:18'0     ~~~
           26: } 
check:18'0     ~~
           27:  
check:18'0     ~
           28: fn main() -> () { 
check:18'0     ~~~~~~~~~~~~~~~~~~
           29:  let mut _0: (); 
---
---- [mir-opt] tests/mir-opt/optimize_none.rs#NO-OPT stdout end ----
---- [mir-opt] tests/mir-opt/optimize_none.rs#SPEED-OPT stdout ----
------FileCheck stdout------------------------------

------FileCheck stderr------------------------------
/checkout/tests/mir-opt/optimize_none.rs:18:12: error: CHECK: expected string not found in input
 // CHECK: [[BOOL:_[0-9]+]] = const true;
           ^
/checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.SPEED-OPT/optimize_none.mir:19:17: note: scanning from here
fn const_branch() -> i32 {
                ^
/checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.SPEED-OPT/optimize_none.mir:23:2: note: possible intended match here
 _0 = const 1_i32;
 ^

Input file: /checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.SPEED-OPT/optimize_none.mir
Check file: /checkout/tests/mir-opt/optimize_none.rs

-dump-input=help explains the following input dump.

Input was:
<<<<<<
            1: // WARNING: This output format is intended for human consumers only 
            2: // and is subject to change without notice. Knock yourself out. 
            3: // HINT: See also -Z dump-mir for MIR at specific points during compilation. 
            4: fn add_noopt() -> i32 { 
            5:  let mut _0: i32; 
            6:  let mut _1: (i32, bool); 
            7:  
            8:  bb0: { 
            9:  _1 = AddWithOverflow(const 1_i32, const 2_i32); 
           10:  assert(!move (_1.1: bool), "attempt to compute `{} + {}`, which would overflow", const 1_i32, const 2_i32) -> [success: bb1, unwind continue]; 
           11:  } 
           12:  
           13:  bb1: { 
           14:  _0 = move (_1.0: i32); 
           15:  return; 
           16:  } 
           17: } 
           18:  
           19: fn const_branch() -> i32 { 
check:18'0                     X~~~~~~~~~~ error: no match found
           20:  let mut _0: i32; 
check:18'0     ~~~~~~~~~~~~~~~~~~
           21:  
check:18'0     ~
           22:  bb0: { 
check:18'0     ~~~~~~~~
           23:  _0 = const 1_i32; 
check:18'0     ~~~~~~~~~~~~~~~~~~~
check:18'1      ?                  possible intended match
           24:  return; 
check:18'0     ~~~~~~~~~
           25:  } 
check:18'0     ~~~
           26: } 
check:18'0     ~~
           27:  
check:18'0     ~
           28: fn main() -> () { 
check:18'0     ~~~~~~~~~~~~~~~~~~
           29:  let mut _0: (); 
---
>>>>>>

------------------------------------------

error in revision `SPEED-OPT`: verification with 'FileCheck' failed
status: exit status: 1
command: "/usr/lib/llvm-21/bin/FileCheck" "--input-file" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.SPEED-OPT/optimize_none.mir" "/checkout/tests/mir-opt/optimize_none.rs" "--check-prefix=CHECK" "--check-prefix" "SPEED-OPT" "--allow-unused-prefixes" "--dump-input-context" "100"
stdout: none
--- stderr -------------------------------
/checkout/tests/mir-opt/optimize_none.rs:18:12: error: CHECK: expected string not found in input
 // CHECK: [[BOOL:_[0-9]+]] = const true;
           ^
/checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.SPEED-OPT/optimize_none.mir:19:17: note: scanning from here
fn const_branch() -> i32 {
                ^
/checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.SPEED-OPT/optimize_none.mir:23:2: note: possible intended match here
 _0 = const 1_i32;
 ^

Input file: /checkout/obj/build/aarch64-unknown-linux-gnu/test/mir-opt/optimize_none.SPEED-OPT/optimize_none.mir
Check file: /checkout/tests/mir-opt/optimize_none.rs

-dump-input=help explains the following input dump.

Input was:
<<<<<<
            1: // WARNING: This output format is intended for human consumers only 
            2: // and is subject to change without notice. Knock yourself out. 
            3: // HINT: See also -Z dump-mir for MIR at specific points during compilation. 
            4: fn add_noopt() -> i32 { 
            5:  let mut _0: i32; 
            6:  let mut _1: (i32, bool); 
            7:  
            8:  bb0: { 
            9:  _1 = AddWithOverflow(const 1_i32, const 2_i32); 
           10:  assert(!move (_1.1: bool), "attempt to compute `{} + {}`, which would overflow", const 1_i32, const 2_i32) -> [success: bb1, unwind continue]; 
           11:  } 
           12:  
           13:  bb1: { 
           14:  _0 = move (_1.0: i32); 
           15:  return; 
           16:  } 
           17: } 
           18:  
           19: fn const_branch() -> i32 { 
check:18'0                     X~~~~~~~~~~ error: no match found
           20:  let mut _0: i32; 
check:18'0     ~~~~~~~~~~~~~~~~~~
           21:  
check:18'0     ~
           22:  bb0: { 
check:18'0     ~~~~~~~~
           23:  _0 = const 1_i32; 
check:18'0     ~~~~~~~~~~~~~~~~~~~
check:18'1      ?                  possible intended match
           24:  return; 
check:18'0     ~~~~~~~~~
           25:  } 
check:18'0     ~~~
           26: } 
check:18'0     ~~
           27:  
check:18'0     ~
           28: fn main() -> () { 
check:18'0     ~~~~~~~~~~~~~~~~~~
           29:  let mut _0: (); 

@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

/// e.g. if it just adds extra debug checks that one can turn off.
optimization: bool,
/// If this is an optimization pass, define the minimum optimization level to enable
/// the pass.

@RalfJung RalfJung Aug 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// the pass.
/// the pass. If this is `Some(n)`, the pass only runs if `min_optimization_level` is true *and*
/// mir-opt-level is at least n.

View changes since the review

fn policy(&self, sess: &rustc_session::Session) -> PassPolicy {
// If `inline_mir` is specified, return that.
// Otherwise, enable when either:
// - mir-opt-level == 2 && opt-level >= 2 && !incremental, OR

@RalfJung RalfJung Aug 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was about to say, I am not sure that we can represent all intended policies with a new field in PassPolicy. The alternative is to replace the sess argument by something that provides the current mir-opt-level (and forwards everything else to Session).

I am also quite surprised that this checks both mir-opt-level and opt-level. @wesleywiser @saethlin do you know why?

View changes since the review

@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?

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants