[Mlir-commits] [mlir] 4e8e856 - [mlir][tosa] Fix no op clamp canonicalization when nan_mode = ignore (#229887)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Oct 12 03:00:39 PDT 2026


Author: Luke Hutton
Date: 2026-10-12T11:00:34+01:00
New Revision: 4e8e856301a5a5506eb50568b4a180000d8f4ed3

URL: https://github.com/llvm/llvm-project/commit/4e8e856301a5a5506eb50568b4a180000d8f4ed3
DIFF: https://github.com/llvm/llvm-project/commit/4e8e856301a5a5506eb50568b4a180000d8f4ed3.diff

LOG: [mlir][tosa] Fix no op clamp canonicalization when nan_mode = ignore (#229887)

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.

Added: 
    

Modified: 
    mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
    mlir/test/Dialect/Tosa/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index 9d3e6ef381f9c..f97446c294247 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..48e429846b669 100644
--- a/mlir/test/Dialect/Tosa/canonicalize.mlir
+++ b/mlir/test/Dialect/Tosa/canonicalize.mlir
@@ -297,6 +297,25 @@ 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_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