Skip to content
This repository was archived by the owner on Oct 31, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 7 additions & 29 deletions crates/rustc_codegen_spirv/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::attr::{AggregatedSpirvAttributes, IntrinsicType};
use crate::codegen_cx::CodegenCx;
use crate::spirv_type::SpirvType;
use rspirv::spirv::{Capability, StorageClass, Word};
use rspirv::spirv::{StorageClass, Word};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::ErrorReported;
use rustc_middle::bug;
Expand Down Expand Up @@ -58,18 +58,11 @@ impl<'tcx> RecursivePointeeCache<'tcx> {
cx.emit_global()
.type_forward_pointer(new_id, StorageClass::Generic);
entry.insert(PointeeDefState::DefiningWithForward(new_id));
if !cx.builder.has_capability(Capability::Addresses)
&& !cx
.builder
.has_capability(Capability::PhysicalStorageBufferAddresses)
{
cx.zombie_with_span(
new_id,
span,
"OpTypeForwardPointer without OpCapability \
Addresses or PhysicalStorageBufferAddresses",
);
}
cx.zombie_with_span(
new_id,
span,
"Cannot create self-referential types, even through pointers",
);
Some(new_id)
}
// State: This is the third or more time we've seen this type, and we've already emitted an
Expand Down Expand Up @@ -424,10 +417,7 @@ fn trans_scalar<'tcx>(
}

match scalar.value {
Primitive::Int(width, mut signedness) => {
if cx.target.is_kernel() {
signedness = false;
}
Primitive::Int(width, signedness) => {
SpirvType::Integer(width.size().bits() as u32, signedness).def(span, cx)
}
Primitive::F32 => SpirvType::Float(32).def(span, cx),
Expand Down Expand Up @@ -652,18 +642,6 @@ pub fn auto_struct_layout<'tcx>(

// see struct_llfields in librustc_codegen_llvm for implementation hints
fn trans_struct<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>) -> Word {
if let TyKind::Foreign(_) = ty.ty.kind() {
// "An unsized FFI type that is opaque to Rust", `extern type A;` (currently unstable)
if cx.target.is_kernel() {
// TODO: This should use the name of the struct as the name. However, names are not stable across crates,
// e.g. core::fmt::Opaque in one crate and fmt::Opaque in core.
return SpirvType::Opaque {
name: "".to_string(),
}
.def(span, cx);
}
// otherwise fall back
};
let size = if ty.is_unsized() { None } else { Some(ty.size) };
let align = ty.align.abi;
let mut field_types = Vec::new();
Expand Down
20 changes: 1 addition & 19 deletions crates/rustc_codegen_spirv/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ pub enum SpirvAttribute {

// `fn`/closure attributes:
UnrollLoops,
InternalBufferLoad,
InternalBufferStore,
}

// HACK(eddyb) this is similar to `rustc_span::Spanned` but with `value` as the
Expand Down Expand Up @@ -124,8 +122,6 @@ pub struct AggregatedSpirvAttributes {

// `fn`/closure attributes:
pub unroll_loops: Option<Spanned<()>>,
pub internal_buffer_load: Option<Spanned<()>>,
pub internal_buffer_store: Option<Spanned<()>>,
}

struct MultipleAttrs {
Expand Down Expand Up @@ -213,18 +209,6 @@ impl AggregatedSpirvAttributes {
"#[spirv(attachment_index)]",
),
UnrollLoops => try_insert(&mut self.unroll_loops, (), span, "#[spirv(unroll_loops)]"),
InternalBufferLoad => try_insert(
&mut self.internal_buffer_load,
(),
span,
"#[spirv(internal_buffer_load)]",
),
InternalBufferStore => try_insert(
&mut self.internal_buffer_store,
(),
span,
"#[spirv(internal_buffer_store)]",
),
}
}
}
Expand Down Expand Up @@ -349,9 +333,7 @@ impl CheckSpirvAttrVisitor<'_> {

_ => Err(Expected("function parameter")),
},
SpirvAttribute::InternalBufferLoad
| SpirvAttribute::InternalBufferStore
| SpirvAttribute::UnrollLoops => match target {
SpirvAttribute::UnrollLoops => match target {
Target::Fn
| Target::Closure
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {
Expand Down
56 changes: 10 additions & 46 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
)),
},
SpirvType::Adt { .. } => self.fatal("memset on structs not implemented yet"),
SpirvType::Opaque { .. } => self.fatal("memset on opaque type is invalid"),
SpirvType::Vector { element, count } => {
let elem_pat = self.memset_const_pattern(&self.lookup_type(element), fill_byte);
self.constant_composite(
Expand Down Expand Up @@ -266,7 +265,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
)),
},
SpirvType::Adt { .. } => self.fatal("memset on structs not implemented yet"),
SpirvType::Opaque { .. } => self.fatal("memset on opaque type is invalid"),
SpirvType::Array { element, count } => {
let elem_pat = self.memset_dynamic_pattern(&self.lookup_type(element), fill_var);
let count = self.builder.lookup_const_u64(count).unwrap() as usize;
Expand Down Expand Up @@ -355,29 +353,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}

