Skip to content

Commit 258c189

Browse files
committed
const folding: icmp for int and bool
1 parent eb136e4 commit 258c189

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,6 +2611,74 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
26112611
use IntPredicate::*;
26122612
assert_ty_eq!(self, lhs.ty, rhs.ty);
26132613
let b = SpirvType::Bool.def(self.span(), self);
2614+
2615+
if let Some(const_lhs) = self.try_get_const_int_value(lhs) {
2616+
if let Some(const_rhs) = self.try_get_const_int_value(rhs) {
2617+
let const_result = match self.lookup_type(lhs.ty) {
2618+
SpirvType::Integer(_, _) => match (const_lhs, const_rhs, op) {
2619+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntEQ) => {
2620+
Some(lhs.eq(&rhs))
2621+
}
2622+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntEQ) => {
2623+
Some(lhs.eq(&rhs))
2624+
}
2625+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntNE) => {
2626+
Some(lhs.ne(&rhs))
2627+
}
2628+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntNE) => {
2629+
Some(lhs.ne(&rhs))
2630+
}
2631+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntUGT) => {
2632+
Some(lhs.gt(&rhs))
2633+
}
2634+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntUGE) => {
2635+
Some(lhs.ge(&rhs))
2636+
}
2637+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntULT) => {
2638+
Some(lhs.lt(&rhs))
2639+
}
2640+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntULE) => {
2641+
Some(lhs.le(&rhs))
2642+
}
2643+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntUGT) => {
2644+
Some(lhs.gt(&rhs))
2645+
}
2646+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntUGE) => {
2647+
Some(lhs.ge(&rhs))
2648+
}
2649+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntULT) => {
2650+
Some(lhs.lt(&rhs))
2651+
}
2652+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntULE) => {
2653+
Some(lhs.le(&rhs))
2654+
}
2655+
(_, _, _) => None,
2656+
},
2657+
SpirvType::Bool => match (const_lhs, const_rhs, op) {
2658+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntEQ) => Some(lhs.eq(&rhs)),
2659+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntNE) => Some(lhs.ne(&rhs)),
2660+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntUGT) => {
2661+
Some(lhs.gt(&rhs))
2662+
}
2663+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntUGE) => {
2664+
Some(lhs.ge(&rhs))
2665+
}
2666+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntULT) => {
2667+
Some(lhs.lt(&rhs))
2668+
}
2669+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntULE) => {
2670+
Some(lhs.le(&rhs))
2671+
}
2672+
(_, _, _) => None,
2673+
},
2674+
_ => None,
2675+
};
2676+
if let Some(result) = const_result {
2677+
return self.const_bool(result);
2678+
}
2679+
}
2680+
}
2681+
26142682
match self.lookup_type(lhs.ty) {
26152683
SpirvType::Integer(_, _) => match op {
26162684
IntEQ => self.emit().i_equal(b, None, lhs.def(self), rhs.def(self)),

0 commit comments

Comments
 (0)