Skip to content

Commit 847b6fe

Browse files
Rollup merge of #132246 - workingjubilee:campaign-on-irform, r=compiler-errors
Rename `rustc_abi::Abi` to `BackendRepr` Remove the confabulation of `rustc_abi::Abi` with what "ABI" actually means by renaming it to `BackendRepr`, and rename `Abi::Aggregate` to `BackendRepr::Memory`. The type never actually represented how things are passed, as that has to have `PassMode` considered, at minimum, but rather it just is how we represented some things to the backend. This conflation arose because LLVM, the primary backend at the time, would lower certain IR forms using certain ABIs. Even that only somewhat was true, as it broke down when one ventured significantly afield of what is described by the System V AMD64 ABI either by using different architectures, ABI-modifying IR annotations, the same architecture **with different ISA extensions enabled**, or other... unexpected delights. Unfortunately both names are still somewhat of a misnomer right now, as people have written code for years based on this misunderstanding. Still, their original names are even moreso, and for better or worse, this backend code hasn't received as much maintenance as the rest of the compiler, lately. Actually arriving at a correct end-state will simply require us to disentangle a lot of code in order to fix, much of it pointlessly repeated in several places. Thus this is not an "actual fix", just a way to deflect further misunderstandings.
2 parents 62ba25d + 083a362 commit 847b6fe

File tree

98 files changed

+876
-646
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+876
-646
lines changed

