Skip to content

Commit 8d1fb7e

Browse files
committed
Remove VariantLayout::largest_niche field
The only place inspecting the niches of a variant layout is `compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs`, which can be rewritten to use the niches of the enclosing enum instead.
1 parent d87d0cf commit 8d1fb7e

18 files changed

Lines changed: 42 additions & 179 deletions

compiler/rustc_abi/src/layout.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -592,14 +592,24 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
592592
let mut unadjusted_abi_align = align;
593593
let mut combined_seed = repr.field_shuffle_seed;
594594

595+
let mut largest_variant_size = Size::ZERO;
596+
let mut largest_variant_index = VariantIdx::new(0);
597+
let mut largest_variant_niche = None;
598+
595599
let mut variants_info = IndexVec::<VariantIdx, _>::with_capacity(variants.len());
596600
let mut variant_layouts = variants
597-
.iter()
598-
.map(|v| {
601+
.iter_enumerated()
602+
.map(|(i, v)| {
599603
let st = self.univariant(v, repr, StructKind::AlwaysSized).ok()?;
600604

601605
variants_info.push(VariantLayoutInfo { align_abi: st.align.abi });
602606

607+
if st.size > largest_variant_size {
608+
largest_variant_index = i;
609+
largest_variant_size = st.size;
610+
largest_variant_niche = st.largest_niche;
611+
}
612+
603613
align = align.max(st.align.abi);
604614
max_repr_align = max_repr_align.max(st.max_repr_align);
605615
unadjusted_abi_align = unadjusted_abi_align.max(st.unadjusted_abi_align);
@@ -609,11 +619,6 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
609619
})
610620
.collect::<Option<IndexVec<VariantIdx, _>>>()?;
611621

612-
let largest_variant_index = variant_layouts
613-
.iter_enumerated()
614-
.max_by_key(|(_i, layout)| layout.size.bytes())
615-
.map(|(i, _layout)| i)?;
616-
617622
let all_indices = variants.indices();
618623
let needs_disc =
619624
|index: VariantIdx| index != largest_variant_index && !absent(&variants[index]);
@@ -624,19 +629,17 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
624629
(niche_variants.end().index() as u128 - niche_variants.start().index() as u128) + 1;
625630

626631
// Use the largest niche in the largest variant.
627-
let niche = variant_layouts[largest_variant_index].largest_niche?;
632+
let niche = largest_variant_niche?;
628633
let (niche_start, niche_scalar) = niche.reserve(dl, count)?;
629634
let niche_offset = niche.offset;
630635
let niche_size = niche.value.size(dl);
631-
let size = variant_layouts[largest_variant_index].size.align_to(align);
636+
let size = largest_variant_size.align_to(align);
632637

633638
let all_variants_fit = variant_layouts.iter_enumerated_mut().all(|(i, layout)| {
634639
if i == largest_variant_index {
635640
return true;
636641
}
637642

638-
layout.largest_niche = None;
639-
640643
if layout.size <= niche_offset {
641644
// This variant will fit before the niche.
642645
return true;

compiler/rustc_abi/src/layout/simple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
161161
},
162162
variants: Variants::Single { index },
163163
backend_repr: layout.backend_repr,
164-
largest_niche: layout.largest_niche,
164+
largest_niche: None,
165165
uninhabited: layout.uninhabited,
166166
size: layout.size,
167167
align: parent.align,

compiler/rustc_abi/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,7 +2239,6 @@ pub struct VariantLayout<FieldIdx: Idx> {
22392239
pub backend_repr: BackendRepr,
22402240
pub field_offsets: IndexVec<FieldIdx, Size>,
22412241
fields_in_memory_order: IndexVec<u32, FieldIdx>,
2242-
largest_niche: Option<Niche>,
22432242
uninhabited: bool,
22442243
}
22452244

@@ -2254,7 +2253,6 @@ impl<FieldIdx: Idx> VariantLayout<FieldIdx> {
22542253
backend_repr: layout.backend_repr,
22552254
field_offsets: offsets,
22562255
fields_in_memory_order: in_memory_order,
2257-
largest_niche: layout.largest_niche,
22582256
uninhabited: layout.uninhabited,
22592257
}
22602258
}

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22

3-
use rustc_abi::{FieldIdx, TagEncoding, VariantIdx, Variants};
3+
use rustc_abi::{FieldIdx, TagEncoding, VariantIdx, Variants, WrappingRange};
44
use rustc_codegen_ssa::debuginfo::type_names::{compute_debuginfo_type_name, cpp_like_debuginfo};
55
use rustc_codegen_ssa::debuginfo::{tag_base_type, wants_c_like_enum_debuginfo};
66
use rustc_codegen_ssa::traits::MiscCodegenMethods;
@@ -399,16 +399,36 @@ fn compute_discriminant_value<'ll, 'tcx>(
399399
),
400400
&Variants::Multiple {
401401
tag_encoding: TagEncoding::Niche { ref niche_variants, niche_start, untagged_variant },
402+
tag_field,
402403
tag,
403404
..
404405
} => {
405406
if variant_index == untagged_variant {
406-
let valid_range = enum_type_and_layout
407-
.for_variant(cx, variant_index)
408-
.largest_niche
409-
.as_ref()
410-
.unwrap()
411-
.valid_range;
407+
let niche_end = niche_start
408+
.wrapping_sub(niche_variants.start().as_u32() as u128)
409+
.wrapping_add(niche_variants.end().as_u32() as u128);
410+
411+
// Remove discriminant values of the other variants from the largest niche. This assumes
412+
// that the largest niche, when it exists, always corresponds to the enum discriminant.
413+
let mut valid_range = WrappingRange {
414+
start: niche_end.wrapping_add(1),
415+
end: niche_start.wrapping_sub(1),
416+
};
417+
if let Some(niche) = &enum_type_and_layout.largest_niche {
418+
assert_eq!(enum_type_and_layout.fields.offset(tag_field.into()), niche.offset);
419+
420+
if niche.valid_range.start == niche_start {
421+
valid_range.end = niche.valid_range.end;
422+
} else if niche.valid_range.end == niche_end {
423+
valid_range.start = niche.valid_range.start;
424+
} else {
425+
bug!(
426+
"largest niche (range: {:?}) doesn't match with niched tag encoding (range: {:?})",
427+
niche.valid_range,
428+
&WrappingRange { start: niche_start, end: niche_end },
429+
)
430+
}
431+
}
412432

413433
let min = valid_range.start.min(valid_range.end);
414434
let min = tag.size(cx).truncate(min);

tests/ui/enum-discriminant/wrapping_niche.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ error: layout_of(UnsignedAroundZero) = Layout {
4949
},
5050
field_offsets: [],
5151
fields_in_memory_order: [],
52-
largest_niche: None,
5352
uninhabited: false,
5453
},
5554
VariantLayout {
@@ -59,7 +58,6 @@ error: layout_of(UnsignedAroundZero) = Layout {
5958
},
6059
field_offsets: [],
6160
fields_in_memory_order: [],
62-
largest_niche: None,
6361
uninhabited: false,
6462
},
6563
VariantLayout {
@@ -69,7 +67,6 @@ error: layout_of(UnsignedAroundZero) = Layout {
6967
},
7068
field_offsets: [],
7169
fields_in_memory_order: [],
72-
largest_niche: None,
7370
uninhabited: false,
7471
},
7572
],
@@ -134,7 +131,6 @@ error: layout_of(SignedAroundZero) = Layout {
134131
},
135132
field_offsets: [],
136133
fields_in_memory_order: [],
137-
largest_niche: None,
138134
uninhabited: false,
139135
},
140136
VariantLayout {
@@ -144,7 +140,6 @@ error: layout_of(SignedAroundZero) = Layout {
144140
},
145141
field_offsets: [],
146142
fields_in_memory_order: [],
147-
largest_niche: None,
148143
uninhabited: false,
149144
},
150145
VariantLayout {
@@ -154,7 +149,6 @@ error: layout_of(SignedAroundZero) = Layout {
154149
},
155150
field_offsets: [],
156151
fields_in_memory_order: [],
157-
largest_niche: None,
158152
uninhabited: false,
159153
},
160154
],

tests/ui/layout/debug.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ error: layout_of(E) = Layout {
4949
},
5050
field_offsets: [],
5151
fields_in_memory_order: [],
52-
largest_niche: None,
5352
uninhabited: false,
5453
},
5554
VariantLayout {
@@ -67,7 +66,6 @@ error: layout_of(E) = Layout {
6766
1,
6867
2,
6968
],
70-
largest_niche: None,
7169
uninhabited: true,
7270
},
7371
],
@@ -228,7 +226,6 @@ error: layout_of(Result<i32, i32>) = Layout {
228226
fields_in_memory_order: [
229227
0,
230228
],
231-
largest_niche: None,
232229
uninhabited: false,
233230
},
234231
VariantLayout {
@@ -255,7 +252,6 @@ error: layout_of(Result<i32, i32>) = Layout {
255252
fields_in_memory_order: [
256253
0,
257254
],
258-
largest_niche: None,
259255
uninhabited: false,
260256
},
261257
],

