Skip to content

Commit e63081b

Browse files
committed
codegen: update attr handling for rustc 1d8897a4e
Use exact rust_gpu attribute paths with get_attrs_by_path, let AggregatedSpirvAttributes parse filtered iterators directly, and replace the manual debug filename arena allocation with DroplessArena::alloc_str. Also port the rustc-private API drift in this nightly by importing assert_matches from std, using rustc_span::Spanned directly, and renaming BackendRepr::ScalableVector to SimdScalableVector.
1 parent bbe7d17 commit e63081b

6 files changed

Lines changed: 41 additions & 37 deletions

File tree

crates/rustc_codegen_spirv/src/abi.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub(crate) fn provide(providers: &mut Providers) {
103103

104104
// SPIR-V backend lowers arguments by-value and cannot handle
105105
// backend-specific indirection/casts at this layer.
106-
if matches!(arg.mode, PassMode::Cast { .. } | PassMode::Indirect { .. }) {
106+
if matches!(arg.mode, PassMode::Cast { .. }) {
107107
arg.mode = PassMode::Direct(ArgAttributes::new());
108108
}
109109

@@ -330,8 +330,18 @@ impl<'tcx> ConvSpirvType<'tcx> for TyAndLayout<'tcx> {
330330
span = cx.tcx.def_span(adt.did());
331331
}
332332

333-
#[allow(deprecated)]
334-
let attrs = AggregatedSpirvAttributes::parse(cx, cx.tcx.get_all_attrs(adt.did()));
333+
let attrs = AggregatedSpirvAttributes::parse(
334+
cx,
335+
cx.tcx
336+
.get_attrs_by_path(
337+
adt.did(),
338+
&[cx.sym.rust_gpu, cx.sym.spirv_attr_with_version],
339+
)
340+
.chain(cx.tcx.get_attrs_by_path(
341+
adt.did(),
342+
&[cx.sym.rust_gpu, cx.sym.vector, cx.sym.v1],
343+
)),
344+
);
335345

336346
if let Some(intrinsic_type_attr) = attrs.intrinsic_type.map(|attr| attr.value)
337347
&& let Ok(spirv_type) =
@@ -435,7 +445,7 @@ impl<'tcx> ConvSpirvType<'tcx> for TyAndLayout<'tcx> {
435445
}
436446
.def(span, cx)
437447
}
438-
BackendRepr::ScalableVector { .. } => cx
448+
BackendRepr::SimdScalableVector { .. } => cx
439449
.tcx
440450
.dcx()
441451
.fatal("scalable vectors are not supported in SPIR-V backend"),
@@ -662,7 +672,6 @@ fn trans_aggregate<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>
662672
}
663673
}
664674

