Skip to content

Commit 89e0576

Browse files
committed
Auto merge of #106340 - saethlin:propagate-operands, r=oli-obk
Always permit ConstProp to exploit arithmetic identities Fixes #72751 Initially, I thought I would need to enable operand propagation then do something else, but actually #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`
2 parents c54c8cb + 82f0973 commit 89e0576

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -655,11 +655,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
655655
return None;
656656
}
657657

658-
if self.tcx.sess.mir_opt_level() >= 4 {
659-
self.eval_rvalue_with_identities(rvalue, place)
660-
} else {
661-
self.use_ecx(|this| this.ecx.eval_rvalue_into_place(rvalue, place))
662-
}
658+
self.eval_rvalue_with_identities(rvalue, place)
663659
}
664660

665661
// Attempt to use algebraic identities to eliminate constant expressions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// MIR for `const_dividend` after PreCodegen
2+
3+
fn const_dividend(_1: i32) -> i32 {
4+
debug a => _1; // in scope 0 at $DIR/div_overflow.rs:+0:23: +0:24
5+
let mut _0: i32; // return place in scope 0 at $DIR/div_overflow.rs:+0:34: +0:37
6+
let mut _2: bool; // in scope 0 at $DIR/div_overflow.rs:+1:5: +1:12
7+
8+
bb0: {
9+
_2 = Eq(_1, const 0_i32); // scope 0 at $DIR/div_overflow.rs:+1:5: +1:12
10+
assert(!move _2, "attempt to divide `{}` by zero", const 256_i32) -> bb1; // scope 0 at $DIR/div_overflow.rs:+1:5: +1:12
11+
}
12+
13+
bb1: {
14+
_0 = Div(const 256_i32, move _1); // scope 0 at $DIR/div_overflow.rs:+1:5: +1:12
15+
return; // scope 0 at $DIR/div_overflow.rs:+2:2: +2:2
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// MIR for `const_divisor` after PreCodegen
2+
3+
fn const_divisor(_1: i32) -> i32 {
4+
debug a => _1; // in scope 0 at $DIR/div_overflow.rs:+0:22: +0:23
5+
let mut _0: i32; // return place in scope 0 at $DIR/div_overflow.rs:+0:33: +0:36
6+
7+
bb0: {
8+
_0 = Div(move _1, const 256_i32); // scope 0 at $DIR/div_overflow.rs:+1:5: +1:12
9+
return; // scope 0 at $DIR/div_overflow.rs:+2:2: +2:2
10+
}
11+
}

src/test/mir-opt/div_overflow.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// compile-flags: -Copt-level=0 -Coverflow-checks=yes
2+
3+
// Tests that division with a const does not emit a panicking branch for overflow
4+
5+
// EMIT_MIR div_overflow.const_divisor.PreCodegen.after.mir
6+
pub fn const_divisor(a: i32) -> i32 {
7+
a / 256
8+
}
9+
10+
// EMIT_MIR div_overflow.const_dividend.PreCodegen.after.mir
11+
pub fn const_dividend(a: i32) -> i32 {
12+
256 / a
13+
}
14+
15+
fn main() {
16+
const_divisor(123);
17+
const_dividend(123);
18+
}

0 commit comments

Comments
 (0)