[Mlir-commits] [mlir] [mlir][linalg] Avoid asserts in IndexingMapOpInterface (PR #179072)

Samarth Narang llvmlistbot at llvm.org
Sat Jan 31 14:42:05 PST 2026


https://github.com/snarang181 updated https://github.com/llvm/llvm-project/pull/179072

>From 84eb812906871f074f4b859a12d2974ca4e68ea1 Mon Sep 17 00:00:00 2001
From: Samarth Narang <snarang at utexas.edu>
Date: Sat, 31 Jan 2026 17:17:28 -0500
Subject: [PATCH 1/2] [mlir][linalg] Avoid asserts in IndexingMapOpInterface

---
 .../mlir/Dialect/Linalg/IR/LinalgInterfaces.td       | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td b/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td
index 9f1e88a040f5f..365d5344953b6 100644
--- a/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td
+++ b/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td
@@ -385,9 +385,9 @@ def LinalgStructuredInterface
           return 0;
         // Tensor and Memref container types have a rank.
         if (auto shapedType = ::llvm::dyn_cast<ShapedType>(t)) {
-          // Failsafe.
-          assert((isa<MemRefType>(t) || isa<RankedTensorType>(t)) &&
-                 "expected a ranked tensor or memref in LinalgInterface::getRank");
+          // Only ranked tensors and MemRefs have well defined ranks.
+          if (!(isa<MemRefType>(t) || isa<RankedTensorType>(t)))
+            return 0;
           return shapedType.getRank();
         }
         return 0;
@@ -703,9 +703,9 @@ def LinalgStructuredInterface
         if (isa<VectorType>(t))
           return {};
         if (auto shapedType = ::llvm::dyn_cast<ShapedType>(t)) {
-          // Failsafe.
-          assert((isa<MemRefType>(t) || isa<RankedTensorType>(t)) &&
-                 "expected a ranked tensor or memref in LinalgInterface::getRank");
+          // Only ranked tensors and MemRefs have well defined shapes.
+          if (!(isa<MemRefType>(t) || isa<RankedTensorType>(t)))
+            return {};
           return shapedType.getShape();
         }
         return {};

>From 2ec78d322320cd95b2a13302c85b86baa67ce756 Mon Sep 17 00:00:00 2001
From: Samarth Narang <snarang at utexas.edu>
Date: Sat, 31 Jan 2026 17:41:48 -0500
Subject: [PATCH 2/2] Add test case

---
 mlir/test/Dialect/Linalg/invalid.mlir | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/mlir/test/Dialect/Linalg/invalid.mlir b/mlir/test/Dialect/Linalg/invalid.mlir
index 355d801f8732c..d97b5ee31f97b 100644
--- a/mlir/test/Dialect/Linalg/invalid.mlir
+++ b/mlir/test/Dialect/Linalg/invalid.mlir
@@ -217,6 +217,25 @@ func.func @generic_indexing_map_with_symbol(%arg0: tensor<8xf32>) -> tensor<8xf3
   return %0 : tensor<8xf32>
 }
 
+// -----
+
+// Unranked tensor inputs must be diagnosed.
+func.func @generic_unranked_input_tensor(%in: tensor<*xf32>) {
+  %out = tensor.empty() : tensor<16x16xf32>
+  // expected-error @+1 {{expected operand #0 rank (0) to match the result rank of indexing_map (2)}}
+  %r = linalg.generic {
+    indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>,
+                     affine_map<(d0, d1) -> (d0, d1)>],
+    iterator_types = ["parallel", "parallel"]}
+      ins(%in : tensor<*xf32>)
+     outs(%out : tensor<16x16xf32>) {
+      ^bb0(%a: f32, %b: f32):
+        linalg.yield %a : f32
+     } -> tensor<16x16xf32>
+  return
+}
+
+
 ////////////////////////////////////////////////////////////////////////////////
 ///////////////////////////// Region tests /////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////



More information about the Mlir-commits mailing list