compiler/rustc_abi/src/callconv.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ mod abi {
66
#[cfg(feature = "nightly")]
77
use rustc_macros::HashStable_Generic;
88

9-
#[cfg(feature = "nightly")]
10-
use crate::{Abi, FieldsShape, TyAbiInterface, TyAndLayout};
119
use crate::{Align, HasDataLayout, Size};
10+
#[cfg(feature = "nightly")]
11+
use crate::{BackendRepr, FieldsShape, TyAbiInterface, TyAndLayout};
1212

1313
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
1414
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
@@ -128,27 +128,27 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
128128
where
129129
Ty: TyAbiInterface<'a, C> + Copy,
130130
{
131-
match self.abi {
132-
Abi::Uninhabited => Err(Heterogeneous),
131+
match self.backend_repr {
132+
BackendRepr::Uninhabited => Err(Heterogeneous),
133133

134134
// The primitive for this algorithm.
135-
Abi::Scalar(scalar) => {
135+
BackendRepr::Scalar(scalar) => {
136136
let kind = match scalar.primitive() {
137137
abi::Int(..) | abi::Pointer(_) => RegKind::Integer,
138138
abi::Float(_) => RegKind::Float,
139139
};
140140
Ok(HomogeneousAggregate::Homogeneous(Reg { kind, size: self.size }))
141141
}
142142

143-
Abi::Vector { .. } => {
143+
BackendRepr::Vector { .. } => {
144144
assert!(!self.is_zst());
145145
Ok(HomogeneousAggregate::Homogeneous(Reg {
146146
kind: RegKind::Vector,
147147
size: self.size,
148148
}))
149149
}
150150

151-
Abi::ScalarPair(..) | Abi::Aggregate { sized: true } => {
151+
BackendRepr::ScalarPair(..) | BackendRepr::Memory { sized: true } => {
152152
// Helper for computing `homogeneous_aggregate`, allowing a custom
153153
// starting offset (used below for handling variants).
154154
let from_fields_at =
@@ -246,7 +246,7 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
246246
Ok(result)
247247
}
248248
}
249-
Abi::Aggregate { sized: false } => Err(Heterogeneous),
249+
BackendRepr::Memory { sized: false } => Err(Heterogeneous),
250250
}
251251
}
252252
}

compiler/rustc_abi/src/layout.rs

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

88
use crate::{
9-
Abi, AbiAndPrefAlign, Align, FieldsShape, HasDataLayout, IndexSlice, IndexVec, Integer,
9+
AbiAndPrefAlign, Align, BackendRepr, FieldsShape, HasDataLayout, IndexSlice, IndexVec, Integer,
1010
LayoutData, Niche, NonZeroUsize, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding,
1111
Variants, WrappingRange,
1212
};
@@ -125,7 +125,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
125125
offsets: [Size::ZERO, b_offset].into(),
126126
memory_index: [0, 1].into(),
127127
},
128-
abi: Abi::ScalarPair(a, b),
128+
backend_repr: BackendRepr::ScalarPair(a, b),
129129
largest_niche,
130130
align,
131131
size,
@@ -216,7 +216,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
216216
LayoutData {
217217
variants: Variants::Single { index: VariantIdx::new(0) },
218218
fields: FieldsShape::Primitive,
219-
abi: Abi::Uninhabited,
219+
backend_repr: BackendRepr::Uninhabited,
220220
largest_niche: None,
221221
align: dl.i8_align,
222222
size: Size::ZERO,
@@ -331,7 +331,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
331331

332332
if let Ok(common) = common_non_zst_abi_and_align {
333333
// Discard valid range information and allow undef
334-
let field_abi = field.abi.to_union();
334+
let field_abi = field.backend_repr.to_union();
335335

336336
if let Some((common_abi, common_align)) = common {
337337
if common_abi != field_abi {
@@ -340,7 +340,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
340340
} else {
341341
// Fields with the same non-Aggregate ABI should also
342342
// have the same alignment
343-
if !matches!(common_abi, Abi::Aggregate { .. }) {
343+
if !matches!(common_abi, BackendRepr::Memory { .. }) {
344344
assert_eq!(
345345
common_align, field.align.abi,
346346
"non-Aggregate field with matching ABI but differing alignment"
@@ -369,11 +369,11 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
369369
// If all non-ZST fields have the same ABI, we may forward that ABI
370370
// for the union as a whole, unless otherwise inhibited.
371371
let abi = match common_non_zst_abi_and_align {
372-
Err(AbiMismatch) | Ok(None) => Abi::Aggregate { sized: true },
372+
Err(AbiMismatch) | Ok(None) => BackendRepr::Memory { sized: true },
373373
Ok(Some((abi, _))) => {
374374
if abi.inherent_align(dl).map(|a| a.abi) != Some(align.abi) {
375375
// Mismatched alignment (e.g. union is #[repr(packed)]): disable opt
376-
Abi::Aggregate { sized: true }
376+
BackendRepr::Memory { sized: true }
377377
} else {
378378
abi
379379
}
@@ -387,7 +387,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
387387
Ok(LayoutData {
388388
variants: Variants::Single { index: only_variant_idx },
389389
fields: FieldsShape::Union(union_field_count),
390-
abi,
390+
backend_repr: abi,
391391
largest_niche: None,
392392
align,
393393
size: size.align_to(align.abi),
@@ -434,23 +434,23 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
434434
// Already doesn't have any niches
435435
Scalar::Union { .. } => {}
436436
};
437-
match &mut st.abi {
438-
Abi::Uninhabited => {}
439-
Abi::Scalar(scalar) => hide_niches(scalar),
440-
Abi::ScalarPair(a, b) => {
437+
match &mut st.backend_repr {
438+
BackendRepr::Uninhabited => {}
439+
BackendRepr::Scalar(scalar) => hide_niches(scalar),
440+
BackendRepr::ScalarPair(a, b) => {
441441
hide_niches(a);
442442
hide_niches(b);
443443
}
444-
Abi::Vector { element, count: _ } => hide_niches(element),
445-
Abi::Aggregate { sized: _ } => {}
444+
BackendRepr::Vector { element, count: _ } => hide_niches(element),
445+
BackendRepr::Memory { sized: _ } => {}
446446
}
447447
st.largest_niche = None;
448448
return Ok(st);
449449
}
450450

451451
let (start, end) = scalar_valid_range;
452-
match st.abi {
453-
Abi::Scalar(ref mut scalar) | Abi::ScalarPair(ref mut scalar, _) => {
452+
match st.backend_repr {
453+
BackendRepr::Scalar(ref mut scalar) | BackendRepr::ScalarPair(ref mut scalar, _) => {
454454
// Enlarging validity ranges would result in missed
455455
// optimizations, *not* wrongly assuming the inner
456456
// value is valid. e.g. unions already enlarge validity ranges,
@@ -607,8 +607,8 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
607607
}
608608

609609
// It can't be a Scalar or ScalarPair because the offset isn't 0.
610-
if !layout.abi.is_uninhabited() {
611-
layout.abi = Abi::Aggregate { sized: true };
610+
if !layout.is_uninhabited() {
611+
layout.backend_repr = BackendRepr::Memory { sized: true };
612612
}
613613
layout.size += this_offset;
614614

@@ -627,26 +627,26 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
627627
let same_size = size == variant_layouts[largest_variant_index].size;
628628
let same_align = align == variant_layouts[largest_variant_index].align;
629629

630-
let abi = if variant_layouts.iter().all(|v| v.abi.is_uninhabited()) {
631-
Abi::Uninhabited
630+
let abi = if variant_layouts.iter().all(|v| v.is_uninhabited()) {
631+
BackendRepr::Uninhabited
632632
} else if same_size && same_align && others_zst {
633-
match variant_layouts[largest_variant_index].abi {
633+
match variant_layouts[largest_variant_index].backend_repr {
634634
// When the total alignment and size match, we can use the
635635
// same ABI as the scalar variant with the reserved niche.
636-
Abi::Scalar(_) => Abi::Scalar(niche_scalar),
637-
Abi::ScalarPair(first, second) => {
636+
BackendRepr::Scalar(_) => BackendRepr::Scalar(niche_scalar),
637+
BackendRepr::ScalarPair(first, second) => {
638638
// Only the niche is guaranteed to be initialised,
639639
// so use union layouts for the other primitive.
640640
if niche_offset == Size::ZERO {
641-
Abi::ScalarPair(niche_scalar, second.to_union())
641+
BackendRepr::ScalarPair(niche_scalar, second.to_union())
642642
} else {
643-
Abi::ScalarPair(first.to_union(), niche_scalar)
643+
BackendRepr::ScalarPair(first.to_union(), niche_scalar)
644644
}
645645
}
646-
_ => Abi::Aggregate { sized: true },
646+
_ => BackendRepr::Memory { sized: true },
647647
}
648648
} else {
649-
Abi::Aggregate { sized: true }
649+
BackendRepr::Memory { sized: true }
650650
};
651651

652652
let layout = LayoutData {
@@ -664,7 +664,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
664664
offsets: [niche_offset].into(),
665665
memory_index: [0].into(),
666666
},
667-
abi,
667+
backend_repr: abi,
668668
largest_niche,
669669
size,
670670
align,
@@ -833,14 +833,14 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
833833
end: (max as u128 & tag_mask),
834834
},
835835
};
836-
let mut abi = Abi::Aggregate { sized: true };
836+
let mut abi = BackendRepr::Memory { sized: true };
837837

838-
if layout_variants.iter().all(|v| v.abi.is_uninhabited()) {
839-
abi = Abi::Uninhabited;
838+
if layout_variants.iter().all(|v| v.is_uninhabited()) {
839+
abi = BackendRepr::Uninhabited;
840840
} else if tag.size(dl) == size {
841841
// Make sure we only use scalar layout when the enum is entirely its
842842
// own tag (i.e. it has no padding nor any non-ZST variant fields).
843-
abi = Abi::Scalar(tag);
843+
abi = BackendRepr::Scalar(tag);
844844
} else {
845845
// Try to use a ScalarPair for all tagged enums.
846846
// That's possible only if we can find a common primitive type for all variants.
@@ -864,8 +864,8 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
864864
break;
865865
}
866866
};
867-
let prim = match field.abi {
868-
Abi::Scalar(scalar) => {
867+
let prim = match field.backend_repr {
868+
BackendRepr::Scalar(scalar) => {
869869
common_prim_initialized_in_all_variants &=
870870
matches!(scalar, Scalar::Initialized { .. });
871871
scalar.primitive()
@@ -934,20 +934,22 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
934934
{
935935
// We can use `ScalarPair` only when it matches our
936936
// already computed layout (including `#[repr(C)]`).
937-
abi = pair.abi;
937+
abi = pair.backend_repr;
938938
}
939939
}
940940
}
941941

942942
// If we pick a "clever" (by-value) ABI, we might have to adjust the ABI of the
943943
// variants to ensure they are consistent. This is because a downcast is
944944
// semantically a NOP, and thus should not affect layout.
945-
if matches!(abi, Abi::Scalar(..) | Abi::ScalarPair(..)) {
945+
if matches!(abi, BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)) {
946946
for variant in &mut layout_variants {
947947
// We only do this for variants with fields; the others are not accessed anyway.
948948
// Also do not overwrite any already existing "clever" ABIs.
949-
if variant.fields.count() > 0 && matches!(variant.abi, Abi::Aggregate { .. }) {
950-
variant.abi = abi;
949+
if variant.fields.count() > 0
950+
&& matches!(variant.backend_repr, BackendRepr::Memory { .. })
951+
{
952+
variant.backend_repr = abi;
951953
// Also need to bump up the size and alignment, so that the entire value fits
952954
// in here.
953955
variant.size = cmp::max(variant.size, size);
@@ -970,7 +972,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
970972
memory_index: [0].into(),
971973
},
972974
largest_niche,
973-
abi,
975+
backend_repr: abi,
974976
align,
975977
size,
976978
max_repr_align,
@@ -1252,7 +1254,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
12521254
}
12531255
let mut layout_of_single_non_zst_field = None;
12541256
let sized = unsized_field.is_none();
1255-
let mut abi = Abi::Aggregate { sized };
1257+
let mut abi = BackendRepr::Memory { sized };
12561258

12571259
let optimize_abi = !repr.inhibit_newtype_abi_optimization();
12581260

@@ -1270,16 +1272,16 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
12701272
// Field fills the struct and it has a scalar or scalar pair ABI.
12711273
if offsets[i].bytes() == 0 && align.abi == field.align.abi && size == field.size
12721274
{
1273-
match field.abi {
1275+
match field.backend_repr {
12741276
// For plain scalars, or vectors of them, we can't unpack
12751277
// newtypes for `#[repr(C)]`, as that affects C ABIs.
1276-
Abi::Scalar(_) | Abi::Vector { .. } if optimize_abi => {
1277-
abi = field.abi;
1278+
BackendRepr::Scalar(_) | BackendRepr::Vector { .. } if optimize_abi => {
1279+
abi = field.backend_repr;
12781280
}
12791281
// But scalar pairs are Rust-specific and get
12801282
// treated as aggregates by C ABIs anyway.
1281-
Abi::ScalarPair(..) => {
1282-
abi = field.abi;
1283+
BackendRepr::ScalarPair(..) => {
1284+
abi = field.backend_repr;
12831285
}
12841286
_ => {}
12851287
}
@@ -1288,8 +1290,8 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
12881290

12891291
// Two non-ZST fields, and they're both scalars.
12901292
(Some((i, a)), Some((j, b)), None) => {
1291-
match (a.abi, b.abi) {
1292-
(Abi::Scalar(a), Abi::Scalar(b)) => {
1293+
match (a.backend_repr, b.backend_repr) {
1294+
(BackendRepr::Scalar(a), BackendRepr::Scalar(b)) => {
12931295
// Order by the memory placement, not source order.
12941296
let ((i, a), (j, b)) = if offsets[i] < offsets[j] {
12951297
((i, a), (j, b))
@@ -1315,7 +1317,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
13151317
{
13161318
// We can use `ScalarPair` only when it matches our
13171319
// already computed layout (including `#[repr(C)]`).
1318-
abi = pair.abi;
1320+
abi = pair.backend_repr;
13191321
}
13201322
}
13211323
_ => {}
@@ -1325,8 +1327,8 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
13251327
_ => {}
13261328
}
13271329
}
1328-
if fields.iter().any(|f| f.abi.is_uninhabited()) {
1329-
abi = Abi::Uninhabited;
1330+
if fields.iter().any(|f| f.is_uninhabited()) {
1331+
abi = BackendRepr::Uninhabited;
13301332
}
13311333

13321334
let unadjusted_abi_align = if repr.transparent() {
@@ -1344,7 +1346,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
13441346
Ok(LayoutData {
13451347
variants: Variants::Single { index: VariantIdx::new(0) },
13461348
fields: FieldsShape::Arbitrary { offsets, memory_index },
1347-
abi,
1349+
backend_repr: abi,
13481350
largest_niche,
13491351
align,
13501352
size,

compiler/rustc_abi/src/layout/ty.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ impl<'a> Layout<'a> {
8383
&self.0.0.variants
8484
}
8585

86-
pub fn abi(self) -> Abi {
87-
self.0.0.abi
86+
pub fn backend_repr(self) -> BackendRepr {
87+
self.0.0.backend_repr
8888
}
8989

9090
pub fn largest_niche(self) -> Option<Niche> {
@@ -114,7 +114,7 @@ impl<'a> Layout<'a> {
114114
pub fn is_pointer_like(self, data_layout: &TargetDataLayout) -> bool {
115115
self.size() == data_layout.pointer_size
116116
&& self.align().abi == data_layout.pointer_align.abi
117-
&& matches!(self.abi(), Abi::Scalar(Scalar::Initialized { .. }))
117+
&& matches!(self.backend_repr(), BackendRepr::Scalar(Scalar::Initialized { .. }))
118118
}
119119
}
120120

@@ -196,9 +196,9 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
196196
Ty: TyAbiInterface<'a, C>,
197197
C: HasDataLayout,
198198
{
199-
match self.abi {
200-
Abi::Scalar(scalar) => matches!(scalar.primitive(), Float(F32 | F64)),
201-
Abi::Aggregate { .. } => {
199+
match self.backend_repr {
200+
BackendRepr::Scalar(scalar) => matches!(scalar.primitive(), Float(F32 | F64)),
201+
BackendRepr::Memory { .. } => {
202202
if self.fields.count() == 1 && self.fields.offset(0).bytes() == 0 {
203203
self.field(cx, 0).is_single_fp_element(cx)
204204
} else {

0 commit comments

Comments
 (0)