Skip to content

Commit 3349203

Browse files
committed
const folding: shift operations
1 parent 15ef700 commit 3349203

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
}
@@ -1765,24 +1790,24 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
17651790
simple_op! {frem_algebraic, float: f_rem} // algebraic=normal
17661791
simple_shift_op! {
17671792
shl,
1768-
int: shift_left_logical
1769-
// fold_const {
1770-
// int(a, b) => a.wrapping_shl(b as u32);
1771-
// }
1793+
int: shift_left_logical,
1794+
fold_const {
1795+
int(a, b) => a.wrapping_shl(b as u32);
1796+
}
17721797
}
17731798
simple_shift_op! {
17741799
lshr,
1775-
int: shift_right_logical
1776-
// fold_const {
1777-
// int(a, b) => a.wrapping_shr(b as u32);
1778-
// }
1800+
uint: shift_right_logical,
1801+
fold_const {
1802+
uint(a, b) => a.wrapping_shr(b as u32);
1803+
}
17791804
}
17801805
simple_shift_op! {
17811806
ashr,
1782-
int: shift_right_arithmetic
1783-
// fold_const {
1784-
// int(a, b) => a.wrapping_shr(b as u32);
1785-
// }
1807+
sint: shift_right_arithmetic,
1808+
fold_const {
1809+
sint(a, b) => a.wrapping_shr(b as u32);
1810+
}
17861811
}
17871812
simple_uni_op! {
17881813
neg,

0 commit comments

Comments
 (0)