Skip to content

Commit 1d1f642

Browse files
Remove notion of "preferred" alignment AKA __alignof
In rust-lang#90877 (comment) T-lang decided they did not wish to remove `intrinsics::pref_align_of`. However, the intrinsic 1. is a nightly feature, so can be removed at compiler/libs discretion 2. requires considerable effort in the compiler to support 3. has been justified based on relevance to codegen, but it is only a requirement for C++ (not C, not Rust) stack frame layout for AIX[^1], in ways Rust would not interact with, even with increased C++ interop 4. the AIX XLC compiler's layout rules for C have unrelated concerns[^2] 5. otherwise it is used to pad static layout[^3], not correctness 6. these static align-up clauses can be replaced by other rules 7. there is only one clear benefactor we are aware of: tools automating C -> Rust translation handling GNU extensions[^4]. 8. because the GNU extension is a "false friend" of a standard C form, `alignof` or `_Alignof`, the C code likely meant `mem::align_of`, which makes the choice to support such a mapping very questionable 9. its presence makes it easy to do incorrect codegen with the compiler, as Rust rules regarding alignment (e.g. size == align * N) do not hold with preferred alignment, and this actually happened[^5] Thus remove the intrinsic and supporting code, as a cost-benefit analysis cannot possibly justify going to such lengths and risking correctness just to help automated translation of an ill-considered GNU extension. [^1]: cite AIX cookie layout [^2]: cite AIX "power alignment" issues [^3]: cite static alignment usages [^4]: cite c2rust commit [^5]: cite bjorn3's PR
1 parent ce36a96 commit 1d1f642

28 files changed

+221
-424
lines changed

compiler/rustc_abi/src/layout.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_index::Idx;
77
use tracing::debug;
88

99
use crate::{
10-
AbiAndPrefAlign, Align, BackendRepr, FieldsShape, HasDataLayout, IndexSlice, IndexVec, Integer,
10+
AbiAlign, Align, BackendRepr, FieldsShape, HasDataLayout, IndexSlice, IndexVec, Integer,
1111
LayoutData, Niche, NonZeroUsize, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding,
1212
Variants, WrappingRange,
1313
};
@@ -361,13 +361,13 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
361361
}
362362

