Skip to content

Lift a few limitations on ConstProp optimization pass #107856

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// NOTE: Unlike binops, negation doesn't have its own
// checked operation, just a comparison with the minimum
// value, so we have to check for the assert message.
if !bx.check_overflow() {
if let AssertKind::OverflowNeg(_) = *msg {
if !bx.cx().check_overflow() {
if let AssertKind::OverflowNeg(_)
| AssertKind::Overflow(
mir::BinOp::Add
| mir::BinOp::Sub
| mir::BinOp::Mul
| mir::BinOp::Shl
| mir::BinOp::Shr,
..,
) = *msg
{
const_cond = Some(expected);
}
}
Expand Down
437 changes: 153 additions & 284 deletions compiler/rustc_mir_transform/src/const_prop.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
StorageLive(_3); // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:18: +2:19
- _3 = _1; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:18: +2:19
- _4 = Eq(_3, const 0_i32); // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
- assert(!move _4, "attempt to divide `{}` by zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
+ _3 = const 0_i32; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:18: +2:19
+ _4 = const true; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
assert(!move _4, "attempt to divide `{}` by zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
+ assert(!const true, "attempt to divide `{}` by zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

bb0: {
StorageLive(_1); // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21
_1 = const _; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21
- _1 = const _; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21
- switchInt(move _1) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21
+ _1 = const false; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21
+ switchInt(const false) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21
}

Expand Down
6 changes: 2 additions & 4 deletions tests/mir-opt/const_prop/slice_len.main.ConstProp.32bit.diff
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@
StorageDead(_3); // scope 0 at $DIR/slice_len.rs:+1:18: +1:19
StorageLive(_6); // scope 0 at $DIR/slice_len.rs:+1:31: +1:32
_6 = const 1_usize; // scope 0 at $DIR/slice_len.rs:+1:31: +1:32
- _7 = Len((*_2)); // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
_7 = const 3_usize; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
- _8 = Lt(_6, _7); // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> bb1; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
+ _7 = const 3_usize; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
+ _8 = const true; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
+ assert(const true, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> bb1; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
}

bb1: {
- _1 = (*_2)[_6]; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
+ _1 = const 2_u32; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
_1 = (*_2)[_6]; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
StorageDead(_6); // scope 0 at $DIR/slice_len.rs:+1:33: +1:34
StorageDead(_4); // scope 0 at $DIR/slice_len.rs:+1:33: +1:34
StorageDead(_2); // scope 0 at $DIR/slice_len.rs:+1:33: +1:34
Expand Down
6 changes: 2 additions & 4 deletions tests/mir-opt/const_prop/slice_len.main.ConstProp.64bit.diff
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@
StorageDead(_3); // scope 0 at $DIR/slice_len.rs:+1:18: +1:19
StorageLive(_6); // scope 0 at $DIR/slice_len.rs:+1:31: +1:32
_6 = const 1_usize; // scope 0 at $DIR/slice_len.rs:+1:31: +1:32
- _7 = Len((*_2)); // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
_7 = const 3_usize; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
- _8 = Lt(_6, _7); // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> bb1; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
+ _7 = const 3_usize; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
+ _8 = const true; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
+ assert(const true, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> bb1; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
}

bb1: {
- _1 = (*_2)[_6]; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
+ _1 = const 2_u32; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
_1 = (*_2)[_6]; // scope 0 at $DIR/slice_len.rs:+1:5: +1:33
StorageDead(_6); // scope 0 at $DIR/slice_len.rs:+1:33: +1:34
StorageDead(_4); // scope 0 at $DIR/slice_len.rs:+1:33: +1:34
StorageDead(_2); // scope 0 at $DIR/slice_len.rs:+1:33: +1:34
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/slice_len.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// unit-test: ConstProp
// compile-flags: -Zmir-enable-passes=+InstCombine
// compile-flags: -Zmir-enable-passes=+InstCombine,+NormalizeArrayLen
// EMIT_MIR_FOR_EACH_BIT_WIDTH

// EMIT_MIR slice_len.main.ConstProp.diff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
bb0: {
_1 = const u8::MAX; // scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47
_2 = const 1_u8; // scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47
_5 = CheckedAdd(const u8::MAX, const 1_u8); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
assert(!move (_5.1: bool), "attempt to compute `{} + {}`, which would overflow", const u8::MAX, const 1_u8) -> bb1; // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
_5 = const (0_u8, true); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const u8::MAX, const 1_u8) -> bb1; // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
}

bb1: {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/consts/const-eval/issue-50814.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ struct Sum<A, B>(A, B);
impl<A: Unsigned, B: Unsigned> Unsigned for Sum<A, B> {
const MAX: u8 = A::MAX + B::MAX;
//~^ ERROR evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed
//~| ERROR evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed
}

fn foo<T>(_: T) -> &'static u8 {
&Sum::<U8, U8>::MAX
//~^ constant
//~| constant
}

fn main() {
Expand Down
24 changes: 21 additions & 3 deletions tests/ui/consts/const-eval/issue-50814.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,35 @@ LL | const MAX: u8 = A::MAX + B::MAX;
| ^^^^^^^^^^^^^^^ attempt to compute `u8::MAX + u8::MAX`, which would overflow

note: erroneous constant used
--> $DIR/issue-50814.rs:20:6
--> $DIR/issue-50814.rs:21:6
|
LL | &Sum::<U8, U8>::MAX
| ^^^^^^^^^^^^^^^^^^

note: erroneous constant used
--> $DIR/issue-50814.rs:21:5
|
LL | &Sum::<U8, U8>::MAX
| ^^^^^^^^^^^^^^^^^^^

error[E0080]: evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed
--> $DIR/issue-50814.rs:15:21
|
LL | const MAX: u8 = A::MAX + B::MAX;
| ^^^^^^^^^^^^^^^ attempt to compute `u8::MAX + u8::MAX`, which would overflow

note: erroneous constant used
--> $DIR/issue-50814.rs:21:6
|
LL | &Sum::<U8, U8>::MAX
| ^^^^^^^^^^^^^^^^^^

note: the above error was encountered while instantiating `fn foo::<i32>`
--> $DIR/issue-50814.rs:25:5
--> $DIR/issue-50814.rs:27:5
|
LL | foo(0);
| ^^^^^^

error: aborting due to previous error
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.