-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Suboptimal debug codegen for integer division with a constant rhs #72751
Comments
This commit moves over more array accesses to the `i!` macro to avoid bounds checks when debug assertions are disabled. This is surfaced from rust-lang/compiler-builtins#360 where recent changes in codegen units has caused some bounds checks to not get elided in release mode. This also adds a `div!` macro to work around rust-lang/rust#72751.
This commit moves over more array accesses to the `i!` macro to avoid bounds checks when debug assertions are disabled. This is surfaced from rust-lang/compiler-builtins#360 where recent changes in codegen units has caused some bounds checks to not get elided in release mode. This also adds a `div!` macro to work around rust-lang/rust#72751.
This commit moves over more array accesses to the `i!` macro to avoid bounds checks when debug assertions are disabled. This is surfaced from rust-lang/compiler-builtins#360 where recent changes in codegen units has caused some bounds checks to not get elided in release mode. This also adds a `div!` macro to work around rust-lang/rust#72751.
* Use macros for more division/array checks This commit moves over more array accesses to the `i!` macro to avoid bounds checks when debug assertions are disabled. This is surfaced from rust-lang/compiler-builtins#360 where recent changes in codegen units has caused some bounds checks to not get elided in release mode. This also adds a `div!` macro to work around rust-lang/rust#72751. * Don't test/bench our shim crate It's not intended to run all our tests
I assume the task here is teaching the MIR optimizer to constant fold |
fixed by #74491 |
Should this be closed then? |
This doesn't appear to be fixed by default: https://rust.godbolt.org/z/K8zx5nrYW |
You need to turn on optimizations with https://rust.godbolt.org/z/hfPY1T7x4
|
My bad, I forgot this was about debug builds, it seems like we aren't performing enough mir optimizations in the debug context? |
FWIW, pub fn foo(x: bool) -> bool {
false && x
} generates this MIR in debug mode: // WARNING: This output format is intended for human consumers only
// and is subject to change without notice. Knock yourself out.
fn foo(_1: bool) -> bool {
debug x => _1; // in scope 0 at src/lib.rs:1:12: 1:13
let mut _0: bool; // return place in scope 0 at src/lib.rs:1:24: 1:28
bb0: {
_0 = const false; // scope 0 at src/lib.rs:2:5: 2:15
return; // scope 0 at src/lib.rs:3:2: 3:2
}
} So it seems that in simple cases the compiler is smart enough. |
I'm looking into this |
Always permit ConstProp to exploit arithmetic identities Fixes rust-lang/rust#72751 Initially, I thought I would need to enable operand propagation then do something else, but actually rust-lang/rust#74491 already has the fix for the issue in question! It looks like this optimization was put under MIR opt level 3 due to possible soundness/stability implications, then demoted further to MIR opt level 4 when MIR opt level 2 became associated with `--release`. Perhaps in the past we were doing CTFE on optimized MIR? We aren't anymore, so this optimization has no stability implications. r? `@oli-obk`
We are causing this issue to occur again: #109900 (comment) I was unable to find a motivation for fixing this issue beyond "nice to have if easily done". We ususally don't put a lot of effort into avoiding codegen of dead code (like the panic path here). Thus I am not reopening this issue. If you have a motivating use case, please don't hesistate to inform us of it and either open a new issue or comment here to ask for this one to get reopened. |
Codegen for division in general needs to generate code to panic if the division will panic (e.g. divide by zero or integer overflow). In the case of constants, however, it looks like rustc almost optimizes debug codegen but not quite. For example this code:
generates this IR in debug mode:
It looks like rustc is "smart enough" to generate
%_5 = and i1 false, %_4, !dbg !14
to realize the rhs is not negative one and it's not zero, but it's not quite smart enough to know thatfalse && x
is alwaysfalse
, so it still generates a branch to panic.The text was updated successfully, but these errors were encountered: