From 11553f21618a223e20c732ed0ca9d76e362e0530 Mon Sep 17 00:00:00 2001 From: julianknodt Date: Fri, 12 Apr 2024 01:59:40 -0700 Subject: [PATCH 1/2] Account for signedness in memset const pat Fixes https://github.com/Rust-GPU/rust-gpu/issues/85. Fixes https://github.com/EmbarkStudios/rust-gpu/issues/1061 Thanks to @LegNeato --- .../src/builder/builder_methods.rs | 27 +++++++++++++++++-- .../src/codegen_cx/constant.rs | 10 +++++++ tests/ui/lang/core/array/init_array_i16.rs | 10 +++++++ tests/ui/lang/core/array/init_array_i32.rs | 10 +++++++ tests/ui/lang/core/array/init_array_i64.rs | 10 +++++++ tests/ui/lang/core/array/init_array_i8.rs | 10 +++++++ 6 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tests/ui/lang/core/array/init_array_i16.rs create mode 100644 tests/ui/lang/core/array/init_array_i32.rs create mode 100644 tests/ui/lang/core/array/init_array_i64.rs create mode 100644 tests/ui/lang/core/array/init_array_i8.rs diff --git a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs index aa01668dfa..687a5f0572 100644 --- a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs +++ b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs @@ -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)) @@ -208,6 +208,29 @@ 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(fill_byte) }) + .def(self), + 16 => self + .constant_i16(self.span(), unsafe { + std::mem::transmute(memset_fill_u16(fill_byte)) + }) + .def(self), + 32 => self + .constant_i32(self.span(), unsafe { + std::mem::transmute(memset_fill_u32(fill_byte)) + }) + .def(self), + 64 => self + .constant_i64(self.span(), unsafe { + std::mem::transmute(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))) @@ -318,7 +341,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()); } } diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs b/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs index a53e1e0de6..0ba47a6b42 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs @@ -18,6 +18,11 @@ impl<'tcx> CodegenCx<'tcx> { self.builder.def_constant_cx(ty, val, self) } + pub fn constant_i8(&self, span: Span, val: i8) -> SpirvValue { + let ty = SpirvType::Integer(8, true).def(span, self); + self.def_constant(ty, SpirvConst::U32(val as u32)) + } + pub fn constant_u8(&self, span: Span, val: u8) -> SpirvValue { self.constant_int_from_native_unsigned(span, val) } @@ -42,6 +47,11 @@ impl<'tcx> CodegenCx<'tcx> { self.constant_int_from_native_unsigned(span, val) } + pub fn constant_i64(&self, span: Span, val: u64) -> SpirvValue { + let ty = SpirvType::Integer(64, true).def(span, self); + self.def_constant(ty, SpirvConst::U64(val)) + } + pub fn constant_u64(&self, span: Span, val: u64) -> SpirvValue { self.constant_int_from_native_unsigned(span, val) } diff --git a/tests/ui/lang/core/array/init_array_i16.rs b/tests/ui/lang/core/array/init_array_i16.rs new file mode 100644 index 0000000000..d0f3269e0c --- /dev/null +++ b/tests/ui/lang/core/array/init_array_i16.rs @@ -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]; +} diff --git a/tests/ui/lang/core/array/init_array_i32.rs b/tests/ui/lang/core/array/init_array_i32.rs new file mode 100644 index 0000000000..c6fea6b900 --- /dev/null +++ b/tests/ui/lang/core/array/init_array_i32.rs @@ -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]; +} diff --git a/tests/ui/lang/core/array/init_array_i64.rs b/tests/ui/lang/core/array/init_array_i64.rs new file mode 100644 index 0000000000..8f5120c27c --- /dev/null +++ b/tests/ui/lang/core/array/init_array_i64.rs @@ -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]; +} diff --git a/tests/ui/lang/core/array/init_array_i8.rs b/tests/ui/lang/core/array/init_array_i8.rs new file mode 100644 index 0000000000..fda9b1abcd --- /dev/null +++ b/tests/ui/lang/core/array/init_array_i8.rs @@ -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]; +} From ffa710b8623a3fd2bd439757df77f2e8150c4955 Mon Sep 17 00:00:00 2001 From: Christian Legnitto Date: Wed, 9 Apr 2025 13:06:19 -0400 Subject: [PATCH 2/2] Port old change to latest main --- .../rustc_codegen_spirv/src/builder/builder_methods.rs | 10 ++++++---- crates/rustc_codegen_spirv/src/codegen_cx/constant.rs | 10 ++-------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs index 687a5f0572..954599fb1d 100644 --- a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs +++ b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs @@ -210,21 +210,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { }, SpirvType::Integer(width, true) => match width { 8 => self - .constant_i8(self.span(), unsafe { std::mem::transmute(fill_byte) }) + .constant_i8(self.span(), unsafe { + std::mem::transmute::(fill_byte) + }) .def(self), 16 => self .constant_i16(self.span(), unsafe { - std::mem::transmute(memset_fill_u16(fill_byte)) + std::mem::transmute::(memset_fill_u16(fill_byte)) }) .def(self), 32 => self .constant_i32(self.span(), unsafe { - std::mem::transmute(memset_fill_u32(fill_byte)) + std::mem::transmute::(memset_fill_u32(fill_byte)) }) .def(self), 64 => self .constant_i64(self.span(), unsafe { - std::mem::transmute(memset_fill_u64(fill_byte)) + std::mem::transmute::(memset_fill_u64(fill_byte)) }) .def(self), _ => self.fatal(format!( diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs b/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs index 0ba47a6b42..07e617c898 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs @@ -18,11 +18,6 @@ impl<'tcx> CodegenCx<'tcx> { self.builder.def_constant_cx(ty, val, self) } - pub fn constant_i8(&self, span: Span, val: i8) -> SpirvValue { - let ty = SpirvType::Integer(8, true).def(span, self); - self.def_constant(ty, SpirvConst::U32(val as u32)) - } - pub fn constant_u8(&self, span: Span, val: u8) -> SpirvValue { self.constant_int_from_native_unsigned(span, val) } @@ -47,9 +42,8 @@ impl<'tcx> CodegenCx<'tcx> { self.constant_int_from_native_unsigned(span, val) } - pub fn constant_i64(&self, span: Span, val: u64) -> SpirvValue { - let ty = SpirvType::Integer(64, true).def(span, self); - self.def_constant(ty, SpirvConst::U64(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 {