[Mlir-commits] [mlir] [mlir][ArithToSPIRV] Fix crash on tensor arith.uitofp/sitofp lowering (PR #227102)

Igor Wodiany llvmlistbot at llvm.org
Mon Oct 5 02:26:01 PDT 2026


https://github.com/IgWod updated https://github.com/llvm/llvm-project/pull/227102

>From 6108c8365b66a6647d0326179d664840af44f6ed Mon Sep 17 00:00:00 2001
From: Vinicius Silva <viniciusilva at ieee.org>
Date: Mon, 28 Sep 2026 20:11:41 +0000
Subject: [PATCH 1/8] [mlir][ArithToSPIRV] Fix crash on tensor
 arith.uitofp/sitofp lowering (#226380)

---
 mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
index 40c0ac7064fdb..8d44c343f73e2 100644
--- a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
+++ b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
@@ -731,6 +731,14 @@ struct IntToFPPattern final : public OpConversionPattern<ArithOp> {
     // Check if the source integer type was widened during type conversion.
     unsigned originalBitwidth =
         getElementTypeOrSelf(op.getIn().getType()).getIntOrFloatBitWidth();
+
+    if (!srcType ||
+        !(isa<IntegerType, FloatType>(getElementTypeOrSelf(srcType)) ||
+          isa<mlir::VectorType>(srcType))) {
+      return rewriter.notifyMatchFailure(op,
+                                         "unsupported type for uitofp/sitofp");
+    }
+
     unsigned convertedBitwidth =
         getElementTypeOrSelf(srcType).getIntOrFloatBitWidth();
 

>From d49e46380ed20353a10dce19a24a36d82667f6c3 Mon Sep 17 00:00:00 2001
From: Vinicius Silva <viniciusilva at ieee.org>
Date: Tue, 29 Sep 2026 08:59:42 -0300
Subject: [PATCH 2/8] Update mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp

Co-authored-by: Igor Wodiany <dev at wodiany.com>
---
 mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
index 8d44c343f73e2..90a6860da2e36 100644
--- a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
+++ b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
@@ -734,7 +734,7 @@ struct IntToFPPattern final : public OpConversionPattern<ArithOp> {
 
     if (!srcType ||
         !(isa<IntegerType, FloatType>(getElementTypeOrSelf(srcType)) ||
-          isa<mlir::VectorType>(srcType))) {
+          isa<VectorType>(srcType))) {
       return rewriter.notifyMatchFailure(op,
                                          "unsupported type for uitofp/sitofp");
     }

>From 0b9c8b9d960caab1a82ab1db363984282674338d Mon Sep 17 00:00:00 2001
From: Vinicius Silva <viniciusilva at ieee.org>
Date: Tue, 29 Sep 2026 10:16:18 -0300
Subject: [PATCH 3/8] Adding regression tests

---
 .../arith_itofp_tensor_regression.mlir        | 40 +++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 mlir/test/Conversion/ArithToSPIRV/arith_itofp_tensor_regression.mlir

diff --git a/mlir/test/Conversion/ArithToSPIRV/arith_itofp_tensor_regression.mlir b/mlir/test/Conversion/ArithToSPIRV/arith_itofp_tensor_regression.mlir
new file mode 100644
index 0000000000000..6d15360c01d23
--- /dev/null
+++ b/mlir/test/Conversion/ArithToSPIRV/arith_itofp_tensor_regression.mlir
@@ -0,0 +1,40 @@
+// RUN: mlir-opt -split-input-file -convert-arith-to-spirv -verify-diagnostics %s
+
+// -----
+
+// Regression test: arith.uitofp on tensor types should not crash
+// (https://github.com/llvm/llvm-project/issues/226380).
+func.func @uitofp_tensor_no_crash(%arg0: tensor<1xi32>) -> tensor<1xf32> {
+  // expected-error @+1 {{failed to legalize operation 'arith.uitofp'}}
+  %0 = arith.uitofp %arg0 : tensor<1xi32> to tensor<1xf32>
+  return %0 : tensor<1xf32>
+}
+
+// -----
+
+// Regression test: arith.sitofp on tensor types should not crash
+// (https://github.com/llvm/llvm-project/issues/226380).
+func.func @sitofp_tensor_no_crash(%arg0: tensor<1xi32>) -> tensor<1xf32> {
+  // expected-error @+1 {{failed to legalize operation 'arith.sitofp'}}
+  %0 = arith.sitofp %arg0 : tensor<1xi32> to tensor<1xf32>
+  return %0 : tensor<1xf32>
+}
+
+// -----
+
+// Dynamically shaped tensors must not crash uitofp lowering.
+func.func @uitofp_dynamic_tensor_no_crash(%arg0: tensor<?xi16>) -> tensor<?xf32> {
+  // expected-error @+1 {{failed to legalize operation 'arith.uitofp'}}
+  %0 = arith.uitofp %arg0 : tensor<?xi16> to tensor<?xf32>
+  return %0 : tensor<?xf32>
+}
+
+// -----
+
+// Multi-dimensional tensors must not crash sitofp lowering.
+func.func @sitofp_multidim_tensor_no_crash(%arg0: tensor<2x2xi16>) -> tensor<2x2xf32> {
+  // expected-error @+1 {{failed to legalize operation 'arith.sitofp'}}
+  %0 = arith.sitofp %arg0 : tensor<2x2xi16> to tensor<2x2xf32>
+  return %0 : tensor<2x2xf32>
+}
+

>From 9539c1fe7e194e8aa9ebe666ac08faf5451e92b2 Mon Sep 17 00:00:00 2001
From: Vinicius Silva <viniciusilva at ieee.org>
Date: Thu, 1 Oct 2026 22:11:19 -0300
Subject: [PATCH 4/8] [mlir][ArithToSPIRV] Check element type in IntToFPPattern
 and rename test

---
 mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp            | 5 ++---
 ...or_regression.mlir => arith-itofp-tensor-regression.mlir} | 0
 2 files changed, 2 insertions(+), 3 deletions(-)
 rename mlir/test/Conversion/ArithToSPIRV/{arith_itofp_tensor_regression.mlir => arith-itofp-tensor-regression.mlir} (100%)

diff --git a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
index 90a6860da2e36..4f9d0075c027d 100644
--- a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
+++ b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
@@ -732,9 +732,8 @@ struct IntToFPPattern final : public OpConversionPattern<ArithOp> {
     unsigned originalBitwidth =
         getElementTypeOrSelf(op.getIn().getType()).getIntOrFloatBitWidth();
 
-    if (!srcType ||
-        !(isa<IntegerType, FloatType>(getElementTypeOrSelf(srcType)) ||
-          isa<VectorType>(srcType))) {
+    Type srcElemType = getElementTypeOrSelf(srcType);
+    if (!srcElemType.isIntOrFloat()) {
       return rewriter.notifyMatchFailure(op,
                                          "unsupported type for uitofp/sitofp");
     }
diff --git a/mlir/test/Conversion/ArithToSPIRV/arith_itofp_tensor_regression.mlir b/mlir/test/Conversion/ArithToSPIRV/arith-itofp-tensor-regression.mlir
similarity index 100%
rename from mlir/test/Conversion/ArithToSPIRV/arith_itofp_tensor_regression.mlir
rename to mlir/test/Conversion/ArithToSPIRV/arith-itofp-tensor-regression.mlir

>From 555b2f71c9ecda378629096d530ee7af4fbcbd3e Mon Sep 17 00:00:00 2001
From: Vinicius Silva <viniciusilva at ieee.org>
Date: Fri, 2 Oct 2026 10:12:53 -0300
Subject: [PATCH 5/8] Update
 mlir/test/Conversion/ArithToSPIRV/arith-itofp-tensor-regression.mlir

Co-authored-by: Igor Wodiany <dev at wodiany.com>
---
 .../Conversion/ArithToSPIRV/arith-itofp-tensor-regression.mlir  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/test/Conversion/ArithToSPIRV/arith-itofp-tensor-regression.mlir b/mlir/test/Conversion/ArithToSPIRV/arith-itofp-tensor-regression.mlir
index 6d15360c01d23..76f564b62b563 100644
--- a/mlir/test/Conversion/ArithToSPIRV/arith-itofp-tensor-regression.mlir
+++ b/mlir/test/Conversion/ArithToSPIRV/arith-itofp-tensor-regression.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt -split-input-file -convert-arith-to-spirv -verify-diagnostics %s
+// RUN: mlir-opt --split-input-file --convert-arith-to-spirv --verify-diagnostics %s
 
 // -----
 

>From 9fd97cffd07e9e50d549a83a1c72ab90841c6c72 Mon Sep 17 00:00:00 2001
From: Vinicius Silva <viniciusilva at ieee.org>
Date: Fri, 2 Oct 2026 10:14:24 -0300
Subject: [PATCH 6/8] Update mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp

Co-authored-by: Igor Wodiany <dev at wodiany.com>
---
 mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
index 4f9d0075c027d..ab159eb6fa662 100644
--- a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
+++ b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
@@ -739,7 +739,7 @@ struct IntToFPPattern final : public OpConversionPattern<ArithOp> {
     }
 
     unsigned convertedBitwidth =
-        getElementTypeOrSelf(srcType).getIntOrFloatBitWidth();
+        srcElemType .getIntOrFloatBitWidth();
 
     if (originalBitwidth >= convertedBitwidth) {
       rewriter.replaceOpWithNewOp<SPIRVOp>(op, dstType, adaptor.getOperands());

>From d63e0c87202e289db2af93f4e1e761e0ad53bbf5 Mon Sep 17 00:00:00 2001
From: Vinicius Silva <viniciusilva at ieee.org>
Date: Fri, 2 Oct 2026 10:30:05 -0300
Subject: [PATCH 7/8] Fix formatting issue in ArithToSPIRV.cpp

---
 mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
index ab159eb6fa662..68d0eb4d633d5 100644
--- a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
+++ b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
@@ -739,7 +739,7 @@ struct IntToFPPattern final : public OpConversionPattern<ArithOp> {
     }
 
     unsigned convertedBitwidth =
-        srcElemType .getIntOrFloatBitWidth();
+        srcElemType.getIntOrFloatBitWidth();
 
     if (originalBitwidth >= convertedBitwidth) {
       rewriter.replaceOpWithNewOp<SPIRVOp>(op, dstType, adaptor.getOperands());

>From cc24e3823563de555d917bdfab5de17ca7a1dfdb Mon Sep 17 00:00:00 2001
From: Vinicius Silva <viniciusilva at ieee.org>
Date: Fri, 2 Oct 2026 11:40:20 -0300
Subject: [PATCH 8/8] Formatting ArithToSPIRV.cpp file

---
 mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
index 68d0eb4d633d5..d73208323c414 100644
--- a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
+++ b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
@@ -738,8 +738,7 @@ struct IntToFPPattern final : public OpConversionPattern<ArithOp> {
                                          "unsupported type for uitofp/sitofp");
     }
 
-    unsigned convertedBitwidth =
-        srcElemType.getIntOrFloatBitWidth();
+    unsigned convertedBitwidth = srcElemType.getIntOrFloatBitWidth();
 
     if (originalBitwidth >= convertedBitwidth) {
       rewriter.replaceOpWithNewOp<SPIRVOp>(op, dstType, adaptor.getOperands());



More information about the Mlir-commits mailing list