Skip to content

Commit 4ab2a3c

Browse files
moulinsworkingjubilee
authored andcommitted
Use rustc_abi code for SIMD layout in rust-analyzer
1 parent 66ebf38 commit 4ab2a3c

File tree

3 files changed

+7
-39
lines changed

3 files changed

+7
-39
lines changed

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

+6-31
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ 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;
1817
use rustc_index::IndexVec;
1918

2019
use triomphe::Arc;
@@ -23,7 +22,6 @@ 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(
@@ -197,7 +172,7 @@ pub fn layout_of_ty_query(
197172
let data = db.struct_data(*s);
198173
let repr = data.repr.unwrap_or_default();
199174
if repr.simd() {
200-
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);
201176
}
202177
};
203178
return db.layout_of_adt(*def, subst.clone(), trait_env);

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

+1-5
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,

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

-3
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)