Skip to content

Commit 22774c8

Browse files
authored
Rollup merge of #138158 - moulins:move-layout-to-rustc_abi, r=workingjubilee
Move more layouting logic to `rustc_abi` Move all `LayoutData`-constructing code to `rustc_abi`: - Infaillible operations get a new `LayoutData` constructor method; - Faillible ones get a new method on `LayoutCalculator`.
2 parents cff0817 + 1877283 commit 22774c8

File tree

3 files changed

+27
-117
lines changed

3 files changed

+27
-117
lines changed

crates/hir-ty/src/layout.rs

Lines changed: 26 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@ use base_db::ra_salsa::Cycle;
66
use chalk_ir::{AdtId, FloatTy, IntTy, TyKind, UintTy};
77
use hir_def::{
88
layout::{
9-
BackendRepr, FieldsShape, Float, Integer, LayoutCalculator, LayoutCalculatorError,
10-
LayoutData, Primitive, ReprOptions, Scalar, Size, StructKind, TargetDataLayout,
9+
Float, Integer, LayoutCalculator, LayoutCalculatorError,
10+
LayoutData, Primitive, ReprOptions, Scalar, StructKind, TargetDataLayout,
1111
WrappingRange,
1212
},
1313
LocalFieldId, StructId,
1414
};
1515
use la_arena::{Idx, RawIdx};
1616
use rustc_abi::AddressSpace;
17-
use rustc_hashes::Hash64;
18-
use rustc_index::{IndexSlice, IndexVec};
17+
use rustc_index::IndexVec;
1918

2019
use triomphe::Arc;
2120

2221
use crate::{
2322
consteval::try_const_usize,
2423
db::{HirDatabase, InternedClosure},
2524
infer::normalize,
26-
layout::adt::struct_variant_idx,
2725
utils::ClosureSubst,
2826
Interner, ProjectionTy, Substitution, TraitEnvironment, Ty,
2927
};
@@ -125,10 +123,10 @@ impl<'a> LayoutCx<'a> {
125123
}
126124
}
127125

128-
// FIXME: move this to the `rustc_abi`.
129126
fn layout_of_simd_ty(
130127
db: &dyn HirDatabase,
131128
id: StructId,
129+
repr_packed: bool,
132130
subst: &Substitution,
133131
env: Arc<TraitEnvironment>,
134132
dl: &TargetDataLayout,
@@ -149,33 +147,10 @@ fn layout_of_simd_ty(
149147
};
150148

151149
let e_len = try_const_usize(db, &e_len).ok_or(LayoutError::HasErrorConst)? as u64;
152-
153-
// Compute the ABI of the element type:
154150
let e_ly = db.layout_of_ty(e_ty, env)?;
155-
let BackendRepr::Scalar(e_abi) = e_ly.backend_repr else {
156-
return Err(LayoutError::Unknown);
157-
};
158151

159-
// Compute the size and alignment of the vector:
160-
let size = e_ly
161-
.size
162-
.checked_mul(e_len, dl)
163-
.ok_or(LayoutError::BadCalc(LayoutCalculatorError::SizeOverflow))?;
164-
let align = dl.llvmlike_vector_align(size);
165-
let size = size.align_to(align.abi);
166-
167-
Ok(Arc::new(Layout {
168-
variants: Variants::Single { index: struct_variant_idx() },
169-
fields: FieldsShape::Arbitrary { offsets: [Size::ZERO].into(), memory_index: [0].into() },
170-
backend_repr: BackendRepr::SimdVector { element: e_abi, count: e_len },
171-
largest_niche: e_ly.largest_niche,
172-
uninhabited: false,
173-
size,
174-
align,
175-
max_repr_align: None,
176-
unadjusted_abi_align: align.abi,
177-
randomization_seed: Hash64::ZERO,
178-
}))
152+
let cx = LayoutCx::new(dl);
153+
Ok(Arc::new(cx.calc.simd_type(e_ly, e_len, repr_packed)?))
179154
}
180155

