Skip to content

Commit a0111af

Browse files
committed
Auto merge of rust-lang#110824 - cjgillot:const-prop-index, r=JakobDegen,oli-obk
ConstProp into PlaceElem::Index. Noticed this while looking at keccak output MIR. This pass aims to replace `ProjectionElem::Index` with `ProjectionElem::ConstantIndex` during ConstProp. r? `@ghost`
2 parents ce04288 + a898851 commit a0111af

5 files changed

+26
-4
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

+18
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,24 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
806806
}
807807
}
808808

809+
fn process_projection_elem(
810+
&mut self,
811+
elem: PlaceElem<'tcx>,
812+
_: Location,
813+
) -> Option<PlaceElem<'tcx>> {
814+
if let PlaceElem::Index(local) = elem
815+
&& let Some(value) = self.get_const(local.into())
816+
&& self.should_const_prop(&value)
817+
&& let interpret::Operand::Immediate(interpret::Immediate::Scalar(scalar)) = *value
818+
&& let Ok(offset) = scalar.to_target_usize(&self.tcx)
819+
&& let Some(min_length) = offset.checked_add(1)
820+
{
821+
Some(PlaceElem::ConstantIndex { offset, min_length, from_end: false })
822+
} else {
823+
None
824+
}
825+
}
826+
809827
fn visit_assign(
810828
&mut self,
811829
place: &mut Place<'tcx>,

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.32bit.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
}
4646

4747
bb1: {
48-
_5 = (*_1)[_6]; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
48+
- _5 = (*_1)[_6]; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
49+
+ _5 = (*_1)[3 of 4]; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
4950
StorageDead(_6); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:25: +3:26
5051
_0 = const (); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+2:5: +4:6
5152
StorageDead(_5); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+4:5: +4:6

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.64bit.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
}
4646

4747
bb1: {
48-
_5 = (*_1)[_6]; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
48+
- _5 = (*_1)[_6]; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
49+
+ _5 = (*_1)[3 of 4]; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
4950
StorageDead(_6); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:25: +3:26
5051
_0 = const (); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+2:5: +4:6
5152
StorageDead(_5); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+4:5: +4:6

tests/mir-opt/const_prop/large_array_index.main.ConstProp.32bit.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828

2929
bb1: {
30-
_1 = _2[_3]; // scope 0 at $DIR/large_array_index.rs:+2:17: +2:32
30+
- _1 = _2[_3]; // scope 0 at $DIR/large_array_index.rs:+2:17: +2:32
31+
+ _1 = _2[2 of 3]; // scope 0 at $DIR/large_array_index.rs:+2:17: +2:32
3132
StorageDead(_3); // scope 0 at $DIR/large_array_index.rs:+2:32: +2:33
3233
StorageDead(_2); // scope 0 at $DIR/large_array_index.rs:+2:32: +2:33
3334
_0 = const (); // scope 0 at $DIR/large_array_index.rs:+0:11: +3:2

tests/mir-opt/const_prop/large_array_index.main.ConstProp.64bit.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828

2929
bb1: {
30-
_1 = _2[_3]; // scope 0 at $DIR/large_array_index.rs:+2:17: +2:32
30+
- _1 = _2[_3]; // scope 0 at $DIR/large_array_index.rs:+2:17: +2:32
31+
+ _1 = _2[2 of 3]; // scope 0 at $DIR/large_array_index.rs:+2:17: +2:32
3132
StorageDead(_3); // scope 0 at $DIR/large_array_index.rs:+2:32: +2:33
3233
StorageDead(_2); // scope 0 at $DIR/large_array_index.rs:+2:32: +2:33
3334
_0 = const (); // scope 0 at $DIR/large_array_index.rs:+0:11: +3:2

0 commit comments

Comments
 (0)