Skip to content

Commit 7db643c

Browse files
committed
Port backend trait surface to rustc 1.96 APIs
1 parent 10389c9 commit 7db643c

12 files changed

Lines changed: 147 additions & 164 deletions

File tree

crates/rustc_codegen_spirv/build.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,6 @@ pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {",
322322
let pqp_cg_ssa_top_level = all_extern_crates
323323
+ r#"
324324
325-
// HACK(eddyb) reexporting macro output for further macro use (can't patch macro).
326-
use maybe_pqp_cg_ssa::fluent_generated;
327-
328325
#[allow(unused, clippy::all, clippy::pedantic, clippy::restriction)]
329326
#[path = "pqp_cg_ssa/src/lib.rs"]
330327
mod maybe_pqp_cg_ssa;

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,11 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
18251825
self.declare_func_local_var(self.type_array(self.type_i8(), size.bytes()), align)
18261826
}
18271827

1828+
fn scalable_alloca(&mut self, elt: u64, align: Align, element_ty: Ty<'_>) -> Self::Value {
1829+
let element = self.layout_of(element_ty).spirv_type(self.span(), self);
1830+
self.declare_func_local_var(self.type_array(element, elt), align)
1831+
}
1832+
18281833
fn load(&mut self, ty: Self::Type, ptr: Self::Value, _align: Align) -> Self::Value {
18291834
let (ptr, access_ty) = self.adjust_pointer_for_typed_access(ptr, ty);
18301835
let loaded_val = ptr.const_fold_load(self).unwrap_or_else(|| {
@@ -3077,6 +3082,10 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
30773082
todo!()
30783083
}
30793084

3085+
fn get_funclet_cleanuppad(&self, _funclet: &Self::Funclet) -> Self::Value {
3086+
bug!("Funclets are not supported")
3087+
}
3088+
30803089
fn atomic_cmpxchg(
30813090
&mut self,
30823091
dst: Self::Value,

crates/rustc_codegen_spirv/src/builder/intrinsics.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
8383
let ret_ty = self.layout_of(sig.output()).spirv_type(self.span(), self);
8484

8585
let value = match name {
86-
sym::likely | sym::unlikely => {
86+
_ if name == sym::unlikely || name.as_str() == "likely" => {
8787
// Ignore these for now.
8888
args[0].immediate()
8989
}
@@ -369,6 +369,15 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
369369
Ok(())
370370
}
371371

372+
fn codegen_llvm_intrinsic_call(
373+
&mut self,
374+
instance: ty::Instance<'tcx>,
375+
_args: &[OperandRef<'tcx, Self::Value>],
376+
_is_cleanup: bool,
377+
) -> Self::Value {
378+
bug!("LLVM intrinsic call not supported in SPIR-V backend: {instance:?}")
379+
}
380+
372381
fn abort(&mut self) {
373382
self.abort_with_kind_and_message_debug_printf("abort", "intrinsics::abort() called", []);
374383
}
@@ -386,7 +395,7 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
386395
&mut self,
387396
_llvtable: Self::Value,
388397
_vtable_byte_offset: u64,
389-
_typeid: Self::Metadata,
398+
_typeid: &[u8],
390399
) -> Self::Value {
391400
todo!()
392401
}

crates/rustc_codegen_spirv/src/builder/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ impl<'a, 'tcx> StaticBuilderMethods for Builder<'a, 'tcx> {
291291

292292
impl<'a, 'tcx> BackendTypes for Builder<'a, 'tcx> {
293293
type Value = <CodegenCx<'tcx> as BackendTypes>::Value;
294-
type Metadata = <CodegenCx<'tcx> as BackendTypes>::Metadata;
295294
type Function = <CodegenCx<'tcx> as BackendTypes>::Function;
296295

297296
type BasicBlock = <CodegenCx<'tcx> as BackendTypes>::BasicBlock;
298297
type Type = <CodegenCx<'tcx> as BackendTypes>::Type;
298+
type FunctionSignature = <CodegenCx<'tcx> as BackendTypes>::FunctionSignature;
299299
type Funclet = <CodegenCx<'tcx> as BackendTypes>::Funclet;
300300

301301
type DIScope = <CodegenCx<'tcx> as BackendTypes>::DIScope;

crates/rustc_codegen_spirv/src/builder_spirv.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ 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;
1819
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1920
use rustc_middle::bug;
2021
use rustc_middle::mir::interpret::ConstAllocation;
2122
use rustc_middle::ty::TyCtxt;
2223
use rustc_span::source_map::SourceMap;
2324
use rustc_span::symbol::Symbol;
24-
use rustc_span::{DUMMY_SP, FileName, FileNameDisplayPreference, SourceFile, Span};
25-
use std::assert_matches::assert_matches;
25+
use rustc_span::{DUMMY_SP, SourceFile, Span};
2626
use std::cell::{RefCell, RefMut};
2727
use std::hash::{Hash, Hasher};
2828
use std::iter;
@@ -883,12 +883,7 @@ impl<'tcx> BuilderSpirv<'tcx> {
883883
// `RealFileName::to_string_lossy` returning `Cow<'_, str>`,
884884
// but sadly that `'_` is the lifetime of the temporary `Arc`,
885885
// not `'tcx`, so we have to arena-allocate to get `&'tcx str`.
886-
let file_name = match &sf.name {
887-
FileName::Real(name) => {
888-
name.to_string_lossy(FileNameDisplayPreference::Remapped)
889-
}
890-
_ => sf.name.prefer_remapped_unconditionally().to_string().into(),
891-
};
886+
let file_name = sf.name.prefer_remapped_unconditionally().to_string_lossy();
892887
let file_name = {
893888
// FIXME(eddyb) it should be possible to arena-allocate a
894889
// `&str` directly, but it would require upstream changes,

crates/rustc_codegen_spirv/src/codegen_cx/constant.rs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,14 @@ impl ConstCodegenMethods for CodegenCx<'_> {
250250
let alloc_id = prov.alloc_id();
251251
let (base_addr, _base_addr_space) = match self.tcx.global_alloc(alloc_id) {
252252
GlobalAlloc::Memory(alloc) => {
253-
let pointee = match self.lookup_type(ty) {
253+
let _pointee = match self.lookup_type(ty) {
254254
SpirvType::Pointer { pointee } => pointee,
255255
other => self.tcx.dcx().fatal(format!(
256256
"GlobalAlloc::Memory type not implemented: {}",
257257
other.debug(ty, self)
258258
)),
259259
};
260-
// FIXME(eddyb) always use `const_data_from_alloc`, and
261-
// defer the actual `try_read_from_const_alloc` step.
262-
let init = self
263-
.try_read_from_const_alloc(alloc, pointee)
264-
.unwrap_or_else(|| self.const_data_from_alloc(alloc));
265-
let value = self.static_addr_of(init, alloc.inner().align, None);
260+
let value = self.static_addr_of(alloc, None);
266261
(value, AddressSpace::ZERO)
267262
}
268263
GlobalAlloc::Function { instance } => (
@@ -279,19 +274,14 @@ impl ConstCodegenMethods for CodegenCx<'_> {
279274
}),
280275
)))
281276
.unwrap_memory();
282-
let pointee = match self.lookup_type(ty) {
277+
let _pointee = match self.lookup_type(ty) {
283278
SpirvType::Pointer { pointee } => pointee,
284279
other => self.tcx.dcx().fatal(format!(
285280
"GlobalAlloc::VTable type not implemented: {}",
286281
other.debug(ty, self)
287282
)),
288283
};
289-
// FIXME(eddyb) always use `const_data_from_alloc`, and
290-
// defer the actual `try_read_from_const_alloc` step.
291-
let init = self
292-
.try_read_from_const_alloc(alloc, pointee)
293-
.unwrap_or_else(|| self.const_data_from_alloc(alloc));
294-
let value = self.static_addr_of(init, alloc.inner().align, None);
284+
let value = self.static_addr_of(alloc, None);
295285
(value, AddressSpace::ZERO)
296286
}
297287
GlobalAlloc::Static(def_id) => {
@@ -317,20 +307,6 @@ impl ConstCodegenMethods for CodegenCx<'_> {
317307
}
318308
}
319309

320-
// HACK(eddyb) this uses a symbolic `ConstDataFromAlloc`, to allow deferring
321-
// the actual value generation until after a pointer to this value is cast
322-
// to its final type (e.g. that will be loaded as).
323-
// FIXME(eddyb) replace this with `qptr` handling of constant data.
324-
fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> Self::Value {
325-
// HACK(eddyb) the `ConstCodegenMethods` trait no longer guarantees the
326-
// lifetime that `alloc` is interned for, but since it *is* interned,
327-
// we can cheaply recover it (see also the `ty::Lift` infrastructure).
328-
let alloc = self.tcx.lift(alloc).unwrap();
329-
330-
let void_type = SpirvType::Void.def(DUMMY_SP, self);
331-
self.def_constant(void_type, SpirvConst::ConstDataFromAlloc(alloc))
332-
}
333-
334310
fn const_ptr_byte_offset(&self, val: Self::Value, offset: Size) -> Self::Value {
335311
if offset == Size::ZERO {
336312
val
@@ -345,6 +321,20 @@ impl ConstCodegenMethods for CodegenCx<'_> {
345321
}
346322

347323
impl<'tcx> CodegenCx<'tcx> {
324+
// HACK(eddyb) this uses a symbolic `ConstDataFromAlloc`, to allow deferring
325+
// the actual value generation until after a pointer to this value is cast
326+
// to its final type (e.g. that will be loaded as).
327+
// FIXME(eddyb) replace this with `qptr` handling of constant data.
328+
pub(crate) fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> SpirvValue {
329+
// HACK(eddyb) the `ConstCodegenMethods` trait no longer guarantees the
330+
// lifetime that `alloc` is interned for, but since it *is* interned,
331+
// we can cheaply recover it (see also the `ty::Lift` infrastructure).
332+
let alloc = self.tcx.lift(alloc).unwrap();
333+
334+
let void_type = SpirvType::Void.def(DUMMY_SP, self);
335+
self.def_constant(void_type, SpirvConst::ConstDataFromAlloc(alloc))
336+
}
337+
348338
pub fn const_bitcast(&self, val: SpirvValue, ty: Word) -> SpirvValue {
349339
// HACK(eddyb) special-case `const_data_from_alloc` + `static_addr_of`
350340
// as the old `from_const_alloc` (now `OperandRef::from_const_alloc`).
@@ -353,9 +343,9 @@ impl<'tcx> CodegenCx<'tcx> {
353343
&& let Some(SpirvConst::ConstDataFromAlloc(alloc)) =
354344
self.builder.lookup_const_by_id(pointee)
355345
&& let SpirvType::Pointer { pointee } = self.lookup_type(ty)
356-
&& let Some(init) = self.try_read_from_const_alloc(alloc, pointee)
346+
&& self.try_read_from_const_alloc(alloc, pointee).is_some()
357347
{
358-
return self.static_addr_of(init, alloc.inner().align, None);
348+
return self.static_addr_of(alloc, None);
359349
}
360350

361351
if val.ty == ty {

crates/rustc_codegen_spirv/src/codegen_cx/declare.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use crate::custom_decorations::{CustomDecoration, SrcLocDecoration};
99
use crate::spirv_type::SpirvType;
1010
use itertools::Itertools;
1111
use rspirv::spirv::{FunctionControl, LinkageType, StorageClass, Word};
12-
use rustc_abi::Align;
1312
use rustc_codegen_ssa::traits::{PreDefineCodegenMethods, StaticCodegenMethods};
1413
use rustc_hir::attrs::{InlineAttr, Linkage};
1514
use rustc_middle::bug;
1615
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
16+
use rustc_middle::mir::interpret::ConstAllocation;
1717
use rustc_middle::mir::mono::{MonoItem, Visibility};
1818
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
1919
use rustc_middle::ty::{self, Instance, TypeVisitableExt, TypingEnv};
@@ -368,7 +368,8 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'tcx> {
368368
}
369369

370370
impl<'tcx> StaticCodegenMethods for CodegenCx<'tcx> {
371-
fn static_addr_of(&self, cv: Self::Value, _align: Align, _kind: Option<&str>) -> Self::Value {
371+
fn static_addr_of(&self, alloc: ConstAllocation<'_>, _kind: Option<&str>) -> Self::Value {
372+
let cv = self.const_data_from_alloc(alloc);
372373
self.def_constant(
373374
self.type_ptr_to(cv.ty),
374375
SpirvConst::PtrTo {

crates/rustc_codegen_spirv/src/codegen_cx/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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;
1617
use rustc_data_structures::fx::FxHashMap;
1718
use rustc_errors::MultiSpan;
1819
use rustc_hir as hir;
@@ -21,7 +22,6 @@ use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
2122
use rustc_middle::ty::{self, Instance, Ty};
2223
use rustc_span::{DUMMY_SP, Span};
2324
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
24-
use std::assert_matches::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/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,11 @@ impl FromStr for ModuleOutputType {
788788

789789
impl<'tcx> BackendTypes for CodegenCx<'tcx> {
790790
type Value = SpirvValue;
791-
type Metadata = ();
792791
type Function = SpirvFunctionCursor;
793792

794793
type BasicBlock = SpirvBlockCursor;
795794
type Type = Word;
795+
type FunctionSignature = Word;
796796
// Funclet: A structure representing an active landing pad for the duration of a basic block. (??)
797797
// https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_llvm/common/struct.Funclet.html
798798
//
@@ -880,7 +880,7 @@ impl<'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'tcx> {
880880
todo!()
881881
}
882882

883-
fn declare_c_main(&self, _fn_type: Self::Type) -> Option<Self::Function> {
883+
fn declare_c_main(&self, _fn_type: Self::FunctionSignature) -> Option<Self::Function> {
884884
todo!()
885885
}
886886
}

crates/rustc_codegen_spirv/src/codegen_cx/type_.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'tcx> {
7676
)
7777
}
7878

79-
fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Type {
79+
fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::FunctionSignature {
8080
fn_abi.spirv_type(DUMMY_SP, self)
8181
}
8282

@@ -171,7 +171,7 @@ impl BaseTypeCodegenMethods for CodegenCx<'_> {
171171
.def(DUMMY_SP, self)
172172
}
173173

174-
fn type_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::Type {
174+
fn type_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::FunctionSignature {
175175
SpirvType::Function {
176176
return_type: ret,
177177
arguments: args,

0 commit comments

Comments
 (0)