Skip to content

Commit 56cc132

Browse files
LegNeatoFirestar99
authored andcommitted
Fix divide by zero
We assumed every array/vector/matrix element has a non-zero stride, but `[()]` inside `BitSlice` produced a zero stride which is divide by zero. Fixes #424.
1 parent dbf3737 commit 56cc132

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
767767
let stride = ty_kind.sizeof(self)?;
768768
ty_size = MaybeSized::Sized(stride);
769769

770-
indices.push((offset.bytes() / stride.bytes()).try_into().ok()?);
771-
offset = Size::from_bytes(offset.bytes() % stride.bytes());
770+
if stride == Size::ZERO {
771+
if offset != Size::ZERO {
772+
trace!("zero-sized element with non-zero offset: {:?}", offset);
773+
return None;
774+
}
775+
776+
indices.push(0);
777+
offset = Size::ZERO;
778+
} else {
779+
indices.push((offset.bytes() / stride.bytes()).try_into().ok()?);
780+
offset = Size::from_bytes(offset.bytes() % stride.bytes());
781+
}
772782
}
773783
_ => {
774784
trace!("recovering access chain from SOMETHING ELSE, RETURNING NONE");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// build-pass
2+
3+
#![no_std]
4+
5+
use core::marker::PhantomData;
6+
use spirv_std::spirv;
7+
8+
pub struct BitSlice<T, O> {
9+
_ord: PhantomData<O>,
10+
_typ: PhantomData<[T]>,
11+
_mem: [()],
12+
}
13+
14+
#[spirv(compute(threads(1)))]
15+
pub fn issue424() {}

0 commit comments

Comments
 (0)