Skip to content

Commit d780bfe

Browse files
committed
const folding: shift operations
1 parent a700548 commit d780bfe

File tree

1 file changed

+78
-12
lines changed

1 file changed

+78
-12
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,72 @@ macro_rules! simple_op {
139139
};
140140
}
141141

142+
// shl and shr allow different types as their operands
143+
macro_rules! simple_shift_op {
144+
(
145+
$func_name:ident
146+
$(, int: $inst_int:ident)?
147+
$(, uint: $inst_uint:ident)?
148+
$(, sint: $inst_sint:ident)?
149+
$(, fold_const {
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;)?
153+
})?
154+
) => {
155+
fn $func_name(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value {
156+
let result_type = lhs.ty;
157+
158+
$(
159+
#[allow(unreachable_patterns, clippy::collapsible_match)]
160+
if let Some(const_lhs) = self.try_get_const_value(lhs)
161+
&& let Some(const_rhs) = self.try_get_const_value(rhs)
162+
{
163+
#[allow(unreachable_patterns)]
164+
match (const_lhs, const_rhs) {
165+
$(
166+
(ConstValue::Unsigned($int_lhs), ConstValue::Unsigned($int_rhs)) => return self.const_uint_big(result_type, $fold_int),
167+
(ConstValue::Unsigned($int_lhs), ConstValue::Signed($int_rhs)) => return self.const_uint_big(result_type, $fold_int),
168+
(ConstValue::Signed($int_lhs), ConstValue::Unsigned($int_rhs)) => return self.const_uint_big(result_type, $fold_int as u128),
169+
(ConstValue::Signed($int_lhs), ConstValue::Signed($int_rhs)) => return self.const_uint_big(result_type, $fold_int as u128),
170+
)?
171+
$(
172+
(ConstValue::Unsigned($uint_lhs), ConstValue::Unsigned($uint_rhs)) => return self.const_uint_big(result_type, $fold_uint),
173+
(ConstValue::Unsigned($uint_lhs), ConstValue::Signed($uint_rhs)) => return self.const_uint_big(result_type, $fold_uint),
174+
)?
175+
$(
176+
(ConstValue::Signed($sint_lhs), ConstValue::Unsigned($sint_rhs)) => return self.const_uint_big(result_type, $fold_sint as u128),
177+
(ConstValue::Signed($sint_lhs), ConstValue::Signed($sint_rhs)) => return self.const_uint_big(result_type, $fold_sint as u128),
178+
)?
179+
_ => (),
180+
}
181+
}
182+
)?
183+
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)
204+
}
205+
};
206+
}
207+
142208
macro_rules! simple_uni_op {
143209
(
144210
$func_name:ident
@@ -1540,24 +1606,24 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
15401606
simple_op! {frem_algebraic, float: f_rem} // algebraic=normal
15411607
simple_shift_op! {
15421608
shl,
1543-
int: shift_left_logical
1544-
// fold_const {
1545-
// int(a, b) => a.wrapping_shl(b as u32);
1546-
// }
1609+
int: shift_left_logical,
1610+
fold_const {
1611+
int(a, b) => a.wrapping_shl(b as u32);
1612+
}
15471613
}
15481614
simple_shift_op! {
15491615
lshr,
1550-
int: shift_right_logical
1551-
// fold_const {
1552-
// int(a, b) => a.wrapping_shr(b as u32);
1553-
// }
1616+
uint: shift_right_logical,
1617+
fold_const {
1618+
uint(a, b) => a.wrapping_shr(b as u32);
1619+
}
15541620
}
15551621
simple_shift_op! {
15561622
ashr,
1557-
int: shift_right_arithmetic
1558-
// fold_const {
1559-
// int(a, b) => a.wrapping_shr(b as u32);
1560-
// }
1623+
sint: shift_right_arithmetic,
1624+
fold_const {
1625+
sint(a, b) => a.wrapping_shr(b as u32);
1626+
}
15611627
}
15621628
simple_uni_op! {
15631629
neg,

0 commit comments

Comments
 (0)