Skip to content

Commit c1fd945

Browse files
committed
const folding: shift operations
1 parent 580ca4e commit c1fd945

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
}
@@ -1600,24 +1625,24 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
16001625
simple_op! {frem_algebraic, float: f_rem} // algebraic=normal
16011626
simple_shift_op! {
16021627
shl,
1603-
int: shift_left_logical
1604-
// fold_const {
1605-
// int(a, b) => a.wrapping_shl(b as u32);
1606-
// }
1628+
int: shift_left_logical,
1629+
fold_const {
1630+
int(a, b) => a.wrapping_shl(b as u32);
1631+
}
16071632
}
16081633
simple_shift_op! {
16091634
lshr,
1610-
int: shift_right_logical
1611-
// fold_const {
1612-
// int(a, b) => a.wrapping_shr(b as u32);
1613-
// }
1635+
uint: shift_right_logical,
1636+
fold_const {
1637+
uint(a, b) => a.wrapping_shr(b as u32);
1638+
}
16141639
}
16151640
simple_shift_op! {
16161641
ashr,
1617-
int: shift_right_arithmetic
1618-
// fold_const {
1619-
// int(a, b) => a.wrapping_shr(b as u32);
1620-
// }
1642+
sint: shift_right_arithmetic,
1643+
fold_const {
1644+
sint(a, b) => a.wrapping_shr(b as u32);
1645+
}
16211646
}
16221647
simple_uni_op! {
16231648
neg,

0 commit comments

Comments
 (0)