[Mlir-commits] [mlir] [mlir][linalg] Reject unsigned pooling on non-integer element types (PR #166070)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sat Dec 13 18:39:14 PST 2025
https://github.com/Men-cotton updated https://github.com/llvm/llvm-project/pull/166070
>From 03ef5fc57064198c3aa4424a722077ab94fbbda5 Mon Sep 17 00:00:00 2001
From: mencotton <mencotton0410 at gmail.com>
Date: Sun, 2 Nov 2025 22:59:39 +0900
Subject: [PATCH 1/5] [mlir][linalg] Reject unsigned pooling on non-integer
element types
---
mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp | 18 +++++++---
mlir/test/Dialect/Linalg/named-ops-fail.mlir | 15 +++++++-
mlir/test/Dialect/Linalg/named-ops.mlir | 34 +++++++++++++++++++
.../Linalg/transform-op-decompose.mlir | 28 +++++++--------
4 files changed, 76 insertions(+), 19 deletions(-)
diff --git a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
index 3dc45edf4a23f..8eb03dc182ae9 100644
--- a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
+++ b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
@@ -579,13 +579,23 @@ class RegionBuilderHelper {
return arith::MinSIOp::create(builder, arg0.getLoc(), arg0, arg1);
case BinaryFn::max_unsigned:
assert(!allComplex);
- if (allFloatingPoint)
- return arith::MaximumFOp::create(builder, arg0.getLoc(), arg0, arg1);
+ if (!allInteger || allBool) {
+ if (emitError) {
+ emitError() << "unsupported operation: unsigned max not on uint";
+ return nullptr;
+ }
+ llvm_unreachable("unsupported operation: unsigned max not on uint");
+ }
return arith::MaxUIOp::create(builder, arg0.getLoc(), arg0, arg1);
case BinaryFn::min_unsigned:
assert(!allComplex);
- if (allFloatingPoint)
- return arith::MinimumFOp::create(builder, arg0.getLoc(), arg0, arg1);
+ if (!allInteger || allBool) {
+ if (emitError) {
+ emitError() << "unsupported operation: unsigned min not on uint";
+ return nullptr;
+ }
+ llvm_unreachable("unsupported operation: unsigned min not on uint");
+ }
return arith::MinUIOp::create(builder, arg0.getLoc(), arg0, arg1);
case BinaryFn::powf:
assert(allFloatingPoint);
diff --git a/mlir/test/Dialect/Linalg/named-ops-fail.mlir b/mlir/test/Dialect/Linalg/named-ops-fail.mlir
index 552a0abaa797c..4ecf685b4c695 100644
--- a/mlir/test/Dialect/Linalg/named-ops-fail.mlir
+++ b/mlir/test/Dialect/Linalg/named-ops-fail.mlir
@@ -80,6 +80,20 @@ func.func @divu_broadcast(%arg0: memref<8x16xi32>, %arg1: memref<4x8x16xi32>, %a
// -----
+func.func @pooling_nhwc_max_unsigned_float(
+ %input: tensor<?x?x?x?xf32>,
+ %filter: tensor<?x?xf32>,
+ %init_val: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32> {
+ // CHECK: unsupported operation: unsigned max not on uint
+ linalg.pooling_nhwc_max_unsigned {dilations = dense<1> : tensor<2xi64>,
+ strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<?x?x?x?xf32>, tensor<?x?xf32>)
+ outs (%init_val: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32>
+ return %init_val : tensor<?x?x?x?xf32>
+}
+
+// -----
+
func.func @exp_type_cast(%arg: memref<4x8x16xf16>, %out: memref<4x8x16xf32>) {
// CHECK: operand 1 ('f16') doesn't match the element type of the enclosing linalg.generic op ('f32')
linalg.exp ins(%arg : memref<4x8x16xf16>) outs(%out: memref<4x8x16xf32>)
@@ -349,4 +363,3 @@ func.func @select_wrong_condition_type(%arg0: memref<4x8x16xf32>, %arg1: memref<
linalg.select ins(%arg0, %arg1, %arg2 : memref<4x8x16xf32>, memref<4x8x16xf32>, memref<4x8x16xf32>) outs(%arg3: memref<4x8x16xf32>)
return
}
-
diff --git a/mlir/test/Dialect/Linalg/named-ops.mlir b/mlir/test/Dialect/Linalg/named-ops.mlir
index a93e9799ceb3f..c2a8f24624d8e 100644
--- a/mlir/test/Dialect/Linalg/named-ops.mlir
+++ b/mlir/test/Dialect/Linalg/named-ops.mlir
@@ -705,6 +705,23 @@ func.func @pooling_nhwc_max_tensor(%input: tensor<1x4x4x1xf32>) -> tensor<1x2x2x
return %res : tensor<1x2x2x1xf32>
}
+// -----
+
+// CHECK-LABEL: func @pooling_nhwc_max_unsigned_tensor
+// CHECK: %{{.+}} = linalg.pooling_nhwc_max_unsigned
+// CHECK-SAME: ins(%{{.+}}, %{{.+}} : tensor<1x4x4x1xi32>, tensor<3x3xi32>)
+// CHECK-SAME: outs(%{{.+}} : tensor<1x2x2x1xi32>) -> tensor<1x2x2x1xi32>
+func.func @pooling_nhwc_max_unsigned_tensor(%input: tensor<1x4x4x1xi32>) -> tensor<1x2x2x1xi32> {
+ %fake = tensor.empty() : tensor<3x3xi32>
+ %init = tensor.empty() : tensor<1x2x2x1xi32>
+ %cst = arith.constant 0 : i32
+ %fill = linalg.fill ins(%cst : i32) outs(%init : tensor<1x2x2x1xi32>) -> tensor<1x2x2x1xi32>
+ %res = linalg.pooling_nhwc_max_unsigned {dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}
+ ins(%input, %fake: tensor<1x4x4x1xi32>, tensor<3x3xi32>)
+ outs(%fill: tensor<1x2x2x1xi32>) -> tensor<1x2x2x1xi32>
+ return %res : tensor<1x2x2x1xi32>
+}
+
// -----
// CHECK-LABEL: func @pooling_nwc_max_tensor
// CHECK: %{{.+}} = linalg.pooling_nwc_max
@@ -1017,6 +1034,23 @@ func.func @pooling_nhwc_min_tensor(%input: tensor<1x4x4x1xf32>) -> tensor<1x2x2x
// -----
+// CHECK-LABEL: func @pooling_nhwc_min_unsigned_tensor
+// CHECK: %{{.+}} = linalg.pooling_nhwc_min_unsigned
+// CHECK-SAME: ins(%{{.+}}, %{{.+}} : tensor<1x4x4x1xi32>, tensor<3x3xi32>)
+// CHECK-SAME: outs(%{{.+}} : tensor<1x2x2x1xi32>) -> tensor<1x2x2x1xi32>
+func.func @pooling_nhwc_min_unsigned_tensor(%input: tensor<1x4x4x1xi32>) -> tensor<1x2x2x1xi32> {
+ %fake = tensor.empty() : tensor<3x3xi32>
+ %init = tensor.empty() : tensor<1x2x2x1xi32>
+ %cst = arith.constant 0 : i32
+ %fill = linalg.fill ins(%cst : i32) outs(%init : tensor<1x2x2x1xi32>) -> tensor<1x2x2x1xi32>
+ %res = linalg.pooling_nhwc_min_unsigned {dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}
+ ins(%input, %fake: tensor<1x4x4x1xi32>, tensor<3x3xi32>)
+ outs(%fill: tensor<1x2x2x1xi32>) -> tensor<1x2x2x1xi32>
+ return %res : tensor<1x2x2x1xi32>
+}
+
+// -----
+
// CHECK-LABEL: func @pooling_nwc_min_tensor
// CHECK: %{{.+}} = linalg.pooling_nwc_min
// CHECK-SAME: dilations = dense<1> : tensor<1xi64>
diff --git a/mlir/test/Dialect/Linalg/transform-op-decompose.mlir b/mlir/test/Dialect/Linalg/transform-op-decompose.mlir
index 72acf43361f50..60a4c555fa19a 100644
--- a/mlir/test/Dialect/Linalg/transform-op-decompose.mlir
+++ b/mlir/test/Dialect/Linalg/transform-op-decompose.mlir
@@ -131,10 +131,10 @@ func.func @pooling_nhwc_max(%input: tensor<?x1x?x?xf32>, %filter: tensor<1x?xf32
}
// CHECK-LABEL: @pooling_nhwc_max_unsigned
-// CHECK-SAME: %[[ARG0:.+]]: tensor<?x1x?x?xf32>,
-// CHECK-SAME: %[[ARG1:.+]]: tensor<1x?xf32>
-// CHECK-SAME: %[[ARG2:.+]]: tensor<?x1x?x?xf32>
-func.func @pooling_nhwc_max_unsigned(%input: tensor<?x1x?x?xf32>, %filter: tensor<1x?xf32>, %init: tensor<?x1x?x?xf32>) -> tensor<?x1x?x?xf32> {
+// CHECK-SAME: %[[ARG0:.+]]: tensor<?x1x?x?xi32>,
+// CHECK-SAME: %[[ARG1:.+]]: tensor<1x?xi32>
+// CHECK-SAME: %[[ARG2:.+]]: tensor<?x1x?x?xi32>
+func.func @pooling_nhwc_max_unsigned(%input: tensor<?x1x?x?xi32>, %filter: tensor<1x?xi32>, %init: tensor<?x1x?x?xi32>) -> tensor<?x1x?x?xi32> {
// CHECK: %[[SLICE0:.+]] = tensor.extract_slice %[[ARG0]]
// CHECK: %[[SLICE1:.+]] = tensor.extract_slice %[[ARG1]]
// CHECK: %[[SLICE2:.+]] = tensor.extract_slice %[[ARG2]]
@@ -142,10 +142,10 @@ func.func @pooling_nhwc_max_unsigned(%input: tensor<?x1x?x?xf32>, %filter: tenso
// CHECK: %[[RES:.+]] = tensor.insert_slice %[[SLICERES]] into %[[ARG2]]
%0 = linalg.pooling_nhwc_max_unsigned {dilations = dense<1> : tensor<2xi64>,
strides = dense<1> : tensor<2xi64>}
- ins (%input, %filter: tensor<?x1x?x?xf32>, tensor<1x?xf32>)
- outs (%init: tensor<?x1x?x?xf32>) -> tensor<?x1x?x?xf32>
+ ins (%input, %filter: tensor<?x1x?x?xi32>, tensor<1x?xi32>)
+ outs (%init: tensor<?x1x?x?xi32>) -> tensor<?x1x?x?xi32>
// CHECK: return %[[RES]]
- return %0 : tensor<?x1x?x?xf32>
+ return %0 : tensor<?x1x?x?xi32>
}
// CHECK-LABEL: @pooling_nhwc_min
@@ -167,10 +167,10 @@ func.func @pooling_nhwc_min(%input: tensor<?x1x?x?xf32>, %filter: tensor<1x?xf32
}
// CHECK-LABEL: @pooling_nhwc_min_unsigned
-// CHECK-SAME: %[[ARG0:.+]]: tensor<?x1x?x?xf32>,
-// CHECK-SAME: %[[ARG1:.+]]: tensor<1x?xf32>
-// CHECK-SAME: %[[ARG2:.+]]: tensor<?x1x?x?xf32>
-func.func @pooling_nhwc_min_unsigned(%input: tensor<?x1x?x?xf32>, %filter: tensor<1x?xf32>, %init: tensor<?x1x?x?xf32>) -> tensor<?x1x?x?xf32> {
+// CHECK-SAME: %[[ARG0:.+]]: tensor<?x1x?x?xi32>,
+// CHECK-SAME: %[[ARG1:.+]]: tensor<1x?xi32>
+// CHECK-SAME: %[[ARG2:.+]]: tensor<?x1x?x?xi32>
+func.func @pooling_nhwc_min_unsigned(%input: tensor<?x1x?x?xi32>, %filter: tensor<1x?xi32>, %init: tensor<?x1x?x?xi32>) -> tensor<?x1x?x?xi32> {
// CHECK: %[[SLICE0:.+]] = tensor.extract_slice %[[ARG0]]
// CHECK: %[[SLICE1:.+]] = tensor.extract_slice %[[ARG1]]
// CHECK: %[[SLICE2:.+]] = tensor.extract_slice %[[ARG2]]
@@ -178,10 +178,10 @@ func.func @pooling_nhwc_min_unsigned(%input: tensor<?x1x?x?xf32>, %filter: tenso
// CHECK: %[[RES:.+]] = tensor.insert_slice %[[SLICERES]] into %[[ARG2]]
%0 = linalg.pooling_nhwc_min_unsigned {dilations = dense<1> : tensor<2xi64>,
strides = dense<1> : tensor<2xi64>}
- ins (%input, %filter: tensor<?x1x?x?xf32>, tensor<1x?xf32>)
- outs (%init: tensor<?x1x?x?xf32>) -> tensor<?x1x?x?xf32>
+ ins (%input, %filter: tensor<?x1x?x?xi32>, tensor<1x?xi32>)
+ outs (%init: tensor<?x1x?x?xi32>) -> tensor<?x1x?x?xi32>
// CHECK: return %[[RES]]
- return %0 : tensor<?x1x?x?xf32>
+ return %0 : tensor<?x1x?x?xi32>
}
// CHECK-LABEL: @pooling_nchw_max
>From 9e394abf7505217e9b072e6964a7568510cf3c33 Mon Sep 17 00:00:00 2001
From: mencotton <mencotton0410 at gmail.com>
Date: Mon, 3 Nov 2025 22:06:44 +0900
Subject: [PATCH 2/5] [mlir][linalg][python] Reject unsigned pooling on
non-integer element types in Python
---
.../dialects/linalg/opdsl/lang/emitter.py | 18 ++++---
.../dialects/linalg/opdsl/emit_pooling.py | 48 +++++++++++++++++++
2 files changed, 60 insertions(+), 6 deletions(-)
diff --git a/mlir/python/mlir/dialects/linalg/opdsl/lang/emitter.py b/mlir/python/mlir/dialects/linalg/opdsl/lang/emitter.py
index 254458a978828..fb2570c7bb498 100644
--- a/mlir/python/mlir/dialects/linalg/opdsl/lang/emitter.py
+++ b/mlir/python/mlir/dialects/linalg/opdsl/lang/emitter.py
@@ -532,9 +532,9 @@ def _binary_max_signed(self, lhs: Value, rhs: Value) -> Value:
raise NotImplementedError("Unsupported 'max' operands: {lhs}, {rhs}")
def _binary_max_unsigned(self, lhs: Value, rhs: Value) -> Value:
- if _is_floating_point_type(lhs.type):
- return arith.MaximumFOp(lhs, rhs).result
- if _is_integer_type(lhs.type) or _is_index_type(lhs.type):
+ if (
+ _is_integer_type(lhs.type) and not _is_bool_type(lhs.type)
+ ) or _is_index_type(lhs.type):
return arith.MaxUIOp(lhs, rhs).result
raise NotImplementedError("Unsupported 'max_unsigned' operands: {lhs}, {rhs}")
@@ -546,9 +546,9 @@ def _binary_min_signed(self, lhs: Value, rhs: Value) -> Value:
raise NotImplementedError("Unsupported 'min' operands: {lhs}, {rhs}")
def _binary_min_unsigned(self, lhs: Value, rhs: Value) -> Value:
- if _is_floating_point_type(lhs.type):
- return arith.MinimumFOp(lhs, rhs).result
- if _is_integer_type(lhs.type) or _is_index_type(lhs.type):
+ if (
+ _is_integer_type(lhs.type) and not _is_bool_type(lhs.type)
+ ) or _is_index_type(lhs.type):
return arith.MinUIOp(lhs, rhs).result
raise NotImplementedError("Unsupported 'min_unsigned' operands: {lhs}, {rhs}")
@@ -634,6 +634,12 @@ def _is_index_type(t: Type) -> bool:
return IndexType.isinstance(t)
+def _is_bool_type(t: Type) -> bool:
+ if not IntegerType.isinstance(t):
+ return False
+ return IntegerType(t).width == 1
+
+
def _get_floating_point_width(t: Type) -> int:
# TODO: Create a FloatType in the Python API and implement the switch
# there.
diff --git a/mlir/test/python/dialects/linalg/opdsl/emit_pooling.py b/mlir/test/python/dialects/linalg/opdsl/emit_pooling.py
index 4ce0fbc1dbe53..0df87de6393d8 100644
--- a/mlir/test/python/dialects/linalg/opdsl/emit_pooling.py
+++ b/mlir/test/python/dialects/linalg/opdsl/emit_pooling.py
@@ -150,3 +150,51 @@ def test_f32f32_min_pooling(input, shape, init_result):
print(module)
+
+with Context() as ctx, Location.unknown():
+ module = Module.create()
+ with InsertionPoint(module.body):
+ f32 = F32Type.get()
+ bool_t = IntegerType.get_signless(1)
+
+ # CHECK: bool_max_unsigned_error: Unsupported 'max_unsigned' operands
+ @func.FuncOp.from_py_func(
+ RankedTensorType.get((1, 4, 16, 1), f32),
+ RankedTensorType.get((2, 2), f32),
+ RankedTensorType.get((1, 2, 4, 1), bool_t),
+ )
+ def test_bool_i1_max_unsigned_pooling_error(input, shape, init_result):
+ try:
+ pooling_poly(
+ input,
+ shape,
+ outs=[init_result],
+ reduce=BinaryFn.max_unsigned,
+ cast=TypeFn.cast_unsigned,
+ strides=[2, 4],
+ dilations=[1, 2],
+ )
+ except NotImplementedError as e:
+ print(f"bool_max_unsigned_error: {e}")
+ return init_result
+
+ # CHECK: float_max_unsigned_error: Unsupported 'max_unsigned' operands
+ @func.FuncOp.from_py_func(
+ RankedTensorType.get((1, 4, 16, 1), f32),
+ RankedTensorType.get((2, 2), f32),
+ RankedTensorType.get((1, 2, 4, 1), f32),
+ )
+ def test_f32f32_max_unsigned_pooling_error(input, shape, init_result):
+ try:
+ pooling_poly(
+ input,
+ shape,
+ outs=[init_result],
+ reduce=BinaryFn.max_unsigned,
+ cast=TypeFn.cast_unsigned,
+ strides=[2, 4],
+ dilations=[1, 2],
+ )
+ except NotImplementedError as e:
+ print(f"float_max_unsigned_error: {e}")
+ return init_result
>From 13f41de5ce457d60aa046437b74af2f9d13f223b Mon Sep 17 00:00:00 2001
From: mencotton <mencotton0410 at gmail.com>
Date: Sun, 16 Nov 2025 00:34:08 +0900
Subject: [PATCH 3/5] [mlir][linalg][test] Drop float min_unsigned roundtrip
---
.../convolution/roundtrip-convolution.mlir | 106 ++++++++++++++++++
1 file changed, 106 insertions(+)
create mode 100644 mlir/test/Dialect/Linalg/convolution/roundtrip-convolution.mlir
diff --git a/mlir/test/Dialect/Linalg/convolution/roundtrip-convolution.mlir b/mlir/test/Dialect/Linalg/convolution/roundtrip-convolution.mlir
new file mode 100644
index 0000000000000..ffea6ad4c5b50
--- /dev/null
+++ b/mlir/test/Dialect/Linalg/convolution/roundtrip-convolution.mlir
@@ -0,0 +1,106 @@
+// The following test examples of linalg convolution named ops lowered to linalg.generic and then
+// lifted back up to named op.
+// RUN: mlir-opt %s -linalg-generalize-named-ops | mlir-opt --linalg-specialize-generic-ops | FileCheck %s --implicit-check-not=linalg.generic
+
+// NOTE: Most tests in this file use dynamic shapes as the underlying transformations don't modify shapes. There's one exception that's added as a smoke test.
+func.func @depthwise_conv_1d_nwc_wc_static(%input: tensor<1x25x8xi8>, %filter: tensor<3x8xi8>, %output: tensor<1x10x8xi32>) -> tensor<1x10x8xi32> {
+ %0 = linalg.depthwise_conv_1d_nwc_wc
+ {dilations = dense<3> : tensor<1xi64>, strides = dense<2> : tensor<1xi64>}
+ ins (%input, %filter: tensor<1x25x8xi8>, tensor<3x8xi8>)
+ outs (%output: tensor<1x10x8xi32>) -> tensor<1x10x8xi32>
+ return %0 : tensor<1x10x8xi32>
+}
+// CHECK: @depthwise_conv_1d_nwc_wc_static
+// CHECK: linalg.depthwise_conv_1d_nwc_wc
+// CHECK-SAME: dilations = dense<3> : tensor<1xi64>, strides = dense<2> : tensor<1xi64>
+
+// -----
+
+func.func @depthwise_conv_2d_nchw_chw(%input: tensor<?x?x?x?xf16>, %filter: tensor<?x?x?xf16>, %output: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32> {
+ %0 = linalg.depthwise_conv_2d_nchw_chw
+ {dilations = dense<[2,3]> : vector<2xi64>, strides = dense<[4,5]> : vector<2xi64>}
+ ins (%input, %filter: tensor<?x?x?x?xf16>, tensor<?x?x?xf16>)
+ outs (%output: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32>
+ return %0 : tensor<?x?x?x?xf32>
+}
+// CHECK: @depthwise_conv_2d_nchw_chw
+// CHECK: linalg.depthwise_conv_2d_nchw_chw
+// CHECK-SAME: dilations = dense<[2, 3]> : tensor<2xi64>, strides = dense<[4, 5]> : tensor<2xi64>
+
+// -----
+
+func.func @depthwise_conv_3d_ndhwc_dhwcm(%input: tensor<?x?x?x?x?xf32>, %filter: tensor<?x?x?x?x?xf32>, %output: tensor<?x?x?x?x?x?xf32>) -> tensor<?x?x?x?x?x?xf32> {
+ %0 = linalg.depthwise_conv_3d_ndhwc_dhwcm
+ {dilations = dense<1> : tensor<3xi64>, strides = dense<1> : tensor<3xi64>}
+ ins (%input, %filter: tensor<?x?x?x?x?xf32>, tensor<?x?x?x?x?xf32>)
+ outs (%output: tensor<?x?x?x?x?x?xf32>) -> tensor<?x?x?x?x?x?xf32>
+ return %0 : tensor<?x?x?x?x?x?xf32>
+}
+// CHECK: @depthwise_conv_3d_ndhwc_dhwcm
+// CHECK: linalg.depthwise_conv_3d_ndhwc_dhwcm
+// CHECK-SAME: dilations = dense<1> : tensor<3xi64>, strides = dense<1> : tensor<3xi64>
+
+// -----
+
+func.func @pooling_nhwc_max(%input: tensor<?x?x?x?xf32>, %filter: tensor<?x?xf32>, %output: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32> {
+ %0 = linalg.pooling_nhwc_max
+ {dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<?x?x?x?xf32>, tensor<?x?xf32>)
+ outs (%output: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32>
+ return %0 : tensor<?x?x?x?xf32>
+}
+// CHECK: @pooling_nhwc_max
+// CHECK: linalg.pooling_nhwc_max
+// CHECK-SAME: dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>
+
+// -----
+
+func.func @pooling_nhwc_min(%input: tensor<?x?x?x?xf32>, %filter: tensor<?x?xf32>, %output: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32> {
+ %0 = linalg.pooling_nhwc_min
+ {dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<?x?x?x?xf32>, tensor<?x?xf32>)
+ outs (%output: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32>
+ return %0 : tensor<?x?x?x?xf32>
+}
+// CHECK: @pooling_nhwc_min
+// CHECK: linalg.pooling_nhwc_min
+// CHECK-SAME: dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>
+
+// -----
+
+func.func @pooling_nhwc_sum(%input: tensor<?x?x?x?xf32>, %filter: tensor<?x?xf32>, %output: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32> {
+ %0 = linalg.pooling_nhwc_sum
+ {dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<?x?x?x?xf32>, tensor<?x?xf32>)
+ outs (%output: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32>
+ return %0 : tensor<?x?x?x?xf32>
+}
+// CHECK: @pooling_nhwc_sum
+// CHECK: linalg.pooling_nhwc_sum
+// CHECK-SAME: dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>
+
+// -----
+
+func.func @pooling_nhwc_max_unsigned(%input: tensor<?x?x?x?xi8>, %filter: tensor<?x?xi8>, %output: tensor<?x?x?x?xi32>) -> tensor<?x?x?x?xi32> {
+ %0 = linalg.pooling_nhwc_max_unsigned
+ {dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<?x?x?x?xi8>, tensor<?x?xi8>)
+ outs (%output: tensor<?x?x?x?xi32>) -> tensor<?x?x?x?xi32>
+ return %0 : tensor<?x?x?x?xi32>
+}
+// CHECK: @pooling_nhwc_max_unsigned
+// CHECK: linalg.pooling_nhwc_max_unsigned
+// CHECK-SAME: dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>
+
+// -----
+
+func.func @pooling_nhwc_min_unsigned_integer(%input: tensor<?x?x?x?xi32>, %filter: tensor<?x?xi32>, %output: tensor<?x?x?x?xi32>) -> tensor<?x?x?x?xi32> {
+ %0 = linalg.pooling_nhwc_min_unsigned
+ {dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<?x?x?x?xi32>, tensor<?x?xi32>)
+ outs (%output: tensor<?x?x?x?xi32>) -> tensor<?x?x?x?xi32>
+ return %0 : tensor<?x?x?x?xi32>
+}
+// CHECK: @pooling_nhwc_min_unsigned_integer
+// CHECK: linalg.pooling_nhwc_min_unsigned
+// CHECK-SAME: dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>
>From 2e49cdf52897793fb782bbb8cdbc2e1709fdeb82 Mon Sep 17 00:00:00 2001
From: mencotton <mencotton0410 at gmail.com>
Date: Fri, 21 Nov 2025 22:00:14 +0900
Subject: [PATCH 4/5] [mlir][linalg][test] Refactor: Move unsigned pooling
float check to invalid.mlir
---
mlir/test/Dialect/Linalg/invalid.mlir | 28 ++++++++++++++++++++
mlir/test/Dialect/Linalg/named-ops-fail.mlir | 14 ----------
2 files changed, 28 insertions(+), 14 deletions(-)
diff --git a/mlir/test/Dialect/Linalg/invalid.mlir b/mlir/test/Dialect/Linalg/invalid.mlir
index fabc8e610612d..2f08c4a8cb67c 100644
--- a/mlir/test/Dialect/Linalg/invalid.mlir
+++ b/mlir/test/Dialect/Linalg/invalid.mlir
@@ -1939,3 +1939,31 @@ func.func @matmul_invalid_mixed_types(%t: tensor<?xf16>, %f: vector<4xf16>)
outs(%f : vector<4xf16>) -> tensor<?xf16>
func.return %0, %f : tensor<?xf16>, vector<4xf16>
}
+
+// -----
+
+func.func @pooling_nhwc_max_unsigned_non_integer_elem_type(
+ %input: tensor<1x4x4x1xf32>,
+ %filter: tensor<2x2xf32>,
+ %init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32> {
+ // expected-error @+1 {{unsupported operation: unsigned max not on uint}}
+ %0 = linalg.pooling_nhwc_max_unsigned {dilations = dense<1> : tensor<2xi64>,
+ strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<1x4x4x1xf32>, tensor<2x2xf32>)
+ outs (%init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32>
+ return %0 : tensor<1x2x2x1xf32>
+}
+
+// -----
+
+func.func @pooling_nhwc_min_unsigned_non_integer_elem_type(
+ %input: tensor<1x4x4x1xf32>,
+ %filter: tensor<2x2xf32>,
+ %init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32> {
+ // expected-error @+1 {{unsupported operation: unsigned min not on uint}}
+ %0 = linalg.pooling_nhwc_min_unsigned {dilations = dense<1> : tensor<2xi64>,
+ strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<1x4x4x1xf32>, tensor<2x2xf32>)
+ outs (%init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32>
+ return %0 : tensor<1x2x2x1xf32>
+}
diff --git a/mlir/test/Dialect/Linalg/named-ops-fail.mlir b/mlir/test/Dialect/Linalg/named-ops-fail.mlir
index 4ecf685b4c695..665119d94e534 100644
--- a/mlir/test/Dialect/Linalg/named-ops-fail.mlir
+++ b/mlir/test/Dialect/Linalg/named-ops-fail.mlir
@@ -80,20 +80,6 @@ func.func @divu_broadcast(%arg0: memref<8x16xi32>, %arg1: memref<4x8x16xi32>, %a
// -----
-func.func @pooling_nhwc_max_unsigned_float(
- %input: tensor<?x?x?x?xf32>,
- %filter: tensor<?x?xf32>,
- %init_val: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32> {
- // CHECK: unsupported operation: unsigned max not on uint
- linalg.pooling_nhwc_max_unsigned {dilations = dense<1> : tensor<2xi64>,
- strides = dense<1> : tensor<2xi64>}
- ins (%input, %filter: tensor<?x?x?x?xf32>, tensor<?x?xf32>)
- outs (%init_val: tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32>
- return %init_val : tensor<?x?x?x?xf32>
-}
-
-// -----
-
func.func @exp_type_cast(%arg: memref<4x8x16xf16>, %out: memref<4x8x16xf32>) {
// CHECK: operand 1 ('f16') doesn't match the element type of the enclosing linalg.generic op ('f32')
linalg.exp ins(%arg : memref<4x8x16xf16>) outs(%out: memref<4x8x16xf32>)
>From db73052f3491d6695530881fb780edff9bd09e1f Mon Sep 17 00:00:00 2001
From: mencotton <mencotton0410 at gmail.com>
Date: Sun, 14 Dec 2025 11:37:53 +0900
Subject: [PATCH 5/5] Improve tests
---
mlir/test/Dialect/Linalg/invalid.mlir | 146 +++++++++++++++++++-----
mlir/test/Dialect/Linalg/named-ops.mlir | 46 +++++++-
2 files changed, 161 insertions(+), 31 deletions(-)
diff --git a/mlir/test/Dialect/Linalg/invalid.mlir b/mlir/test/Dialect/Linalg/invalid.mlir
index 2f08c4a8cb67c..d905e7ba52b2b 100644
--- a/mlir/test/Dialect/Linalg/invalid.mlir
+++ b/mlir/test/Dialect/Linalg/invalid.mlir
@@ -1913,6 +1913,125 @@ func.func @reduce_non_operation_name(%arg0: tensor<4xf32>, %arg1: tensor<f32>) -
// -----
+//===----------------------------------------------------------------------===//
+// linalg.pooling_nhwc_*
+//===----------------------------------------------------------------------===//
+
+func.func @pooling_nhwc_max_unsigned_float_type(
+ %input: tensor<1x4x4x1xf32>,
+ %filter: tensor<2x2xf32>,
+ %init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32> {
+ // expected-error @+1 {{unsupported operation: unsigned max not on uint}}
+ %0 = linalg.pooling_nhwc_max_unsigned {dilations = dense<1> : tensor<2xi64>,
+ strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<1x4x4x1xf32>, tensor<2x2xf32>)
+ outs (%init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32>
+ return %0 : tensor<1x2x2x1xf32>
+}
+
+// -----
+
+func.func @pooling_nhwc_max_unsigned_i1(
+ %input: tensor<1x4x4x1xi1>,
+ %filter: tensor<2x2xi1>,
+ %init_val: tensor<1x2x2x1xi1>) -> tensor<1x2x2x1xi1> {
+ // expected-error @+1 {{unsupported operation: unsigned max not on uint}}
+ %0 = linalg.pooling_nhwc_max_unsigned {dilations = dense<1> : tensor<2xi64>,
+ strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<1x4x4x1xi1>, tensor<2x2xi1>)
+ outs (%init_val: tensor<1x2x2x1xi1>) -> tensor<1x2x2x1xi1>
+ return %0 : tensor<1x2x2x1xi1>
+}
+
+// -----
+
+func.func @pooling_nhwc_min_unsigned_float_type(
+ %input: tensor<1x4x4x1xf32>,
+ %filter: tensor<2x2xf32>,
+ %init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32> {
+ // expected-error @+1 {{unsupported operation: unsigned min not on uint}}
+ %0 = linalg.pooling_nhwc_min_unsigned {dilations = dense<1> : tensor<2xi64>,
+ strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<1x4x4x1xf32>, tensor<2x2xf32>)
+ outs (%init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32>
+ return %0 : tensor<1x2x2x1xf32>
+}
+
+// -----
+
+func.func @pooling_nhwc_min_unsigned_i1(
+ %input: tensor<1x4x4x1xi1>,
+ %filter: tensor<2x2xi1>,
+ %init_val: tensor<1x2x2x1xi1>) -> tensor<1x2x2x1xi1> {
+ // expected-error @+1 {{unsupported operation: unsigned min not on uint}}
+ %0 = linalg.pooling_nhwc_min_unsigned {dilations = dense<1> : tensor<2xi64>,
+ strides = dense<1> : tensor<2xi64>}
+ ins (%input, %filter: tensor<1x4x4x1xi1>, tensor<2x2xi1>)
+ outs (%init_val: tensor<1x2x2x1xi1>) -> tensor<1x2x2x1xi1>
+ return %0 : tensor<1x2x2x1xi1>
+}
+
+// -----
+
+//===----------------------------------------------------------------------===//
+// linalg.pooling_nwc_*
+//===----------------------------------------------------------------------===//
+
+func.func @pooling_nwc_max_unsigned_float_type(
+ %input: tensor<1x4x1xf32>,
+ %filter: tensor<2xf32>,
+ %init_val: tensor<1x2x1xf32>) -> tensor<1x2x1xf32> {
+ // expected-error @+1 {{unsupported operation: unsigned max not on uint}}
+ %0 = linalg.pooling_nwc_max_unsigned {dilations = dense<1> : tensor<1xi64>,
+ strides = dense<1> : tensor<1xi64>}
+ ins (%input, %filter: tensor<1x4x1xf32>, tensor<2xf32>)
+ outs (%init_val: tensor<1x2x1xf32>) -> tensor<1x2x1xf32>
+ return %0 : tensor<1x2x1xf32>
+}
+
+// -----
+
+func.func @pooling_nwc_max_unsigned_i1(
+ %input: tensor<1x4x1xi1>,
+ %filter: tensor<2xi1>,
+ %init_val: tensor<1x2x1xi1>) -> tensor<1x2x1xi1> {
+ // expected-error @+1 {{unsupported operation: unsigned max not on uint}}
+ %0 = linalg.pooling_nwc_max_unsigned {dilations = dense<1> : tensor<1xi64>,
+ strides = dense<1> : tensor<1xi64>}
+ ins (%input, %filter: tensor<1x4x1xi1>, tensor<2xi1>)
+ outs (%init_val: tensor<1x2x1xi1>) -> tensor<1x2x1xi1>
+ return %0 : tensor<1x2x1xi1>
+}
+
+// -----
+
+func.func @pooling_nwc_min_unsigned_float_type(
+ %input: tensor<1x4x1xf32>,
+ %filter: tensor<2xf32>,
+ %init_val: tensor<1x2x1xf32>) -> tensor<1x2x1xf32> {
+ // expected-error @+1 {{unsupported operation: unsigned min not on uint}}
+ %0 = linalg.pooling_nwc_min_unsigned {dilations = dense<1> : tensor<1xi64>,
+ strides = dense<1> : tensor<1xi64>}
+ ins (%input, %filter: tensor<1x4x1xf32>, tensor<2xf32>)
+ outs (%init_val: tensor<1x2x1xf32>) -> tensor<1x2x1xf32>
+ return %0 : tensor<1x2x1xf32>
+}
+
+// -----
+
+func.func @pooling_nwc_min_unsigned_i1(
+ %input: tensor<1x4x1xi1>,
+ %filter: tensor<2xi1>,
+ %init_val: tensor<1x2x1xi1>) -> tensor<1x2x1xi1> {
+ // expected-error @+1 {{unsupported operation: unsigned min not on uint}}
+ %0 = linalg.pooling_nwc_min_unsigned {dilations = dense<1> : tensor<1xi64>,
+ strides = dense<1> : tensor<1xi64>}
+ ins (%input, %filter: tensor<1x4x1xi1>, tensor<2xi1>)
+ outs (%init_val: tensor<1x2x1xi1>) -> tensor<1x2x1xi1>
+ return %0 : tensor<1x2x1xi1>
+}
+
+// -----
//===----------------------------------------------------------------------===//
// Tests for generic infrastructure for named Ops. The actual Ops used are
@@ -1940,30 +2059,3 @@ func.func @matmul_invalid_mixed_types(%t: tensor<?xf16>, %f: vector<4xf16>)
func.return %0, %f : tensor<?xf16>, vector<4xf16>
}
-// -----
-
-func.func @pooling_nhwc_max_unsigned_non_integer_elem_type(
- %input: tensor<1x4x4x1xf32>,
- %filter: tensor<2x2xf32>,
- %init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32> {
- // expected-error @+1 {{unsupported operation: unsigned max not on uint}}
- %0 = linalg.pooling_nhwc_max_unsigned {dilations = dense<1> : tensor<2xi64>,
- strides = dense<1> : tensor<2xi64>}
- ins (%input, %filter: tensor<1x4x4x1xf32>, tensor<2x2xf32>)
- outs (%init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32>
- return %0 : tensor<1x2x2x1xf32>
-}
-
-// -----
-
-func.func @pooling_nhwc_min_unsigned_non_integer_elem_type(
- %input: tensor<1x4x4x1xf32>,
- %filter: tensor<2x2xf32>,
- %init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32> {
- // expected-error @+1 {{unsupported operation: unsigned min not on uint}}
- %0 = linalg.pooling_nhwc_min_unsigned {dilations = dense<1> : tensor<2xi64>,
- strides = dense<1> : tensor<2xi64>}
- ins (%input, %filter: tensor<1x4x4x1xf32>, tensor<2x2xf32>)
- outs (%init_val: tensor<1x2x2x1xf32>) -> tensor<1x2x2x1xf32>
- return %0 : tensor<1x2x2x1xf32>
-}
diff --git a/mlir/test/Dialect/Linalg/named-ops.mlir b/mlir/test/Dialect/Linalg/named-ops.mlir
index c2a8f24624d8e..1e356c8fb4e72 100644
--- a/mlir/test/Dialect/Linalg/named-ops.mlir
+++ b/mlir/test/Dialect/Linalg/named-ops.mlir
@@ -707,11 +707,11 @@ func.func @pooling_nhwc_max_tensor(%input: tensor<1x4x4x1xf32>) -> tensor<1x2x2x
// -----
-// CHECK-LABEL: func @pooling_nhwc_max_unsigned_tensor
+// CHECK-LABEL: func @pooling_nhwc_max_unsigned_i32
// CHECK: %{{.+}} = linalg.pooling_nhwc_max_unsigned
// CHECK-SAME: ins(%{{.+}}, %{{.+}} : tensor<1x4x4x1xi32>, tensor<3x3xi32>)
// CHECK-SAME: outs(%{{.+}} : tensor<1x2x2x1xi32>) -> tensor<1x2x2x1xi32>
-func.func @pooling_nhwc_max_unsigned_tensor(%input: tensor<1x4x4x1xi32>) -> tensor<1x2x2x1xi32> {
+func.func @pooling_nhwc_max_unsigned_i32(%input: tensor<1x4x4x1xi32>) -> tensor<1x2x2x1xi32> {
%fake = tensor.empty() : tensor<3x3xi32>
%init = tensor.empty() : tensor<1x2x2x1xi32>
%cst = arith.constant 0 : i32
@@ -722,6 +722,25 @@ func.func @pooling_nhwc_max_unsigned_tensor(%input: tensor<1x4x4x1xi32>) -> tens
return %res : tensor<1x2x2x1xi32>
}
+// -----
+
+// CHECK-LABEL: func @pooling_nwc_max_unsigned_i32
+// CHECK: %{{.+}} = linalg.pooling_nwc_max_unsigned
+// CHECK-SAME: dilations = dense<1> : tensor<1xi64>
+// CHECK-SAME: strides = dense<1> : tensor<1xi64>
+// CHECK-SAME: ins(%{{.+}}, %{{.+}} : tensor<1x4x1xi32>, tensor<3xi32>)
+// CHECK-SAME: outs(%{{.+}} : tensor<1x2x1xi32>) -> tensor<1x2x1xi32>
+func.func @pooling_nwc_max_unsigned_i32(%input: tensor<1x4x1xi32>) -> tensor<1x2x1xi32> {
+ %fake = tensor.empty() : tensor<3xi32>
+ %init = tensor.empty() : tensor<1x2x1xi32>
+ %cst = arith.constant 0 : i32
+ %fill = linalg.fill ins(%cst : i32) outs(%init : tensor<1x2x1xi32>) -> tensor<1x2x1xi32>
+ %res = linalg.pooling_nwc_max_unsigned {dilations = dense<1> : tensor<1xi64>, strides = dense<1> : tensor<1xi64>}
+ ins(%input, %fake: tensor<1x4x1xi32>, tensor<3xi32>)
+ outs(%fill: tensor<1x2x1xi32>) -> tensor<1x2x1xi32>
+ return %res : tensor<1x2x1xi32>
+}
+
// -----
// CHECK-LABEL: func @pooling_nwc_max_tensor
// CHECK: %{{.+}} = linalg.pooling_nwc_max
@@ -1034,11 +1053,11 @@ func.func @pooling_nhwc_min_tensor(%input: tensor<1x4x4x1xf32>) -> tensor<1x2x2x
// -----
-// CHECK-LABEL: func @pooling_nhwc_min_unsigned_tensor
+// CHECK-LABEL: func @pooling_nhwc_min_unsigned_i32
// CHECK: %{{.+}} = linalg.pooling_nhwc_min_unsigned
// CHECK-SAME: ins(%{{.+}}, %{{.+}} : tensor<1x4x4x1xi32>, tensor<3x3xi32>)
// CHECK-SAME: outs(%{{.+}} : tensor<1x2x2x1xi32>) -> tensor<1x2x2x1xi32>
-func.func @pooling_nhwc_min_unsigned_tensor(%input: tensor<1x4x4x1xi32>) -> tensor<1x2x2x1xi32> {
+func.func @pooling_nhwc_min_unsigned_i32(%input: tensor<1x4x4x1xi32>) -> tensor<1x2x2x1xi32> {
%fake = tensor.empty() : tensor<3x3xi32>
%init = tensor.empty() : tensor<1x2x2x1xi32>
%cst = arith.constant 0 : i32
@@ -1051,6 +1070,25 @@ func.func @pooling_nhwc_min_unsigned_tensor(%input: tensor<1x4x4x1xi32>) -> tens
// -----
+// CHECK-LABEL: func @pooling_nwc_min_unsigned_i32
+// CHECK: %{{.+}} = linalg.pooling_nwc_min_unsigned
+// CHECK-SAME: dilations = dense<1> : tensor<1xi64>
+// CHECK-SAME: strides = dense<1> : tensor<1xi64>
+// CHECK-SAME: ins(%{{.+}}, %{{.+}} : tensor<1x4x1xi32>, tensor<3xi32>)
+// CHECK-SAME: outs(%{{.+}} : tensor<1x2x1xi32>) -> tensor<1x2x1xi32>
+func.func @pooling_nwc_min_unsigned_i32(%input: tensor<1x4x1xi32>) -> tensor<1x2x1xi32> {
+ %fake = tensor.empty() : tensor<3xi32>
+ %init = tensor.empty() : tensor<1x2x1xi32>
+ %cst = arith.constant 0 : i32
+ %fill = linalg.fill ins(%cst : i32) outs(%init : tensor<1x2x1xi32>) -> tensor<1x2x1xi32>
+ %res = linalg.pooling_nwc_min_unsigned {dilations = dense<1> : tensor<1xi64>, strides = dense<1> : tensor<1xi64>}
+ ins(%input, %fake: tensor<1x4x1xi32>, tensor<3xi32>)
+ outs(%fill: tensor<1x2x1xi32>) -> tensor<1x2x1xi32>
+ return %res : tensor<1x2x1xi32>
+}
+
+// -----
+
// CHECK-LABEL: func @pooling_nwc_min_tensor
// CHECK: %{{.+}} = linalg.pooling_nwc_min
// CHECK-SAME: dilations = dense<1> : tensor<1xi64>
More information about the Mlir-commits
mailing list