Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4c02625
builder: use the Rust types of `asm!` operands to protect against unt…
eddyb Oct 15, 2024
9758996
intrinsics: use `GLOp::RoundEven` for `round_ties_even`.
eddyb May 7, 2025
1a167dd
rustup: update to `nightly-2024-12-01`.
eddyb May 6, 2025
90d1117
Update target specs (remove os=unknown, add crt-static-respected).
eddyb May 2, 2025
337590e
Fix new `clippy::unnecessary_map_or` warnings.
eddyb May 10, 2025
769cf30
rustup: update to `nightly-2024-12-10`.
eddyb May 6, 2025
e47bacf
rustup: update to `nightly-2024-12-15`.
eddyb May 6, 2025
b4d8287
rustup: update to `nightly-2024-12-16`.
eddyb May 6, 2025
7f3c707
rustup: update to `nightly-2024-12-17`.
eddyb May 6, 2025
f9b9873
rustup: update to `nightly-2024-12-20`.
eddyb May 6, 2025
e015a97
rustup: update to `nightly-2024-12-25`.
eddyb May 6, 2025
e78dc4d
rustup: update to `nightly-2025-01-03` (~1.85).
eddyb May 4, 2025
efe2b33
Fix new `clippy::precedence` warnings.
eddyb May 10, 2025
fac23be
rustup: update to `nightly-2025-01-09`.
eddyb Jul 5, 2025
bc88f9d
rustup: update to `nightly-2025-01-12`.
eddyb Jul 5, 2025
fbf8ba1
rustup: update to `nightly-2025-01-13`.
eddyb Jul 5, 2025
e500c04
rustup: update to `nightly-2025-01-24`.
eddyb Jul 5, 2025
b25f64e
rustfmt using `nightly-2025-02-16`.
eddyb May 7, 2025
79017a0
rustup: update to `nightly-2025-02-16` (~1.86).
eddyb May 7, 2025
8e8af28
Fix new clippy warnings.
eddyb May 10, 2025
411b54d
rustc_codegen_spirv: switch to Rust 2024 edition.
eddyb May 7, 2025
8493bee
rustup: update to `nightly-2025-03-29` (~1.87).
eddyb May 7, 2025
a1cd17e
abi: more workarounds for old-style `#[repr(simd)] struct`s.
eddyb May 7, 2025
867f358
Fix new clippy warnings.
eddyb May 12, 2025
9cc759e
rustup: update to `nightly-2025-04-13`.
eddyb May 12, 2025
26d35a0
rustup: update to `nightly-2025-04-14`.
eddyb May 12, 2025
dc53ae1
abi: unhide `bool`'s niche, breaking `Option<bool>` for Rust-GPU but …
eddyb May 12, 2025
e5d9488
Switch all crates to Rust 2024 edition.
LegNeato Apr 30, 2025
a6a2bf8
rustup: update to `nightly-2025-04-27`.
eddyb May 12, 2025
546f071
Fix new `clippy::uninlined_format_args` warnings.
eddyb May 12, 2025
b5fcb49
rustup to `nightly-2025-04-28`
LegNeato Apr 30, 2025
bc17d27
rustup: update to `nightly-2025-05-09` (~1.88).
eddyb May 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
abi: more workarounds for old-style #[repr(simd)] structs.
  • Loading branch information
