Skip to content

Commit 345186c

Browse files
Firestar99eddyb
authored andcommitted
const folding: icmp for int and bool
1 parent 38890ef commit 345186c

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
@@ -2455,6 +2455,62 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
24552455

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

0 commit comments

Comments
 (0)