Skip to content

Commit f98365f

Browse files
committed
Rename align to platform_align on Scalar and Primitive
To emphasize that just because you see a `Scalar(I32)` that doesn't really tell you anything about the alignment it has -- one should be looking at the type (well, the place) for that. No actual layout or behaviour changes in *this* PR.
1 parent 40557f6 commit f98365f

14 files changed

Lines changed: 57 additions & 32 deletions

File tree

compiler/rustc_abi/src/layout.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,14 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
477477
Ok(Some((repr, _))) => match repr {
478478
// Mismatched alignment (e.g. union is #[repr(packed)]): disable opt
479479
BackendRepr::Scalar(_) | BackendRepr::ScalarPair(_, _)
480-
if repr.scalar_align(dl).unwrap() != align =>
480+
if repr.scalar_platform_align(dl).unwrap() != align =>
481481
{
482482
BackendRepr::Memory { sized: true }
483483
}
484484
// Vectors require at least element alignment, else disable the opt
485-
BackendRepr::SimdVector { element, count: _ } if element.align(dl).abi > align => {
485+
BackendRepr::SimdVector { element, count: _ }
486+
if element.default_align(dl).abi > align =>
487+
{
486488
BackendRepr::Memory { sized: true }
487489
}
488490
// the alignment tests passed and we can use this
@@ -986,7 +988,8 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
986988
// roundtripping pointers through ptrtoint/inttoptr.
987989
(p @ Primitive::Pointer(_), i @ Primitive::Int(..))
988990
| (i @ Primitive::Int(..), p @ Primitive::Pointer(_))
989-
if p.size(dl) == i.size(dl) && p.align(dl) == i.align(dl) =>
991+
if p.size(dl) == i.size(dl)
992+
&& p.default_align(dl) == i.default_align(dl) =>
990993
{
991994
p
992995
}

compiler/rustc_abi/src/layout/simple.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
4949
pub fn scalar<C: HasDataLayout>(cx: &C, scalar: Scalar) -> Self {
5050
let largest_niche = Niche::from_scalar(cx, Size::ZERO, scalar);
5151
let size = scalar.size(cx);
52-
let align = scalar.align(cx);
52+
let align = scalar.default_align(cx);
5353

5454
let range = scalar.valid_range(cx);
5555

@@ -90,8 +90,8 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
9090

9191
pub fn scalar_pair<C: HasDataLayout>(cx: &C, a: Scalar, b: Scalar) -> Self {
9292
let dl = cx.data_layout();
93-
let b_align = b.align(dl).abi;
94-
let align = a.align(dl).abi.max(b_align).max(dl.aggregate_align);
93+
let b_align = b.default_align(dl).abi;
94+
let align = a.default_align(dl).abi.max(b_align).max(dl.aggregate_align);
9595
let b_offset = a.size(dl).align_to(b_align);
9696
let size = (b_offset + b.size(dl)).align_to(align);
9797

compiler/rustc_abi/src/lib.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,11 @@ impl Primitive {
14041404
}
14051405
}
14061406

1407-
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAlign {
1407+
/// The *platform-specific* ABI alignment of this primitive.
1408+
///
1409+
/// This is the type alignment for the corresponding built-in.
1410+
/// In other contexts it might have different alignment.
1411+
pub fn default_align<C: HasDataLayout>(self, cx: &C) -> AbiAlign {
14081412
use Primitive::*;
14091413
let dl = cx.data_layout();
14101414

@@ -1579,8 +1583,12 @@ impl Scalar {
15791583
}
15801584
}
15811585

1582-
pub fn align(self, cx: &impl HasDataLayout) -> AbiAlign {
1583-
self.primitive().align(cx)
1586+
/// The *platform-specific* ABI alignment of this scalar.
1587+
///
1588+
/// This is the type alignment for the corresponding built-in.
1589+
/// In other contexts it might have different alignment.
1590+
pub fn default_align(self, cx: &impl HasDataLayout) -> AbiAlign {
1591+
self.primitive().default_align(cx)
15841592
}
15851593

15861594
pub fn size(self, cx: &impl HasDataLayout) -> Size {
@@ -1792,6 +1800,13 @@ impl IntoDiagArg for NumScalableVectors {
17921800
#[cfg_attr(feature = "nightly", derive(StableHash))]
17931801
pub enum BackendRepr {
17941802
Scalar(Scalar),
1803+
/// Two scalars listed in *memory* order, so the first is at offset zero
1804+
/// and the second at a non-zero offset.
1805+
/// These need not be `FieldIdx(0)` and `FieldIdx(1)`.
1806+
///
1807+
/// As of June 2026 the offset to the second scalar is the size of the first
1808+
/// scalar rounded up to the platform alignment of the second scalar.
1809+
/// That may soon change, however; see MCP#1007.
17951810
ScalarPair(Scalar, Scalar),
17961811
SimdScalableVector {
17971812
element: Scalar,
@@ -1857,10 +1872,16 @@ impl BackendRepr {
18571872
/// The psABI alignment for a `Scalar` or `ScalarPair`
18581873
///
18591874
/// `None` for other variants.
1860-
pub fn scalar_align<C: HasDataLayout>(&self, cx: &C) -> Option<Align> {
1875+
///
1876+
/// It's unclear whether this is a meaningful operation, and MCP#1007 proposes changes.
1877+
/// You should generally be using the alignment of the place or the type,
1878+
/// not calculating something from the `Scalar`s.
1879+
pub fn scalar_platform_align<C: HasDataLayout>(&self, cx: &C) -> Option<Align> {
18611880
match *self {
1862-
BackendRepr::Scalar(s) => Some(s.align(cx).abi),
1863-
BackendRepr::ScalarPair(s1, s2) => Some(s1.align(cx).max(s2.align(cx)).abi),
1881+
BackendRepr::Scalar(s) => Some(s.default_align(cx).abi),
1882+
BackendRepr::ScalarPair(s1, s2) => {
1883+
Some(s1.default_align(cx).max(s2.default_align(cx)).abi)
1884+
}
18641885
// The align of a Vector can vary in surprising ways
18651886
BackendRepr::SimdVector { .. }
18661887
| BackendRepr::Memory { .. }
@@ -1877,9 +1898,9 @@ impl BackendRepr {
18771898
BackendRepr::Scalar(s) => Some(s.size(cx)),
18781899
// May have some padding between the pair.
18791900
BackendRepr::ScalarPair(s1, s2) => {
1880-
let field2_offset = s1.size(cx).align_to(s2.align(cx).abi);
1901+
let field2_offset = s1.size(cx).align_to(s2.default_align(cx).abi);
18811902
let size = (field2_offset + s2.size(cx)).align_to(
1882-
self.scalar_align(cx)
1903+
self.scalar_platform_align(cx)
18831904
// We absolutely must have an answer here or everything is FUBAR.
18841905
.unwrap(),
18851906
);

compiler/rustc_codegen_cranelift/src/value_and_place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn codegen_field<'tcx>(
5656
}
5757

5858
fn scalar_pair_calculate_b_offset(tcx: TyCtxt<'_>, a_scalar: Scalar, b_scalar: Scalar) -> Offset32 {
59-
let b_offset = a_scalar.size(&tcx).align_to(b_scalar.align(&tcx).abi);
59+
let b_offset = a_scalar.size(&tcx).align_to(b_scalar.default_align(&tcx).abi);
6060
Offset32::new(b_offset.bytes().try_into().unwrap())
6161
}
6262

compiler/rustc_codegen_gcc/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
10641064
},
10651065
)
10661066
} else if let abi::BackendRepr::ScalarPair(ref a, ref b) = place.layout.backend_repr {
1067-
let b_offset = a.size(self).align_to(b.align(self).abi);
1067+
let b_offset = a.size(self).align_to(b.default_align(self).abi);
10681068

10691069
let mut load = |i, scalar: &abi::Scalar, align| {
10701070
let ptr = if i == 0 {

compiler/rustc_codegen_gcc/src/type_of.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
325325
return cx.type_i1();
326326
}
327327

328-
let offset = if index == 0 { Size::ZERO } else { a.size(cx).align_to(b.align(cx).abi) };
328+
let offset =
329+
if index == 0 { Size::ZERO } else { a.size(cx).align_to(b.default_align(cx).abi) };
329330
self.scalar_gcc_type_at(cx, scalar, offset)
330331
}
331332

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
789789
});
790790
OperandValue::Immediate(llval)
791791
} else if let abi::BackendRepr::ScalarPair(a, b) = place.layout.backend_repr {
792-
let b_offset = a.size(self).align_to(b.align(self).abi);
792+
let b_offset = a.size(self).align_to(b.default_align(self).abi);
793793

794794
let mut load = |i, scalar: abi::Scalar, layout, align, offset| {
795795
let llptr = if i == 0 {

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
227227
b @ abi::Scalar::Initialized { .. },
228228
) => {
229229
let (a_size, b_size) = (a.size(bx), b.size(bx));
230-
let b_offset = (offset + a_size).align_to(b.align(bx).abi);
230+
let b_offset = (offset + a_size).align_to(b.default_align(bx).abi);
231231
assert!(b_offset.bytes() > 0);
232232
let a_val = read_scalar(
233233
offset,
@@ -388,7 +388,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
388388
assert_eq!(field.size, a.size(bx.cx()));
389389
(Some(a), a_llval)
390390
} else {
391-
assert_eq!(offset, a.size(bx.cx()).align_to(b.align(bx.cx()).abi));
391+
assert_eq!(offset, a.size(bx.cx()).align_to(b.default_align(bx.cx()).abi));
392392
assert_eq!(field.size, b.size(bx.cx()));
393393
(Some(b), b_llval)
394394
}
@@ -962,7 +962,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
962962
let BackendRepr::ScalarPair(a_scalar, b_scalar) = dest.layout.backend_repr else {
963963
bug!("store_with_flags: invalid ScalarPair layout: {:#?}", dest.layout);
964964
};
965-
let b_offset = a_scalar.size(bx).align_to(b_scalar.align(bx).abi);
965+
let b_offset = a_scalar.size(bx).align_to(b_scalar.default_align(bx).abi);
966966

967967
let val = bx.from_immediate(a);
968968
let align = dest.val.align;

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
419419
Immediate::from(if offset.bytes() == 0 {
420420
a_val
421421
} else {
422-
assert_eq!(offset, a.size(cx).align_to(b.align(cx).abi));
422+
assert_eq!(offset, a.size(cx).align_to(b.default_align(cx).abi));
423423
b_val
424424
})
425425
}
@@ -614,7 +614,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
614614
// We would anyway check against `ptr_align.restrict_for_offset(b_offset)`,
615615
// which `ptr.offset(b_offset)` cannot possibly fail to satisfy.
616616
let (a_size, b_size) = (a.size(self), b.size(self));
617-
let b_offset = a_size.align_to(b.align(self).abi);
617+
let b_offset = a_size.align_to(b.default_align(self).abi);
618618
assert!(b_offset.bytes() > 0); // in `operand_field` we use the offset to tell apart the fields
619619
let a_val = alloc.read_scalar(
620620
alloc_range(Size::ZERO, a_size),

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ where
733733
)
734734
};
735735
let a_size = a_val.size();
736-
let b_offset = a_size.align_to(b.align(&tcx).abi);
736+
let b_offset = a_size.align_to(b.default_align(&tcx).abi);
737737
assert!(b_offset.bytes() > 0); // in `operand_field` we use the offset to tell apart the fields
738738

739739
// It is tempting to verify `b_offset` against `layout.fields.offset(1)`,

0 commit comments

Comments
 (0)