rust-lang / rust-lang/rust

precondition_check functions can survive MonoReachable

Open
#131,578 0 comments 0 reactions 2 assignees View on GitHub

@cjgillot is already working on this.

Since Oct 14, 2025.

A-codegen C-bug C-optimization T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The motivating example is this: https://rust.godbolt.org/z/cYvn1bxWs, the IR of the function at the top which is IndexRange as SliceIndex<[T]>>::get_unchecked_mut contains a precondition check. The fact that the call is in a block without any predecessors does not make this unimportant, https://github.com/rust-lang/rust/pull/121421 is not working here.

The MIR for the function in question looks like this:

bb0: {   
    _3 = UbChecks();
    switchInt(copy _3) -> [0: bb3, otherwise: bb1];
}

bb1: {   
    <snip>
    _4 = <IndexRange as SliceIndex<[T]>>::get_unchecked_mut::precondition_check(move _5, move _6) -> [return: bb2, unwind unreachable];
}

bb2: {   
    <snip>
    goto -> bb4; 
}

bb3: {   
    <snip>
    switchInt(copy _3) -> [0: bb5, otherwise: bb4];
}

bb4: {   
    _10 = num::<impl usize>::unchecked_sub::precondition_check(copy _9, copy _7) -> [return: bb5, unwind unreachable];
}

bb5: {
    <snip>
    return;
}

The MIR optimization pipeline has helpfully hoisted the assignment of UbChecks() to a local then consulted that twice. But mono-reachable traversal only understands that the very specific pattern in bb0, and does not figure out that bb3 is also a switch on a constant that it could handle.

Post-mono GVN is really the way out of this. We'd love to have switchInt(const true) or switchInt(const false) in the MIR. Codegen could figure that out easily. I'm going to see what it takes to paper another hack over this instead, on the off-chance that it is highly effective and easy to do.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.