[Mlir-commits] [mlir] [mlir] Fix StridedMemRefRankOf to check isStrided() (PR #201415)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Jun 30 00:24:40 PDT 2026


https://github.com/ConvolutedDog updated https://github.com/llvm/llvm-project/pull/201415

>From 6a5e22bd549834962b63beb33b3d553e52e36b17 Mon Sep 17 00:00:00 2001
From: ConvolutedDog <yangjianchao16 at nudt.edu.cn>
Date: Thu, 4 Jun 2026 01:13:55 +0800
Subject: [PATCH 1/4] [mlir] Fix StridedMemRefRankOf to check isStrided()

StridedMemRefRankOf was equivalent to MemRefRankOf: it only applied HasAnyRankOfPred and never HasStridesPred, so non-strided memref layouts (e.g. multi-result affine maps) incorrectly passed ODS verification on ops using this constraint (e.g. sparse_tensor.push_back).
---
 mlir/include/mlir/IR/CommonTypeConstraints.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/include/mlir/IR/CommonTypeConstraints.td b/mlir/include/mlir/IR/CommonTypeConstraints.td
index d8615a4730c32..54d0918ce8d32 100644
--- a/mlir/include/mlir/IR/CommonTypeConstraints.td
+++ b/mlir/include/mlir/IR/CommonTypeConstraints.td
@@ -930,7 +930,7 @@ class AnyStridedMemRefOfRank<int rank> :
        AnyStridedMemRef.summary # " of rank " # rank>;
 
 class StridedMemRefRankOf<list<Type> allowedTypes, list<int> ranks> :
-    ConfinedType<MemRefOf<allowedTypes>, [HasAnyRankOfPred<ranks>],
+    ConfinedType<MemRefOf<allowedTypes>, [HasAnyRankOfPred<ranks>, HasStridesPred],
          !interleave(!foreach(rank, ranks, rank # "D"), "/") # " " #
          MemRefOf<allowedTypes>.summary>;
 

>From 06ed01c429f4e47a7307edf4ab0d11ac23abe157 Mon Sep 17 00:00:00 2001
From: ConvolutedDog <yangjianchao16 at nudt.edu.cn>
Date: Thu, 4 Jun 2026 01:18:24 +0800
Subject: [PATCH 2/4] Add invalid ir of non-strided push_back

The inBuffer of push_back uses StridedMemRefRankOf, which requires a strided memref layout (HasStridesPred). A non-strided layout must be rejected.
---
 mlir/test/Dialect/SparseTensor/invalid.mlir | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mlir/test/Dialect/SparseTensor/invalid.mlir b/mlir/test/Dialect/SparseTensor/invalid.mlir
index d14229b011f11..338c7269f336e 100644
--- a/mlir/test/Dialect/SparseTensor/invalid.mlir
+++ b/mlir/test/Dialect/SparseTensor/invalid.mlir
@@ -307,6 +307,14 @@ func.func @sparse_push_back_n(%arg0: index, %arg1: memref<?xf32>, %arg2: f32) ->
 
 // -----
 
+func.func @sparse_push_back_non_strided(%arg0: index, %arg1: memref<?xf64, affine_map<(d0) -> (d0 mod 2)>>, %arg2: f64) {
+  // expected-error at +1 {{'sparse_tensor.push_back' op operand #1 must be 1D memref of any type values, but got 'memref<?xf64, affine_map<(d0) -> (d0 mod 2)>>'}}
+  %0:2 = sparse_tensor.push_back %arg0, %arg1, %arg2 : index, memref<?xf64, affine_map<(d0) -> (d0 mod 2)>>, f64
+  return
+}
+
+// -----
+
 func.func @sparse_unannotated_expansion(%arg0: tensor<128xf64>) {
   // expected-error at +1 {{'sparse_tensor.expand' op operand #0 must be sparse tensor of any non-token type values, but got 'tensor<128xf64>'}}
   %values, %filled, %added, %count = sparse_tensor.expand %arg0

>From 108d1e825b671e2c256e7c0d2d1abe8b4b048187 Mon Sep 17 00:00:00 2001
From: ConvolutedDog <yangjianchao16 at nudt.edu.cn>
Date: Wed, 10 Jun 2026 19:58:24 +0800
Subject: [PATCH 3/4] Fix formatting mesage in CommonTypeConstraints.td

---
 mlir/include/mlir/IR/CommonTypeConstraints.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/include/mlir/IR/CommonTypeConstraints.td b/mlir/include/mlir/IR/CommonTypeConstraints.td
index 54d0918ce8d32..20c3a4734fe81 100644
--- a/mlir/include/mlir/IR/CommonTypeConstraints.td
+++ b/mlir/include/mlir/IR/CommonTypeConstraints.td
@@ -931,7 +931,7 @@ class AnyStridedMemRefOfRank<int rank> :
 
 class StridedMemRefRankOf<list<Type> allowedTypes, list<int> ranks> :
     ConfinedType<MemRefOf<allowedTypes>, [HasAnyRankOfPred<ranks>, HasStridesPred],
-         !interleave(!foreach(rank, ranks, rank # "D"), "/") # " " #
+         !interleave(!foreach(rank, ranks, rank # "D"), "/") # " strided " #
          MemRefOf<allowedTypes>.summary>;
 
 // This represents a generic tuple without any constraints on element type.

>From 96e5c10b6f568e65f322d8f6423fd96f6fbdc02f Mon Sep 17 00:00:00 2001
From: ConvolutedDog <yangjianchao16 at nudt.edu.cn>
Date: Wed, 10 Jun 2026 20:00:10 +0800
Subject: [PATCH 4/4] Update error messages for sparse tensor operations

---
 mlir/test/Dialect/SparseTensor/invalid.mlir | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mlir/test/Dialect/SparseTensor/invalid.mlir b/mlir/test/Dialect/SparseTensor/invalid.mlir
index 338c7269f336e..3291141c3343d 100644
--- a/mlir/test/Dialect/SparseTensor/invalid.mlir
+++ b/mlir/test/Dialect/SparseTensor/invalid.mlir
@@ -308,7 +308,7 @@ func.func @sparse_push_back_n(%arg0: index, %arg1: memref<?xf32>, %arg2: f32) ->
 // -----
 
 func.func @sparse_push_back_non_strided(%arg0: index, %arg1: memref<?xf64, affine_map<(d0) -> (d0 mod 2)>>, %arg2: f64) {
-  // expected-error at +1 {{'sparse_tensor.push_back' op operand #1 must be 1D memref of any type values, but got 'memref<?xf64, affine_map<(d0) -> (d0 mod 2)>>'}}
+  // expected-error at +1 {{operand #1 must be 1D strided memref of any non-token type values, but got 'memref<?xf64, affine_map<(d0) -> (d0 mod 2)>>}}
   %0:2 = sparse_tensor.push_back %arg0, %arg1, %arg2 : index, memref<?xf64, affine_map<(d0) -> (d0 mod 2)>>, f64
   return
 }
@@ -851,7 +851,7 @@ func.func @sparse_tensor_foreach(%arg0: tensor<2x4xf64, #DCSR>, %arg1: f32) -> (
 #MAP = affine_map<(i,j) -> (i,j)>
 
 func.func @sparse_sort_coo_x_type( %arg0: index, %arg1: memref<?xf32>) {
-  // expected-error at +1 {{operand #1 must be 1D memref of integer or index values}}
+  // expected-error at +1 {{operand #1 must be 1D strided memref of integer or index values}}
   sparse_tensor.sort insertion_sort_stable %arg0, %arg1 {perm_map = #MAP} : memref<?xf32>
   return
 }



More information about the Mlir-commits mailing list