Skip to content

Commit 62295d3

Browse files
committed
const folding: icmp for int and bool
1 parent 4dc7cd8 commit 62295d3

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,6 +2462,62 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
24622462

24632463
assert_ty_eq!(self, lhs.ty, rhs.ty);
24642464
let b = SpirvType::Bool.def(self.span(), self);
2465+
2466+
if let Some(const_lhs) = self.try_get_const_value(lhs)
2467+
&& let Some(const_rhs) = self.try_get_const_value(rhs)
2468+
{
2469+
let const_result = match self.lookup_type(lhs.ty) {
2470+
SpirvType::Integer(_, _) => match (const_lhs, const_rhs, op) {
2471+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntEQ) => {
2472+
Some(lhs.eq(&rhs))
2473+
}
2474+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntEQ) => Some(lhs.eq(&rhs)),
2475+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntNE) => {
2476+
Some(lhs.ne(&rhs))
2477+
}
2478+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntNE) => Some(lhs.ne(&rhs)),
2479+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntUGT) => {
2480+
Some(lhs.gt(&rhs))
2481+
}
2482+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntUGE) => {
2483+
Some(lhs.ge(&rhs))
2484+
}
2485+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntULT) => {
2486+
Some(lhs.lt(&rhs))
2487+
}
2488+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntULE) => {
2489+
Some(lhs.le(&rhs))
2490+
}
2491+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntUGT) => {
2492+
Some(lhs.gt(&rhs))
2493+
}
2494+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntUGE) => {
2495+
Some(lhs.ge(&rhs))
2496+
}
2497+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntULT) => {
2498+
Some(lhs.lt(&rhs))
2499+
}
2500+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntULE) => {
2501+
Some(lhs.le(&rhs))
2502+
}
2503+
(_, _, _) => None,
2504+
},
2505+
SpirvType::Bool => match (const_lhs, const_rhs, op) {
2506+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntEQ) => Some(lhs.eq(&rhs)),
2507+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntNE) => Some(lhs.ne(&rhs)),
2508+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntUGT) => Some(lhs.gt(&rhs)),
2509+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntUGE) => Some(lhs.ge(&rhs)),
2510+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntULT) => Some(lhs.lt(&rhs)),
2511+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntULE) => Some(lhs.le(&rhs)),
2512+
(_, _, _) => None,
2513+
},
2514+
_ => None,
2515+
};
2516+
if let Some(result) = const_result {
2517+
return self.const_bool(result);
2518+
}
2519+
}
2520+
24652521
match self.lookup_type(lhs.ty) {
24662522
SpirvType::Integer(_, _) => match op {
24672523
IntEQ => self.emit().i_equal(b, None, lhs.def(self), rhs.def(self)),

0 commit comments

Comments
 (0)