diff --git a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs index 10927fccbd0..33815ee27ec 100644 --- a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs +++ b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs @@ -553,6 +553,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .sizeof(self) .expect("Memset on unsized values not supported"); let count = size_bytes / size_elem.bytes(); + if !size_bytes.is_multiple_of(size_elem.bytes()) { + self.err(format!( + "memset of {size_bytes} bytes into `{}` is not supported yet", + self.debug_type(pat.ty) + )); + } if count == 1 { self.store(pat, ptr, Align::from_bytes(0).unwrap()); } else { @@ -2940,6 +2946,22 @@ 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: the fill leaves the memory uninitialized. + return; + } + + // The pointee type of `ptr` can be a bigger aggregate than the memory to + // fill (e.g. a fill of the first field of a struct, where `ptr` still has + // the whole struct as its pointee type), so look for the field/element of + // the fill's size, like `memcpy`. If there is none, `ptr` is kept. + 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/difftests/tests/Cargo.lock b/tests/difftests/tests/Cargo.lock index e4e75a72328..70d2101b609 100644 --- a/tests/difftests/tests/Cargo.lock +++ b/tests/difftests/tests/Cargo.lock @@ -570,6 +570,22 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" +[[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 2fc956e3e18..a9b8b28089b 100644 --- a/tests/difftests/tests/Cargo.toml +++ b/tests/difftests/tests/Cargo.toml @@ -27,6 +27,8 @@ 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/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..dd5b8b74f16 --- /dev/null +++ b/tests/difftests/tests/lang/core/array/fill_offset_zero/fill_offset_zero-rust/src/lib.rs @@ -0,0 +1,126 @@ +//! Assigns an array repeat expression such as `[0; 4]` to the first field of a +//! struct or to the first element of an array of arrays. `rustc` lowers these +//! fills to `memset`, and the destination pointer still points to the whole +//! enclosing aggregate. +#![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)] +#[derive(Clone, Copy)] +pub struct Nested { + pub head: u32, + pub first: First, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct Only { + pub arr: [u32; 4], +} + +pub fn reset(s: &mut First) { + s.arr = [0; 4]; +} + +/// Taking a reference keeps the struct whole, so the fill is not turned into +/// a fill of a separate array variable. +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() {}