363363
if let Some(pack) = repr.pack {
364-
align = align.min(AbiAndPrefAlign::new(pack));
364+
align = align.min(AbiAlign::new(pack));
365365
}
366366
// The unadjusted ABI alignment does not include repr(align), but does include repr(pack).
367367
// See documentation on `LayoutS::unadjusted_abi_align`.
368368
let unadjusted_abi_align = align.abi;
369369
if let Some(repr_align) = repr.align {
370-
align = align.max(AbiAndPrefAlign::new(repr_align));
370+
align = align.max(AbiAlign::new(repr_align));
371371
}
372372
// `align` must not be modified after this, or `unadjusted_abi_align` could be inaccurate.
373373
let align = align;
@@ -1203,7 +1203,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
12031203
if let StructKind::Prefixed(prefix_size, prefix_align) = kind {
12041204
let prefix_align =
12051205
if let Some(pack) = pack { prefix_align.min(pack) } else { prefix_align };
1206-
align = align.max(AbiAndPrefAlign::new(prefix_align));
1206+
align = align.max(AbiAlign::new(prefix_align));
12071207
offset = prefix_size.align_to(prefix_align);
12081208
}
12091209
for &i in &inverse_memory_index {
@@ -1222,7 +1222,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
12221222

12231223
// Invariant: offset < dl.obj_size_bound() <= 1<<61
12241224
let field_align = if let Some(pack) = pack {
1225-
field.align.min(AbiAndPrefAlign::new(pack))
1225+
field.align.min(AbiAlign::new(pack))
12261226
} else {
12271227
field.align
12281228
};
@@ -1256,7 +1256,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
12561256
// See documentation on `LayoutS::unadjusted_abi_align`.
12571257
let unadjusted_abi_align = align.abi;
12581258
if let Some(repr_align) = repr.align {
1259-
align = align.max(AbiAndPrefAlign::new(repr_align));
1259+
align = align.max(AbiAlign::new(repr_align));
12601260
}
12611261
// `align` must not be modified after this point, or `unadjusted_abi_align` could be inaccurate.
12621262
let align = align;

compiler/rustc_abi/src/layout/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_data_structures::intern::Interned;
55
use rustc_macros::HashStable_Generic;
66

77
use crate::{
8-
AbiAndPrefAlign, Align, BackendRepr, FieldsShape, Float, HasDataLayout, LayoutData, Niche,
8+
AbiAlign, Align, BackendRepr, FieldsShape, Float, HasDataLayout, LayoutData, Niche,
99
PointeeInfo, Primitive, Scalar, Size, TargetDataLayout, Variants,
1010
};
1111

@@ -93,7 +93,7 @@ impl<'a> Layout<'a> {
9393
self.0.0.largest_niche
9494
}
9595

96-
pub fn align(self) -> AbiAndPrefAlign {
96+
pub fn align(self) -> AbiAlign {
9797
self.0.0.align
9898
}
9999

compiler/rustc_abi/src/lib.rs

+44-46
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,22 @@ impl ReprOptions {
210210
#[derive(Debug, PartialEq, Eq)]
211211
pub struct TargetDataLayout {
212212
pub endian: Endian,
213-
pub i1_align: AbiAndPrefAlign,
214-
pub i8_align: AbiAndPrefAlign,
215-
pub i16_align: AbiAndPrefAlign,
216-
pub i32_align: AbiAndPrefAlign,
217-
pub i64_align: AbiAndPrefAlign,
218-
pub i128_align: AbiAndPrefAlign,
219-
pub f16_align: AbiAndPrefAlign,
220-
pub f32_align: AbiAndPrefAlign,
221-
pub f64_align: AbiAndPrefAlign,
222-
pub f128_align: AbiAndPrefAlign,
213+
pub i1_align: AbiAlign,
214+
pub i8_align: AbiAlign,
215+
pub i16_align: AbiAlign,
216+
pub i32_align: AbiAlign,
217+
pub i64_align: AbiAlign,
218+
pub i128_align: AbiAlign,
219+
pub f16_align: AbiAlign,
220+
pub f32_align: AbiAlign,
221+
pub f64_align: AbiAlign,
222+
pub f128_align: AbiAlign,
223223
pub pointer_size: Size,
224-
pub pointer_align: AbiAndPrefAlign,
225-
pub aggregate_align: AbiAndPrefAlign,
224+
pub pointer_align: AbiAlign,
225+
pub aggregate_align: AbiAlign,
226226

227227
/// Alignments for vector types.
228-
pub vector_align: Vec<(Size, AbiAndPrefAlign)>,
228+
pub vector_align: Vec<(Size, AbiAlign)>,
229229

230230
pub instruction_address_space: AddressSpace,
231231

@@ -241,22 +241,22 @@ impl Default for TargetDataLayout {
241241
let align = |bits| Align::from_bits(bits).unwrap();
242242
TargetDataLayout {
243243
endian: Endian::Big,
244-
i1_align: AbiAndPrefAlign::new(align(8)),
245-
i8_align: AbiAndPrefAlign::new(align(8)),
246-
i16_align: AbiAndPrefAlign::new(align(16)),
247-
i32_align: AbiAndPrefAlign::new(align(32)),
248-
i64_align: AbiAndPrefAlign { abi: align(32), pref: align(64) },
249-
i128_align: AbiAndPrefAlign { abi: align(32), pref: align(64) },
250-
f16_align: AbiAndPrefAlign::new(align(16)),
251-
f32_align: AbiAndPrefAlign::new(align(32)),
252-
f64_align: AbiAndPrefAlign::new(align(64)),
253-
f128_align: AbiAndPrefAlign::new(align(128)),
244+
i1_align: AbiAlign::new(align(8)),
245+
i8_align: AbiAlign::new(align(8)),
246+
i16_align: AbiAlign::new(align(16)),
247+
i32_align: AbiAlign::new(align(32)),
248+
i64_align: AbiAlign::new(align(32)),
249+
i128_align: AbiAlign::new(align(32)),
250+
f16_align: AbiAlign::new(align(16)),
251+
f32_align: AbiAlign::new(align(32)),
252+
f64_align: AbiAlign::new(align(64)),
253+
f128_align: AbiAlign::new(align(128)),
254254
pointer_size: Size::from_bits(64),
255-
pointer_align: AbiAndPrefAlign::new(align(64)),
256-
aggregate_align: AbiAndPrefAlign { abi: align(0), pref: align(64) },
255+
pointer_align: AbiAlign::new(align(64)),
256+
aggregate_align: AbiAlign { abi: align(8) },
257257
vector_align: vec![
258-
(Size::from_bits(64), AbiAndPrefAlign::new(align(64))),
259-
(Size::from_bits(128), AbiAndPrefAlign::new(align(128))),
258+
(Size::from_bits(64), AbiAlign::new(align(64))),
259+
(Size::from_bits(128), AbiAlign::new(align(128))),
260260
],
261261
instruction_address_space: AddressSpace::DATA,
262262
c_enum_min_size: Integer::I32,
@@ -314,8 +314,7 @@ impl TargetDataLayout {
314314
.map_err(|err| TargetDataLayoutErrors::InvalidAlignment { cause, err })
315315
};
316316
let abi = parse_bits(s[0], "alignment", cause)?;
317-
let pref = s.get(1).map_or(Ok(abi), |pref| parse_bits(pref, "alignment", cause))?;
318-
Ok(AbiAndPrefAlign { abi: align_from_bits(abi)?, pref: align_from_bits(pref)? })
317+
Ok(AbiAlign::new(align_from_bits(abi)?))
319318
};
320319

321320
let mut dl = TargetDataLayout::default();
@@ -409,15 +408,15 @@ impl TargetDataLayout {
409408
}
410409

411410
#[inline]
412-
pub fn vector_align(&self, vec_size: Size) -> AbiAndPrefAlign {
411+
pub fn vector_align(&self, vec_size: Size) -> AbiAlign {
413412
for &(size, align) in &self.vector_align {
414413
if size == vec_size {
415414
return align;
416415
}
417416
}
418417
// Default to natural alignment, which is what LLVM does.
419418
// 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())
419+
AbiAlign::new(Align::from_bytes(vec_size.bytes().next_power_of_two()).unwrap())
421420
}
422421
}
423422

@@ -838,25 +837,24 @@ impl Align {
838837
/// It is of effectively no consequence for layout in structs and on the stack.
839838
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
840839
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
841-
pub struct AbiAndPrefAlign {
840+
pub struct AbiAlign {
842841
pub abi: Align,
843-
pub pref: Align,
844842
}
845843

846-
impl AbiAndPrefAlign {
844+
impl AbiAlign {
847845
#[inline]
848-
pub fn new(align: Align) -> AbiAndPrefAlign {
849-
AbiAndPrefAlign { abi: align, pref: align }
846+
pub fn new(align: Align) -> AbiAlign {
847+
AbiAlign { abi: align }
850848
}
851849

852850
#[inline]
853-
pub fn min(self, other: AbiAndPrefAlign) -> AbiAndPrefAlign {
854-
AbiAndPrefAlign { abi: self.abi.min(other.abi), pref: self.pref.min(other.pref) }
851+
pub fn min(self, other: AbiAlign) -> AbiAlign {
852+
AbiAlign { abi: self.abi.min(other.abi) }
855853
}
856854

857855
#[inline]
858-
pub fn max(self, other: AbiAndPrefAlign) -> AbiAndPrefAlign {
859-
AbiAndPrefAlign { abi: self.abi.max(other.abi), pref: self.pref.max(other.pref) }
856+
pub fn max(self, other: AbiAlign) -> AbiAlign {
857+
AbiAlign { abi: self.abi.max(other.abi) }
860858
}
861859
}
862860

@@ -916,7 +914,7 @@ impl Integer {
916914
}
917915
}
918916

919-
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign {
917+
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAlign {
920918
use Integer::*;
921919
let dl = cx.data_layout();
922920

@@ -1029,7 +1027,7 @@ impl Float {
10291027
}
10301028
}
10311029

1032-
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign {
1030+
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAlign {
10331031
use Float::*;
10341032
let dl = cx.data_layout();
10351033

@@ -1073,7 +1071,7 @@ impl Primitive {
10731071
}
10741072
}
10751073

1076-
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign {
1074+
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAlign {
10771075
use Primitive::*;
10781076
let dl = cx.data_layout();
10791077

@@ -1196,7 +1194,7 @@ impl Scalar {
11961194
}
11971195
}
11981196

1199-
pub fn align(self, cx: &impl HasDataLayout) -> AbiAndPrefAlign {
1197+
pub fn align(self, cx: &impl HasDataLayout) -> AbiAlign {
12001198
self.primitive().align(cx)
12011199
}
12021200

@@ -1464,7 +1462,7 @@ impl BackendRepr {
14641462
}
14651463

14661464
/// Returns the fixed alignment of this ABI, if any is mandated.
1467-
pub fn inherent_align<C: HasDataLayout>(&self, cx: &C) -> Option<AbiAndPrefAlign> {
1465+
pub fn inherent_align<C: HasDataLayout>(&self, cx: &C) -> Option<AbiAlign> {
14681466
Some(match *self {
14691467
BackendRepr::Scalar(s) => s.align(cx),
14701468
BackendRepr::ScalarPair(s1, s2) => s1.align(cx).max(s2.align(cx)),
@@ -1705,7 +1703,7 @@ pub struct LayoutData<FieldIdx: Idx, VariantIdx: Idx> {
17051703
/// (i.e. outside of its `valid_range`), if it exists.
17061704
pub largest_niche: Option<Niche>,
17071705

1708-
pub align: AbiAndPrefAlign,
1706+
pub align: AbiAlign,
17091707
pub size: Size,
17101708

17111709
/// The largest alignment explicitly requested with `repr(align)` on this type or any field.

compiler/rustc_const_eval/src/interpret/intrinsics.rs

-7
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
5050
ensure_monomorphic_enough(tcx, tp_ty)?;
5151
ConstValue::from_bool(tp_ty.needs_drop(tcx, typing_env))
5252
}
53-
sym::pref_align_of => {
54-
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
55-
let layout = tcx
56-
.layout_of(typing_env.as_query_input(tp_ty))
57-
.map_err(|e| err_inval!(Layout(*e)))?;
58-
ConstValue::from_target_usize(layout.align.pref.bytes(), &tcx)
59-
}
6053
sym::type_id => {
6154
ensure_monomorphic_enough(tcx, tp_ty)?;
6255
ConstValue::from_u128(tcx.type_id_hash(tp_ty).as_u128())

compiler/rustc_ty_utils/src/layout.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hir::def_id::DefId;
55
use rustc_abi::Integer::{I8, I32};
66
use rustc_abi::Primitive::{self, Float, Int, Pointer};
77
use rustc_abi::{
8-
AbiAndPrefAlign, AddressSpace, Align, BackendRepr, FIRST_VARIANT, FieldIdx, FieldsShape,
8+
AbiAlign, AddressSpace, Align, BackendRepr, FIRST_VARIANT, FieldIdx, FieldsShape,
99
HasDataLayout, Layout, LayoutCalculatorError, LayoutData, Niche, ReprOptions, Scalar, Size,
1010
StructKind, TagEncoding, VariantIdx, Variants, WrappingRange,
1111
};
@@ -560,13 +560,7 @@ fn layout_of_uncached<'tcx>(
560560
// Non-power-of-two vectors have padding up to the next power-of-two.
561561
// If we're a packed repr, remove the padding while keeping the alignment as close
562562
// to a vector as possible.
563-
(
564-
BackendRepr::Memory { sized: true },
565-
AbiAndPrefAlign {
566-
abi: Align::max_for_offset(size),
567-
pref: dl.vector_align(size).pref,
568-
},
569-
)
563+
(BackendRepr::Memory { sized: true }, AbiAlign { abi: Align::max_for_offset(size) })
570564
} else {
571565
(BackendRepr::Vector { element: e_abi, count: e_len }, dl.vector_align(size))
572566
};

library/core/src/intrinsics/mod.rs

-12
Original file line numberDiff line numberDiff line change
@@ -4173,18 +4173,6 @@ pub const fn min_align_of<T>() -> usize {
41734173
unreachable!()
41744174
}
41754175

4176-
/// The preferred alignment of a type.
4177-
///
4178-
/// This intrinsic does not have a stable counterpart.
4179-
/// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971).
4180-
#[rustc_nounwind]
4181-
#[unstable(feature = "core_intrinsics", issue = "none")]
4182-
#[rustc_intrinsic]
4183-
#[rustc_intrinsic_must_be_overridden]
4184-
pub const unsafe fn pref_align_of<T>() -> usize {
4185-
unreachable!()
4186-
}
4187-
41884176
/// Returns the number of variants of the type `T` cast to a `usize`;
41894177
/// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
41904178
///

tests/ui/abi/c-zst.aarch64-darwin.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ error: fn_abi_of(pass_zst) = FnAbi {
55
ty: (),
66
layout: Layout {
77
size: Size(0 bytes),
8-
align: AbiAndPrefAlign {
8+
align: AbiAlign {
99
abi: $SOME_ALIGN,
10-
pref: $SOME_ALIGN,
1110
},
1211
abi: Memory {
1312
sized: true,
@@ -33,9 +32,8 @@ error: fn_abi_of(pass_zst) = FnAbi {
3332
ty: (),
3433
layout: Layout {
3534
size: Size(0 bytes),
36-
align: AbiAndPrefAlign {
35+
align: AbiAlign {
3736
abi: $SOME_ALIGN,
38-
pref: $SOME_ALIGN,
3937
},
4038
abi: Memory {
4139
sized: true,

tests/ui/abi/c-zst.powerpc-linux.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ error: fn_abi_of(pass_zst) = FnAbi {
55
ty: (),
66
layout: Layout {
77
size: Size(0 bytes),
8-
align: AbiAndPrefAlign {
8+
align: AbiAlign {
99
abi: $SOME_ALIGN,
10-
pref: $SOME_ALIGN,
1110
},
1211
abi: Memory {
1312
sized: true,
@@ -44,9 +43,8 @@ error: fn_abi_of(pass_zst) = FnAbi {
4443
ty: (),
4544
layout: Layout {
4645
size: Size(0 bytes),
47-
align: AbiAndPrefAlign {
46+
align: AbiAlign {
4847
abi: $SOME_ALIGN,
49-
pref: $SOME_ALIGN,
5048
},
5149
abi: Memory {
5250
sized: true,

tests/ui/abi/c-zst.s390x-linux.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ error: fn_abi_of(pass_zst) = FnAbi {
55
ty: (),
66
layout: Layout {
77
size: Size(0 bytes),
8-
align: AbiAndPrefAlign {
8+
align: AbiAlign {
99
abi: $SOME_ALIGN,
10-
pref: $SOME_ALIGN,
1110
},
1211
abi: Memory {
1312
sized: true,
@@ -44,9 +43,8 @@ error: fn_abi_of(pass_zst) = FnAbi {
4443
ty: (),
4544
layout: Layout {
4645
size: Size(0 bytes),
47-
align: AbiAndPrefAlign {
46+
align: AbiAlign {
4847
abi: $SOME_ALIGN,
49-
pref: $SOME_ALIGN,
5048
},
5149
abi: Memory {
5250
sized: true,

0 commit comments

Comments
 (0)