Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions compiler/rustc_codegen_ssa/src/size_of_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
// Furthermore, `align >= unsized_align`, and therefore we only need to do:
// let full_size = (unsized_offset_unadjusted + unsized_size).align_to(full_align);

let full_size = bx.add(unsized_offset_unadjusted, unsized_size);
// total <= isize::MAX, so nuw+nsw.
let unrounded_size = bx.unchecked_suadd(unsized_offset_unadjusted, unsized_size);

// Issue #27023: must add any necessary padding to `size`
// (to make it a multiple of `align`) before returning it.
Expand All @@ -173,10 +174,15 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
// `(size + (align-1)) & -align`
let one = bx.const_usize(1);
let addend = bx.sub(full_align, one);
let add = bx.add(full_size, addend);
let add = bx.add(unrounded_size, addend);
let neg = bx.neg(full_align);
let full_size = bx.and(add, neg);

// round_up(x, a) >= x for pow2 a; with nuw above LLVM deduces
// full_size >= unrounded_size >= offset > 0 (#152788).
let size_ge = bx.icmp(IntPredicate::IntUGE, full_size, unrounded_size);
bx.assume(size_ge);

(full_size, full_align)
}
_ => bug!("size_and_align_of_dst: {t} not supported"),
Expand Down
31 changes: 31 additions & 0 deletions tests/codegen-llvm/dst-size-of-val-not-zst.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//@ compile-flags: -Copt-level=3 -Z merge-functions=disabled
//@ min-llvm-version: 21
//@ needs-deterministic-layouts

#![crate_type = "lib"]

// Regression test for #152788: `size_of_val(p) == 0` folds to `false` for
// DSTs with a non-zero prefix (nuw+nsw on offset+tail, assume on rounding).

pub struct Foo<T: ?Sized>(pub [u32; 3], pub T);

// CHECK-LABEL: @size_of_val_dyn_not_zero
#[no_mangle]
pub fn size_of_val_dyn_not_zero(p: &Foo<dyn std::fmt::Debug>) -> bool {
// CHECK: ret i1 false
std::mem::size_of_val(p) == 0
}

// CHECK-LABEL: @size_of_val_slice_u8_not_zero
#[no_mangle]
pub fn size_of_val_slice_u8_not_zero(p: &Foo<[u8]>) -> bool {
// CHECK: ret i1 false
std::mem::size_of_val(p) == 0
}

// CHECK-LABEL: @size_of_val_slice_i32_not_zero
#[no_mangle]
pub fn size_of_val_slice_i32_not_zero(p: &Foo<[i32]>) -> bool {
// CHECK: ret i1 false
std::mem::size_of_val(p) == 0
}
15 changes: 13 additions & 2 deletions tests/codegen-llvm/dst-vtable-align-nonzero.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//@ compile-flags: -Copt-level=3 -Z merge-functions=disabled
//@ revisions: LLVM20 CURRENT
//@ [LLVM20] max-llvm-major-version: 20
//@ [CURRENT] min-llvm-version: 21

#![crate_type = "lib"]
#![feature(core_intrinsics)]
Expand Down Expand Up @@ -30,10 +33,18 @@ pub struct Struct<W: ?Sized> {
pub fn eliminates_runtime_check_when_align_1(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An option if it makes your life easier: since this one is "just" an optimization, you could pull it into a separate file with min-llvm-version: 21 and not worry about llvm20

x: &Struct<WrapperWithAlign1<dyn Trait>>,
) -> &WrapperWithAlign1<dyn Trait> {
// CHECK: load [[USIZE:i[0-9]+]], {{.+}} !range [[RANGE_META:![0-9]+]]
// LLVM20: load [[USIZE:i[0-9]+]], {{.+}} !range {{![0-9]+}}
// LLVM20: load [[USIZE]], {{.+}} !range [[RANGE_META:![0-9]+]]
// CURRENT: load [[USIZE:i[0-9]+]], {{.+}} !range [[RANGE_META:![0-9]+]]
// CHECK-NOT: llvm.umax
// CHECK-NOT: icmp
// CHECK-NOT: select
// CURRENT-NOT: icmp
// LLVM20-NOT: icmp
// LLVM20: [[DOES_NOT_SHRINK:%.+]] = icmp ug{{[et]}}
// LLVM20-NEXT: call void @llvm.assume(i1 [[DOES_NOT_SHRINK]])
// LLVM20-NOT: llvm.umax
// LLVM20-NOT: icmp
// LLVM20-NOT: select
// CHECK: ret
&x.dst
}
Expand Down
Loading