tests/ui/layout/hexagon-enum.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ error: layout_of(A) = Layout {
4949
},
5050
field_offsets: [],
5151
fields_in_memory_order: [],
52-
largest_niche: None,
5352
uninhabited: false,
5453
},
5554
],
@@ -114,7 +113,6 @@ error: layout_of(B) = Layout {
114113
},
115114
field_offsets: [],
116115
fields_in_memory_order: [],
117-
largest_niche: None,
118116
uninhabited: false,
119117
},
120118
],
@@ -179,7 +177,6 @@ error: layout_of(C) = Layout {
179177
},
180178
field_offsets: [],
181179
fields_in_memory_order: [],
182-
largest_niche: None,
183180
uninhabited: false,
184181
},
185182
],
@@ -244,7 +241,6 @@ error: layout_of(P) = Layout {
244241
},
245242
field_offsets: [],
246243
fields_in_memory_order: [],
247-
largest_niche: None,
248244
uninhabited: false,
249245
},
250246
],
@@ -309,7 +305,6 @@ error: layout_of(T) = Layout {
309305
},
310306
field_offsets: [],
311307
fields_in_memory_order: [],
312-
largest_niche: None,
313308
uninhabited: false,
314309
},
315310
],

tests/ui/layout/issue-96158-scalarpair-payload-might-be-uninit.stderr

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ error: layout_of(MissingPayloadField) = Layout {
7171
fields_in_memory_order: [
7272
0,
7373
],
74-
largest_niche: None,
7574
uninhabited: false,
7675
},
7776
VariantLayout {
@@ -81,7 +80,6 @@ error: layout_of(MissingPayloadField) = Layout {
8180
},
8281
field_offsets: [],
8382
fields_in_memory_order: [],
84-
largest_niche: None,
8583
uninhabited: false,
8684
},
8785
],
@@ -170,7 +168,6 @@ error: layout_of(CommonPayloadField) = Layout {
170168
fields_in_memory_order: [
171169
0,
172170
],
173-
largest_niche: None,
174171
uninhabited: false,
175172
},
176173
VariantLayout {
@@ -197,7 +194,6 @@ error: layout_of(CommonPayloadField) = Layout {
197194
fields_in_memory_order: [
198195
0,
199196
],
200-
largest_niche: None,
201197
uninhabited: false,
202198
},
203199
],
@@ -284,7 +280,6 @@ error: layout_of(CommonPayloadFieldIsMaybeUninit) = Layout {
284280
fields_in_memory_order: [
285281
0,
286282
],
287-
largest_niche: None,
288283
uninhabited: false,
289284
},
290285
VariantLayout {
@@ -310,7 +305,6 @@ error: layout_of(CommonPayloadFieldIsMaybeUninit) = Layout {
310305
fields_in_memory_order: [
311306
0,
312307
],
313-
largest_niche: None,
314308
uninhabited: false,
315309
},
316310
],
@@ -404,16 +398,6 @@ error: layout_of(NicheFirst) = Layout {
404398
0,
405399
1,
406400
],
407-
largest_niche: Some(
408-
Niche {
409-
offset: Size(0 bytes),
410-
value: Int(
411-
I8,
412-
false,
413-
),
414-
valid_range: 0..=2,
415-
},
416-
),
417401
uninhabited: false,
418402
},
419403
VariantLayout {
@@ -423,7 +407,6 @@ error: layout_of(NicheFirst) = Layout {
423407
},
424408
field_offsets: [],
425409
fields_in_memory_order: [],
426-
largest_niche: None,
427410
uninhabited: false,
428411
},
429412
VariantLayout {
@@ -433,7 +416,6 @@ error: layout_of(NicheFirst) = Layout {
433416
},
434417
field_offsets: [],
435418
fields_in_memory_order: [],
436-
largest_niche: None,
437419
uninhabited: false,
438420
},
439421
],
@@ -527,16 +509,6 @@ error: layout_of(NicheSecond) = Layout {
527509
1,
528510
0,
529511
],
530-
largest_niche: Some(
531-
Niche {
532-
offset: Size(0 bytes),
533-
value: Int(
534-
I8,
535-
false,
536-
),
537-
valid_range: 0..=2,
538-
},
539-
),
540512
uninhabited: false,
541513
},
542514
VariantLayout {
@@ -546,7 +518,6 @@ error: layout_of(NicheSecond) = Layout {
546518
},
547519
field_offsets: [],
548520
fields_in_memory_order: [],
549-
largest_niche: None,
550521
uninhabited: false,
551522
},
552523
VariantLayout {
@@ -556,7 +527,6 @@ error: layout_of(NicheSecond) = Layout {
556527
},
557528
field_offsets: [],
558529
fields_in_memory_order: [],
559-
largest_niche: None,
560530
uninhabited: false,
561531
},
562532
],

0 commit comments

Comments
 (0)