Skip to content

Commit be9013f

Browse files
committed
Make overflow flag propagation conditional
1 parent 1dde908 commit be9013f

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_mir_dataflow::value_analysis::{
77
Map, ProjElem, State, ValueAnalysis, ValueOrPlace, ValueOrPlaceOrRef,
88
};
99
use rustc_mir_dataflow::{lattice::FlatSet, Analysis, ResultsVisitor, SwitchIntEdgeEffects};
10-
use rustc_span::DUMMY_SP;
10+
use rustc_span::{sym, DUMMY_SP};
1111

1212
use crate::MirPass;
1313

@@ -38,6 +38,7 @@ struct ConstAnalysis<'tcx> {
3838
tcx: TyCtxt<'tcx>,
3939
ecx: InterpCx<'tcx, 'tcx, DummyMachine>,
4040
param_env: ty::ParamEnv<'tcx>,
41+
propagate_overflow: bool,
4142
}
4243

4344
impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'tcx> {
@@ -72,7 +73,11 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'tcx> {
7273
});
7374

7475
if value_target.is_some() || overflow_target.is_some() {
75-
let (val, overflow) = self.binary_op(state, *op, left, right);
76+
let (val, mut overflow) = self.binary_op(state, *op, left, right);
77+
78+
if !self.propagate_overflow {
79+
overflow = FlatSet::Top;
80+
}
7681

7782
if let Some(value_target) = value_target {
7883
state.assign_idx(value_target, ValueOrPlaceOrRef::Value(val), self.map());
@@ -202,11 +207,20 @@ impl<'tcx> std::fmt::Debug for ScalarTy<'tcx> {
202207

203208
impl<'tcx> ConstAnalysis<'tcx> {
204209
pub fn new(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, map: Map) -> Self {
210+
// It can happen that overflow will be detected even though overflow checks are disabled.
211+
// This is caused by inlining functions that have #[rustc_inherit_overflow_checks]. Such
212+
// overflows must not be propagated if `-C overflow-checks=off`. Also, if the function we
213+
// are optimizing here has #[rustc_inherit_overflow_checks], the overflow checks may
214+
// actually not be triggered by the consuming crate, so we have to ignore them too.
215+
// Related to https://github.com/rust-lang/rust/issues/35310.
216+
let propagate_overflow = tcx.sess.overflow_checks()
217+
&& !tcx.has_attr(body.source.def_id(), sym::rustc_inherit_overflow_checks);
205218
Self {
206219
map,
207220
tcx,
208221
ecx: InterpCx::new(tcx, DUMMY_SP, ty::ParamEnv::empty(), DummyMachine),
209222
param_env: tcx.param_env(body.source.def_id()),
223+
propagate_overflow,
210224
}
211225
}
212226

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
- // MIR for `main` before DataflowConstProp
2+
+ // MIR for `main` after DataflowConstProp
3+
4+
fn main() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/inherit_overflow_checks_use.rs:+0:11: +0:11
6+
let mut _1: u8; // in scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:13: +3:47
7+
let mut _2: u8; // in scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:13: +3:47
8+
let mut _3: u8; // in scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:13: +3:47
9+
scope 1 {
10+
}
11+
scope 2 (inlined <u8 as Add>::add) { // at $DIR/inherit_overflow_checks_use.rs:7:13: 7:47
12+
debug self => _2; // in scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
13+
debug other => _3; // in scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
14+
let mut _4: u8; // in scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
15+
let mut _5: u8; // in scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
16+
let mut _6: (u8, bool); // in scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
17+
}
18+
19+
bb0: {
20+
StorageLive(_1); // scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:13: +3:47
21+
StorageLive(_2); // scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:13: +3:47
22+
_2 = const u8::MAX; // scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:13: +3:47
23+
StorageLive(_3); // scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:13: +3:47
24+
_3 = const 1_u8; // scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:13: +3:47
25+
StorageLive(_4); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
26+
_4 = const u8::MAX; // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
27+
StorageLive(_5); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
28+
_5 = const 1_u8; // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
29+
_6 = CheckedAdd(const u8::MAX, const 1_u8); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
30+
assert(!move (_6.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
31+
}
32+
33+
bb1: {
34+
- _1 = move (_6.0: u8); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
35+
+ _1 = const 0_u8; // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
36+
StorageDead(_5); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
37+
StorageDead(_4); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL
38+
StorageDead(_3); // scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:13: +3:47
39+
StorageDead(_2); // scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:13: +3:47
40+
StorageDead(_1); // scope 0 at $DIR/inherit_overflow_checks_use.rs:+3:47: +3:48
41+
nop; // scope 0 at $DIR/inherit_overflow_checks_use.rs:+0:11: +4:2
42+
return; // scope 0 at $DIR/inherit_overflow_checks_use.rs:+4:2: +4:2
43+
}
44+
}
45+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// compile-flags: -C overflow-checks=off
2+
3+
// EMIT_MIR inherit_overflow_checks_use.main.DataflowConstProp.diff
4+
fn main() {
5+
// After inlining, this will contain a `CheckedBinaryOp`. The overflow
6+
// must be ignored by the constant propagation to avoid triggering a panic.
7+
let _ = <u8 as std::ops::Add>::add(255, 1);
8+
}

0 commit comments

Comments
 (0)