Skip to content
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
31 changes: 22 additions & 9 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
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

Original file line number Diff line number Diff line change
@@ -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];
}
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
}
33 changes: 33 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.

4 changes: 4 additions & 0 deletions tests/difftests/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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
Loading
Loading