Skip to content

Array init with 0i32 uses 0u32 instead #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2025
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
29 changes: 27 additions & 2 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
match *ty {
SpirvType::Void => self.fatal("memset invalid on void pattern"),
SpirvType::Bool => self.fatal("memset invalid on bool pattern"),
SpirvType::Integer(width, _signedness) => match width {
SpirvType::Integer(width, false) => match width {
8 => self.constant_u8(self.span(), fill_byte).def(self),
16 => self
.constant_u16(self.span(), memset_fill_u16(fill_byte))
Expand All @@ -208,6 +208,31 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
"memset on integer width {width} not implemented yet"
)),
},
SpirvType::Integer(width, true) => match width {
8 => self
.constant_i8(self.span(), unsafe {
std::mem::transmute::<u8, i8>(fill_byte)
})
.def(self),
16 => self
.constant_i16(self.span(), unsafe {
std::mem::transmute::<u16, i16>(memset_fill_u16(fill_byte))
})
.def(self),
32 => self
.constant_i32(self.span(), unsafe {
std::mem::transmute::<u32, i32>(memset_fill_u32(fill_byte))
})
.def(self),
64 => self
.constant_i64(self.span(), unsafe {
std::mem::transmute::<u64, i64>(memset_fill_u64(fill_byte))
})
.def(self),
_ => self.fatal(format!(
"memset on integer width {width} not implemented yet"
)),
},
SpirvType::Float(width) => match width {
32 => self
.constant_f32(self.span(), f32::from_bits(memset_fill_u32(fill_byte)))
Expand Down Expand Up @@ -318,7 +343,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
} else {
for index in 0..count {
let const_index = self.constant_u32(self.span(), index as u32);
let gep_ptr = self.gep(pat.ty, ptr, &[const_index]);
let gep_ptr = self.inbounds_gep(pat.ty, ptr, &[const_index]);
self.store(pat, gep_ptr, Align::from_bytes(0).unwrap());
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rustc_codegen_spirv/src/codegen_cx/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl<'tcx> CodegenCx<'tcx> {
self.constant_int_from_native_unsigned(span, val)
}

pub fn constant_i64(&self, span: Span, val: i64) -> SpirvValue {
self.constant_int_from_native_signed(span, val)
}

pub fn constant_u64(&self, span: Span, val: u64) -> SpirvValue {
self.constant_int_from_native_unsigned(span, val)
}
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/lang/core/array/init_array_i16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Test creating an array.
// build-pass

use spirv_std::macros::spirv;

#[spirv(fragment)]
pub fn main(o: &mut i16) {
let array = [0i16; 4];
*o = array[1];
}
10 changes: 10 additions & 0 deletions tests/ui/lang/core/array/init_array_i32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Test creating an array.
// build-pass

use spirv_std::macros::spirv;

#[spirv(fragment)]
pub fn main(o: &mut i32) {
let array = [0i32; 4];
*o = array[1];
}
10 changes: 10 additions & 0 deletions tests/ui/lang/core/array/init_array_i64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Test creating an array.
// build-pass

use spirv_std::macros::spirv;

#[spirv(fragment)]
pub fn main(o: &mut i64) {
let array = [0i64; 4];
*o = array[1];
}
10 changes: 10 additions & 0 deletions tests/ui/lang/core/array/init_array_i8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Test creating an array.
// build-pass

use spirv_std::macros::spirv;

#[spirv(fragment)]
pub fn main(o: &mut i8) {
let array = [0i8; 4];
*o = array[1];
}