Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b5ddb76

Browse files
committed
Force stack slot size to be a multiple of 16
This ensures that all stack slots are aligned to 16 bytes. Without this linking against crates compiled with cg_llvm may cause a crash due to simd instructions requiring a 16 byte alignment.
1 parent 139a6d1 commit b5ddb76

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/abi/pass_mode.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
144144
None,
145145
vec![AbiParam::new(scalar_to_clif_type(tcx, scalar.clone()))],
146146
),
147-
// FIXME implement Vector Abi in a cg_llvm compatible way
148147
Abi::Vector { .. } => {
149148
let vector_ty = crate::intrinsics::clif_vector_type(tcx, self.layout).unwrap();
150149
(None, vec![AbiParam::new(vector_ty)])
@@ -210,15 +209,17 @@ pub(super) fn from_casted_value<'tcx>(
210209
cast: CastTarget,
211210
) -> CValue<'tcx> {
212211
let abi_params = cast_target_to_abi_params(cast);
213-
let size = abi_params
212+
let size: u32 = abi_params
214213
.iter()
215214
.map(|param| param.value_type.bytes())
216215
.sum();
217216
// Stack slot size may be bigger for for example `[u8; 3]` which is packed into an `i32`.
218217
assert!(u64::from(size) >= layout.size.bytes());
219218
let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
220219
kind: StackSlotKind::ExplicitSlot,
221-
size,
220+
// FIXME Don't force the size to a multiple of 16 bytes once Cranelift gets a way to
221+
// specify stack slot alignment.
222+
size: (size + 15) / 16 * 16,
222223
offset: None,
223224
});
224225
let ptr = Pointer::new(fx.bcx.ins().stack_addr(pointer_ty(fx.tcx), stack_slot, 0));
@@ -306,9 +307,7 @@ pub(super) fn cvalue_for_param<'tcx>(
306307
arg_abi.layout,
307308
))
308309
}
309-
PassMode::Cast(cast) => {
310-
Some(from_casted_value(fx, &block_params, arg_abi.layout, cast))
311-
}
310+
PassMode::Cast(cast) => Some(from_casted_value(fx, &block_params, arg_abi.layout, cast)),
312311
PassMode::Indirect {
313312
attrs: _,
314313
extra_attrs: None,

src/value_and_place.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ impl<'tcx> CPlace<'tcx> {
334334

335335
let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
336336
kind: StackSlotKind::ExplicitSlot,
337-
size: u32::try_from(layout.size.bytes()).unwrap(),
337+
// FIXME Don't force the size to a multiple of 16 bytes once Cranelift gets a way to
338+
// specify stack slot alignment.
339+
size: (u32::try_from(layout.size.bytes()).unwrap() + 15) / 16 * 16,
338340
offset: None,
339341
});
340342
CPlace {
@@ -498,7 +500,9 @@ impl<'tcx> CPlace<'tcx> {
498500
// FIXME do something more efficient for transmutes between vectors and integers.
499501
let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
500502
kind: StackSlotKind::ExplicitSlot,
501-
size: src_ty.bytes(),
503+
// FIXME Don't force the size to a multiple of 16 bytes once Cranelift gets a way to
504+
// specify stack slot alignment.
505+
size: (src_ty.bytes() + 15) / 16 * 16,
502506
offset: None,
503507
});
504508
let ptr = Pointer::stack_slot(stack_slot);

0 commit comments

Comments
 (0)