[Mlir-commits] [mlir] [mlir][tosa] Fix no op clamp canonicalization when nan_mode = ignore (PR #229887)
Luke Hutton
llvmlistbot at llvm.org
Mon Oct 12 01:51:37 PDT 2026
https://github.com/lhutton1 updated https://github.com/llvm/llvm-project/pull/229887
>From 87dabd4b5dc4e5916c28360e1bf156f242734c43 Mon Sep 17 00:00:00 2001
From: Luke Hutton <luke.hutton at arm.com>
Date: Wed, 7 Oct 2026 20:29:41 +0100
Subject: [PATCH 1/2] [mlir][tosa] Fix no op clamp canonicalization when
nan_mode = ignore
When nan_mode = ignore, a no op clamp should not be folded. This is
because if a value of NaN is specified in the input, the output
becomes the minimum specified value. A regression test with clamp
bounds of infinity is provided.
Change-Id: Ia79ea43e84c896eaa02acece352d7bb36b7b0daf
---
mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp | 3 +++
mlir/test/Dialect/Tosa/canonicalize.mlir | 10 ++++++++++
2 files changed, 13 insertions(+)
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index 21dd49875d420..e23925ce2394d 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
@@ -599,6 +599,9 @@ struct ClampIsNoOp : public OpRewritePattern<tosa::ClampOp> {
auto inputElementType = inputType.getElementType();
if (isa<FloatType>(inputElementType)) {
+ if (op.getNanMode() == tosa::NanPropagationMode::IGNORE)
+ return failure();
+
// Unlike integer types, floating point types can represent infinity.
const auto minClamp =
llvm::cast<mlir::FloatAttr>(op.getMinValAttr()).getValue();
diff --git a/mlir/test/Dialect/Tosa/canonicalize.mlir b/mlir/test/Dialect/Tosa/canonicalize.mlir
index f39a5c4379eb4..063648cbadb9f 100644
--- a/mlir/test/Dialect/Tosa/canonicalize.mlir
+++ b/mlir/test/Dialect/Tosa/canonicalize.mlir
@@ -297,6 +297,16 @@ func.func @clamp_f32_is_noop(%arg0: tensor<4xf32>) -> tensor<4xf32> {
// -----
+// CHECK-LABEL: @clamp_f32_nan_ignore_not_noop
+func.func @clamp_f32_nan_ignore_not_noop(%arg0: tensor<4xf32>) -> tensor<4xf32> {
+ // CHECK: %[[CLAMP:.*]] = tosa.clamp %arg0 min_val(0xFF800000 : f32) max_val(0x7F800000 : f32) nan_mode<IGNORE>
+ // CHECK-NEXT: return %[[CLAMP]] : tensor<4xf32>
+ %0 = tosa.clamp %arg0 min_val(0xFF800000 : f32) max_val(0x7F800000 : f32) nan_mode<IGNORE> : (tensor<4xf32>) -> tensor<4xf32>
+ return %0 : tensor<4xf32>
+}
+
+// -----
+
// CHECK-LABEL: @clamp_boolean_is_noop
func.func @clamp_boolean_is_noop(%arg0: tensor<4xi1>) -> tensor<4xi1> {
// CHECK: return %arg0
>From 2fd74c00c7c583dd8e2be60787cd226279d7918c Mon Sep 17 00:00:00 2001
From: Luke Hutton <luke.hutton at arm.com>
Date: Mon, 12 Oct 2026 09:51:07 +0100
Subject: [PATCH 2/2] add integer test case
Change-Id: I63846561b2e241cd409fa83327e616c4dcf474a9
---
mlir/test/Dialect/Tosa/canonicalize.mlir | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/mlir/test/Dialect/Tosa/canonicalize.mlir b/mlir/test/Dialect/Tosa/canonicalize.mlir
index 063648cbadb9f..48e429846b669 100644
--- a/mlir/test/Dialect/Tosa/canonicalize.mlir
+++ b/mlir/test/Dialect/Tosa/canonicalize.mlir
@@ -307,6 +307,15 @@ func.func @clamp_f32_nan_ignore_not_noop(%arg0: tensor<4xf32>) -> tensor<4xf32>
// -----
+// CHECK-LABEL: @clamp_i8_nan_ignore_noop
+func.func @clamp_i8_nan_ignore_noop(%arg0: tensor<4xi8>) -> tensor<4xi8> {
+ // CHECK: return %arg0 : tensor<4xi8>
+ %0 = tosa.clamp %arg0 min_val(-128 : i8) max_val(127 : i8) nan_mode<IGNORE> : (tensor<4xi8>) -> tensor<4xi8>
+ return %0 : tensor<4xi8>
+}
+
+// ----
+
// CHECK-LABEL: @clamp_boolean_is_noop
func.func @clamp_boolean_is_noop(%arg0: tensor<4xi1>) -> tensor<4xi1> {
// CHECK: return %arg0
More information about the Mlir-commits
mailing list