diff --git a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs index 589e19ece24..71c32a3a740 100644 --- a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs +++ b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs @@ -590,16 +590,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .lookup_type(pat.ty) .sizeof(self) .expect("Memset on unsized values not supported"); - let count = size_bytes / size_elem.bytes(); - if count == 1 { - self.store(pat, ptr, Align::from_bytes(0).unwrap()); - } else { - for index in 0..count { - let const_index = self.constant_u32(self.span(), index as u32); - let gep_ptr = self.inbounds_gep(pat.ty, ptr, &[const_index]); - self.store(pat, gep_ptr, Align::from_bytes(0).unwrap()); - } + if size_bytes != size_elem.bytes() { + self.err(format!( + "memset of {size_bytes} bytes into `{}` is not supported yet", + self.debug_type(pat.ty) + )); + return; } + self.store(pat, ptr, Align::from_bytes(0).unwrap()); } // TODO: Test this is correct @@ -2994,6 +2992,21 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> { .builder .lookup_const_scalar(size) .and_then(|size| Some(Size::from_bytes(u64::try_from(size).ok()?))); + if const_size == Some(Size::ZERO) { + // Nothing to do! + return; + } + if self.builder.lookup_const(fill_byte) == Some(SpirvConst::Undef) { + // Nothing to do: storing an undefined value can result in any value, + // including the one already there. `rustc_codegen_ssa` itself emits + // nothing for the non-repeat form (`x = MaybeUninit::uninit()`). + return; + } + + // `ptr` can point to an enclosing aggregate, e.g. the struct for a fill of its first field + let ptr = const_size + .and_then(|size| self.adjust_pointer_for_sized_access(ptr, size)) + .map_or(ptr, |(ptr, _)| ptr); let elem_ty = match self.lookup_type(ptr.ty) { SpirvType::Pointer { pointee } => pointee, diff --git a/tests/compiletests/ui/lang/core/array/init_array_uninit.rs b/tests/compiletests/ui/lang/core/array/init_array_uninit.rs new file mode 100644 index 00000000000..b15aef7bca6 --- /dev/null +++ b/tests/compiletests/ui/lang/core/array/init_array_uninit.rs @@ -0,0 +1,31 @@ +// Test filling an array with uninitialized values. +// build-pass + +use core::mem::MaybeUninit; +use spirv_std::macros::spirv; + +#[repr(C)] +pub struct Buf { + data: [MaybeUninit; 4], + len: u32, +} + +pub fn clear_field(buf: &mut Buf) { + buf.data = [const { MaybeUninit::uninit() }; 4]; + buf.len = 0; +} + +pub fn clear_whole(data: &mut [MaybeUninit; 4]) { + *data = [const { MaybeUninit::uninit() }; 4]; +} + +#[spirv(fragment)] +pub fn main(o: &mut u32) { + let mut buf = Buf { + data: [MaybeUninit::new(*o); 4], + len: 4, + }; + clear_field(&mut buf); + clear_whole(&mut buf.data); + *o = buf.len; +} diff --git a/tests/compiletests/ui/lang/core/array/init_array_union_field.rs b/tests/compiletests/ui/lang/core/array/init_array_union_field.rs new file mode 100644 index 00000000000..5fb338f1e49 --- /dev/null +++ b/tests/compiletests/ui/lang/core/array/init_array_union_field.rs @@ -0,0 +1,16 @@ +// Test that filling the smaller field of a union is an error, instead of being ignored. +// build-fail + +use spirv_std::macros::spirv; + +pub union Words { + two: [u32; 2], + four: [u32; 4], +} + +#[spirv(fragment)] +pub fn main(o: &mut u32) { + let mut words = Words { four: [1; 4] }; + words.two = [0; 2]; + *o = unsafe { words.four[3] }; +} diff --git a/tests/compiletests/ui/lang/core/array/init_array_union_field.stderr b/tests/compiletests/ui/lang/core/array/init_array_union_field.stderr new file mode 100644 index 00000000000..15eec828d1e --- /dev/null +++ b/tests/compiletests/ui/lang/core/array/init_array_union_field.stderr @@ -0,0 +1,8 @@ +error: memset of 8 bytes into `[u32; 4]` is not supported yet + --> $DIR/init_array_union_field.rs:14:5 + | +LL | words.two = [0; 2]; + | ^^^^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/compiletests/ui/lang/core/intrinsics/write_bytes_dynamic_size.rs b/tests/compiletests/ui/lang/core/intrinsics/write_bytes_dynamic_size.rs new file mode 100644 index 00000000000..2b1eb5f1e3c --- /dev/null +++ b/tests/compiletests/ui/lang/core/intrinsics/write_bytes_dynamic_size.rs @@ -0,0 +1,15 @@ +// Test that `write_bytes` with a run-time size does not compile. +// build-fail + +#![feature(core_intrinsics)] +#![allow(internal_features)] + +use core::intrinsics::write_bytes; +use spirv_std::spirv; + +#[spirv(fragment)] +pub fn main(#[spirv(flat)] n: u32, o: &mut u32) { + let mut a = [n; 4]; + unsafe { write_bytes(&raw mut a[1], 0, n as usize) }; + *o = a[1]; +} diff --git a/tests/compiletests/ui/lang/core/intrinsics/write_bytes_dynamic_size.stderr b/tests/compiletests/ui/lang/core/intrinsics/write_bytes_dynamic_size.stderr new file mode 100644 index 00000000000..2f4c1471826 --- /dev/null +++ b/tests/compiletests/ui/lang/core/intrinsics/write_bytes_dynamic_size.stderr @@ -0,0 +1,31 @@ +error: `u8` type used without `OpCapability Int8` + | + = note: used by unnamed type +note: used from within `write_bytes_dynamic_size::main` + --> <$DIR/write_bytes_dynamic_size.rs>:13:14 + | +LL | unsafe { write_bytes(&raw mut a[1], 0, n as usize) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: called by Fragment entry-point `main` + --> <$DIR/write_bytes_dynamic_size.rs>:11:8 + | +LL | pub fn main(#[spirv(flat)] n: u32, o: &mut u32) { + | ^^^^ + +error: `u64` type used without `OpCapability Int64` + | + = note: used by unnamed constant + = note: used by unnamed type +note: used from within `write_bytes_dynamic_size::main` + --> <$DIR/write_bytes_dynamic_size.rs>:13:14 + | +LL | unsafe { write_bytes(&raw mut a[1], 0, n as usize) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: called by Fragment entry-point `main` + --> <$DIR/write_bytes_dynamic_size.rs>:11:8 + | +LL | pub fn main(#[spirv(flat)] n: u32, o: &mut u32) { + | ^^^^ + +error: aborting due to 2 previous errors + diff --git a/tests/compiletests/ui/lang/core/intrinsics/write_bytes_larger_than_pointee.rs b/tests/compiletests/ui/lang/core/intrinsics/write_bytes_larger_than_pointee.rs new file mode 100644 index 00000000000..ce0fe0bf2cc --- /dev/null +++ b/tests/compiletests/ui/lang/core/intrinsics/write_bytes_larger_than_pointee.rs @@ -0,0 +1,29 @@ +// Test that `write_bytes` over more than its pointee is an error. +// build-fail + +#![feature(core_intrinsics)] +#![allow(internal_features)] + +use core::intrinsics::write_bytes; +use spirv_std::spirv; + +pub struct S { + arr: [u32; 4], + t: u32, +} + +#[spirv(fragment)] +pub fn main(o: &mut u32) { + let mut a = [*o; 4]; + let mut s = S { + arr: [*o; 4], + t: *o, + }; + unsafe { + write_bytes(&raw mut a[1], 0, 2); + write_bytes(&raw mut a as *mut u32, 0, 3); + // Past the end of `arr` + write_bytes(&raw mut s.arr[3], 0, 2); + } + *o = a[1] + s.arr[3] + s.t; +} diff --git a/tests/compiletests/ui/lang/core/intrinsics/write_bytes_larger_than_pointee.stderr b/tests/compiletests/ui/lang/core/intrinsics/write_bytes_larger_than_pointee.stderr new file mode 100644 index 00000000000..9b30c5c6c88 --- /dev/null +++ b/tests/compiletests/ui/lang/core/intrinsics/write_bytes_larger_than_pointee.stderr @@ -0,0 +1,20 @@ +error: memset of 8 bytes into `u32` is not supported yet + --> $DIR/write_bytes_larger_than_pointee.rs:23:9 + | +LL | write_bytes(&raw mut a[1], 0, 2); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: memset of 12 bytes into `u32` is not supported yet + --> $DIR/write_bytes_larger_than_pointee.rs:24:9 + | +LL | write_bytes(&raw mut a as *mut u32, 0, 3); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: memset of 8 bytes into `u32` is not supported yet + --> $DIR/write_bytes_larger_than_pointee.rs:26:9 + | +LL | write_bytes(&raw mut s.arr[3], 0, 2); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + diff --git a/tests/compiletests/ui/lang/core/intrinsics/write_bytes_zero_size.rs b/tests/compiletests/ui/lang/core/intrinsics/write_bytes_zero_size.rs new file mode 100644 index 00000000000..46dced93c38 --- /dev/null +++ b/tests/compiletests/ui/lang/core/intrinsics/write_bytes_zero_size.rs @@ -0,0 +1,19 @@ +// Test `write_bytes` with a size of zero. +// build-pass + +#![feature(core_intrinsics)] +#![allow(internal_features)] + +use core::intrinsics::write_bytes; +use spirv_std::spirv; + +#[spirv(fragment)] +pub fn main(o: &mut u32) { + let mut unit = (); + let mut flag = *o != 0; + unsafe { + write_bytes(&raw mut unit, 0xab, 1); + write_bytes(&raw mut flag, 0, 0); + } + *o = flag as u32; +} diff --git a/tests/difftests/tests/Cargo.lock b/tests/difftests/tests/Cargo.lock index 169ef471718..50ab21e0a5f 100644 --- a/tests/difftests/tests/Cargo.lock +++ b/tests/difftests/tests/Cargo.lock @@ -604,6 +604,39 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" +[[package]] +name = "fill-runtime-byte-cpu" +version = "0.0.0" +dependencies = [ + "difftest", + "fill-runtime-byte-rust", +] + +[[package]] +name = "fill-runtime-byte-rust" +version = "0.0.0" +dependencies = [ + "bytemuck", + "difftest", + "spirv-std", +] + +[[package]] +name = "fill_offset_zero-cpu" +version = "0.0.0" +dependencies = [ + "difftest", + "fill_offset_zero-rust", +] + +[[package]] +name = "fill_offset_zero-rust" +version = "0.0.0" +dependencies = [ + "difftest", + "spirv-std", +] + [[package]] name = "find-msvc-tools" version = "0.1.14" diff --git a/tests/difftests/tests/Cargo.toml b/tests/difftests/tests/Cargo.toml index 7d878edb6b4..8e006523d49 100644 --- a/tests/difftests/tests/Cargo.toml +++ b/tests/difftests/tests/Cargo.toml @@ -27,6 +27,10 @@ members = [ "lang/control_flow/control_flow-wgsl", "lang/control_flow_complex/control_flow_complex-rust", "lang/control_flow_complex/control_flow_complex-wgsl", + "lang/core/array/fill_offset_zero/fill_offset_zero-rust", + "lang/core/array/fill_offset_zero/fill_offset_zero-cpu", + "lang/core/array/fill_runtime_byte/fill-runtime-byte-rust", + "lang/core/array/fill_runtime_byte/fill-runtime-byte-cpu", "lang/core/ops/math_ops/math_ops-rust", "lang/core/ops/math_ops/math_ops-wgsl", "lang/core/ops/vector_ops/vector_ops-rust", diff --git a/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-cpu/Cargo.toml b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-cpu/Cargo.toml new file mode 100644 index 00000000000..3217a530739 --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-cpu/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "fill_offset_zero-cpu" +edition.workspace = true + +[lints] +workspace = true + +[dependencies] +difftest.workspace = true +fill_offset_zero-rust = { path = "../fill_offset_zero-rust" } diff --git a/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-cpu/src/main.rs b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-cpu/src/main.rs new file mode 100644 index 00000000000..9c8f821a46d --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-cpu/src/main.rs @@ -0,0 +1,8 @@ +use difftest::config::{Config, TestMetadata}; +use fill_offset_zero_rust::{INPUT, eval}; + +fn main() { + let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap(); + config.write_result(eval(&INPUT).as_flattened()).unwrap(); + config.write_metadata(&TestMetadata::u32()).unwrap(); +} diff --git a/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-rust/Cargo.toml b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-rust/Cargo.toml new file mode 100644 index 00000000000..ac745d9a2c6 --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-rust/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "fill_offset_zero-rust" +edition.workspace = true + +[lints] +workspace = true + +# GPU deps +[dependencies] +spirv-std.workspace = true + +# CPU deps (for the test harness) +[target.'cfg(not(target_arch = "spirv"))'.dependencies] +difftest.workspace = true diff --git a/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-rust/src/lib.rs b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-rust/src/lib.rs new file mode 100644 index 00000000000..75026e79f50 --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-rust/src/lib.rs @@ -0,0 +1,118 @@ +#![no_std] + +use spirv_std::spirv; + +// Non-zero, so a fill that writes nothing is visible +pub const INPUT: [u32; 4] = [1, 2, 3, 4]; + +pub const ROWS: usize = 9; +pub const WORDS: usize = 6; +// One row per fill, the contents of the aggregate after it +pub type Output = [[u32; WORDS]; ROWS]; + +// `repr(C)` keeps the array at offset 0 +#[repr(C)] +#[derive(Clone, Copy)] +pub struct First { + pub arr: [u32; 4], + pub tail: u32, +} + +#[repr(C)] +pub struct Nested { + pub head: u32, + pub first: First, +} + +pub struct Only { + pub arr: [u32; 4], +} + +pub fn reset(s: &mut First) { + s.arr = [0; 4]; +} + +// Borrowed, so the struct is not split into separate variables +fn first_words(s: &First) -> [u32; WORDS] { + [s.arr[0], s.arr[1], s.arr[2], s.arr[3], s.tail, 0] +} + +fn nested_words(n: &Nested) -> [u32; WORDS] { + let [a0, a1, a2, a3] = n.first.arr; + [n.head, a0, a1, a2, a3, n.first.tail] +} + +fn only_words(s: &Only) -> [u32; WORDS] { + [s.arr[0], s.arr[1], s.arr[2], s.arr[3], 0, 0] +} + +fn table_words(t: &[[u32; 2]; 3]) -> [u32; WORDS] { + [t[0][0], t[0][1], t[1][0], t[1][1], t[2][0], t[2][1]] +} + +pub fn eval(input: &[u32; 4]) -> Output { + let [a, b, c, d] = *input; + let first = First { + arr: [a, b, c, d], + tail: a, + }; + let table = [[a, b], [c, d], [a, d]]; + + let mut field = first; + field.arr = [0; 4]; + + let mut param = first; + reset(&mut param); + + // The first field of a struct that is itself at a non-zero offset + let mut nested = Nested { head: b, first }; + nested.first.arr = [u32::MAX; 4]; + + let mut elem0 = table; + elem0[0] = [0; 2]; + + let mut reborrow = table; + let row = &mut reborrow[0]; + *row = [0; 2]; + + let mut floats = [[a as f32, b as f32], [c as f32, d as f32]]; + floats[0] = [0.0; 2]; + + // For comparison: a non-zero offset, an array that is the only field, and + // a fill that is not lowered to `memset` + let mut elem1 = table; + elem1[1] = [0; 2]; + + let mut only = Only { arr: [a, b, c, d] }; + only.arr = [0; 4]; + + let mut ones = first; + ones.arr = [1; 4]; + + [ + first_words(&field), + first_words(¶m), + nested_words(&nested), + table_words(&elem0), + table_words(&reborrow), + [ + floats[0][0].to_bits(), + floats[0][1].to_bits(), + floats[1][0].to_bits(), + floats[1][1].to_bits(), + 0, + 0, + ], + table_words(&elem1), + only_words(&only), + first_words(&ones), + ] +} + +#[spirv(compute(threads(1)))] +pub fn main_cs( + #[spirv(storage_buffer, descriptor_set = 0, binding = 0)] input: &[u32; 4], + #[spirv(storage_buffer, descriptor_set = 0, binding = 1)] output: &mut Output, +) { + *output = eval(input); +} diff --git a/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-rust/src/main.rs b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-rust/src/main.rs new file mode 100644 index 00000000000..2db701ead23 --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-rust/src/main.rs @@ -0,0 +1,21 @@ +#[cfg(not(target_arch = "spirv"))] +fn main() { + use difftest::config::{Config, TestMetadata}; + use difftest::scaffold::compute::{BufferConfig, RustComputeShader, WgpuComputeTest}; + use fill_offset_zero_rust::{INPUT, Output}; + + let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap(); + let test = WgpuComputeTest::new( + RustComputeShader::default(), + [1, 1, 1], + vec![ + BufferConfig::read_only(&INPUT), + BufferConfig::writeback(size_of::()), + ], + ); + config.write_metadata(&TestMetadata::u32()).unwrap(); + test.run_test(&config).unwrap(); +} + +#[cfg(target_arch = "spirv")] +fn main() {} diff --git a/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-cpu/Cargo.toml b/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-cpu/Cargo.toml new file mode 100644 index 00000000000..a484702aefc --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-cpu/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "fill-runtime-byte-cpu" +edition.workspace = true + +[lints] +workspace = true + +[dependencies] +difftest.workspace = true +fill-runtime-byte-rust = { path = "../fill-runtime-byte-rust" } diff --git a/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-cpu/src/main.rs b/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-cpu/src/main.rs new file mode 100644 index 00000000000..2aa409a87e9 --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-cpu/src/main.rs @@ -0,0 +1,16 @@ +use difftest::config::{Config, TestMetadata}; +use fill_runtime_byte_rust::{TEST_DATA, fill_case}; + +fn main() { + let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap(); + + let output: Vec = TEST_DATA + .iter() + .flat_map(|&value| fill_case(value)) + .collect(); + + config.write_result(&output).unwrap(); + config + .write_metadata(&TestMetadata::u32()) + .expect("Failed to write metadata"); +} diff --git a/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-rust/Cargo.toml b/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-rust/Cargo.toml new file mode 100644 index 00000000000..98dcaaef1d9 --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-rust/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "fill-runtime-byte-rust" +edition.workspace = true + +[lints] +workspace = true + +# GPU deps +[dependencies] +spirv-std.workspace = true + +# CPU deps (for the test harness) +[target.'cfg(not(target_arch = "spirv"))'.dependencies] +difftest.workspace = true +bytemuck.workspace = true diff --git a/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-rust/src/lib.rs b/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-rust/src/lib.rs new file mode 100644 index 00000000000..28b22d41d16 --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-rust/src/lib.rs @@ -0,0 +1,87 @@ +#![no_std] +#![feature(core_intrinsics)] +#![allow(internal_features)] + +use core::intrinsics::write_bytes; +use spirv_std::spirv; + +pub const WORDS_PER_CASE: usize = 16; + +pub const TEST_DATA: [u32; 4] = [0x00, 0x01, 0x80, 0xFF]; + +// `repr(C)` keeps the array at offset 0 +#[repr(C)] +pub struct BytesFirst { + pub bytes: [u8; 4], + pub tail: u32, +} + +#[repr(C)] +pub struct FloatsFirst { + pub floats: [f32; 2], + pub tail: u32, +} + +pub fn fill_bytes(s: &mut BytesFirst, byte: u8) { + s.bytes = [byte; 4]; +} + +pub fn fill_floats(s: &mut FloatsFirst, byte: u8) { + unsafe { write_bytes(&raw mut s.floats, byte, 1) }; +} + +pub fn fill_case(value: u32) -> [u32; WORDS_PER_CASE] { + let byte = value as u8; + + let mut field = BytesFirst { + bytes: [1, 2, 3, 4], + tail: 5, + }; + fill_bytes(&mut field, byte); + + let whole = [byte; 4]; + + let mut words = [1u32, 2, 3, 4]; + unsafe { write_bytes(&raw mut words, byte, 1) }; + + let mut floats = FloatsFirst { + floats: [1.0, 2.0], + tail: 5, + }; + fill_floats(&mut floats, byte); + + [ + field.bytes[0] as u32, + field.bytes[1] as u32, + field.bytes[2] as u32, + field.bytes[3] as u32, + field.tail, + whole[0] as u32, + whole[1] as u32, + whole[2] as u32, + whole[3] as u32, + words[0], + words[1], + words[2], + words[3], + floats.floats[0].to_bits(), + floats.floats[1].to_bits(), + floats.tail, + ] +} + +#[spirv(compute(threads(1)))] +pub fn main_cs( + #[spirv(storage_buffer, descriptor_set = 0, binding = 0)] input: &[u32], + #[spirv(storage_buffer, descriptor_set = 0, binding = 1)] output: &mut [u32], + #[spirv(global_invocation_id)] global_id: spirv_std::glam::UVec3, +) { + let tid = global_id.x as usize; + + if tid < input.len() { + let words = fill_case(input[tid]); + for i in 0..WORDS_PER_CASE { + output[tid * WORDS_PER_CASE + i] = words[i]; + } + } +} diff --git a/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-rust/src/main.rs b/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-rust/src/main.rs new file mode 100644 index 00000000000..8fc33c73b3c --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_runtime_byte/fill-runtime-byte-rust/src/main.rs @@ -0,0 +1,55 @@ +#[cfg(not(target_arch = "spirv"))] +fn main() { + use difftest::config::{Config, TestMetadata}; + use difftest::scaffold::compute::{ + AshBackend, BufferConfig, BufferUsage, ComputeShaderTest, RustComputeShader, + }; + use difftest::spirv_builder::Capability; + use fill_runtime_byte_rust::{TEST_DATA, WORDS_PER_CASE}; + + let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap(); + + // Skip on macOS due to Vulkan/MoltenVK configuration issues + #[cfg(target_os = "macos")] + { + use difftest::scaffold::Skip; + + let skip = Skip::new("Ash tests are skipped on macOS due to MoltenVK configuration issues"); + skip.run_test(&config).unwrap(); + return; + } + + #[cfg(not(target_os = "macos"))] + { + let input_bytes: Vec = bytemuck::cast_slice(&TEST_DATA).to_vec(); + let output_size = (TEST_DATA.len() * WORDS_PER_CASE * std::mem::size_of::()) as u64; + + let buffers = vec![ + BufferConfig { + size: input_bytes.len() as u64, + usage: BufferUsage::StorageReadOnly, + initial_data: Some(input_bytes), + }, + BufferConfig { + size: output_size, + usage: BufferUsage::Storage, + initial_data: None, + }, + ]; + + // Use Ash backend since wgpu/naga doesn't support Int8 + let shader = RustComputeShader::default().with_capability(Capability::Int8); + let num_workgroups = TEST_DATA.len() as u32; + let test = ComputeShaderTest::::new(shader, [num_workgroups, 1, 1], buffers) + .unwrap(); + + config + .write_metadata(&TestMetadata::u32()) + .expect("Failed to write metadata"); + + test.run_test(&config).unwrap(); + } +} + +#[cfg(target_arch = "spirv")] +fn main() {}