Skip to content

Commit a6e2c48

Browse files
committed
const folding: icmp for int and bool
1 parent 3349203 commit a6e2c48

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
@@ -2617,6 +2617,74 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
26172617
use IntPredicate::*;
26182618
assert_ty_eq!(self, lhs.ty, rhs.ty);
26192619
let b = SpirvType::Bool.def(self.span(), self);
2620+
2621+
if let Some(const_lhs) = self.try_get_const_value(lhs) {
2622+
if let Some(const_rhs) = self.try_get_const_value(rhs) {
2623+
let const_result = match self.lookup_type(lhs.ty) {
2624+
SpirvType::Integer(_, _) => match (const_lhs, const_rhs, op) {
2625+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntEQ) => {
2626+
Some(lhs.eq(&rhs))
2627+
}
2628+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntEQ) => {
2629+
Some(lhs.eq(&rhs))
2630+
}
2631+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntNE) => {
2632+
Some(lhs.ne(&rhs))
2633+
}
2634+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntNE) => {
2635+
Some(lhs.ne(&rhs))
2636+
}
2637+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntUGT) => {
2638+
Some(lhs.gt(&rhs))
2639+
}
2640+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntUGE) => {
2641+
Some(lhs.ge(&rhs))
2642+
}
2643+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntULT) => {
2644+
Some(lhs.lt(&rhs))
2645+
}
2646+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntULE) => {
2647+
Some(lhs.le(&rhs))
2648+
}
2649+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntUGT) => {
2650+
Some(lhs.gt(&rhs))
2651+
}
2652+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntUGE) => {
2653+
Some(lhs.ge(&rhs))
2654+
}
2655+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntULT) => {
2656+
Some(lhs.lt(&rhs))
2657+
}
2658+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntULE) => {
2659+
Some(lhs.le(&rhs))
2660+
}
2661+
(_, _, _) => None,
2662+
},
2663+
SpirvType::Bool => match (const_lhs, const_rhs, op) {
2664+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntEQ) => Some(lhs.eq(&rhs)),
2665+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntNE) => Some(lhs.ne(&rhs)),
2666+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntUGT) => {
2667+
Some(lhs.gt(&rhs))
2668+
}
2669+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntUGE) => {
2670+
Some(lhs.ge(&rhs))
2671+
}
2672+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntULT) => {
2673+
Some(lhs.lt(&rhs))
2674+
}
2675+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntULE) => {
2676+
Some(lhs.le(&rhs))
2677+
}
2678+
(_, _, _) => None,
2679+
},
2680+
_ => None,
2681+
};
2682+
if let Some(result) = const_result {
2683+
return self.const_bool(result);
2684+
}
2685+
}
2686+
}
2687+
26202688
match self.lookup_type(lhs.ty) {
26212689
SpirvType::Integer(_, _) => match op {
26222690
IntEQ => self.emit().i_equal(b, None, lhs.def(self), rhs.def(self)),

0 commit comments

Comments
 (0)