Skip to content

Commit b29d2b8

Browse files
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.
1 parent f993b2d commit b29d2b8

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

compiler/rustc_abi/src/callconv/reg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl Reg {
5757
128 => dl.f128_align.abi,
5858
_ => panic!("unsupported float: {self:?}"),
5959
},
60-
RegKind::Vector => dl.vector_align(self.size).abi,
60+
RegKind::Vector => dl.llvmlike_vector_align(self.size).abi,
6161
}
6262
}
6363
}

compiler/rustc_abi/src/lib.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -408,16 +408,21 @@ impl TargetDataLayout {
408408
}
409409
}
410410

411+
/// psABI-mandated alignment for a vector type, if any
411412
#[inline]
412-
pub fn vector_align(&self, vec_size: Size) -> AbiAndPrefAlign {
413-
for &(size, align) in &self.vector_align {
414-
if size == vec_size {
415-
return align;
416-
}
417-
}
418-
// Default to natural alignment, which is what LLVM does.
419-
// That is, use the size, rounded up to a power of 2.
420-
AbiAndPrefAlign::new(Align::from_bytes(vec_size.bytes().next_power_of_two()).unwrap())
413+
fn cabi_vector_align(&self, vec_size: Size) -> Option<AbiAndPrefAlign> {
414+
self.vector_align
415+
.iter()
416+
.find(|(size, _align)| *size == vec_size)
417+
.map(|(_size, align)| *align)
418+
}
419+
420+
/// an alignment resembling the one LLVM would pick for a vector
421+
#[inline]
422+
pub fn llvmlike_vector_align(&self, vec_size: Size) -> AbiAndPrefAlign {
423+
self.cabi_vector_align(vec_size).unwrap_or(AbiAndPrefAlign::new(
424+
Align::from_bytes(vec_size.bytes().next_power_of_two()).unwrap(),
425+
))
421426
}
422427
}
423428

@@ -1178,13 +1183,10 @@ impl Scalar {
11781183
#[inline]
11791184
pub fn is_bool(&self) -> bool {
11801185
use Integer::*;
1181-
matches!(
1182-
self,
1183-
Scalar::Initialized {
1184-
value: Primitive::Int(I8, false),
1185-
valid_range: WrappingRange { start: 0, end: 1 }
1186-
}
1187-
)
1186+
matches!(self, Scalar::Initialized {
1187+
value: Primitive::Int(I8, false),
1188+
valid_range: WrappingRange { start: 0, end: 1 }
1189+
})
11881190
}
11891191

11901192
/// Get the primitive representation of this type, ignoring the valid range and whether the

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,14 @@ fn layout_of_uncached<'tcx>(
564564
BackendRepr::Memory { sized: true },
565565
AbiAndPrefAlign {
566566
abi: Align::max_aligned_factor(size),
567-
pref: dl.vector_align(size).pref,
567+
pref: dl.llvmlike_vector_align(size).pref,
568568
},
569569
)
570570
} else {
571-
(BackendRepr::Vector { element: e_abi, count: e_len }, dl.vector_align(size))
571+
(
572+
BackendRepr::Vector { element: e_abi, count: e_len },
573+
dl.llvmlike_vector_align(size),
574+
)
572575
};
573576
let size = size.align_to(align.abi);
574577

0 commit comments

Comments
 (0)