Skip to content

Commit f085e7a

Browse files
committed
Bless
1 parent 07f1631 commit f085e7a

11 files changed

+44
-90
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ macro_rules! throw_machine_stop_str {
5757
}};
5858
}
5959

60-
pub struct ConstProp;
60+
pub struct ConstProp {
61+
only_bools: bool,
62+
}
63+
impl ConstProp {
64+
pub fn new(only_bools: bool) -> Self {
65+
ConstProp { only_bools }
66+
}
67+
}
6168

6269
impl<'tcx> MirPass<'tcx> for ConstProp {
6370
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
@@ -149,7 +156,7 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
149156
// constants, instead of just checking for const-folding succeeding.
150157
// That would require a uniform one-def no-mutation analysis
151158
// and RPO (or recursing when needing the value of a local).
152-
let mut optimization_finder = ConstPropagator::new(body, dummy_body, tcx);
159+
let mut optimization_finder = ConstPropagator::new(body, dummy_body, tcx, self.only_bools);
153160
optimization_finder.visit_body(body);
154161

155162
trace!("ConstProp done for {:?}", def_id);
@@ -373,12 +380,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
373380
body: &Body<'tcx>,
374381
dummy_body: &'mir Body<'tcx>,
375382
tcx: TyCtxt<'tcx>,
383+
only_bools: bool,
376384
) -> ConstPropagator<'mir, 'tcx> {
377385
let def_id = body.source.def_id();
378386
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
379387
let param_env = tcx.param_env_reveal_all_normalized(def_id);
380388

381-
let can_const_prop = CanConstProp::check(tcx, param_env, body);
389+
let can_const_prop = CanConstProp::check(tcx, param_env, body, only_bools);
382390
let mut only_propagate_inside_block_locals = BitSet::new_empty(can_const_prop.len());
383391
for (l, mode) in can_const_prop.iter_enumerated() {
384392
if *mode == ConstPropMode::OnlyInsideOwnBlock {
@@ -858,6 +866,7 @@ impl CanConstProp {
858866
tcx: TyCtxt<'tcx>,
859867
param_env: ParamEnv<'tcx>,
860868
body: &Body<'tcx>,
869+
only_bools: bool,
861870
) -> IndexVec<Local, ConstPropMode> {
862871
let mut cpv = CanConstProp {
863872
can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls),
@@ -869,6 +878,12 @@ impl CanConstProp {
869878
};
870879
for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
871880
let ty = body.local_decls[local].ty;
881+
if only_bools {
882+
if ty != tcx.types.bool {
883+
*val = ConstPropMode::NoPropagation;
884+
continue;
885+
}
886+
}
872887
match tcx.layout_of(param_env.and(ty)) {
873888
Ok(layout) if layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) => {}
874889
// Either the layout fails to compute, then we can't use this local anyway

compiler/rustc_mir_transform/src/const_prop_lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
182182
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
183183
let param_env = tcx.param_env_reveal_all_normalized(def_id);
184184

185-
let can_const_prop = CanConstProp::check(tcx, param_env, body);
185+
let can_const_prop = CanConstProp::check(tcx, param_env, body, false);
186186
let mut only_propagate_inside_block_locals = BitSet::new_empty(can_const_prop.len());
187187
for (l, mode) in can_const_prop.iter_enumerated() {
188188
if *mode == ConstPropMode::OnlyInsideOwnBlock {

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -
410410
pm::run_passes(
411411
tcx,
412412
&mut body,
413-
&[&const_prop::ConstProp],
413+
&[&const_prop::ConstProp::new(false)],
414414
Some(MirPhase::Runtime(RuntimePhase::Optimized)),
415415
);
416416
}
@@ -555,7 +555,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
555555
&unreachable_prop::UnreachablePropagation,
556556
&uninhabited_enum_branching::UninhabitedEnumBranching,
557557
&o1(simplify::SimplifyCfg::new("after-uninhabited-enum-branching")),
558-
&const_prop::ConstProp,
558+
&const_prop::ConstProp::new(true),
559559
&const_debuginfo::ConstDebugInfo,
560560
&o1(simplify_branches::SimplifyConstCondition::new("before-inline")),
561561
&o1(simplify::SimplifyCfg::new("before-inline")),
@@ -572,7 +572,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
572572
&separate_const_switch::SeparateConstSwitch,
573573
//
574574
// FIXME(#70073): This pass is responsible for both optimization as well as some lints.
575-
&const_prop::ConstProp,
575+
&const_prop::ConstProp::new(false),
576576
&dataflow_const_prop::DataflowConstProp,
577577
//
578578
// Const-prop runs unconditionally, but doesn't mutate the MIR at mir-opt-level=0.

src/test/mir-opt/const_debuginfo.main.ConstDebugInfo.diff

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@
1111
let mut _14: u32; // in scope 0 at $DIR/const_debuginfo.rs:+13:13: +13:16
1212
let mut _15: u32; // in scope 0 at $DIR/const_debuginfo.rs:+13:19: +13:22
1313
scope 1 {
14-
- debug x => _1; // in scope 1 at $DIR/const_debuginfo.rs:+1:9: +1:10
15-
+ debug x => const 1_u8; // in scope 1 at $DIR/const_debuginfo.rs:+1:9: +1:10
14+
debug x => const 1_u8; // in scope 1 at $DIR/const_debuginfo.rs:+1:9: +1:10
1615
let _2: u8; // in scope 1 at $DIR/const_debuginfo.rs:+2:9: +2:10
1716
scope 2 {
18-
- debug y => _2; // in scope 2 at $DIR/const_debuginfo.rs:+2:9: +2:10
19-
+ debug y => const 2_u8; // in scope 2 at $DIR/const_debuginfo.rs:+2:9: +2:10
17+
debug y => const 2_u8; // in scope 2 at $DIR/const_debuginfo.rs:+2:9: +2:10
2018
let _3: u8; // in scope 2 at $DIR/const_debuginfo.rs:+3:9: +3:10
2119
scope 3 {
22-
- debug z => _3; // in scope 3 at $DIR/const_debuginfo.rs:+3:9: +3:10
23-
+ debug z => const 3_u8; // in scope 3 at $DIR/const_debuginfo.rs:+3:9: +3:10
20+
debug z => const 3_u8; // in scope 3 at $DIR/const_debuginfo.rs:+3:9: +3:10
2421
let _4: u8; // in scope 3 at $DIR/const_debuginfo.rs:+4:9: +4:12
2522
scope 4 {
2623
- debug sum => _4; // in scope 4 at $DIR/const_debuginfo.rs:+4:9: +4:12
2724
+ debug sum => const 6_u8; // in scope 4 at $DIR/const_debuginfo.rs:+4:9: +4:12
2825
let _9: &str; // in scope 4 at $DIR/const_debuginfo.rs:+6:9: +6:10
2926
scope 5 {
30-
- debug s => _9; // in scope 5 at $DIR/const_debuginfo.rs:+6:9: +6:10
31-
+ debug s => const "hello, world!"; // in scope 5 at $DIR/const_debuginfo.rs:+6:9: +6:10
27+
debug s => const "hello, world!"; // in scope 5 at $DIR/const_debuginfo.rs:+6:9: +6:10
3228
let _10: (bool, bool, u32); // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
3329
let _16: bool; // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
3430
let _17: bool; // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10

src/test/mir-opt/const_prop/bad_op_div_by_zero.main.ConstProp.diff

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@
3131

3232
bb1: {
3333
- _5 = Eq(_3, const -1_i32); // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
34-
- _6 = Eq(const 1_i32, const i32::MIN); // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
34+
+ _5 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
35+
_6 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
3536
- _7 = BitAnd(move _5, move _6); // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
3637
- assert(!move _7, "attempt to compute `{} / {}`, which would overflow", const 1_i32, _3) -> bb2; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
37-
+ _5 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
38-
+ _6 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
3938
+ _7 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
4039
+ assert(!const false, "attempt to compute `{} / {}`, which would overflow", const 1_i32, _3) -> bb2; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19
4140
}

src/test/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,9 @@
3232

3333
bb1: {
3434
- _5 = Eq(_3, const -1_i32); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
35-
- _6 = Eq(const 1_i32, const i32::MIN); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
36-
- _7 = BitAnd(move _5, move _6); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
37-
- assert(!move _7, "attempt to compute the remainder of `{} % {}`, which would overflow", const 1_i32, _3) -> bb2; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
3835
+ _5 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
39-
+ _6 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
40-
+ _7 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
41-
+ assert(!const false, "attempt to compute the remainder of `{} % {}`, which would overflow", const 1_i32, const 0_i32) -> bb2; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
42-
}
43-
44-
bb2: {
36+
_6 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
37+
_7 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
4538
- _2 = Rem(const 1_i32, move _3); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
4639
+ _2 = Rem(const 1_i32, const 0_i32); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19
4740
StorageDead(_3); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:18: +2:19

src/test/mir-opt/const_prop/boolean_identities.test.ConstProp.diff

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414
StorageLive(_3); // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:15
1515
StorageLive(_4); // scope 0 at $DIR/boolean_identities.rs:+1:6: +1:7
1616
_4 = _2; // scope 0 at $DIR/boolean_identities.rs:+1:6: +1:7
17-
- _3 = BitOr(move _4, const true); // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:15
18-
+ _3 = const true; // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:15
17+
_3 = const true; // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:15
1918
StorageDead(_4); // scope 0 at $DIR/boolean_identities.rs:+1:14: +1:15
2019
StorageLive(_5); // scope 0 at $DIR/boolean_identities.rs:+1:18: +1:29
2120
StorageLive(_6); // scope 0 at $DIR/boolean_identities.rs:+1:19: +1:20
2221
_6 = _1; // scope 0 at $DIR/boolean_identities.rs:+1:19: +1:20
23-
- _5 = BitAnd(move _6, const false); // scope 0 at $DIR/boolean_identities.rs:+1:18: +1:29
24-
+ _5 = const false; // scope 0 at $DIR/boolean_identities.rs:+1:18: +1:29
22+
_5 = const false; // scope 0 at $DIR/boolean_identities.rs:+1:18: +1:29
2523
StorageDead(_6); // scope 0 at $DIR/boolean_identities.rs:+1:28: +1:29
26-
- _0 = BitAnd(move _3, move _5); // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:29
27-
+ _0 = const false; // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:29
24+
_0 = const false; // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:29
2825
StorageDead(_5); // scope 0 at $DIR/boolean_identities.rs:+1:28: +1:29
2926
StorageDead(_3); // scope 0 at $DIR/boolean_identities.rs:+1:28: +1:29
3027
return; // scope 0 at $DIR/boolean_identities.rs:+2:2: +2:2

src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,6 @@
99
bb0: {
1010
StorageLive(_1); // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21
1111
_1 = const _; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21
12-
- switchInt(move _1) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21
13-
+ switchInt(const false) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21
14-
}
15-
16-
bb1: {
17-
StorageLive(_2); // scope 0 at $SRC_DIR/std/src/panic.rs:LL:COL
18-
_2 = begin_panic::<&str>(const "explicit panic"); // scope 0 at $SRC_DIR/std/src/panic.rs:LL:COL
19-
// mir::Constant
20-
// + span: $SRC_DIR/std/src/panic.rs:LL:COL
21-
// + literal: Const { ty: fn(&str) -> ! {begin_panic::<&str>}, val: Value(<ZST>) }
22-
// mir::Constant
23-
// + span: $SRC_DIR/std/src/panic.rs:LL:COL
24-
// + literal: Const { ty: &str, val: Value(Slice(..)) }
25-
}
26-
27-
bb2: {
2812
nop; // scope 0 at $DIR/control_flow_simplification.rs:+3:6: +3:6
2913
StorageDead(_1); // scope 0 at $DIR/control_flow_simplification.rs:+3:5: +3:6
3014
return; // scope 0 at $DIR/control_flow_simplification.rs:+4:2: +4:2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//EMIT_MIR dead_code_does_not_inline.main.Inline.after.mir
2+
fn main() {
3+
if false {
4+
callee();
5+
}
6+
}
7+
8+
fn callee() {
9+
println!("Wooooo I'm invisible");
10+
}

src/test/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,7 @@
99
bb0: {
1010
StorageLive(_1); // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13
1111
_1 = const false; // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13
12-
- switchInt(const false) -> [0: bb3, otherwise: bb1]; // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13
13-
+ goto -> bb3; // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13
14-
}
15-
16-
bb1: {
17-
StorageLive(_2); // scope 0 at $DIR/simplify_if.rs:+2:9: +2:15
18-
_2 = noop() -> bb2; // scope 0 at $DIR/simplify_if.rs:+2:9: +2:15
19-
// mir::Constant
20-
// + span: $DIR/simplify_if.rs:7:9: 7:13
21-
// + literal: Const { ty: fn() {noop}, val: Value(<ZST>) }
22-
}
23-
24-
bb2: {
25-
StorageDead(_2); // scope 0 at $DIR/simplify_if.rs:+2:15: +2:16
26-
nop; // scope 0 at $DIR/simplify_if.rs:+1:14: +3:6
27-
goto -> bb4; // scope 0 at $DIR/simplify_if.rs:+1:5: +3:6
28-
}
29-
30-
bb3: {
3112
nop; // scope 0 at $DIR/simplify_if.rs:+3:6: +3:6
32-
goto -> bb4; // scope 0 at $DIR/simplify_if.rs:+1:5: +3:6
33-
}
34-
35-
bb4: {
3613
StorageDead(_1); // scope 0 at $DIR/simplify_if.rs:+3:5: +3:6
3714
return; // scope 0 at $DIR/simplify_if.rs:+4:2: +4:2
3815
}

0 commit comments

Comments
 (0)