Skip to content

Commit 9fa9b5d

Browse files
committed
Fix memset into the first field or element of an aggregate
`rustc` lowers `[v; N]` to `memset` when all bytes of `v` are equal. For a destination at offset 0 of a bigger aggregate (`s.arr = [0; 4]`, `table[0] = [0.0; 3]`), the pointer still has the whole aggregate as its pointee type, so the store count was 0 and nothing was written. `memset` now looks for the field or element of the fill's size first, with `adjust_pointer_for_sized_access`, like `memcpy`, and returns early for a size of 0. If there is no such field or element and the size is not a multiple of the pointee size (e.g. the smaller field of a union), the fill is an error instead of being ignored.
1 parent fab2962 commit 9fa9b5d

11 files changed

Lines changed: 263 additions & 0 deletions

File tree

‎crates/rustc_codegen_spirv/src/builder/builder_methods.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
591591
.sizeof(self)
592592
.expect("Memset on unsized values not supported");
593593
let count = size_bytes / size_elem.bytes();
594+
if !size_bytes.is_multiple_of(size_elem.bytes()) {
595+
self.err(format!(
596+
"memset of {size_bytes} bytes into `{}` is not supported yet",
597+
self.debug_type(pat.ty)
598+
));
599+
}
594600
if count == 1 {
595601
self.store(pat, ptr, Align::from_bytes(0).unwrap());
596602
} else {
@@ -2994,6 +3000,19 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
29943000
.builder
29953001
.lookup_const_scalar(size)
29963002
.and_then(|size| Some(Size::from_bytes(u64::try_from(size).ok()?)));
3003+
if const_size == Some(Size::ZERO) {
3004+
// Nothing to do!
3005+
return;
3006+
}
3007+
if self.builder.lookup_const(fill_byte) == Some(SpirvConst::Undef) {
3008+
// Nothing to do: the fill leaves the memory uninitialized.
3009+
return;
3010+
}
3011+
3012+
// `ptr` can point to an enclosing aggregate, e.g. the struct for a fill of its first field
3013+
let ptr = const_size
3014+
.and_then(|size| self.adjust_pointer_for_sized_access(ptr, size))
3015+
.map_or(ptr, |(ptr, _)| ptr);
29973016

29983017
let elem_ty = match self.lookup_type(ptr.ty) {
29993018
SpirvType::Pointer { pointee } => pointee,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Test filling an array with uninitialized values.
2+
// build-pass
3+
4+
use core::mem::MaybeUninit;
5+
use spirv_std::macros::spirv;
6+
7+
#[repr(C)]
8+
pub struct Buf {
9+
data: [MaybeUninit<u32>; 4],
10+
len: u32,
11+
}
12+
13+
pub fn clear_field(buf: &mut Buf) {
14+
buf.data = [const { MaybeUninit::uninit() }; 4];
15+
buf.len = 0;
16+
}
17+
18+
pub fn clear_whole(data: &mut [MaybeUninit<u32>; 4]) {
19+
*data = [const { MaybeUninit::uninit() }; 4];
20+
}
21+
22+
#[spirv(fragment)]
23+
pub fn main(o: &mut u32) {
24+
let mut buf = Buf {
25+
data: [MaybeUninit::new(*o); 4],
26+
len: 4,
27+
};
28+
clear_field(&mut buf);
29+
clear_whole(&mut buf.data);
30+
*o = buf.len;
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Test that filling the smaller field of a union is an error, instead of being ignored.
2+
// build-fail
3+
4+
use spirv_std::macros::spirv;
5+
6+
pub union Words {
7+
two: [u32; 2],
8+
four: [u32; 4],
9+
}
10+
11+
#[spirv(fragment)]
12+
pub fn main(o: &mut u32) {
13+
let mut words = Words { four: [1; 4] };
14+
words.two = [0; 2];
15+
*o = unsafe { words.four[3] };
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: memset of 8 bytes into `[u32; 4]` is not supported yet
2+
--> $DIR/init_array_union_field.rs:14:5
3+
|
4+
LL | words.two = [0; 2];
5+
| ^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

‎tests/difftests/tests/Cargo.lock‎

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/difftests/tests/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ members = [
2727
"lang/control_flow/control_flow-wgsl",
2828
"lang/control_flow_complex/control_flow_complex-rust",
2929
"lang/control_flow_complex/control_flow_complex-wgsl",
30+
"lang/core/array/fill_offset_zero/fill_offset_zero-rust",
31+
"lang/core/array/fill_offset_zero/fill_offset_zero-cpu",
3032
"lang/core/ops/math_ops/math_ops-rust",
3133
"lang/core/ops/math_ops/math_ops-wgsl",
3234
"lang/core/ops/vector_ops/vector_ops-rust",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "fill_offset_zero-cpu"
3+
edition.workspace = true
4+
5+
[lints]
6+
workspace = true
7+
8+
[dependencies]
9+
difftest.workspace = true
10+
fill_offset_zero-rust = { path = "../fill_offset_zero-rust" }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use difftest::config::{Config, TestMetadata};
2+
use fill_offset_zero_rust::{INPUT, eval};
3+
4+
fn main() {
5+
let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap();
6+
config.write_result(eval(&INPUT).as_flattened()).unwrap();
7+
config.write_metadata(&TestMetadata::u32()).unwrap();
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "fill_offset_zero-rust"
3+
edition.workspace = true
4+
5+
[lints]
6+
workspace = true
7+
8+
# GPU deps
9+
[dependencies]
10+
spirv-std.workspace = true
11+
12+
# CPU deps (for the test harness)
13+
[target.'cfg(not(target_arch = "spirv"))'.dependencies]
14+
difftest.workspace = true
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#![no_std]
2+
3+
use spirv_std::spirv;
4+
5+
// Non-zero, so a fill that writes nothing is visible
6+
pub const INPUT: [u32; 4] = [1, 2, 3, 4];
7+
8+
pub const ROWS: usize = 9;
9+
pub const WORDS: usize = 6;
10+
// One row per fill, the contents of the aggregate after it
11+
pub type Output = [[u32; WORDS]; ROWS];
12+
13+
// `repr(C)` keeps the array at offset 0
14+
#[repr(C)]
15+
#[derive(Clone, Copy)]
16+
pub struct First {
17+
pub arr: [u32; 4],
18+
pub tail: u32,
19+
}
20+
21+
#[repr(C)]
22+
pub struct Nested {
23+
pub head: u32,
24+
pub first: First,
25+
}
26+
27+
pub struct Only {
28+
pub arr: [u32; 4],
29+
}
30+
31+
pub fn reset(s: &mut First) {
32+
s.arr = [0; 4];
33+
}
34+
35+
// Borrowed, so the struct is not split into separate variables
36+
fn first_words(s: &First) -> [u32; WORDS] {
37+
[s.arr[0], s.arr[1], s.arr[2], s.arr[3], s.tail, 0]
38+
}
39+
40+
fn nested_words(n: &Nested) -> [u32; WORDS] {
41+
let [a0, a1, a2, a3] = n.first.arr;
42+
[n.head, a0, a1, a2, a3, n.first.tail]
43+
}
44+
45+
fn only_words(s: &Only) -> [u32; WORDS] {
46+
[s.arr[0], s.arr[1], s.arr[2], s.arr[3], 0, 0]
47+
}
48+
49+
fn table_words(t: &[[u32; 2]; 3]) -> [u32; WORDS] {
50+
[t[0][0], t[0][1], t[1][0], t[1][1], t[2][0], t[2][1]]
51+
}
52+
53+
pub fn eval(input: &[u32; 4]) -> Output {
54+
let [a, b, c, d] = *input;
55+
let first = First {
56+
arr: [a, b, c, d],
57+
tail: a,
58+
};
59+
let table = [[a, b], [c, d], [a, d]];
60+
61+
let mut field = first;
62+
field.arr = [0; 4];
63+
64+
let mut param = first;
65+
reset(&mut param);
66+
67+
// The first field of a struct that is itself at a non-zero offset
68+
let mut nested = Nested { head: b, first };
69+
nested.first.arr = [u32::MAX; 4];
70+
71+
let mut elem0 = table;
72+
elem0[0] = [0; 2];
73+
74+
let mut reborrow = table;
75+
let row = &mut reborrow[0];
76+
*row = [0; 2];
77+
78+
let mut floats = [[a as f32, b as f32], [c as f32, d as f32]];
79+
floats[0] = [0.0; 2];
80+
81+
// For comparison: a non-zero offset, an array that is the only field, and
82+
// a fill that is not lowered to `memset`
83+
let mut elem1 = table;
84+
elem1[1] = [0; 2];
85+
86+
let mut only = Only { arr: [a, b, c, d] };
87+
only.arr = [0; 4];
88+
89+
let mut ones = first;
90+
ones.arr = [1; 4];
91+
92+
[
93+
first_words(&field),
94+
first_words(&param),
95+
nested_words(&nested),
96+
table_words(&elem0),
97+
table_words(&reborrow),
98+
[
99+
floats[0][0].to_bits(),
100+
floats[0][1].to_bits(),
101+
floats[1][0].to_bits(),
102+
floats[1][1].to_bits(),
103+
0,
104+
0,
105+
],
106+
table_words(&elem1),
107+
only_words(&only),
108+
first_words(&ones),
109+
]
110+
}
111+
112+
#[spirv(compute(threads(1)))]
113+
pub fn main_cs(
114+
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] input: &[u32; 4],
115+
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] output: &mut Output,
116+
) {
117+
*output = eval(input);
118+
}

0 commit comments

Comments
 (0)