Skip to content

Commit 23a1ebb

Browse files
committed
Remove the AllocId from ByRef values
`ByRef` const values have no identity beyond their value, we should not treat them as having identity. The `AllocId` often differed between equal constants, because of the way that the miri-engine evaluates constants.
1 parent e951d8e commit 23a1ebb

File tree

9 files changed

+36
-18
lines changed

9 files changed

+36
-18
lines changed

src/librustc/mir/interpret/value.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ pub enum ConstValue<'tcx> {
4343
end: usize,
4444
},
4545

46-
/// An allocation together with a pointer into the allocation.
47-
/// Invariant: the pointer's `AllocId` resolves to the allocation.
46+
/// An allocation together with an offset into the allocation.
4847
/// The alignment exists to allow `const_field` to have `ByRef` access to nonprimitive fields
4948
/// of `repr(packed)` structs. The alignment may be lower than the type of this constant.
5049
/// This permits reads with lower alignment than what the type would normally require.
5150
/// FIXME(RalfJ,oli-obk): The alignment checks are part of miri, but const eval doesn't really
5251
/// need them. Disabling them may be too hard though.
53-
ByRef(Pointer, Align, &'tcx Allocation),
52+
ByRef(Size, Align, &'tcx Allocation),
5453

5554
/// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
5655
/// variants when the code is monomorphic enough for that.

src/librustc/ty/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
13351335
impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> {
13361336
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
13371337
match *self {
1338-
ConstValue::ByRef(ptr, align, alloc) => ConstValue::ByRef(ptr, align, alloc),
1338+
ConstValue::ByRef(offset, align, alloc) => ConstValue::ByRef(offset, align, alloc),
13391339
ConstValue::Infer(ic) => ConstValue::Infer(ic.fold_with(folder)),
13401340
ConstValue::Param(p) => ConstValue::Param(p.fold_with(folder)),
13411341
ConstValue::Placeholder(p) => ConstValue::Placeholder(p),

src/librustc_codegen_llvm/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn codegen_static_initializer(
7171
let static_ = cx.tcx.const_eval(param_env.and(cid))?;
7272

7373
let alloc = match static_.val {
74-
ConstValue::ByRef(ptr, align, alloc) if ptr.offset.bytes() == 0 && align == alloc.align => {
74+
ConstValue::ByRef(offset, align, alloc) if offset.bytes() == 0 && align == alloc.align => {
7575
alloc
7676
},
7777
_ => bug!("static const eval returned {:#?}", static_),

src/librustc_codegen_ssa/mir/operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
109109
let b_llval = bx.const_usize((end - start) as u64);
110110
OperandValue::Pair(a_llval, b_llval)
111111
},
112-
ConstValue::ByRef(ptr, align, alloc) => {
113-
return bx.load_operand(bx.from_const_alloc(layout, align, alloc, ptr.offset));
112+
ConstValue::ByRef(offset, align, alloc) => {
113+
return bx.load_operand(bx.from_const_alloc(layout, align, alloc, offset));
114114
},
115115
};
116116

src/librustc_codegen_ssa/mir/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
424424
let layout = cx.layout_of(self.monomorphize(&ty));
425425
match bx.tcx().const_eval(param_env.and(cid)) {
426426
Ok(val) => match val.val {
427-
mir::interpret::ConstValue::ByRef(ptr, align, alloc) => {
428-
bx.cx().from_const_alloc(layout, align, alloc, ptr.offset)
427+
mir::interpret::ConstValue::ByRef(offset, align, alloc) => {
428+
bx.cx().from_const_alloc(layout, align, alloc, offset)
429429
}
430430
_ => bug!("promoteds should have an allocation: {:?}", val),
431431
},

src/librustc_mir/const_eval.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn op_to_const<'tcx>(
9999
Ok(mplace) => {
100100
let ptr = mplace.ptr.to_ptr().unwrap();
101101
let alloc = ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
102-
ConstValue::ByRef(ptr, mplace.align, alloc)
102+
ConstValue::ByRef(ptr.offset, mplace.align, alloc)
103103
},
104104
// see comment on `let try_as_immediate` above
105105
Err(ImmTy { imm: Immediate::Scalar(x), .. }) => match x {
@@ -113,7 +113,7 @@ fn op_to_const<'tcx>(
113113
let mplace = op.to_mem_place();
114114
let ptr = mplace.ptr.to_ptr().unwrap();
115115
let alloc = ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
116-
ConstValue::ByRef(ptr, mplace.align, alloc)
116+
ConstValue::ByRef(ptr.offset, mplace.align, alloc)
117117
},
118118
},
119119
Err(ImmTy { imm: Immediate::ScalarPair(a, b), .. }) => {
@@ -542,7 +542,7 @@ fn validate_and_turn_into_const<'tcx>(
542542
let ptr = mplace.ptr.to_ptr()?;
543543
Ok(tcx.mk_const(ty::Const {
544544
val: ConstValue::ByRef(
545-
ptr,
545+
ptr.offset,
546546
mplace.align,
547547
ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id),
548548
),

src/librustc_mir/hair/pattern/_match.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl LiteralExpander<'tcx> {
218218
(ConstValue::Scalar(Scalar::Ptr(p)), x, y) if x == y => {
219219
let alloc = self.tcx.alloc_map.lock().unwrap_memory(p.alloc_id);
220220
ConstValue::ByRef(
221-
p,
221+
p.offset,
222222
// FIXME(oli-obk): this should be the type's layout
223223
alloc.align,
224224
alloc,
@@ -1436,9 +1436,10 @@ fn slice_pat_covered_by_const<'tcx>(
14361436
suffix: &[Pattern<'tcx>],
14371437
) -> Result<bool, ErrorReported> {
14381438
let data: &[u8] = match (const_val.val, &const_val.ty.sty) {
1439-
(ConstValue::ByRef(ptr, _, alloc), ty::Array(t, n)) => {
1439+
(ConstValue::ByRef(offset, _, alloc), ty::Array(t, n)) => {
14401440
assert_eq!(*t, tcx.types.u8);
14411441
let n = n.assert_usize(tcx).unwrap();
1442+
let ptr = Pointer::new(AllocId(0), offset);
14421443
alloc.get_bytes(&tcx, ptr, Size::from_bytes(n)).unwrap()
14431444
},
14441445
(ConstValue::Slice { data, start, end }, ty::Slice(t)) => {
@@ -1758,9 +1759,9 @@ fn specialize<'p, 'a: 'p, 'tcx>(
17581759
let (alloc, offset, n, ty) = match value.ty.sty {
17591760
ty::Array(t, n) => {
17601761
match value.val {
1761-
ConstValue::ByRef(ptr, _, alloc) => (
1762+
ConstValue::ByRef(offset, _, alloc) => (
17621763
alloc,
1763-
ptr.offset,
1764+
offset,
17641765
n.unwrap_usize(cx.tcx),
17651766
t,
17661767
),

src/librustc_mir/interpret/operand.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
538538
self.layout_of(self.monomorphize(val.ty)?)
539539
})?;
540540
let op = match val.val {
541-
ConstValue::ByRef(ptr, align, _alloc) => {
541+
ConstValue::ByRef(offset, align, alloc) => {
542+
let id = self.tcx.alloc_map.lock().create_memory_alloc(alloc);
542543
// We rely on mutability being set correctly in that allocation to prevent writes
543544
// where none should happen.
544-
let ptr = self.tag_static_base_pointer(ptr);
545+
let ptr = self.tag_static_base_pointer(Pointer::new(id, offset));
545546
Operand::Indirect(MemPlace::from_ptr(ptr, align))
546547
},
547548
ConstValue::Scalar(x) =>
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(repr_simd, platform_intrinsics)]
2+
3+
// revisions:rpass1 rpass2
4+
5+
#[repr(simd)]
6+
struct I32x2(i32, i32);
7+
8+
extern "platform-intrinsic" {
9+
fn simd_shuffle2<T, U>(x: T, y: T, idx: [u32; 2]) -> U;
10+
}
11+
12+
fn main() {
13+
unsafe {
14+
let _: I32x2 = simd_shuffle2(I32x2(1, 2), I32x2(3, 4), [0, 0]);
15+
let _: I32x2 = simd_shuffle2(I32x2(1, 2), I32x2(3, 4), [0, 0]);
16+
}
17+
}

0 commit comments

Comments
 (0)