Skip to content
Open
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
22 changes: 22 additions & 0 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions tests/compiletests/ui/lang/core/array/init_array_uninit.rs
Original file line number Diff line number Diff line change
@@ -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<u32>; 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<u32>; 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;
}
16 changes: 16 additions & 0 deletions tests/compiletests/ui/lang/core/array/init_array_union_field.rs
Original file line number Diff line number Diff line change
@@ -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] };
}
Original file line number Diff line number Diff line change
@@ -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

16 changes: 16 additions & 0 deletions tests/difftests/tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/difftests/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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" }
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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(&param),
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);
}
Original file line number Diff line number Diff line change
@@ -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::<Output>()),
],
);
config.write_metadata(&TestMetadata::u32()).unwrap();
test.run_test(&config).unwrap();
}

#[cfg(target_arch = "spirv")]
fn main() {}
Loading