181156
pub fn layout_of_ty_query(
@@ -190,13 +165,14 @@ pub fn layout_of_ty_query(
190165
let dl = &*target;
191166
let cx = LayoutCx::new(dl);
192167
let ty = normalize(db, trait_env.clone(), ty);
193-
let result = match ty.kind(Interner) {
168+
let kind = ty.kind(Interner);
169+
let result = match kind {
194170
TyKind::Adt(AdtId(def), subst) => {
195171
if let hir_def::AdtId::StructId(s) = def {
196172
let data = db.struct_data(*s);
197173
let repr = data.repr.unwrap_or_default();
198174
if repr.simd() {
199-
return layout_of_simd_ty(db, *s, subst, trait_env, &target);
175+
return layout_of_simd_ty(db, *s, repr.packed(), subst, trait_env, &target);
200176
}
201177
};
202178
return db.layout_of_adt(*def, subst.clone(), trait_env);
@@ -216,7 +192,7 @@ pub fn layout_of_ty_query(
216192
valid_range: WrappingRange { start: 0, end: 0x10FFFF },
217193
},
218194
),
219-
chalk_ir::Scalar::Int(i) => scalar(
195+
chalk_ir::Scalar::Int(i) => Layout::scalar(dl, scalar_unit(
220196
dl,
221197
Primitive::Int(
222198
match i {
@@ -229,8 +205,8 @@ pub fn layout_of_ty_query(
229205
},
230206
true,
231207
),
232-
),
233-
chalk_ir::Scalar::Uint(i) => scalar(
208+
)),
209+
chalk_ir::Scalar::Uint(i) => Layout::scalar(dl, scalar_unit(
234210
dl,
235211
Primitive::Int(
236212
match i {
@@ -243,16 +219,16 @@ pub fn layout_of_ty_query(
243219
},
244220
false,
245221
),
246-
),
247-
chalk_ir::Scalar::Float(f) => scalar(
222+
)),
223+
chalk_ir::Scalar::Float(f) => Layout::scalar(dl, scalar_unit(
248224
dl,
249225
Primitive::Float(match f {
250226
FloatTy::F16 => Float::F16,
251227
FloatTy::F32 => Float::F32,
252228
FloatTy::F64 => Float::F64,
253229
FloatTy::F128 => Float::F128,
254230
}),
255-
),
231+
)),
256232
},
257233
TyKind::Tuple(len, tys) => {
258234
let kind = if *len == 0 { StructKind::AlwaysSized } else { StructKind::MaybeUnsized };
@@ -268,56 +244,16 @@ pub fn layout_of_ty_query(
268244
TyKind::Array(element, count) => {
269245
let count = try_const_usize(db, count).ok_or(LayoutError::HasErrorConst)? as u64;
270246
let element = db.layout_of_ty(element.clone(), trait_env)?;
271-
let size = element
272-
.size
273-
.checked_mul(count, dl)
274-
.ok_or(LayoutError::BadCalc(LayoutCalculatorError::SizeOverflow))?;
275-
276-
let backend_repr = BackendRepr::Memory { sized: true };
277-
278-
let largest_niche = if count != 0 { element.largest_niche } else { None };
279-
let uninhabited = if count != 0 { element.uninhabited } else { false };
280-
281-
Layout {
282-
variants: Variants::Single { index: struct_variant_idx() },
283-
fields: FieldsShape::Array { stride: element.size, count },
284-
backend_repr,
285-
largest_niche,
286-
uninhabited,
287-
align: element.align,
288-
size,
289-
max_repr_align: None,
290-
unadjusted_abi_align: element.align.abi,
291-
randomization_seed: Hash64::ZERO,
292-
}
247+
cx.calc.array_like::<_, _, ()>(&element, Some(count))?
293248
}
294249
TyKind::Slice(element) => {
295250
let element = db.layout_of_ty(element.clone(), trait_env)?;
296-
Layout {
297-
variants: Variants::Single { index: struct_variant_idx() },
298-
fields: FieldsShape::Array { stride: element.size, count: 0 },
299-
backend_repr: BackendRepr::Memory { sized: false },
300-
largest_niche: None,
301-
uninhabited: false,
302-
align: element.align,
303-
size: Size::ZERO,
304-
max_repr_align: None,
305-
unadjusted_abi_align: element.align.abi,
306-
randomization_seed: Hash64::ZERO,
307-
}
251+
cx.calc.array_like::<_, _, ()>(&element, None)?
252+
}
253+
TyKind::Str => {
254+
let element = scalar_unit(dl, Primitive::Int(Integer::I8, false));
255+
cx.calc.array_like::<_, _, ()>(&Layout::scalar(dl, element), None)?
308256
}
309-
TyKind::Str => Layout {
310-
variants: Variants::Single { index: struct_variant_idx() },
311-
fields: FieldsShape::Array { stride: Size::from_bytes(1), count: 0 },
312-
backend_repr: BackendRepr::Memory { sized: false },
313-
largest_niche: None,
314-
uninhabited: false,
315-
align: dl.i8_align,
316-
size: Size::ZERO,
317-
max_repr_align: None,
318-
unadjusted_abi_align: dl.i8_align.abi,
319-
randomization_seed: Hash64::ZERO,
320-
},
321257
// Potentially-wide pointers.
322258
TyKind::Ref(_, _, pointee) | TyKind::Raw(_, pointee) => {
323259
let mut data_ptr = scalar_unit(dl, Primitive::Pointer(AddressSpace::DATA));
@@ -355,17 +291,12 @@ pub fn layout_of_ty_query(
355291
};
356292

357293
// Effectively a (ptr, meta) tuple.
358-
cx.calc.scalar_pair(data_ptr, metadata)
294+
LayoutData::scalar_pair(dl, data_ptr, metadata)
359295
}
360-
TyKind::FnDef(_, _) => layout_of_unit(&cx)?,
361-
TyKind::Never => cx.calc.layout_of_never_type(),
362-
TyKind::Dyn(_) | TyKind::Foreign(_) => {
363-
let mut unit = layout_of_unit(&cx)?;
364-
match &mut unit.backend_repr {
365-
BackendRepr::Memory { sized } => *sized = false,
366-
_ => return Err(LayoutError::Unknown),
367-
}
368-
unit
296+
TyKind::Never => LayoutData::never_type(dl),
297+
TyKind::FnDef(..) | TyKind::Dyn(_) | TyKind::Foreign(_) => {
298+
let sized = matches!(kind, TyKind::FnDef(..));
299+
LayoutData::unit(dl, sized)
369300
}
370301
TyKind::Function(_) => {
371302
let mut ptr = scalar_unit(dl, Primitive::Pointer(dl.instruction_address_space));
@@ -434,16 +365,6 @@ pub fn layout_of_ty_recover(
434365
Err(LayoutError::RecursiveTypeWithoutIndirection)
435366
}
436367

437-
fn layout_of_unit(cx: &LayoutCx<'_>) -> Result<Layout, LayoutError> {
438-
cx.calc
439-
.univariant::<RustcFieldIdx, RustcEnumVariantIdx, &&Layout>(
440-
IndexSlice::empty(),
441-
&ReprOptions::default(),
442-
StructKind::AlwaysSized,
443-
)
444-
.map_err(Into::into)
445-
}
446-
447368
fn struct_tail_erasing_lifetimes(db: &dyn HirDatabase, pointee: Ty) -> Ty {
448369
match pointee.kind(Interner) {
449370
TyKind::Adt(AdtId(hir_def::AdtId::StructId(i)), subst) => {
@@ -474,9 +395,5 @@ fn scalar_unit(dl: &TargetDataLayout, value: Primitive) -> Scalar {
474395
Scalar::Initialized { value, valid_range: WrappingRange::full(value.size(dl)) }
475396
}
476397

477-
fn scalar(dl: &TargetDataLayout, value: Primitive) -> Layout {
478-
Layout::scalar(dl, scalar_unit(dl, value))
479-
}
480-
481398
#[cfg(test)]
482399
mod tests;

crates/hir-ty/src/layout/adt.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@ use triomphe::Arc;
1616
use crate::{
1717
db::HirDatabase,
1818
lang_items::is_unsafe_cell,
19-
layout::{field_ty, Layout, LayoutError, RustcEnumVariantIdx},
19+
layout::{field_ty, Layout, LayoutError},
2020
Substitution, TraitEnvironment,
2121
};
2222

2323
use super::LayoutCx;
2424

25-
pub(crate) fn struct_variant_idx() -> RustcEnumVariantIdx {
26-
RustcEnumVariantIdx(0)
27-
}
28-
2925
pub fn layout_of_adt_query(
3026
db: &dyn HirDatabase,
3127
def: AdtId,

crates/hir-ty/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ extern crate ra_ap_rustc_index as rustc_index;
1212
#[cfg(feature = "in-rust-tree")]
1313
extern crate rustc_abi;
1414

15-
#[cfg(feature = "in-rust-tree")]
16-
extern crate rustc_hashes;
17-
1815
#[cfg(not(feature = "in-rust-tree"))]
1916
extern crate ra_ap_rustc_abi as rustc_abi;
2017

0 commit comments

Comments
 (0)