fn zombie_convert_ptr_to_u(&self, def: Word) {
if !self.builder.has_capability(Capability::Addresses)
&& !self
.builder
.has_capability(Capability::PhysicalStorageBufferAddresses)
{
self.zombie(
def,
"OpConvertPtrToU without OpCapability Addresses or PhysicalStorageBufferAddresses",
);
}
self.zombie(def, "Cannot convert pointers to integers");
}

fn zombie_convert_u_to_ptr(&self, def: Word) {
if !self.builder.has_capability(Capability::Addresses)
&& !self
.builder
.has_capability(Capability::PhysicalStorageBufferAddresses)
{
self.zombie(
def,
"OpConvertUToPtr OpCapability Addresses or PhysicalStorageBufferAddresses",
);
}
self.zombie(def, "Cannot convert integers to pointers");
}

fn zombie_ptr_equal(&self, def: Word, inst: &str) {
Expand Down Expand Up @@ -1276,11 +1256,15 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
.unwrap()
.with_type(dest_ty);

if (val_is_ptr || dest_is_ptr) && self.logical_addressing_model() {
if val_is_ptr || dest_is_ptr {
if self.is_system_crate() {
self.zombie(
result.def(self),
"OpBitcast between ptr and non-ptr without AddressingModel != Logical",
&format!(
"Cannot cast between pointer and non-pointer types. From: {}. To: {}.",
self.debug_type(val.ty),
self.debug_type(dest_ty)
),
);
} else {
self.struct_err("Cannot cast between pointer and non-pointer types")
Expand Down Expand Up @@ -1397,7 +1381,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
.access_chain(dest_ty, None, val.def(self), indices)
.unwrap()
.with_type(dest_ty)
} else if self.logical_addressing_model() {
} else {
// Defer the cast so that it has a chance to be avoided.
SpirvValue {
kind: SpirvValueKind::LogicalPtrCast {
Expand All @@ -1407,11 +1391,6 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
},
ty: dest_ty,
}
} else {
self.emit()
.bitcast(dest_ty, None, val.def(self))
.unwrap()
.with_type(dest_ty)
}
}

Expand Down Expand Up @@ -1713,12 +1692,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
empty(),
)
.unwrap();
if !self.builder.has_capability(Capability::Addresses) {
self.zombie(
dst.def(self),
"OpCopyMemorySized without OpCapability Addresses",
);
}
self.zombie(dst.def(self), "Cannot memcpy dynamically sized data");
}
}

Expand Down Expand Up @@ -2184,16 +2158,6 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
// needing to materialize `&core::panic::Location` or `format_args!`.
self.abort();
self.undef(result_type)
} else if self.internal_buffer_load_id.borrow().contains(&callee_val) {
self.codegen_internal_buffer_load(result_type, args)
} else if self.internal_buffer_store_id.borrow().contains(&callee_val) {
self.codegen_internal_buffer_store(args);

let void_ty = SpirvType::Void.def(rustc_span::DUMMY_SP, self);
SpirvValue {
kind: SpirvValueKind::IllegalTypeUsed(void_ty),
ty: void_ty,
}
} else {
let args = args.iter().map(|arg| arg.def(self)).collect::<Vec<_>>();
self.emit()
Expand Down
38 changes: 1 addition & 37 deletions crates/rustc_codegen_spirv/src/builder/ext_inst.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use super::Builder;
use crate::builder_spirv::{SpirvValue, SpirvValueExt};
use rspirv::spirv::{CLOp, GLOp, Word};
use rspirv::spirv::{GLOp, Word};
use rspirv::{dr::Operand, spirv::Capability};

const GLSL_STD_450: &str = "GLSL.std.450";
const OPENCL_STD: &str = "OpenCL.std";

/// Manager for OpExtInst/OpExtImport instructions
#[derive(Default)]
pub struct ExtInst {
glsl: Option<Word>,
opencl: Option<Word>,
integer_functions_2_intel: bool,
}

impl ExtInst {
pub fn import_glsl<'a, 'tcx>(&mut self, bx: &Builder<'a, 'tcx>) -> Word {
assert!(!bx.target.is_kernel());
match self.glsl {
Some(id) => id,
None => {
Expand All @@ -27,25 +24,12 @@ impl ExtInst {
}
}

pub fn import_opencl<'a, 'tcx>(&mut self, bx: &Builder<'a, 'tcx>) -> Word {
assert!(bx.target.is_kernel());
match self.opencl {
Some(id) => id,
None => {
let id = bx.emit_global().ext_inst_import(OPENCL_STD);
self.opencl = Some(id);
id
}
}
}

pub fn require_integer_functions_2_intel<'a, 'tcx>(
&mut self,
bx: &Builder<'a, 'tcx>,
to_zombie: Word,
) {
if !self.integer_functions_2_intel {
assert!(!bx.target.is_kernel());
self.integer_functions_2_intel = true;
if !bx
.builder
Expand Down Expand Up @@ -86,24 +70,4 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
.unwrap()
.with_type(result_type)
}

pub fn cl_op(
&mut self,
op: CLOp,
result_type: Word,
args: impl AsRef<[SpirvValue]>,
) -> SpirvValue {
let args = args.as_ref();
let opencl = self.ext_inst.borrow_mut().import_opencl(self);
self.emit()
.ext_inst(
result_type,
None,
opencl,
op as u32,
args.iter().map(|a| Operand::IdRef(a.def(self))),
)
.unwrap()
.with_type(result_type)
}
}
Loading