Skip to content

Commit 935e283

Browse files
committed
Compute unsizing casts in GVN.
1 parent 4836ce1 commit 935e283

File tree

7 files changed

+33
-21
lines changed

7 files changed

+33
-21
lines changed

compiler/rustc_const_eval/src/interpret/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
416416
}
417417
}
418418

419-
fn unsize_into(
419+
pub fn unsize_into(
420420
&mut self,
421421
src: &OpTy<'tcx, M::Provenance>,
422422
cast_ty: TyAndLayout<'tcx>,

compiler/rustc_mir_transform/src/gvn.rs

+10
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,16 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
553553
}
554554
value.offset(Size::ZERO, to, &self.ecx).ok()?
555555
}
556+
CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize) => {
557+
let src = self.evaluated[value].as_ref()?;
558+
let to = self.ecx.layout_of(to).ok()?;
559+
let dest = self.ecx.allocate(to, MemoryKind::Stack).ok()?;
560+
self.ecx.unsize_into(src, to, &dest.clone().into()).ok()?;
561+
self.ecx
562+
.alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
563+
.ok()?;
564+
dest.into()
565+
}
556566
_ => return None,
557567
},
558568
};

tests/mir-opt/const_prop/slice_len.main.GVN.32bit.panic-abort.diff

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@
3131
StorageDead(_3);
3232
StorageLive(_6);
3333
_6 = const 1_usize;
34-
_7 = Len((*_2));
34+
- _7 = Len((*_2));
3535
- _8 = Lt(_6, _7);
3636
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind unreachable];
37-
+ _8 = Lt(const 1_usize, _7);
38-
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 1_usize) -> [success: bb1, unwind unreachable];
37+
+ _7 = const 3_usize;
38+
+ _8 = const true;
39+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind unreachable];
3940
}
4041

4142
bb1: {
4243
- _1 = (*_2)[_6];
43-
+ _1 = (*_2)[1 of 2];
44+
+ _1 = const 2_u32;
4445
StorageDead(_6);
4546
StorageDead(_4);
4647
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.main.GVN.32bit.panic-unwind.diff

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@
3131
StorageDead(_3);
3232
StorageLive(_6);
3333
_6 = const 1_usize;
34-
_7 = Len((*_2));
34+
- _7 = Len((*_2));
3535
- _8 = Lt(_6, _7);
3636
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind continue];
37-
+ _8 = Lt(const 1_usize, _7);
38-
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 1_usize) -> [success: bb1, unwind continue];
37+
+ _7 = const 3_usize;
38+
+ _8 = const true;
39+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind continue];
3940
}
4041

4142
bb1: {
4243
- _1 = (*_2)[_6];
43-
+ _1 = (*_2)[1 of 2];
44+
+ _1 = const 2_u32;
4445
StorageDead(_6);
4546
StorageDead(_4);
4647
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.main.GVN.64bit.panic-abort.diff

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@
3131
StorageDead(_3);
3232
StorageLive(_6);
3333
_6 = const 1_usize;
34-
_7 = Len((*_2));
34+
- _7 = Len((*_2));
3535
- _8 = Lt(_6, _7);
3636
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind unreachable];
37-
+ _8 = Lt(const 1_usize, _7);
38-
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 1_usize) -> [success: bb1, unwind unreachable];
37+
+ _7 = const 3_usize;
38+
+ _8 = const true;
39+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind unreachable];
3940
}
4041

4142
bb1: {
4243
- _1 = (*_2)[_6];
43-
+ _1 = (*_2)[1 of 2];
44+
+ _1 = const 2_u32;
4445
StorageDead(_6);
4546
StorageDead(_4);
4647
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.main.GVN.64bit.panic-unwind.diff

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@
3131
StorageDead(_3);
3232
StorageLive(_6);
3333
_6 = const 1_usize;
34-
_7 = Len((*_2));
34+
- _7 = Len((*_2));
3535
- _8 = Lt(_6, _7);
3636
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind continue];
37-
+ _8 = Lt(const 1_usize, _7);
38-
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 1_usize) -> [success: bb1, unwind continue];
37+
+ _7 = const 3_usize;
38+
+ _8 = const true;
39+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind continue];
3940
}
4041

4142
bb1: {
4243
- _1 = (*_2)[_6];
43-
+ _1 = (*_2)[1 of 2];
44+
+ _1 = const 2_u32;
4445
StorageDead(_6);
4546
StorageDead(_4);
4647
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ fn main() {
88
// CHECK-LABEL: fn main(
99
// CHECK: debug a => [[a:_.*]];
1010
// CHECK: [[slice:_.*]] = const {{.*}} as &[u32] (PointerCoercion(Unsize));
11-
// FIXME(cjgillot) simplify Len and projection into unsized slice.
12-
// CHECK-NOT: assert(const true,
13-
// CHECK: [[a]] = (*[[slice]])[1 of 2];
14-
// CHECK-NOT: [[a]] = const 2_u32;
11+
// CHECK: assert(const true,
12+
// CHECK: [[a]] = const 2_u32;
1513
let a = (&[1u32, 2, 3] as &[u32])[1];
1614
}

0 commit comments

Comments
 (0)