Skip to content

Commit 11553f2

Browse files
JulianKnodtLegNeato
authored andcommitted
Account for signedness in memset const pat
Fixes #85. Fixes EmbarkStudios/rust-gpu#1061 Thanks to @LegNeato
1 parent 698f10a commit 11553f2

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
193193
match *ty {
194194
SpirvType::Void => self.fatal("memset invalid on void pattern"),
195195
SpirvType::Bool => self.fatal("memset invalid on bool pattern"),
196-
SpirvType::Integer(width, _signedness) => match width {
196+
SpirvType::Integer(width, false) => match width {
197197
8 => self.constant_u8(self.span(), fill_byte).def(self),
198198
16 => self
199199
.constant_u16(self.span(), memset_fill_u16(fill_byte))
@@ -208,6 +208,29 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
208208
"memset on integer width {width} not implemented yet"
209209
)),
210210
},
211+
SpirvType::Integer(width, true) => match width {
212+
8 => self
213+
.constant_i8(self.span(), unsafe { std::mem::transmute(fill_byte) })
214+
.def(self),
215+
16 => self
216+
.constant_i16(self.span(), unsafe {
217+
std::mem::transmute(memset_fill_u16(fill_byte))
218+
})
219+
.def(self),
220+
32 => self
221+
.constant_i32(self.span(), unsafe {
222+
std::mem::transmute(memset_fill_u32(fill_byte))
223+
})
224+
.def(self),
225+
64 => self
226+
.constant_i64(self.span(), unsafe {
227+
std::mem::transmute(memset_fill_u64(fill_byte))
228+
})
229+
.def(self),
230+
_ => self.fatal(format!(
231+
"memset on integer width {width} not implemented yet"
232+
)),
233+
},
211234
SpirvType::Float(width) => match width {
212235
32 => self
213236
.constant_f32(self.span(), f32::from_bits(memset_fill_u32(fill_byte)))
@@ -318,7 +341,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
318341
} else {
319342
for index in 0..count {
320343
let const_index = self.constant_u32(self.span(), index as u32);
321-
let gep_ptr = self.gep(pat.ty, ptr, &[const_index]);
344+
let gep_ptr = self.inbounds_gep(pat.ty, ptr, &[const_index]);
322345
self.store(pat, gep_ptr, Align::from_bytes(0).unwrap());
323346
}
324347
}

crates/rustc_codegen_spirv/src/codegen_cx/constant.rs

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ impl<'tcx> CodegenCx<'tcx> {
1818
self.builder.def_constant_cx(ty, val, self)
1919
}
2020

21+
pub fn constant_i8(&self, span: Span, val: i8) -> SpirvValue {
22+
let ty = SpirvType::Integer(8, true).def(span, self);
23+
self.def_constant(ty, SpirvConst::U32(val as u32))
24+
}
25+
2126
pub fn constant_u8(&self, span: Span, val: u8) -> SpirvValue {
2227
self.constant_int_from_native_unsigned(span, val)
2328
}
@@ -42,6 +47,11 @@ impl<'tcx> CodegenCx<'tcx> {
4247
self.constant_int_from_native_unsigned(span, val)
4348
}
4449

50+
pub fn constant_i64(&self, span: Span, val: u64) -> SpirvValue {
51+
let ty = SpirvType::Integer(64, true).def(span, self);
52+
self.def_constant(ty, SpirvConst::U64(val))
53+
}
54+
4555
pub fn constant_u64(&self, span: Span, val: u64) -> SpirvValue {
4656
self.constant_int_from_native_unsigned(span, val)
4757
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test creating an array.
2+
// build-pass
3+
4+
use spirv_std::macros::spirv;
5+
6+
#[spirv(fragment)]
7+
pub fn main(o: &mut i16) {
8+
let array = [0i16; 4];
9+
*o = array[1];
10+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test creating an array.
2+
// build-pass
3+
4+
use spirv_std::macros::spirv;
5+
6+
#[spirv(fragment)]
7+
pub fn main(o: &mut i32) {
8+
let array = [0i32; 4];
9+
*o = array[1];
10+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test creating an array.
2+
// build-pass
3+
4+
use spirv_std::macros::spirv;
5+
6+
#[spirv(fragment)]
7+
pub fn main(o: &mut i64) {
8+
let array = [0i64; 4];
9+
*o = array[1];
10+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test creating an array.
2+
// build-pass
3+
4+
use spirv_std::macros::spirv;
5+
6+
#[spirv(fragment)]
7+
pub fn main(o: &mut i8) {
8+
let array = [0i8; 4];
9+
*o = array[1];
10+
}

0 commit comments

Comments
 (0)