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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue May 5 09:40:42 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-tosa

@llvm/pr-subscribers-mlir

Author: Ian Tayler Lessa (IanTaylerLessa-arm)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/195882.diff


2 Files Affected:

- (modified) mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp (+7) 
- (modified) mlir/test/Dialect/Tosa/canonicalize.mlir (+12) 


``````````diff
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/195882


More information about the Mlir-commits mailing list