compiler: split vector_align into cabi and llvmlike

Introduce a pair of functions that actually describe what they do,
because it wasn't clear the alignment is sometimes a forgery.
This commit is contained in:
Jubilee Young 2025-02-19 14:53:31 -08:00
parent 5c474fd99b
commit 79f41c773a
3 changed files with 20 additions and 12 deletions

View file

@ -57,7 +57,7 @@ impl Reg {
128 => dl.f128_align.abi, 128 => dl.f128_align.abi,
_ => panic!("unsupported float: {self:?}"), _ => panic!("unsupported float: {self:?}"),
}, },
RegKind::Vector => dl.vector_align(self.size).abi, RegKind::Vector => dl.llvmlike_vector_align(self.size).abi,
} }
} }
} }

View file

@ -408,16 +408,21 @@ impl TargetDataLayout {
} }
} }
/// psABI-mandated alignment for a vector type, if any
#[inline] #[inline]
pub fn vector_align(&self, vec_size: Size) -> AbiAndPrefAlign { fn cabi_vector_align(&self, vec_size: Size) -> Option<AbiAndPrefAlign> {
for &(size, align) in &self.vector_align { self.vector_align
if size == vec_size { .iter()
return align; .find(|(size, _align)| *size == vec_size)
.map(|(_size, align)| *align)
} }
}
// Default to natural alignment, which is what LLVM does. /// an alignment resembling the one LLVM would pick for a vector
// That is, use the size, rounded up to a power of 2. #[inline]
AbiAndPrefAlign::new(Align::from_bytes(vec_size.bytes().next_power_of_two()).unwrap()) pub fn llvmlike_vector_align(&self, vec_size: Size) -> AbiAndPrefAlign {
self.cabi_vector_align(vec_size).unwrap_or(AbiAndPrefAlign::new(
Align::from_bytes(vec_size.bytes().next_power_of_two()).unwrap(),
))
} }
} }

View file

@ -535,11 +535,14 @@ fn layout_of_uncached<'tcx>(
BackendRepr::Memory { sized: true }, BackendRepr::Memory { sized: true },
AbiAndPrefAlign { AbiAndPrefAlign {
abi: Align::max_aligned_factor(size), abi: Align::max_aligned_factor(size),
pref: dl.vector_align(size).pref, pref: dl.llvmlike_vector_align(size).pref,
}, },
) )
} else { } else {
(BackendRepr::Vector { element: e_abi, count: e_len }, dl.vector_align(size)) (
BackendRepr::Vector { element: e_abi, count: e_len },
dl.llvmlike_vector_align(size),
)
}; };
let size = size.align_to(align.abi); let size = size.align_to(align.abi);