Skip to content

Commit 8d35dab

Browse files
committed
const folding: shift operations
1 parent f9eb999 commit 8d35dab

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,13 @@ macro_rules! simple_op {
143143
macro_rules! simple_shift_op {
144144
(
145145
$func_name:ident
146-
, int: $inst_name:ident
146+
$(, int: $inst_int:ident)?
147+
$(, uint: $inst_uint:ident)?
148+
$(, sint: $inst_sint:ident)?
147149
$(, fold_const {
148-
$(shift_uint($shift_uint_lhs:ident, $shift_uint_rhs:ident) => $fold_shift_uint:expr;)?
149-
$(shift_int($shift_int_lhs:ident, $shift_int_rhs:ident) => $fold_shift_int:expr;)?
150+
$(int($int_lhs:ident, $int_rhs:ident) => $fold_int:expr;)?
151+
$(uint($uint_lhs:ident, $uint_rhs:ident) => $fold_uint:expr;)?
152+
$(sint($sint_lhs:ident, $sint_rhs:ident) => $fold_sint:expr;)?
150153
})?
151154
) => {
152155
fn $func_name(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value {
@@ -159,23 +162,45 @@ macro_rules! simple_shift_op {
159162
#[allow(unreachable_patterns)]
160163
match (const_lhs, const_rhs) {
161164
$(
162-
(ConstValue::Unsigned($shift_uint_lhs), ConstValue::Unsigned($shift_uint_rhs)) => return self.const_uint_big(result_type, $fold_shift_uint),
163-
(ConstValue::Unsigned($shift_uint_lhs), ConstValue::Signed($shift_uint_rhs)) => return self.const_uint_big(result_type, $fold_shift_uint),
165+
(ConstValue::Unsigned($int_lhs), ConstValue::Unsigned($int_rhs)) => return self.const_uint_big(result_type, $fold_int),
166+
(ConstValue::Unsigned($int_lhs), ConstValue::Signed($int_rhs)) => return self.const_uint_big(result_type, $fold_int),
167+
(ConstValue::Signed($int_lhs), ConstValue::Unsigned($int_rhs)) => return self.const_uint_big(result_type, $fold_int as u128),
168+
(ConstValue::Signed($int_lhs), ConstValue::Signed($int_rhs)) => return self.const_uint_big(result_type, $fold_int as u128),
169+
)?
170+
$(
171+
(ConstValue::Unsigned($uint_lhs), ConstValue::Unsigned($uint_rhs)) => return self.const_uint_big(result_type, $fold_uint),
172+
(ConstValue::Unsigned($uint_lhs), ConstValue::Signed($uint_rhs)) => return self.const_uint_big(result_type, $fold_uint),
164173
)?
165174
$(
166-
(ConstValue::Signed($shift_int_lhs), ConstValue::Unsigned($shift_uint_rhs)) => return self.const_uint_big(result_type, $fold_shift_int),
167-
(ConstValue::Signed($shift_int_lhs), ConstValue::Signed($shift_uint_rhs)) => return self.const_uint_big(result_type, $fold_shift_int),
175+
(ConstValue::Signed($sint_lhs), ConstValue::Unsigned($sint_rhs)) => return self.const_uint_big(result_type, $fold_sint as u128),
176+
(ConstValue::Signed($sint_lhs), ConstValue::Signed($sint_rhs)) => return self.const_uint_big(result_type, $fold_sint as u128),
168177
)?
169178
_ => (),
170179
}
171180
}
172181
}
173182
)?
174183

175-
self.emit()
176-
.$inst_name(result_type, None, lhs.def(self), rhs.def(self))
177-
.unwrap()
178-
.with_type(result_type)
184+
match self.lookup_type(result_type) {
185+
$(SpirvType::Integer(_, _) => {
186+
self.emit()
187+
.$inst_int(result_type, None, lhs.def(self), rhs.def(self))
188+
})?
189+
$(SpirvType::Integer(_, false) => {
190+
self.emit()
191+
.$inst_uint(result_type, None, lhs.def(self), rhs.def(self))
192+
})?
193+
$(SpirvType::Integer(_, true) => {
194+
self.emit()
195+
.$inst_sint(result_type, None, lhs.def(self), rhs.def(self))
196+
})?
197+
o => self.fatal(format!(
198+
concat!(stringify!($func_name), "() not implemented for type {}"),
199+
o.debug(result_type, self)
200+
)),
201+
}
202+
.unwrap()
203+
.with_type(result_type)
179204
}
180205
};
181206
}
@@ -1587,24 +1612,24 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
15871612
simple_op! {frem_algebraic, float: f_rem} // algebraic=normal
15881613
simple_shift_op! {
15891614
shl,
1590-
int: shift_left_logical
1591-
// fold_const {
1592-
// int(a, b) => a.wrapping_shl(b as u32);
1593-
// }
1615+
int: shift_left_logical,
1616+
fold_const {
1617+
int(a, b) => a.wrapping_shl(b as u32);
1618+
}
15941619
}
15951620
simple_shift_op! {
15961621
lshr,
1597-
int: shift_right_logical
1598-
// fold_const {
1599-
// int(a, b) => a.wrapping_shr(b as u32);
1600-
// }
1622+
uint: shift_right_logical,
1623+
fold_const {
1624+
uint(a, b) => a.wrapping_shr(b as u32);
1625+
}
16011626
}
16021627
simple_shift_op! {
16031628
ashr,
1604-
int: shift_right_arithmetic
1605-
// fold_const {
1606-
// int(a, b) => a.wrapping_shr(b as u32);
1607-
// }
1629+
sint: shift_right_arithmetic,
1630+
fold_const {
1631+
sint(a, b) => a.wrapping_shr(b as u32);
1632+
}
16081633
}
16091634
simple_uni_op! {
16101635
neg,

0 commit comments

Comments
 (0)