Fold arith.cmpf when at least one operand is known to be NaN.

Reviewed By: herhut

Differential Revision: https://reviews.llvm.org/D117101
This commit is contained in:
Christian Sigg 2022-01-12 12:48:38 +01:00
parent b0a309dd7a
commit f6fab68c30
2 changed files with 20 additions and 0 deletions

View File

@ -1299,6 +1299,12 @@ OpFoldResult arith::CmpFOp::fold(ArrayRef<Attribute> operands) {
auto lhs = operands.front().dyn_cast_or_null<FloatAttr>();
auto rhs = operands.back().dyn_cast_or_null<FloatAttr>();
// If one operand is NaN, making them both NaN does not change the result.
if (lhs && lhs.getValue().isNaN())
rhs = lhs;
if (rhs && rhs.getValue().isNaN())
lhs = rhs;
if (!lhs || !rhs)
return {};

View File

@ -691,3 +691,17 @@ func @constant_MinMax(%arg0 : f32) -> f32 {
%res = arith.maxf %const, %min : f32
return %res : f32
}
// -----
// CHECK-LABEL: @cmpf_nan(
func @cmpf_nan(%arg0 : f32) -> (i1, i1, i1, i1) {
// CHECK-DAG: %[[T:.*]] = arith.constant true
// CHECK-DAG: %[[F:.*]] = arith.constant false
// CHECK: return %[[F]], %[[F]], %[[T]], %[[T]]
%nan = arith.constant 0x7fffffff : f32
%0 = arith.cmpf olt, %nan, %arg0 : f32
%1 = arith.cmpf olt, %arg0, %nan : f32
%2 = arith.cmpf ugt, %nan, %arg0 : f32
%3 = arith.cmpf ugt, %arg0, %nan : f32
return %0, %1, %2, %3 : i1, i1, i1, i1
}