Skip to content

Commit 393ed52

Browse files
Firestar99eddyb
authored andcommitted
const folding: use checked operations and allow bailing
1 parent a8949ce commit 393ed52

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,18 @@ macro_rules! simple_op {
107107
if let Some(const_lhs) = self.try_get_const_value(lhs)
108108
&& let Some(const_rhs) = self.try_get_const_value(rhs)
109109
{
110-
#[allow(unreachable_patterns)]
111-
match (const_lhs, const_rhs) {
110+
let result = (|| Some(match (const_lhs, const_rhs) {
112111
$(
113-
(ConstValue::Unsigned($int_lhs), ConstValue::Unsigned($int_rhs)) => return self.const_uint_big(result_type, $fold_int),
114-
(ConstValue::Signed($int_lhs), ConstValue::Signed($int_rhs)) => return self.const_uint_big(result_type, $fold_int as u128),
112+
(ConstValue::Unsigned($int_lhs), ConstValue::Unsigned($int_rhs)) => $fold_int,
113+
(ConstValue::Signed($int_lhs), ConstValue::Signed($int_rhs)) => $fold_int as u128,
115114
)?
116-
$((ConstValue::Unsigned($uint_lhs), ConstValue::Unsigned($uint_rhs)) => return self.const_uint_big(result_type, $fold_uint), )?
117-
$((ConstValue::Signed($sint_lhs), ConstValue::Signed($sint_rhs)) => return self.const_uint_big(result_type, $fold_sint as u128), )?
118-
$((ConstValue::Bool($bool_lhs), ConstValue::Bool($bool_rhs)) => return self.const_uint_big(result_type, ($fold_bool).into()), )?
119-
_ => (),
115+
$((ConstValue::Unsigned($uint_lhs), ConstValue::Unsigned($uint_rhs)) => $fold_uint,)?
116+
$((ConstValue::Signed($sint_lhs), ConstValue::Signed($sint_rhs)) => $fold_sint as u128, )?
117+
$((ConstValue::Bool($bool_lhs), ConstValue::Bool($bool_rhs)) => ($fold_bool).into(), )?
118+
_ => return None,
119+
}))();
120+
if let Some(result) = result {
121+
return self.const_uint_big(result_type, result);
120122
}
121123
}
122124
)?
@@ -174,23 +176,25 @@ macro_rules! simple_shift_op {
174176
if let Some(const_lhs) = self.try_get_const_value(lhs)
175177
&& let Some(const_rhs) = self.try_get_const_value(rhs)
176178
{
177-
#[allow(unreachable_patterns)]
178-
match (const_lhs, const_rhs) {
179+
let result = (|| Some(match (const_lhs, const_rhs) {
179180
$(
180-
(ConstValue::Unsigned($int_lhs), ConstValue::Unsigned($int_rhs)) => return self.const_uint_big(result_type, $fold_int),
181-
(ConstValue::Unsigned($int_lhs), ConstValue::Signed($int_rhs)) => return self.const_uint_big(result_type, $fold_int),
182-
(ConstValue::Signed($int_lhs), ConstValue::Unsigned($int_rhs)) => return self.const_uint_big(result_type, $fold_int as u128),
183-
(ConstValue::Signed($int_lhs), ConstValue::Signed($int_rhs)) => return self.const_uint_big(result_type, $fold_int as u128),
181+
(ConstValue::Unsigned($int_lhs), ConstValue::Unsigned($int_rhs)) => $fold_int,
182+
(ConstValue::Unsigned($int_lhs), ConstValue::Signed($int_rhs)) => $fold_int,
183+
(ConstValue::Signed($int_lhs), ConstValue::Unsigned($int_rhs)) => $fold_int as u128,
184+
(ConstValue::Signed($int_lhs), ConstValue::Signed($int_rhs)) => $fold_int as u128,
184185
)?
185186
$(
186-
(ConstValue::Unsigned($uint_lhs), ConstValue::Unsigned($uint_rhs)) => return self.const_uint_big(result_type, $fold_uint),
187-
(ConstValue::Unsigned($uint_lhs), ConstValue::Signed($uint_rhs)) => return self.const_uint_big(result_type, $fold_uint),
187+
(ConstValue::Unsigned($uint_lhs), ConstValue::Unsigned($uint_rhs)) => $fold_uint,
188+
(ConstValue::Unsigned($uint_lhs), ConstValue::Signed($uint_rhs)) => $fold_uint,
188189
)?
189190
$(
190-
(ConstValue::Signed($sint_lhs), ConstValue::Unsigned($sint_rhs)) => return self.const_uint_big(result_type, $fold_sint as u128),
191-
(ConstValue::Signed($sint_lhs), ConstValue::Signed($sint_rhs)) => return self.const_uint_big(result_type, $fold_sint as u128),
191+
(ConstValue::Signed($sint_lhs), ConstValue::Unsigned($sint_rhs)) => $fold_sint as u128,
192+
(ConstValue::Signed($sint_lhs), ConstValue::Signed($sint_rhs)) => $fold_sint as u128,
192193
)?
193-
_ => (),
194+
_ => return None,
195+
}))();
196+
if let Some(result) = result {
197+
return self.const_uint_big(result_type, result);
194198
}
195199
}
196200
)?
@@ -240,15 +244,18 @@ macro_rules! simple_uni_op {
240244
$(
241245
#[allow(unreachable_patterns, clippy::collapsible_match)]
242246
if let Some(const_val) = self.try_get_const_value(val) {
243-
match const_val {
247+
let result = (|| Some(match (const_val) {
244248
$(
245-
ConstValue::Unsigned($int_val) => return self.const_uint_big(result_type, $fold_int),
246-
ConstValue::Signed($int_val) => return self.const_uint_big(result_type, $fold_int as u128),
249+
ConstValue::Unsigned($int_val) => $fold_int,
250+
ConstValue::Signed($int_val) => $fold_int as u128,
247251
)?
248-
$(ConstValue::Unsigned($uint_val) => return self.const_uint_big(result_type, $fold_uint), )?
249-
$(ConstValue::Signed($sint_val) => return self.const_uint_big(result_type, $fold_sint as u128), )?
250-
$(ConstValue::Bool($bool_val) => return self.const_uint_big(result_type, ($fold_bool).into()), )?
251-
_ => (),
252+
$(ConstValue::Unsigned($uint_val) => $fold_uint, )?
253+
$(ConstValue::Signed($sint_val) => $fold_sint as u128, )?
254+
$(ConstValue::Bool($bool_val) => ($fold_bool).into(), )?
255+
_ => return None,
256+
}))();
257+
if let Some(result) = result {
258+
return self.const_uint_big(result_type, result);
252259
}
253260
}
254261
)?
@@ -1531,7 +1538,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
15311538
add,
15321539
int: i_add,
15331540
fold_const {
1534-
int(a, b) => a.wrapping_add(b);
1541+
int(a, b) => a.checked_add(b)?;
15351542
}
15361543
}
15371544
// FIXME(eddyb) try to annotate the SPIR-V for `fast` and `algebraic`.
@@ -1542,7 +1549,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
15421549
sub,
15431550
int: i_sub,
15441551
fold_const {
1545-
int(a, b) => a.wrapping_sub(b);
1552+
int(a, b) => a.checked_sub(b)?;
15461553
}
15471554
}
15481555
simple_op! {fsub, float: f_sub}
@@ -1552,7 +1559,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
15521559
mul,
15531560
int: i_mul,
15541561
fold_const {
1555-
int(a, b) => a.wrapping_mul(b);
1562+
int(a, b) => a.checked_mul(b)?;
15561563
}
15571564
}
15581565
simple_op! {fmul, float: f_mul}
@@ -1562,7 +1569,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
15621569
udiv,
15631570
uint: u_div,
15641571
fold_const {
1565-
uint(a, b) => a.wrapping_div(b);
1572+
uint(a, b) => a.checked_div(b)?;
15661573
}
15671574
}
15681575
// Note: exactudiv is UB when there's a remainder, so it's valid to implement as a normal div.
@@ -1571,22 +1578,22 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
15711578
exactudiv,
15721579
uint: u_div,
15731580
fold_const {
1574-
uint(a, b) => a.wrapping_div(b);
1581+
uint(a, b) => a.checked_div(b)?;
15751582
}
15761583
}
15771584
simple_op! {
15781585
sdiv,
15791586
sint: s_div,
15801587
fold_const {
1581-
sint(a, b) => a.wrapping_div(b);
1588+
sint(a, b) => a.checked_div(b)?;
15821589
}
15831590
}
15841591
// Same note and TODO as exactudiv
15851592
simple_op! {
15861593
exactsdiv,
15871594
sint: s_div,
15881595
fold_const {
1589-
sint(a, b) => a.wrapping_div(b);
1596+
sint(a, b) => a.checked_div(b)?;
15901597
}
15911598
}
15921599
simple_op! {fdiv, float: f_div}
@@ -1596,14 +1603,14 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
15961603
urem,
15971604
uint: u_mod,
15981605
fold_const {
1599-
uint(a, b) => a.wrapping_rem(b);
1606+
uint(a, b) => a.checked_rem(b)?;
16001607
}
16011608
}
16021609
simple_op! {
16031610
srem,
16041611
sint: s_rem,
16051612
fold_const {
1606-
sint(a, b) => a.wrapping_rem(b);
1613+
sint(a, b) => a.checked_rem(b)?;
16071614
}
16081615
}
16091616
simple_op! {frem, float: f_rem}
@@ -1613,28 +1620,28 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
16131620
shl,
16141621
int: shift_left_logical,
16151622
fold_const {
1616-
int(a, b) => a.wrapping_shl(b as u32);
1623+
int(a, b) => a.checked_shl(b as u32)?;
16171624
}
16181625
}
16191626
simple_shift_op! {
16201627
lshr,
16211628
uint: shift_right_logical,
16221629
fold_const {
1623-
uint(a, b) => a.wrapping_shr(b as u32);
1630+
uint(a, b) => a.checked_shr(b as u32)?;
16241631
}
16251632
}
16261633
simple_shift_op! {
16271634
ashr,
16281635
sint: shift_right_arithmetic,
16291636
fold_const {
1630-
sint(a, b) => a.wrapping_shr(b as u32);
1637+
sint(a, b) => a.checked_shr(b as u32)?;
16311638
}
16321639
}
16331640
simple_uni_op! {
16341641
neg,
16351642
sint: s_negate,
16361643
fold_const {
1637-
sint(a) => a.wrapping_neg();
1644+
sint(a) => a.checked_neg()?;
16381645
}
16391646
}
16401647
simple_uni_op! {fneg, float: f_negate}

0 commit comments

Comments
 (0)