[Mlir-commits] [mlir] 99e6f46 - [mlir][TOSA] Fix folding of fp casts to bool (#232254)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Oct 16 06:08:20 PDT 2026


Author: Dhruv Chauhan
Date: 2026-10-16T14:08:12+01:00
New Revision: 99e6f46877192413c7fd1cbca7f4e215123d0f9a

URL: https://github.com/llvm/llvm-project/commit/99e6f46877192413c7fd1cbca7f4e215123d0f9a
DIFF: https://github.com/llvm/llvm-project/commit/99e6f46877192413c7fd1cbca7f4e215123d0f9a.diff

LOG: [mlir][TOSA] Fix folding of fp casts to bool (#232254)

Fold floating point splat casts to i1 using a nonzero comparison instead
of rounding to an integer.

Add regression test for signed zero and positive and negative fraction
values.

Added: 
    

Modified: 
    mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
    mlir/test/Dialect/Tosa/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index f97446c294247..a482e82e15301 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
@@ -2063,10 +2063,12 @@ OpFoldResult CastOp::fold(FoldAdaptor adaptor) {
     }
 
     if (llvm::isa<FloatType>(inETy) && llvm::isa<IntegerType>(outETy)) {
+      auto floatVal = operand.getSplatValue<APFloat>();
+      if (outETy.isInteger(1))
+        return SplatElementsAttr::get(outTy, !floatVal.isZero());
       auto unsign = llvm::cast<IntegerType>(outETy).isUnsignedInteger();
       auto intVal = APSInt(
           llvm::cast<IntegerType>(outETy).getIntOrFloatBitWidth(), unsign);
-      auto floatVal = operand.getSplatValue<APFloat>();
       bool exact;
       floatVal.convertToInteger(intVal, llvm::RoundingMode::NearestTiesToEven,
                                 &exact);

diff  --git a/mlir/test/Dialect/Tosa/canonicalize.mlir b/mlir/test/Dialect/Tosa/canonicalize.mlir
index 48e429846b669..531fdb18d7fd6 100644
--- a/mlir/test/Dialect/Tosa/canonicalize.mlir
+++ b/mlir/test/Dialect/Tosa/canonicalize.mlir
@@ -314,7 +314,7 @@ func.func @clamp_i8_nan_ignore_noop(%arg0: tensor<4xi8>) -> tensor<4xi8> {
   return %0 : tensor<4xi8>
 }
 
-// ----
+// -----
 
 // CHECK-LABEL: @clamp_boolean_is_noop
 func.func @clamp_boolean_is_noop(%arg0: tensor<4xi1>) -> tensor<4xi1> {
@@ -1524,6 +1524,33 @@ func.func @test_fold_i32_to_i1_cast() -> tensor<i1> {
 
 // -----
 
+// Fold both signed zeros to false and nonzero fractions, infinities and NaN to true.
+// CHECK-LABEL: @test_fold_f32_to_i1_cast
+// CHECK-DAG: %[[FALSE:.*]] = tosa.const values(dense<false> : tensor<i1>)
+// CHECK-DAG: %[[TRUE:.*]] = tosa.const values(dense<true> : tensor<i1>)
+// CHECK: return %[[FALSE]], %[[FALSE]], %[[TRUE]], %[[TRUE]], %[[TRUE]], %[[TRUE]], %[[TRUE]]
+func.func @test_fold_f32_to_i1_cast() ->
+    (tensor<i1>, tensor<i1>, tensor<i1>, tensor<i1>, tensor<i1>, tensor<i1>, tensor<i1>) {
+  %zero = "tosa.const"() <{values = dense<0.0> : tensor<f32>}> : () -> tensor<f32>
+  %neg_zero = "tosa.const"() <{values = dense<-0.0> : tensor<f32>}> : () -> tensor<f32>
+  %positive = "tosa.const"() <{values = dense<0.01> : tensor<f32>}> : () -> tensor<f32>
+  %negative = "tosa.const"() <{values = dense<-0.01> : tensor<f32>}> : () -> tensor<f32>
+  %0 = tosa.cast %zero : (tensor<f32>) -> tensor<i1>
+  %1 = tosa.cast %neg_zero : (tensor<f32>) -> tensor<i1>
+  %2 = tosa.cast %positive : (tensor<f32>) -> tensor<i1>
+  %3 = tosa.cast %negative : (tensor<f32>) -> tensor<i1>
+  %inf = "tosa.const"() <{values = dense<0x7F800000> : tensor<f32>}> : () -> tensor<f32>
+  %neg_inf = "tosa.const"() <{values = dense<0xFF800000> : tensor<f32>}> : () -> tensor<f32>
+  %nan = "tosa.const"() <{values = dense<0x7FC00000> : tensor<f32>}> : () -> tensor<f32>
+  %4 = tosa.cast %inf : (tensor<f32>) -> tensor<i1>
+  %5 = tosa.cast %neg_inf : (tensor<f32>) -> tensor<i1>
+  %6 = tosa.cast %nan : (tensor<f32>) -> tensor<i1>
+  return %0, %1, %2, %3, %4, %5, %6 :
+      tensor<i1>, tensor<i1>, tensor<i1>, tensor<i1>, tensor<i1>, tensor<i1>, tensor<i1>
+}
+
+// -----
+
 // CHECK-LABEL: @test_canonicalize_non_narrowing_cast_i8_to_i32
 // CHECK: %[[OUT:.*]] = tosa.cast %arg0 input_unsigned(false) : (tensor<13x21x3xi8>) -> tensor<13x21x3xi32>
 // CHECK: return %[[OUT]] : tensor<13x21x3xi32>


        


More information about the Mlir-commits mailing list