Skip to content

Commit 74e9ca6

Browse files
authored
Rollup merge of #137852 - moulins:layout-nonarray-simd-deadcode, r=workingjubilee
Remove layouting dead code for non-array SIMD types. These aren't supported anymore, and are already rejected in type checking.
2 parents 1b4e66e + cbe32a7 commit 74e9ca6

File tree

4 files changed

+30
-114
lines changed

4 files changed

+30
-114
lines changed

compiler/rustc_ty_utils/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ ty_utils_logical_op_not_supported = unsupported operation in generic constants,
4242
4343
ty_utils_loop_not_supported = loops and loop control flow are not supported in generic constants
4444
45-
ty_utils_multiple_array_fields_simd_type = monomorphising SIMD type `{$ty}` with more than one array field
46-
4745
ty_utils_needs_drop_overflow = overflow while checking whether `{$query_ty}` requires drop
4846
4947
ty_utils_never_to_any_not_supported = coercing the `never` type is not supported in generic constants

compiler/rustc_ty_utils/src/errors.rs

-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ pub(crate) struct ZeroLengthSimdType<'tcx> {
8282
pub ty: Ty<'tcx>,
8383
}
8484

85-
#[derive(Diagnostic)]
86-
#[diag(ty_utils_multiple_array_fields_simd_type)]
87-
pub(crate) struct MultipleArrayFieldsSimdType<'tcx> {
88-
pub ty: Ty<'tcx>,
89-
}
90-
9185
#[derive(Diagnostic)]
9286
#[diag(ty_utils_oversized_simd_type)]
9387
pub(crate) struct OversizedSimdType<'tcx> {

compiler/rustc_ty_utils/src/layout.rs

+19-70
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ use rustc_span::{Symbol, sym};
2828
use tracing::{debug, instrument, trace};
2929
use {rustc_abi as abi, rustc_hir as hir};
3030

31-
use crate::errors::{
32-
MultipleArrayFieldsSimdType, NonPrimitiveSimdType, OversizedSimdType, ZeroLengthSimdType,
33-
};
31+
use crate::errors::{NonPrimitiveSimdType, OversizedSimdType, ZeroLengthSimdType};
3432

3533
mod invariant;
3634

@@ -450,71 +448,26 @@ fn layout_of_uncached<'tcx>(
450448

