Skip to content

Commit 330a16b

Browse files
committed
Also generate undef scalars and scalar pairs
1 parent 65b01cb commit 330a16b

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,22 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
205205
assert!(alloc_align >= layout.align.abi);
206206

207207
let read_scalar = |start, size, s: abi::Scalar, ty| {
208+
let range = alloc_range(start, size);
208209
match alloc.0.read_scalar(
209210
bx,
210-
alloc_range(start, size),
211+
range,
211212
/*read_provenance*/ matches!(s.primitive(), abi::Primitive::Pointer(_)),
212213
) {
213-
Ok(val) => bx.scalar_to_backend(val, s, ty),
214-
Err(_) => bx.const_poison(ty),
214+
Ok(val) => Some(bx.scalar_to_backend(val, s, ty)),
215+
Err(_) => {
216+
if alloc.0.provenance().range_empty(range, &bx.tcx())
217+
&& alloc.0.init_mask().is_range_initialized(range).unwrap_err() == range
218+
{
219+
Some(bx.const_undef(ty))
220+
} else {
221+
None
222+
}
223+
}
215224
}
216225
};
217226

@@ -222,16 +231,14 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
222231
// check that walks over the type of `mplace` to make sure it is truly correct to treat this
223232
// like a `Scalar` (or `ScalarPair`).
224233
match layout.backend_repr {
225-
BackendRepr::Scalar(s @ abi::Scalar::Initialized { .. }) => {
234+
BackendRepr::Scalar(s) => {
226235
let size = s.size(bx);
227236
assert_eq!(size, layout.size, "abi::Scalar size does not match layout size");
228-
let val = read_scalar(offset, size, s, bx.immediate_backend_type(layout));
229-
OperandRef { val: OperandValue::Immediate(val), layout }
237+
if let Some(val) = read_scalar(offset, size, s, bx.immediate_backend_type(layout)) {
238+
return OperandRef { val: OperandValue::Immediate(val), layout };
239+
}
230240
}
231-
BackendRepr::ScalarPair(
232-
a @ abi::Scalar::Initialized { .. },
233-
b @ abi::Scalar::Initialized { .. },
234-
) => {
241+
BackendRepr::ScalarPair(a, b) => {
235242
let (a_size, b_size) = (a.size(bx), b.size(bx));
236243
let b_offset = (offset + a_size).align_to(b.align(bx).abi);
237244
assert!(b_offset.bytes() > 0);
@@ -247,20 +254,21 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
247254
b,
248255
bx.scalar_pair_element_backend_type(layout, 1, true),
249256
);
250-
OperandRef { val: OperandValue::Pair(a_val, b_val), layout }
251-
}
252-
_ if layout.is_zst() => OperandRef::zero_sized(layout),
253-
_ => {
254-
// Neither a scalar nor scalar pair. Load from a place
255-
// FIXME: should we cache `const_data_from_alloc` to avoid repeating this for the
256-
// same `ConstAllocation`?
257-
let init = bx.const_data_from_alloc(alloc);
258-
let base_addr = bx.static_addr_of(init, alloc_align, None);
259-
260-
let llval = bx.const_ptr_byte_offset(base_addr, offset);
261-
bx.load_operand(PlaceRef::new_sized(llval, layout))
257+
if let (Some(a_val), Some(b_val)) = (a_val, b_val) {
258+
return OperandRef { val: OperandValue::Pair(a_val, b_val), layout };
259+
}
262260
}
261+
_ if layout.is_zst() => return OperandRef::zero_sized(layout),
262+
_ => {}
263263
}
264+
// Neither a scalar nor scalar pair. Load from a place
265+
// FIXME: should we cache `const_data_from_alloc` to avoid repeating this for the
266+
// same `ConstAllocation`?
267+
let init = bx.const_data_from_alloc(alloc);
268+
let base_addr = bx.static_addr_of(init, alloc_align, None);
269+
270+
let llval = bx.const_ptr_byte_offset(base_addr, offset);
271+
bx.load_operand(PlaceRef::new_sized(llval, layout))
264272
}
265273

266274
/// Asserts that this operand refers to a scalar and returns

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl AllocError {
222222
}
223223

224224
/// The information that makes up a memory access: offset and size.
225-
#[derive(Copy, Clone)]
225+
#[derive(Copy, Clone, PartialEq)]
226226
pub struct AllocRange {
227227
pub start: Size,
228228
pub size: Size,

tests/codegen/overaligned-constant.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ pub fn overaligned_constant() {
1717
// CHECK-LABEL: @overaligned_constant
1818
// CHECK: [[full:%_.*]] = alloca [32 x i8], align 8
1919
// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[full]], ptr align 8 @0, i64 32, i1 false)
20-
// CHECK: %b.0 = load i32, ptr @0, align 4
21-
// CHECK: %b.1 = load i32, ptr getelementptr inbounds ({{.*}}), align 4
2220
let mut s = S(1);
2321

2422
s.0 = 3;

0 commit comments

Comments
 (0)