Skip to content

Commit d6913d8

Browse files
committed
const folding: only const fold if const value is valid
prevent const folding from hiding issues
1 parent 38a78c8 commit d6913d8

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5454
match self.lookup_type(val.ty) {
5555
SpirvType::Integer(bits, signed) => {
5656
let size = Size::from_bits(bits);
57-
Some(if signed {
58-
ConstValue::Signed(size.sign_extend(x))
57+
// ensure the u128 constant didn't overflow and const-folding isn't hiding issues
58+
if x == size.truncate(x) {
59+
Some(if signed {
60+
ConstValue::Signed(size.sign_extend(x))
61+
} else {
62+
ConstValue::Unsigned(size.truncate(x))
63+
})
5964
} else {
60-
ConstValue::Unsigned(size.truncate(x))
61-
})
65+
None
66+
}
67+
}
68+
SpirvType::Bool => {
69+
match x {
70+
0 => Some(ConstValue::Bool(false)),
71+
1 => Some(ConstValue::Bool(true)),
72+
// ensure const-folding isn't hiding issues
73+
_ => None,
74+
}
6275
}
63-
SpirvType::Bool => Some(ConstValue::Bool(x != 0)),
6476
_ => None,
6577
}
6678
} else {

0 commit comments

Comments
 (0)