Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

precondition_check functions can survive MonoReachable #131578

Open
saethlin opened this issue Oct 12, 2024 · 0 comments
Open

precondition_check functions can survive MonoReachable #131578

saethlin opened this issue Oct 12, 2024 · 0 comments
Assignees
Labels
A-codegen Area: Code generation C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@saethlin
Copy link
Member

The motivating example is this: https://rust.godbolt.org/z/dGe6EMrEz, 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, #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 onlky 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.

@saethlin saethlin added A-codegen Area: Code generation C-bug Category: This is a bug. labels Oct 12, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 12, 2024
@saethlin saethlin self-assigned this Oct 12, 2024
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants