Skip to content

Commit 613c6d6

Browse files
Remove rustc's notion of "preferred" alignment AKA __alignof
In PR 90877 T-lang decided not to remove `intrinsics::pref_align_of`. However, the intrinsic and its supporting code 1. is a nightly feature, so can be removed at compiler/libs discretion 2. requires considerable effort in the compiler to support, as it necessarily complicates every single site reasoning about alignment 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, in ways Rust would not consider even with increased C++ interop 4. is only used by rustc to overalign some globals, not correctness 5. can be adequately replaced by other rules for globals, as it mostly affects alignments for a few types under 16 bytes of alignment 6. has only one clear benefactor: automating C -> Rust translation for GNU extensions like `__alignof` 7. such code was likely intended to be `alignof` or `_Alignof`, because the GNU extension is a "false friend" of the C keyword, which makes the choice to support such a mapping very questionable 8. makes it easy to do incorrect codegen in the compiler by its mere presence as usual Rust rules of alignment (e.g. `size == align * N`) do not hold with preferred alignment The implementation is clearly damaging the code quality of the compiler. Thus it is within the compiler team's purview to simply rip it out. If T-lang wishes to have this intrinsic restored for c2rust's benefit, it would have to use a radically different implementation that somehow does not cause internal incorrectness. Until then, remove the intrinsic and its supporting code, as one tool and an ill-considered GCC extension cannot justify risking correctness. Because we touch a fair amount of the compiler to change this at all, and unfortunately the duplication of AbiAndPrefAlign is deep-rooted, we keep an "AbiAlign" type which we can wean code off later.
1 parent 15825b7 commit 613c6d6

32 files changed

+231
-445
lines changed

compiler/rustc_abi/src/layout.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_index::bit_set::BitMatrix;
88
use tracing::debug;
99

1010
use crate::{
11-
AbiAndPrefAlign, Align, BackendRepr, FieldsShape, HasDataLayout, IndexSlice, IndexVec, Integer,
11+
AbiAlign, Align, BackendRepr, FieldsShape, HasDataLayout, IndexSlice, IndexVec, Integer,
1212
LayoutData, Niche, NonZeroUsize, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding,
1313
Variants, WrappingRange,
1414
};
@@ -173,13 +173,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
173173
// Non-power-of-two vectors have padding up to the next power-of-two.
174174
// If we're a packed repr, remove the padding while keeping the alignment as close
175175
// to a vector as possible.
176-
(
177-
BackendRepr::Memory { sized: true },
178-
AbiAndPrefAlign {
179-
abi: Align::max_aligned_factor(size),
180-
pref: dl.llvmlike_vector_align(size).pref,
181-
},
182-
)
176+
(BackendRepr::Memory { sized: true }, AbiAlign { abi: Align::max_aligned_factor(size) })
183177
} else {
184178
(BackendRepr::SimdVector { element: e_repr, count }, dl.llvmlike_vector_align(size))
185179
};
@@ -435,13 +429,13 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
435429
}
436430

