Skip to content

Commit 7178adb

Browse files
Firestar99eddyb
authored andcommitted
const folding: only const fold if const value is valid
prevent const folding from hiding issues
1 parent 1d5ae94 commit 7178adb

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
@@ -56,13 +56,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5656
match self.lookup_type(val.ty) {
5757
SpirvType::Integer(bits, signed) => {
5858
let size = Size::from_bits(bits);
59-
Some(if signed {
60-
ConstValue::Signed(size.sign_extend(x))
59+
// ensure the u128 constant didn't overflow and const-folding isn't hiding issues
60+
if x == size.truncate(x) {
61+
Some(if signed {
62+
ConstValue::Signed(size.sign_extend(x))
63+
} else {
64+
ConstValue::Unsigned(size.truncate(x))
65+
})
6166
} else {
62-
ConstValue::Unsigned(size.truncate(x))
63-
})
67+
None
68+
}
69+
}
70+
SpirvType::Bool => {
71+
match x {
72+
0 => Some(ConstValue::Bool(false)),
73+
1 => Some(ConstValue::Bool(true)),
74+
// ensure const-folding isn't hiding issues
75+
_ => None,
76+
}
6477
}
65-
SpirvType::Bool => Some(ConstValue::Bool(x != 0)),
6678
_ => None,
6779
}
6880
} else {

0 commit comments

Comments
 (0)