Skip to content

[eddyb's version/leftovers] TypedBuffer / #[spirv(typed_buffer)]. #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 4, 2025
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
4 changes: 2 additions & 2 deletions crates/rustc_codegen_spirv/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,8 @@ impl<'tcx> ConvSpirvType<'tcx> for TyAndLayout<'tcx> {
// perfectly, in every way that could potentially affect ABI.
if self.fields.offset(i) == Size::ZERO
&& field.size == self.size
&& field.align == self.align
&& field.backend_repr == self.backend_repr
&& field.align.abi == self.align.abi
&& field.backend_repr.eq_up_to_validity(&self.backend_repr)
{
return field.spirv_type(span, cx);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/spirv-std/src/typed_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<T> Deref for TypedBuffer<[T]> {
"%result = OpCompositeConstruct typeof*{result_slot} %inner_ptr %inner_len",
"OpStore {result_slot} %result",
buffer = in(reg) self,
result_slot = in(reg) &mut result_slot,
result_slot = in(reg) result_slot.as_mut_ptr(),
}
result_slot.assume_init()
}
Expand All @@ -94,7 +94,7 @@ impl<T> DerefMut for TypedBuffer<[T]> {
"%result = OpCompositeConstruct typeof*{result_slot} %inner_ptr %inner_len",
"OpStore {result_slot} %result",
buffer = in(reg) self,
result_slot = in(reg) &mut result_slot,
result_slot = in(reg) result_slot.as_mut_ptr(),
}
result_slot.assume_init()
}
Expand Down
25 changes: 25 additions & 0 deletions tests/compiletests/ui/storage_class/typed-buffer-simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// build-pass
// compile-flags: -C target-feature=+RuntimeDescriptorArray,+ext:SPV_EXT_descriptor_indexing

// Tests the simplest `TypedBuffer` case: Multiple structs of of the same type and size.

use spirv_std::{RuntimeArray, TypedBuffer, glam::UVec3, spirv};

#[derive(Clone, Copy)]
pub struct MyData {
some_big_data: [u32; 1 << 24],
}

#[spirv(compute(threads(1)))]
pub fn compute(
#[spirv(global_invocation_id)] global_invocation_id: UVec3,
#[spirv(descriptor_set = 0, binding = 0, storage_buffer)] my_data: &mut RuntimeArray<
TypedBuffer<MyData>,
>,
) {
let mut load_dta = unsafe { my_data.index(global_invocation_id.x as usize) }.some_big_data[0];
load_dta = 32;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we loading data into load_dta: u32 to then immediately replace the value with a const? Could DCE screw us over here?

Also at https://github.com/Rust-GPU/rust-gpu/pull/319/files#diff-734d8d31bf0e2742d96036c34f019f361a6bb9674653012c7e3522c59919f506R16

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I wonder if this was meant to be &mut followed by *... = 32;.

Funnily enough, the bug this test catches (post-rust-lang/rust#134117) happens much earlier, so only MIR optimizations could invalidate that part, even in theory (though ideally we'd not rely on incidental stuff like this, and add separate tests).

Anyway, for reading values I tend to just add an output variable to write them to, guaranteeing the read will be kept around, I might make that change if it's not obvious what the original tests meant to do.


let mut target = unsafe { &mut my_data.index_mut(global_invocation_id.y as usize) };
target.some_big_data[0] = load_dta;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// build-pass
// compile-flags: -C target-feature=+RuntimeDescriptorArray,+ext:SPV_EXT_descriptor_indexing

// Tests the more complex `TypedBuffer` case, where the size of each buffer in the binding is unbound, and also the data type is a struct.

use spirv_std::{RuntimeArray, TypedBuffer, glam::UVec3, spirv};

#[derive(Clone, Copy)]
pub struct MyData {
a: f32,
b: [u32; 3],
}

#[spirv(compute(threads(1)))]
pub fn compute(
#[spirv(global_invocation_id)] global_invocation_id: UVec3,
#[spirv(descriptor_set = 0, binding = 0, storage_buffer)] my_data: &mut RuntimeArray<
TypedBuffer<[MyData]>,
>,
) {
let mut load_dta: MyData = unsafe { my_data.index(global_invocation_id.x as usize) }[0];
load_dta.b[0] = 32;

let mut target = unsafe { &mut my_data.index_mut(global_invocation_id.y as usize)[0] };
*target = load_dta;
}
20 changes: 20 additions & 0 deletions tests/compiletests/ui/storage_class/typed-buffer-unbound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// build-pass
// compile-flags: -C target-feature=+RuntimeDescriptorArray,+ext:SPV_EXT_descriptor_indexing

// Tests the more complex `TypedBuffer` case, where the size of each buffer in the binding is unbound.

use spirv_std::{RuntimeArray, TypedBuffer, glam::UVec3, spirv};

#[spirv(compute(threads(1)))]
pub fn compute(
#[spirv(global_invocation_id)] global_invocation_id: UVec3,
#[spirv(descriptor_set = 0, binding = 0, storage_buffer)] my_data: &mut RuntimeArray<
TypedBuffer<[u32]>,
>,
) {
let mut load_dta: u32 = unsafe { my_data.index(global_invocation_id.x as usize) }[0];
load_dta = 32;

let mut target = unsafe { &mut my_data.index_mut(global_invocation_id.y as usize)[0] };
*target = load_dta;
}