Skip to content

Commit 67def6f

Browse files
committed
const folding: icmp for int and bool
1 parent 8d35dab commit 67def6f

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
@@ -2468,6 +2468,74 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
24682468

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

0 commit comments

Comments
 (0)