[Mlir-commits] [mlir] [mlir][tosa] Avoid introducing int <-> float casts (PR #195882)

Ian Tayler Lessa llvmlistbot at llvm.org
Tue May 5 09:39:45 PDT 2026


https://github.com/IanTaylerLessa-arm created https://github.com/llvm/llvm-project/pull/195882

As part of the NonNarrowingCastsOptimization we were optimizing away some cases where the inner input was an integer and the outer output was a float.

Not all of the resulting dtype combinations for these cases are supported by TOSA, so these scenarios are no longer optimized as part of canonicalizations.

>From 79b7b1c91305fcae4db80367f3d1f9e04fb8b2db Mon Sep 17 00:00:00 2001
From: Ian Tayler Lessa <ian.taylerlessa at arm.com>
Date: Tue, 5 May 2026 16:58:45 +0100
Subject: [PATCH] [mlir][tosa] Avoid introducing int <-> float casts

As part of the NonNarrowingCastsOptimization we were optimizing away
some cases where the inner input was an integer and the outer output was
a float.

Not all of the resulting dtype combinations for these cases are
supported by TOSA, so these scenarios are no longer optimized as part of
canonicalizations.

Signed-off-by: Ian Tayler Lessa <ian.taylerlessa at arm.com>
Change-Id: I4adfe86d4a9f19427fc6425e687de299ebfe9b1f
---
 mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp |  7 +++++++
 mlir/test/Dialect/Tosa/canonicalize.mlir           | 12 ++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index 1c186cd3ae122..33f5633fc534d 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
@@ -941,6 +941,13 @@ struct NonNarrowingCastsOptimization : public OpRewritePattern<tosa::CastOp> {
                   "legal in TOSA");
     }
 
+    if (innerInputType.getElementType().isInteger() !=
+        outerOutputType.getElementType().isInteger()) {
+      return rewriter.notifyMatchFailure(
+          castOp, "integer to float and float to integer casts are not "
+                  "supported to avoid introducing illegal type combinations");
+    }
+
     // Check that the cast we're considering for removal is non-narrowing
     if (isNarrowingCast(innerInputType, innerOutputType))
       return rewriter.notifyMatchFailure(castOp,
diff --git a/mlir/test/Dialect/Tosa/canonicalize.mlir b/mlir/test/Dialect/Tosa/canonicalize.mlir
index 9a7fa3efc8d3c..5ea1b0f73444d 100644
--- a/mlir/test/Dialect/Tosa/canonicalize.mlir
+++ b/mlir/test/Dialect/Tosa/canonicalize.mlir
@@ -1594,6 +1594,18 @@ func.func @test_canonicalize_non_narrowing_cast_f6E3M2FN_to_f8E4M3FN_to_f16_unsu
   return %1 : tensor<13x21x3xf16>
 }
 
+// -----
+
+// CHECK-LABEL: @test_canonicalize_non_narrowing_cast_i1_to_f32_unsupported
+// CHECK: tosa.cast
+// CHECK: tosa.cast
+func.func @test_canonicalize_non_narrowing_cast_i1_to_f32_unsupported(%arg0: tensor<13x21x3xi1>) -> tensor<13x21x3xf32> {
+  %0 = tosa.cast %arg0 : (tensor<13x21x3xi1>) -> tensor<13x21x3xi8>
+  %1 = tosa.cast %0 : (tensor<13x21x3xi8>) -> tensor<13x21x3xf32>
+  return %1 : tensor<13x21x3xf32>
+}
+
+
 // -----
 
 // CHECK-LABEL: @test_canonicalize_cast_from_cast_to_block_scaled_f4E2M1



More information about the Mlir-commits mailing list