437431
if let Some(pack) = repr.pack {
438-
align = align.min(AbiAndPrefAlign::new(pack));
432+
align = align.min(AbiAlign::new(pack));
439433
}
440434
// The unadjusted ABI alignment does not include repr(align), but does include repr(pack).
441435
// See documentation on `LayoutS::unadjusted_abi_align`.
442436
let unadjusted_abi_align = align.abi;
443437
if let Some(repr_align) = repr.align {
444-
align = align.max(AbiAndPrefAlign::new(repr_align));
438+
align = align.max(AbiAlign::new(repr_align));
445439
}
446440
// `align` must not be modified after this, or `unadjusted_abi_align` could be inaccurate.
447441
let align = align;
@@ -1289,7 +1283,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
12891283
if let StructKind::Prefixed(prefix_size, prefix_align) = kind {
12901284
let prefix_align =
12911285
if let Some(pack) = pack { prefix_align.min(pack) } else { prefix_align };
1292-
align = align.max(AbiAndPrefAlign::new(prefix_align));
1286+
align = align.max(AbiAlign::new(prefix_align));
12931287
offset = prefix_size.align_to(prefix_align);
12941288
}
12951289
for &i in &inverse_memory_index {
@@ -1308,7 +1302,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
13081302

13091303
// Invariant: offset < dl.obj_size_bound() <= 1<<61
13101304
let field_align = if let Some(pack) = pack {
1311-
field.align.min(AbiAndPrefAlign::new(pack))
1305+
field.align.min(AbiAlign::new(pack))
13121306
} else {
13131307
field.align
13141308
};
@@ -1342,7 +1336,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
13421336
// See documentation on `LayoutS::unadjusted_abi_align`.
13431337
let unadjusted_abi_align = align.abi;
13441338
if let Some(repr_align) = repr.align {
1345-
align = align.max(AbiAndPrefAlign::new(repr_align));
1339+
align = align.max(AbiAlign::new(repr_align));
13461340
}
13471341
// `align` must not be modified after this point, or `unadjusted_abi_align` could be inaccurate.
13481342
let align = align;

compiler/rustc_abi/src/layout/ty.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,22 @@ pub const MAX_SIMD_LANES: u64 = 1 << 0xF;
225225
#[derive(Debug, PartialEq, Eq)]
226226
pub struct TargetDataLayout {
227227
pub endian: Endian,
228-
pub i1_align: AbiAndPrefAlign,
229-
pub i8_align: AbiAndPrefAlign,
230-
pub i16_align: AbiAndPrefAlign,
231-
pub i32_align: AbiAndPrefAlign,
232-
pub i64_align: AbiAndPrefAlign,
233-
pub i128_align: AbiAndPrefAlign,
234-
pub f16_align: AbiAndPrefAlign,
235-
pub f32_align: AbiAndPrefAlign,
236-
pub f64_align: AbiAndPrefAlign,
237-
pub f128_align: AbiAndPrefAlign,
228+
pub i1_align: AbiAlign,
229+
pub i8_align: AbiAlign,
230+
pub i16_align: AbiAlign,
231+
pub i32_align: AbiAlign,
232+
pub i64_align: AbiAlign,
233+
pub i128_align: AbiAlign,
234+
pub f16_align: AbiAlign,
235+
pub f32_align: AbiAlign,
236+
pub f64_align: AbiAlign,
237+
pub f128_align: AbiAlign,
238238
pub pointer_size: Size,
239-
pub pointer_align: AbiAndPrefAlign,
240-
pub aggregate_align: AbiAndPrefAlign,
239+
pub pointer_align: AbiAlign,
240+
pub aggregate_align: AbiAlign,
241241

242242
/// Alignments for vector types.
243-
pub vector_align: Vec<(Size, AbiAndPrefAlign)>,
243+
pub vector_align: Vec<(Size, AbiAlign)>,
244244

245245
pub instruction_address_space: AddressSpace,
246246

@@ -256,22 +256,22 @@ impl Default for TargetDataLayout {
256256
let align = |bits| Align::from_bits(bits).unwrap();
257257
TargetDataLayout {
258258
endian: Endian::Big,
259-
i1_align: AbiAndPrefAlign::new(align(8)),
260-
i8_align: AbiAndPrefAlign::new(align(8)),
261-
i16_align: AbiAndPrefAlign::new(align(16)),
262-
i32_align: AbiAndPrefAlign::new(align(32)),
263-
i64_align: AbiAndPrefAlign { abi: align(32), pref: align(64) },
264-
i128_align: AbiAndPrefAlign { abi: align(32), pref: align(64) },
265-
f16_align: AbiAndPrefAlign::new(align(16)),
266-
f32_align: AbiAndPrefAlign::new(align(32)),
267-
f64_align: AbiAndPrefAlign::new(align(64)),
268-
f128_align: AbiAndPrefAlign::new(align(128)),
259+
i1_align: AbiAlign::new(align(8)),
260+
i8_align: AbiAlign::new(align(8)),
261+
i16_align: AbiAlign::new(align(16)),
262+
i32_align: AbiAlign::new(align(32)),
263+
i64_align: AbiAlign::new(align(32)),
264+
i128_align: AbiAlign::new(align(32)),
265+
f16_align: AbiAlign::new(align(16)),
266+
f32_align: AbiAlign::new(align(32)),
267+
f64_align: AbiAlign::new(align(64)),
268+
f128_align: AbiAlign::new(align(128)),
269269
pointer_size: Size::from_bits(64),
270-
pointer_align: AbiAndPrefAlign::new(align(64)),
271-
aggregate_align: AbiAndPrefAlign { abi: align(0), pref: align(64) },
270+
pointer_align: AbiAlign::new(align(64)),
271+
aggregate_align: AbiAlign { abi: align(8) },
272272
vector_align: vec![
273-
(Size::from_bits(64), AbiAndPrefAlign::new(align(64))),
274-
(Size::from_bits(128), AbiAndPrefAlign::new(align(128))),
273+
(Size::from_bits(64), AbiAlign::new(align(64))),
274+
(Size::from_bits(128), AbiAlign::new(align(128))),
275275
],
276276
instruction_address_space: AddressSpace::DATA,
277277
c_enum_min_size: Integer::I32,
@@ -329,8 +329,7 @@ impl TargetDataLayout {
329329
.map_err(|err| TargetDataLayoutErrors::InvalidAlignment { cause, err })
330330
};
331331
let abi = parse_bits(s[0], "alignment", cause)?;
332-
let pref = s.get(1).map_or(Ok(abi), |pref| parse_bits(pref, "alignment", cause))?;
333-
Ok(AbiAndPrefAlign { abi: align_from_bits(abi)?, pref: align_from_bits(pref)? })
332+
Ok(AbiAlign::new(align_from_bits(abi)?))
334333
};
335334

336335
let mut dl = TargetDataLayout::default();
@@ -425,7 +424,7 @@ impl TargetDataLayout {
425424

426425
/// psABI-mandated alignment for a vector type, if any
427426
#[inline]
428-
fn cabi_vector_align(&self, vec_size: Size) -> Option<AbiAndPrefAlign> {
427+
fn cabi_vector_align(&self, vec_size: Size) -> Option<AbiAlign> {
429428
self.vector_align
430429
.iter()
431430
.find(|(size, _align)| *size == vec_size)
@@ -434,8 +433,8 @@ impl TargetDataLayout {
434433

435434
/// an alignment resembling the one LLVM would pick for a vector
436435
#[inline]
437-
pub fn llvmlike_vector_align(&self, vec_size: Size) -> AbiAndPrefAlign {
438-
self.cabi_vector_align(vec_size).unwrap_or(AbiAndPrefAlign::new(
436+
pub fn llvmlike_vector_align(&self, vec_size: Size) -> AbiAlign {
437+
self.cabi_vector_align(vec_size).unwrap_or(AbiAlign::new(
439438
Align::from_bytes(vec_size.bytes().next_power_of_two()).unwrap(),
440439
))
441440
}
@@ -863,25 +862,24 @@ impl Align {
863862
/// It is of effectively no consequence for layout in structs and on the stack.
864863
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
865864
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
866-
pub struct AbiAndPrefAlign {
865+
pub struct AbiAlign {
867866
pub abi: Align,
868-
pub pref: Align,
869867
}
870868

871-
impl AbiAndPrefAlign {
869+
impl AbiAlign {
872870
#[inline]
873-
pub fn new(align: Align) -> AbiAndPrefAlign {
874-
AbiAndPrefAlign { abi: align, pref: align }
871+
pub fn new(align: Align) -> AbiAlign {
872+
AbiAlign { abi: align }
875873
}
876874

877875
#[inline]
878-
pub fn min(self, other: AbiAndPrefAlign) -> AbiAndPrefAlign {
879-
AbiAndPrefAlign { abi: self.abi.min(other.abi), pref: self.pref.min(other.pref) }
876+
pub fn min(self, other: AbiAlign) -> AbiAlign {
877+
AbiAlign { abi: self.abi.min(other.abi) }
880878
}
881879

882880
#[inline]
883-
pub fn max(self, other: AbiAndPrefAlign) -> AbiAndPrefAlign {
884-
AbiAndPrefAlign { abi: self.abi.max(other.abi), pref: self.pref.max(other.pref) }
881+
pub fn max(self, other: AbiAlign) -> AbiAlign {
882+
AbiAlign { abi: self.abi.max(other.abi) }
885883
}
886884
}
887885

@@ -944,7 +942,7 @@ impl Integer {
944942
}
945943
}
946944

947-
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign {
945+
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAlign {
948946
use Integer::*;
949947
let dl = cx.data_layout();
950948

@@ -1057,7 +1055,7 @@ impl Float {
10571055
}
10581056
}
10591057

1060-
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign {
1058+
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAlign {
10611059
use Float::*;
10621060
let dl = cx.data_layout();
10631061

@@ -1101,7 +1099,7 @@ impl Primitive {
11011099
}
11021100
}
11031101

1104-
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign {
1102+
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAlign {
11051103
use Primitive::*;
11061104
let dl = cx.data_layout();
11071105

@@ -1224,7 +1222,7 @@ impl Scalar {
12241222
}
12251223
}
12261224

1227-
pub fn align(self, cx: &impl HasDataLayout) -> AbiAndPrefAlign {
1225+
pub fn align(self, cx: &impl HasDataLayout) -> AbiAlign {
12281226
self.primitive().align(cx)
12291227
}
12301228

@@ -1730,7 +1728,7 @@ pub struct LayoutData<FieldIdx: Idx, VariantIdx: Idx> {
17301728
/// especially in the case of by-pointer struct returns, which allocate stack even when unused.
17311729
pub uninhabited: bool,
17321730

1733-
pub align: AbiAndPrefAlign,
1731+
pub align: AbiAlign,
17341732
pub size: Size,
17351733

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

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -812,11 +812,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
812812
dest.write_cvalue(fx, val);
813813
}
814814

815-
sym::pref_align_of
816-
| sym::needs_drop
817-
| sym::type_id
818-
| sym::type_name
819-
| sym::variant_count => {
815+
sym::needs_drop | sym::type_id | sym::type_name | sym::variant_count => {
820816
intrinsic_args!(fx, args => (); intrinsic);
821817

822818
let const_val = fx

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
158158
}
159159
value
160160
}
161-
sym::pref_align_of
162-
| sym::needs_drop
163-
| sym::type_id
164-
| sym::type_name
165-
| sym::variant_count => {
161+
sym::needs_drop | sym::type_id | sym::type_name | sym::variant_count => {
166162
let value = bx.tcx().const_eval_instance(bx.typing_env(), instance, span).unwrap();
167163
OperandRef::from_const(bx, value, ret_ty).immediate_or_packed_pair(bx)
168164
}

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 2 additions & 13 deletions
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())
@@ -144,14 +137,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
144137
self.write_scalar(Scalar::from_target_usize(result, self), dest)?;
145138
}
146139

147-
sym::pref_align_of
148-
| sym::needs_drop
149-
| sym::type_id
150-
| sym::type_name
151-
| sym::variant_count => {
140+
sym::needs_drop | sym::type_id | sym::type_name | sym::variant_count => {
152141
let gid = GlobalId { instance, promoted: None };
153142
let ty = match intrinsic_name {
154-
sym::pref_align_of | sym::variant_count => self.tcx.types.usize,
143+
sym::variant_count => self.tcx.types.usize,
155144
sym::needs_drop => self.tcx.types.bool,
156145
sym::type_id => self.tcx.types.u128,
157146
sym::type_name => Ty::new_static_str(self.tcx.tcx),

compiler/rustc_feature/src/removed.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ declare_features! (
207207
/// Allows exhaustive integer pattern matching with `usize::MAX`/`isize::MIN`/`isize::MAX`.
208208
(removed, precise_pointer_size_matching, "1.32.0", Some(56354),
209209
Some("removed in favor of half-open ranges")),
210+
(removed, pref_align_of, "CURRENT_RUSTC_VERSION", Some(91971),
211+
Some("removed due to marginal use and inducing compiler complications")),
210212
(removed, proc_macro_expr, "1.27.0", Some(54727),
211213
Some("subsumed by `#![feature(proc_macro_hygiene)]`")),
212214
(removed, proc_macro_gen, "1.27.0", Some(54727),

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub(crate) fn check_intrinsic_type(
235235
sym::abort => (0, 0, vec![], tcx.types.never),
236236
sym::unreachable => (0, 0, vec![], tcx.types.never),
237237
sym::breakpoint => (0, 0, vec![], tcx.types.unit),
238-
sym::size_of | sym::pref_align_of | sym::min_align_of | sym::variant_count => {
238+
sym::size_of | sym::min_align_of | sym::variant_count => {
239239
(1, 0, vec![], tcx.types.usize)
240240
}
241241
sym::size_of_val | sym::min_align_of_val => {

library/core/src/intrinsics/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3489,15 +3489,6 @@ pub const fn size_of<T>() -> usize;
34893489
#[rustc_intrinsic]
34903490
pub const fn min_align_of<T>() -> usize;
34913491

3492-
/// The preferred alignment of a type.
3493-
///
3494-
/// This intrinsic does not have a stable counterpart.
3495-
/// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971).
3496-
#[rustc_nounwind]
3497-
#[unstable(feature = "core_intrinsics", issue = "none")]
3498-
#[rustc_intrinsic]
3499-
pub const unsafe fn pref_align_of<T>() -> usize;
3500-
35013492
/// Returns the number of variants of the type `T` cast to a `usize`;
35023493
/// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
35033494
///

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

Lines changed: 2 additions & 4 deletions
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
backend_repr: Memory {
1312
sized: true,
@@ -34,9 +33,8 @@ error: fn_abi_of(pass_zst) = FnAbi {
3433
ty: (),
3534
layout: Layout {
3635
size: Size(0 bytes),
37-
align: AbiAndPrefAlign {
36+
align: AbiAlign {
3837
abi: $SOME_ALIGN,
39-
pref: $SOME_ALIGN,
4038
},
4139
backend_repr: Memory {
4240
sized: true,

0 commit comments

Comments
 (0)