Skip to content

Commit 95fbd7e

Browse files
Only allocate the minimal alignment requested for a type when possible.
1 parent 9d292ca commit 95fbd7e

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/common.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,26 +377,26 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
377377
size.is_multiple_of(align),
378378
"size must be a multiple of alignment (size={size}, align={align})"
379379
);
380+
assert!(align.is_power_of_two(), "alignment must be a power of two (align={align})");
380381

381382
let abi_align = if self.tcx.sess.target.arch == Arch::S390x { 8 } else { 16 };
383+
// Cranelift can only guarantee alignment up to the ABI alignment provided by the target.
384+
// If the requested alignment is less than the abi_align it can be used directly.
382385
if align <= abi_align {
383386
let stack_slot = self.bcx.create_sized_stack_slot(StackSlotData {
384387
kind: StackSlotKind::ExplicitSlot,
385-
// FIXME Don't force the size to a multiple of <abi_align> bytes once Cranelift gets
386-
// a way to specify stack slot alignment.
387-
size: size.div_ceil(abi_align) * abi_align,
388-
align_shift: 4,
388+
size,
389+
// The maximum value of ilog2 is 31 which will always fit in a u8.
390+
align_shift: align.ilog2().try_into().unwrap(),
389391
});
390392
Pointer::stack_slot(stack_slot)
391393
} else {
392394
// Alignment is too big to handle using the above hack. Dynamically realign a stack slot
393395
// instead. This wastes some space for the realignment.
394396
let stack_slot = self.bcx.create_sized_stack_slot(StackSlotData {
395397
kind: StackSlotKind::ExplicitSlot,
396-
// FIXME Don't force the size to a multiple of <abi_align> bytes once Cranelift gets
397-
// a way to specify stack slot alignment.
398-
size: (size + align) / abi_align * abi_align,
399-
align_shift: 4,
398+
size: size + align,
399+
align_shift: abi_align.ilog2().try_into().unwrap(),
400400
});
401401
let base_ptr = self.bcx.ins().stack_addr(self.pointer_type, stack_slot, 0);
402402
let misalign_offset = self.bcx.ins().band_imm(base_ptr, i64::from(align - 1));

0 commit comments

Comments
 (0)