Skip to content

Commit eb136e4

Browse files
committed
const folding: shift operations
1 parent 3e88d2e commit eb136e4

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
@@ -140,10 +140,13 @@ macro_rules! simple_op {
140140
macro_rules! simple_shift_op {
141141
(
142142
$func_name:ident
143-
, int: $inst_name:ident
143+
$(, int: $inst_int:ident)?
144+
$(, uint: $inst_uint:ident)?
145+
$(, sint: $inst_sint:ident)?
144146
$(, fold_const {
145-
$(shift_uint($shift_uint_lhs:ident, $shift_uint_rhs:ident) => $fold_shift_uint:expr;)?
146-
$(shift_int($shift_int_lhs:ident, $shift_int_rhs:ident) => $fold_shift_int:expr;)?
147+
$(int($int_lhs:ident, $int_rhs:ident) => $fold_int:expr;)?
148+
$(uint($uint_lhs:ident, $uint_rhs:ident) => $fold_uint:expr;)?
149+
$(sint($sint_lhs:ident, $sint_rhs:ident) => $fold_sint:expr;)?
147150
})?
148151
) => {
149152
fn $func_name(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value {
@@ -154,22 +157,44 @@ macro_rules! simple_shift_op {
154157
#[allow(unreachable_patterns)]
155158
match (const_lhs, const_rhs) {
156159
$(
157-
(ConstValue::Unsigned($shift_uint_lhs), ConstValue::Unsigned($shift_uint_rhs)) => return self.const_uint_big(result_type, $fold_shift_uint),
158-
(ConstValue::Unsigned($shift_uint_lhs), ConstValue::Signed($shift_uint_rhs)) => return self.const_uint_big(result_type, $fold_shift_uint),
160+
(ConstValue::Unsigned($int_lhs), ConstValue::Unsigned($int_rhs)) => return self.const_uint_big(result_type, $fold_int),
161+
(ConstValue::Unsigned($int_lhs), ConstValue::Signed($int_rhs)) => return self.const_uint_big(result_type, $fold_int),
162+
(ConstValue::Signed($int_lhs), ConstValue::Unsigned($int_rhs)) => return self.const_uint_big(result_type, $fold_int as u128),
163+
(ConstValue::Signed($int_lhs), ConstValue::Signed($int_rhs)) => return self.const_uint_big(result_type, $fold_int as u128),
164+
)?
165+
$(
166+
(ConstValue::Unsigned($uint_lhs), ConstValue::Unsigned($uint_rhs)) => return self.const_uint_big(result_type, $fold_uint),
167+
(ConstValue::Unsigned($uint_lhs), ConstValue::Signed($uint_rhs)) => return self.const_uint_big(result_type, $fold_uint),
159168
)?
160169
$(
161-
(ConstValue::Signed($shift_int_lhs), ConstValue::Unsigned($shift_uint_rhs)) => return self.const_uint_big(result_type, $fold_shift_int),
162-
(ConstValue::Signed($shift_int_lhs), ConstValue::Signed($shift_uint_rhs)) => return self.const_uint_big(result_type, $fold_shift_int),
170+
(ConstValue::Signed($sint_lhs), ConstValue::Unsigned($sint_rhs)) => return self.const_uint_big(result_type, $fold_sint as u128),
171+
(ConstValue::Signed($sint_lhs), ConstValue::Signed($sint_rhs)) => return self.const_uint_big(result_type, $fold_sint as u128),
163172
)?
164173
_ => (),
165174
}
166175
}
167176
})?
168177

169-
self.emit()
170-
.$inst_name(result_type, None, lhs.def(self), rhs.def(self))
171-
.unwrap()
172-
.with_type(result_type)
178+
match self.lookup_type(result_type) {
179+
$(SpirvType::Integer(_, _) => {
180+
self.emit()
181+
.$inst_int(result_type, None, lhs.def(self), rhs.def(self))
182+
})?
183+
$(SpirvType::Integer(_, false) => {
184+
self.emit()
185+
.$inst_uint(result_type, None, lhs.def(self), rhs.def(self))
186+
})?
187+
$(SpirvType::Integer(_, true) => {
188+
self.emit()
189+
.$inst_sint(result_type, None, lhs.def(self), rhs.def(self))
190+
})?
191+
o => self.fatal(format!(
192+
concat!(stringify!($func_name), "() not implemented for type {}"),
193+
o.debug(result_type, self)
194+
)),
195+
}
196+
.unwrap()
197+
.with_type(result_type)
173198
}
174199
};
175200
}
@@ -1759,24 +1784,24 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
17591784
simple_op! {frem_algebraic, float: f_rem} // algebraic=normal
17601785
simple_shift_op! {
17611786
shl,
1762-
int: shift_left_logical
1763-
// fold_const {
1764-
// int(a, b) => a.wrapping_shl(b as u32);
1765-
// }
1787+
int: shift_left_logical,
1788+
fold_const {
1789+
int(a, b) => a.wrapping_shl(b as u32);
1790+
}
17661791
}
17671792
simple_shift_op! {
17681793
lshr,
1769-
int: shift_right_logical
1770-
// fold_const {
1771-
// int(a, b) => a.wrapping_shr(b as u32);
1772-
// }
1794+
uint: shift_right_logical,
1795+
fold_const {
1796+
uint(a, b) => a.wrapping_shr(b as u32);
1797+
}
17731798
}
17741799
simple_shift_op! {
17751800
ashr,
1776-
int: shift_right_arithmetic
1777-
// fold_const {
1778-
// int(a, b) => a.wrapping_shr(b as u32);
1779-
// }
1801+
sint: shift_right_arithmetic,
1802+
fold_const {
1803+
sint(a, b) => a.wrapping_shr(b as u32);
1804+
}
17801805
}
17811806
simple_uni_op! {
17821807
neg,

0 commit comments

Comments
 (0)