Skip to content

Commit c27715b

Browse files
committed
Use abi rather than preferred alignment everywhere
Turns out the size of a type is not necessarily a multiple of the preferred alignment, which broke the realignment logic in create_stack_slot.
1 parent ecad689 commit c27715b

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

src/abi/pass_mode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub(super) fn from_casted_value<'tcx>(
195195
// It may also be smaller for example when the type is a wrapper around an integer with a
196196
// larger alignment than the integer.
197197
std::cmp::max(abi_param_size, layout_size),
198-
u32::try_from(layout.align.pref.bytes()).unwrap(),
198+
u32::try_from(layout.align.abi.bytes()).unwrap(),
199199
);
200200
let mut offset = 0;
201201
let mut block_params_iter = block_params.iter().copied();

src/common.rs

+5
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
382382
}
383383

384384
pub(crate) fn create_stack_slot(&mut self, size: u32, align: u32) -> Pointer {
385+
assert!(
386+
size % align == 0,
387+
"size must be a multiple of alignment (size={size}, align={align})"
388+
);
389+
385390
let abi_align = if self.tcx.sess.target.arch == "s390x" { 8 } else { 16 };
386391
if align <= abi_align {
387392
let stack_slot = self.bcx.create_sized_stack_slot(StackSlotData {

src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl DebugContext {
304304
entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
305305
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(line));
306306

307-
entry.set(gimli::DW_AT_alignment, AttributeValue::Udata(static_layout.align.pref.bytes()));
307+
entry.set(gimli::DW_AT_alignment, AttributeValue::Udata(static_layout.align.abi.bytes()));
308308

309309
let mut expr = Expression::new();
310310
expr.op_addr(address_for_data(data_id));

src/debuginfo/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl DebugContext {
166166
let tuple_entry = self.dwarf.unit.get_mut(tuple_type_id);
167167
tuple_entry.set(gimli::DW_AT_name, AttributeValue::StringRef(self.dwarf.strings.add(name)));
168168
tuple_entry.set(gimli::DW_AT_byte_size, AttributeValue::Udata(layout.size.bytes()));
169-
tuple_entry.set(gimli::DW_AT_alignment, AttributeValue::Udata(layout.align.pref.bytes()));
169+
tuple_entry.set(gimli::DW_AT_alignment, AttributeValue::Udata(layout.align.abi.bytes()));
170170

171171
for (i, (ty, dw_ty)) in components.into_iter().enumerate() {
172172
let member_id = self.dwarf.unit.add(tuple_type_id, gimli::DW_TAG_member);
@@ -179,7 +179,7 @@ impl DebugContext {
179179
member_entry.set(
180180
gimli::DW_AT_alignment,
181181
AttributeValue::Udata(
182-
FullyMonomorphizedLayoutCx(tcx).layout_of(ty).align.pref.bytes(),
182+
FullyMonomorphizedLayoutCx(tcx).layout_of(ty).align.abi.bytes(),
183183
),
184184
);
185185
member_entry.set(

src/value_and_place.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'tcx> CValue<'tcx> {
101101
/// The is represented by a dangling pointer of suitable alignment.
102102
pub(crate) fn zst(layout: TyAndLayout<'tcx>) -> CValue<'tcx> {
103103
assert!(layout.is_zst());
104-
CValue::by_ref(crate::Pointer::dangling(layout.align.pref), layout)
104+
CValue::by_ref(crate::Pointer::dangling(layout.align.abi), layout)
105105
}
106106

107107
pub(crate) fn layout(&self) -> TyAndLayout<'tcx> {
@@ -392,7 +392,7 @@ impl<'tcx> CPlace<'tcx> {
392392
assert!(layout.is_sized());
393393
if layout.size.bytes() == 0 {
394394
return CPlace {
395-
inner: CPlaceInner::Addr(Pointer::dangling(layout.align.pref), None),
395+
inner: CPlaceInner::Addr(Pointer::dangling(layout.align.abi), None),
396396
layout,
397397
};
398398
}
@@ -405,7 +405,7 @@ impl<'tcx> CPlace<'tcx> {
405405

406406
let stack_slot = fx.create_stack_slot(
407407
u32::try_from(layout.size.bytes()).unwrap(),
408-
u32::try_from(layout.align.pref.bytes()).unwrap(),
408+
u32::try_from(layout.align.abi.bytes()).unwrap(),
409409
);
410410
CPlace { inner: CPlaceInner::Addr(stack_slot, None), layout }
411411
}

0 commit comments

Comments
 (0)