Skip to content

Commit 356e3c8

Browse files
committed
const folding: icmp for int and bool
1 parent c1fd945 commit 356e3c8

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
@@ -2467,6 +2467,74 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
24672467
use IntPredicate::*;
24682468
assert_ty_eq!(self, lhs.ty, rhs.ty);
24692469
let b = SpirvType::Bool.def(self.span(), self);
2470+
2471+
if let Some(const_lhs) = self.try_get_const_value(lhs) {
2472+
if let Some(const_rhs) = self.try_get_const_value(rhs) {
2473+
let const_result = match self.lookup_type(lhs.ty) {
2474+
SpirvType::Integer(_, _) => match (const_lhs, const_rhs, op) {
2475+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntEQ) => {
2476+
Some(lhs.eq(&rhs))
2477+
}
2478+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntEQ) => {
2479+
Some(lhs.eq(&rhs))
2480+
}
2481+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntNE) => {
2482+
Some(lhs.ne(&rhs))
2483+
}
2484+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntNE) => {
2485+
Some(lhs.ne(&rhs))
2486+
}
2487+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntUGT) => {
2488+
Some(lhs.gt(&rhs))
2489+
}
2490+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntUGE) => {
2491+
Some(lhs.ge(&rhs))
2492+
}
2493+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntULT) => {
2494+
Some(lhs.lt(&rhs))
2495+
}
2496+
(ConstValue::Unsigned(lhs), ConstValue::Unsigned(rhs), IntULE) => {
2497+
Some(lhs.le(&rhs))
2498+
}
2499+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntUGT) => {
2500+
Some(lhs.gt(&rhs))
2501+
}
2502+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntUGE) => {
2503+
Some(lhs.ge(&rhs))
2504+
}
2505+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntULT) => {
2506+
Some(lhs.lt(&rhs))
2507+
}
2508+
(ConstValue::Signed(lhs), ConstValue::Signed(rhs), IntULE) => {
2509+
Some(lhs.le(&rhs))
2510+
}
2511+
(_, _, _) => None,
2512+
},
2513+
SpirvType::Bool => match (const_lhs, const_rhs, op) {
2514+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntEQ) => Some(lhs.eq(&rhs)),
2515+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntNE) => Some(lhs.ne(&rhs)),
2516+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntUGT) => {
2517+
Some(lhs.gt(&rhs))
2518+
}
2519+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntUGE) => {
2520+
Some(lhs.ge(&rhs))
2521+
}
2522+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntULT) => {
2523+
Some(lhs.lt(&rhs))
2524+
}
2525+
(ConstValue::Bool(lhs), ConstValue::Bool(rhs), IntULE) => {
2526+
Some(lhs.le(&rhs))
2527+
}
2528+
(_, _, _) => None,
2529+
},
2530+
_ => None,
2531+
};
2532+
if let Some(result) = const_result {
2533+
return self.const_bool(result);
2534+
}
2535+
}
2536+
}
2537+
24702538
match self.lookup_type(lhs.ty) {
24712539
SpirvType::Integer(_, _) => match op {
24722540
IntEQ => self.emit().i_equal(b, None, lhs.def(self), rhs.def(self)),

0 commit comments

Comments
 (0)