eddyb committed Jul 9, 2025
commit a1cd17e850484eae617fc06c1bf6262f49839ca9
19 changes: 15 additions & 4 deletions crates/rustc_codegen_spirv/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,16 @@ mod win {",
}
if relative_path == Path::new("src/back/metadata.rs") {
// HACK(eddyb) remove `object` dependency.
src = src.replace("
pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {","
src = src.replace(
"
pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {",
"
pub(crate) fn create_object_file(_: &Session) -> Option<write::Object<'static>> {
None
}
#[cfg(any())]
pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {");
pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {",
);
}

// HACK(eddyb) "typed alloca" patches.
Expand All @@ -225,8 +228,16 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
} else if relative_path == Path::new("src/mir/operand.rs") {
src = src.replace("alloca(field.size,", "typed_alloca(llfield_ty,");

// HACK(eddyb) non-array `#[repr(simd)]` workaround (see `src/abi.rs`).
// HACK(eddyb) non-array `#[repr(simd)]` workarounds (see `src/abi.rs`).
src = src.replace("if constant_ty.is_simd() {", "if false {");
src = src.replace(
"match (self.val, self.layout.backend_repr) {",
"match (self.val, self.layout.backend_repr) {
// `#[repr(simd)]` types are also immediate.
(OperandValue::Immediate(llval), BackendRepr::SimdVector { element, .. }) => {
(Some(element), bx.extract_element(llval, bx.cx().const_usize(i as u64)))
}",
);
}

fs::write(out_path, src)?;
Expand Down
84 changes: 82 additions & 2 deletions crates/rustc_codegen_spirv/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use itertools::Itertools;
use rspirv::spirv::{Dim, ImageFormat, StorageClass, Word};
use rustc_abi::ExternAbi as Abi;
use rustc_abi::{
Align, BackendRepr, FieldIdx, FieldsShape, LayoutData, Primitive, ReprFlags, ReprOptions,
Scalar, Size, TagEncoding, VariantIdx, Variants,
Align, BackendRepr, FieldIdx, FieldsShape, HasDataLayout as _, LayoutData, Primitive,
ReprFlags, ReprOptions, Scalar, Size, TagEncoding, VariantIdx, Variants,
};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::ErrorGuaranteed;
use rustc_hashes::Hash64;
use rustc_index::Idx;
use rustc_middle::query::Providers;
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout};
Expand Down Expand Up @@ -164,6 +165,85 @@ pub(crate) fn provide(providers: &mut Providers) {
}

providers.layout_of = |tcx, key| {
// HACK(eddyb) to special-case any types at all, they must be normalized,
// but when normalization would be needed, `layout_of`'s default provider
// recurses (supposedly for caching reasons), i.e. its calls `layout_of`
// w/ the normalized type in input, which once again reaches this hook,
// without ever needing any explicit normalization here.
let ty = key.value;

// HACK(eddyb) bypassing upstream `#[repr(simd)]` changes (see also
// the later comment above `check_well_formed`, for more details).
let reimplement_old_style_repr_simd = match ty.kind() {
ty::Adt(def, args) if def.repr().simd() && !def.repr().packed() && def.is_struct() => {
Some(def.non_enum_variant()).and_then(|v| {
let (count, e_ty) = v
.fields
.iter()
.map(|f| f.ty(tcx, args))
.dedup_with_count()
.exactly_one()
.ok()?;
let e_len = u64::try_from(count).ok().filter(|&e_len| e_len > 1)?;
Some((def, e_ty, e_len))
})
}
_ => None,
};

// HACK(eddyb) tweaked copy of the old upstream logic for `#[repr(simd)]`:
// https://github.com/rust-lang/rust/blob/1.86.0/compiler/rustc_ty_utils/src/layout.rs#L464-L590
if let Some((adt_def, e_ty, e_len)) = reimplement_old_style_repr_simd {
let cx = rustc_middle::ty::layout::LayoutCx::new(
tcx,
key.typing_env.with_post_analysis_normalized(tcx),
);
let dl = cx.data_layout();

// Compute the ABI of the element type:
let e_ly = cx.layout_of(e_ty)?;
let BackendRepr::Scalar(e_repr) = e_ly.backend_repr else {
// This error isn't caught in typeck, e.g., if
// the element type of the vector is generic.
tcx.dcx().span_fatal(
tcx.def_span(adt_def.did()),
format!(
"SIMD type `{ty}` with a non-primitive-scalar \
(integer/float/pointer) element type `{}`",
e_ly.ty
),
);
};

// Compute the size and alignment of the vector:
let size = e_ly.size.checked_mul(e_len, dl).unwrap();
let align = dl.llvmlike_vector_align(size);
let size = size.align_to(align.abi);

let layout = tcx.mk_layout(LayoutData {
variants: Variants::Single {
index: rustc_abi::FIRST_VARIANT,
},
fields: FieldsShape::Array {
stride: e_ly.size,
count: e_len,
},
backend_repr: BackendRepr::SimdVector {
element: e_repr,
count: e_len,
},
largest_niche: e_ly.largest_niche,
uninhabited: false,
size,
align,
max_repr_align: None,
unadjusted_abi_align: align.abi,
randomization_seed: e_ly.randomization_seed.wrapping_add(Hash64::new(e_len)),
});

return Ok(TyAndLayout { ty, layout });
}

let TyAndLayout { ty, mut layout } =
(rustc_interface::DEFAULT_QUERY_PROVIDERS.layout_of)(tcx, key)?;

Expand Down