665-
#[cfg_attr(not(rustc_codegen_spirv_disable_pqp_cg_ssa), allow(unused))]
666675
// returns (field_offsets, size, align)
667676
pub fn auto_struct_layout(
668677
cx: &CodegenCx<'_>,

crates/rustc_codegen_spirv/src/attr.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ impl AggregatedSpirvAttributes {
153153
///
154154
/// Any errors for malformed/duplicate attributes will have been reported
155155
/// prior to codegen, by the `attr` check pass.
156-
pub fn parse<'tcx>(cx: &CodegenCx<'tcx>, attrs: &'tcx [Attribute]) -> Self {
156+
pub fn parse<'tcx>(
157+
cx: &CodegenCx<'tcx>,
158+
attrs: impl IntoIterator<Item = &'tcx Attribute>,
159+
) -> Self {
157160
let mut aggregated_attrs = Self::default();
158161

159162
// NOTE(eddyb) `span_delayed_bug` ensures that if attribute checking fails
@@ -527,12 +530,17 @@ pub(crate) fn provide(providers: &mut Providers) {
527530
type ParseAttrError = (Span, String);
528531

529532
#[allow(clippy::get_first)]
530-
fn parse_attrs_for_checking<'a>(
531-
sym: &'a Symbols,
532-
attrs: &'a [Attribute],
533-
) -> impl Iterator<Item = Result<(Span, SpirvAttribute), ParseAttrError>> + 'a {
533+
fn parse_attrs_for_checking<'sym, 'attr, I>(
534+
sym: &'sym Symbols,
535+
attrs: I,
536+
) -> impl Iterator<Item = Result<(Span, SpirvAttribute), ParseAttrError>> + 'sym
537+
where
538+
I: IntoIterator<Item = &'attr Attribute> + 'sym,
539+
I::IntoIter: 'sym,
540+
'attr: 'sym,
541+
{
534542
attrs
535-
.iter()
543+
.into_iter()
536544
.map(move |attr| {
537545
// parse the #[rust_gpu::spirv(...)] attr and return the inner list
538546
match attr {

crates/rustc_codegen_spirv/src/builder_spirv.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@ use rspirv::{binary::Assemble, binary::Disassemble};
1515
use rustc_abi::Size;
1616
use rustc_arena::DroplessArena;
1717
use rustc_codegen_ssa::traits::ConstCodegenMethods as _;
18-
use rustc_data_structures::assert_matches;
1918
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2019
use rustc_middle::bug;
2120
use rustc_middle::mir::interpret::ConstAllocation;
2221
use rustc_middle::ty::TyCtxt;
2322
use rustc_span::source_map::SourceMap;
2423
use rustc_span::symbol::Symbol;
2524
use rustc_span::{DUMMY_SP, SourceFile, Span};
25+
use std::assert_matches;
2626
use std::cell::{RefCell, RefMut};
2727
use std::hash::{Hash, Hasher};
2828
use std::iter;
2929
use std::ops::Range;
30-
use std::str;
3130
use std::sync::Arc;
3231
use std::{fs::File, io::Write, path::Path};
3332

@@ -884,23 +883,7 @@ impl<'tcx> BuilderSpirv<'tcx> {
884883
// but sadly that `'_` is the lifetime of the temporary `Arc`,
885884
// not `'tcx`, so we have to arena-allocate to get `&'tcx str`.
886885
let file_name = sf.name.prefer_remapped_unconditionally().to_string_lossy();
887-
let file_name = {
888-
// FIXME(eddyb) it should be possible to arena-allocate a
889-
// `&str` directly, but it would require upstream changes,
890-
// and strings are handled by string interning in `rustc`.
891-
fn arena_alloc_slice<'tcx, T: Copy>(
892-
dropless_arena: &'tcx DroplessArena,
893-
xs: &[T],
894-
) -> &'tcx [T] {
895-
if xs.is_empty() {
896-
&[]
897-
} else {
898-
dropless_arena.alloc_slice(xs)
899-
}
900-
}
901-
str::from_utf8(arena_alloc_slice(self.dropless_arena, file_name.as_bytes()))
902-
.unwrap()
903-
};
886+
let file_name = self.dropless_arena.alloc_str(&file_name);
904887
let file_name_op_string_id = builder.string(file_name.to_owned());
905888

906889
let file_contents = self

crates/rustc_codegen_spirv/src/codegen_cx/declare.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ impl<'tcx> CodegenCx<'tcx> {
142142
self.set_linkage(fn_id, symbol_name.to_owned(), linkage);
143143
}
144144

145-
#[allow(deprecated)]
146-
let attrs = AggregatedSpirvAttributes::parse(self, self.tcx.get_all_attrs(def_id));
145+
let attrs = AggregatedSpirvAttributes::parse(
146+
self,
147+
self.tcx.get_attrs_by_path(
148+
def_id,
149+
&[self.sym.rust_gpu, self.sym.spirv_attr_with_version],
150+
),
151+
);
147152
if let Some(entry) = attrs.entry.map(|attr| attr.value) {
148153
// HACK(eddyb) early insert to let `shader_entry_stub` call this
149154
// very function via `get_fn_addr`.

crates/rustc_codegen_spirv/src/codegen_cx/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rspirv::spirv::{
1313
};
1414
use rustc_abi::FieldsShape;
1515
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods, MiscCodegenMethods as _};
16-
use rustc_data_structures::assert_matches;
1716
use rustc_data_structures::fx::FxHashMap;
1817
use rustc_errors::MultiSpan;
1918
use rustc_hir as hir;
@@ -22,6 +21,7 @@ use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
2221
use rustc_middle::ty::{self, Instance, Ty};
2322
use rustc_span::{DUMMY_SP, Span};
2423
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
24+
use std::assert_matches;
2525

2626
/// Various information about an entry-point parameter, which can only be deduced
2727
/// (and/or checked) in all cases by using the original reference/value Rust type

crates/rustc_codegen_spirv/src/codegen_cx/type_.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use rustc_middle::ty::layout::{
1313
FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
1414
};
1515
use rustc_middle::{bug, span_bug};
16-
use rustc_span::source_map::Spanned;
17-
use rustc_span::{DUMMY_SP, Span};
16+
use rustc_span::{DUMMY_SP, Span, Spanned};
1817
use rustc_target::callconv::{CastTarget, FnAbi};
1918

2019
impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'tcx> {
@@ -98,7 +97,7 @@ impl<'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'tcx> {
9897
fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool {
9998
match layout.backend_repr {
10099
BackendRepr::Scalar(_)
101-
| BackendRepr::ScalableVector { .. }
100+
| BackendRepr::SimdScalableVector { .. }
102101
| BackendRepr::SimdVector { .. } => true,
103102
BackendRepr::ScalarPair(..) => false,
104103
BackendRepr::Memory { .. } => layout.is_zst(),
@@ -109,7 +108,7 @@ impl<'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'tcx> {
109108
match layout.backend_repr {
110109
BackendRepr::ScalarPair(..) => true,
111110
BackendRepr::Scalar(_)
112-
| BackendRepr::ScalableVector { .. }
111+
| BackendRepr::SimdScalableVector { .. }
113112
| BackendRepr::SimdVector { .. }
114113
| BackendRepr::Memory { .. } => false,
115114
}

0 commit comments

Comments
 (0)