451449
// SIMD vector types.
452450
ty::Adt(def, args) if def.repr().simd() => {
453-
if !def.is_struct() {
454-
// Should have yielded E0517 by now.
455-
let guar = tcx
456-
.dcx()
457-
.delayed_bug("#[repr(simd)] was applied to an ADT that is not a struct");
458-
return Err(error(cx, LayoutError::ReferencesError(guar)));
459-
}
460-
461-
let fields = &def.non_enum_variant().fields;
462-
463-
// Supported SIMD vectors are homogeneous ADTs with at least one field:
451+
// Supported SIMD vectors are ADTs with a single array field:
464452
//
465-
// * #[repr(simd)] struct S(T, T, T, T);
466-
// * #[repr(simd)] struct S { x: T, y: T, z: T, w: T }
467453
// * #[repr(simd)] struct S([T; 4])
468454
//
469455
// where T is a primitive scalar (integer/float/pointer).
470-
471-
// SIMD vectors with zero fields are not supported.
472-
// (should be caught by typeck)
473-
if fields.is_empty() {
474-
tcx.dcx().emit_fatal(ZeroLengthSimdType { ty })
475-
}
476-
477-
// Type of the first ADT field:
478-
let f0_ty = fields[FieldIdx::ZERO].ty(tcx, args);
479-
480-
// Heterogeneous SIMD vectors are not supported:
481-
// (should be caught by typeck)
482-
for fi in fields {
483-
if fi.ty(tcx, args) != f0_ty {
484-
let guar = tcx.dcx().delayed_bug(
485-
"#[repr(simd)] was applied to an ADT with heterogeneous field type",
486-
);
487-
return Err(error(cx, LayoutError::ReferencesError(guar)));
488-
}
489-
}
490-
491-
// The element type and number of elements of the SIMD vector
492-
// are obtained from:
493-
//
494-
// * the element type and length of the single array field, if
495-
// the first field is of array type, or
496-
//
497-
// * the homogeneous field type and the number of fields.
498-
let (e_ty, e_len, is_array) = if let ty::Array(e_ty, _) = f0_ty.kind() {
499-
// First ADT field is an array:
500-
501-
// SIMD vectors with multiple array fields are not supported:
502-
// Can't be caught by typeck with a generic simd type.
503-
if def.non_enum_variant().fields.len() != 1 {
504-
tcx.dcx().emit_fatal(MultipleArrayFieldsSimdType { ty });
505-
}
506-
507-
// Extract the number of elements from the layout of the array field:
508-
let FieldsShape::Array { count, .. } = cx.layout_of(f0_ty)?.layout.fields() else {
509-
return Err(error(cx, LayoutError::Unknown(ty)));
510-
};
511-
512-
(*e_ty, *count, true)
513-
} else {
514-
// First ADT field is not an array:
515-
(f0_ty, def.non_enum_variant().fields.len() as _, false)
456+
let Some(ty::Array(e_ty, e_len)) = def
457+
.is_struct()
458+
.then(|| &def.variant(FIRST_VARIANT).fields)
459+
.filter(|fields| fields.len() == 1)
460+
.map(|fields| *fields[FieldIdx::ZERO].ty(tcx, args).kind())
461+
else {
462+
// Invalid SIMD types should have been caught by typeck by now.
463+
let guar = tcx.dcx().delayed_bug("#[repr(simd)] was applied to an invalid ADT");
464+
return Err(error(cx, LayoutError::ReferencesError(guar)));
516465
};
517466

467+
let e_len = extract_const_value(cx, ty, e_len)?
468+
.try_to_target_usize(tcx)
469+
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
470+
518471
// SIMD vectors of zero length are not supported.
519472
// Additionally, lengths are capped at 2^16 as a fixed maximum backends must
520473
// support.
@@ -559,16 +512,12 @@ fn layout_of_uncached<'tcx>(
559512
};
560513
let size = size.align_to(align.abi);
561514

562-
// Compute the placement of the vector fields:
563-
let fields = if is_array {
564-
FieldsShape::Arbitrary { offsets: [Size::ZERO].into(), memory_index: [0].into() }
565-
} else {
566-
FieldsShape::Array { stride: e_ly.size, count: e_len }
567-
};
568-
569515
tcx.mk_layout(LayoutData {
570516
variants: Variants::Single { index: FIRST_VARIANT },
571-
fields,
517+
fields: FieldsShape::Arbitrary {
518+
offsets: [Size::ZERO].into(),
519+
memory_index: [0].into(),
520+
},
572521
backend_repr: abi,
573522
largest_niche: e_ly.largest_niche,
574523
uninhabited: false,

src/tools/rust-analyzer/crates/hir-ty/src/layout.rs

+11-36
Original file line numberDiff line numberDiff line change
@@ -133,40 +133,22 @@ fn layout_of_simd_ty(
133133
env: Arc<TraitEnvironment>,
134134
dl: &TargetDataLayout,
135135
) -> Result<Arc<Layout>, LayoutError> {
136-
let fields = db.field_types(id.into());
137-
138-
// Supported SIMD vectors are homogeneous ADTs with at least one field:
136+
// Supported SIMD vectors are homogeneous ADTs with exactly one array field:
139137
//
140-
// * #[repr(simd)] struct S(T, T, T, T);
141-
// * #[repr(simd)] struct S { it: T, y: T, z: T, w: T }
142138
// * #[repr(simd)] struct S([T; 4])
143139
//
144140
// where T is a primitive scalar (integer/float/pointer).
145-
146-
let f0_ty = match fields.iter().next() {
147-
Some(it) => it.1.clone().substitute(Interner, subst),
148-
None => return Err(LayoutError::InvalidSimdType),
141+
let fields = db.field_types(id.into());
142+
let mut fields = fields.iter();
143+
let Some(TyKind::Array(e_ty, e_len)) = fields
144+
.next()
145+
.filter(|_| fields.next().is_none())
146+
.map(|f| f.1.clone().substitute(Interner, subst).kind(Interner).clone())
147+
else {
148+
return Err(LayoutError::InvalidSimdType);
149149
};
150150

151-
// The element type and number of elements of the SIMD vector
152-
// are obtained from:
153-
//
154-
// * the element type and length of the single array field, if
155-
// the first field is of array type, or
156-
//
157-
// * the homogeneous field type and the number of fields.
158-
let (e_ty, e_len, is_array) = if let TyKind::Array(e_ty, _) = f0_ty.kind(Interner) {
159-
// Extract the number of elements from the layout of the array field:
160-
let FieldsShape::Array { count, .. } = db.layout_of_ty(f0_ty.clone(), env.clone())?.fields
161-
else {
162-
return Err(LayoutError::Unknown);
163-
};
164-
165-
(e_ty.clone(), count, true)
166-
} else {
167-
// First ADT field is not an array:
168-
(f0_ty, fields.iter().count() as u64, false)
169-
};
151+
let e_len = try_const_usize(db, &e_len).ok_or(LayoutError::HasErrorConst)? as u64;
170152

171153
// Compute the ABI of the element type:
172154
let e_ly = db.layout_of_ty(e_ty, env)?;
@@ -182,16 +164,9 @@ fn layout_of_simd_ty(
182164
let align = dl.llvmlike_vector_align(size);
183165
let size = size.align_to(align.abi);
184166

185-
// Compute the placement of the vector fields:
186-
let fields = if is_array {
187-
FieldsShape::Arbitrary { offsets: [Size::ZERO].into(), memory_index: [0].into() }
188-
} else {
189-
FieldsShape::Array { stride: e_ly.size, count: e_len }
190-
};
191-
192167
Ok(Arc::new(Layout {
193168
variants: Variants::Single { index: struct_variant_idx() },
194-
fields,
169+
fields: FieldsShape::Arbitrary { offsets: [Size::ZERO].into(), memory_index: [0].into() },
195170
backend_repr: BackendRepr::SimdVector { element: e_abi, count: e_len },
196171
largest_niche: e_ly.largest_niche,
197172
uninhabited: false,

0 commit comments

Comments
 (0)