[Mlir-commits] [mlir] [mlir][math] Fix the description of the semantics of math.clampf (PR #175012)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Jan 16 08:00:40 PST 2026
https://github.com/jack-slingsby updated https://github.com/llvm/llvm-project/pull/175012
>From 51100644e499e96b222d4abc67d7baeb923aa354 Mon Sep 17 00:00:00 2001
From: Jack Slingsby <jack.slingsby at imgtec.com>
Date: Thu, 8 Jan 2026 16:02:40 +0000
Subject: [PATCH 1/2] Fix the description of the semantics of math.clampf
---
mlir/include/mlir/Dialect/Math/IR/MathOps.td | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
index af65af6fedec6..432f08b14a989 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -365,7 +365,7 @@ def Math_ClampFOp : Math_FloatTernaryOp<"clampf"> {
The semantics of the operation are described by:
```
- clampf(value, min, max) = maxf(minf(value, min), max)
+ clampf(value, min, max) = minf(maxf(value, min), max)
```
Example:
>From ba325857d1a1be73c34569860d564835d0692aaa Mon Sep 17 00:00:00 2001
From: Jack Slingsby <jack.slingsby at imgtec.com>
Date: Fri, 16 Jan 2026 15:48:02 +0000
Subject: [PATCH 2/2] Improve description + update the expand ops transform
---
mlir/include/mlir/Dialect/Math/IR/MathOps.td | 3 ++-
mlir/lib/Dialect/Math/Transforms/ExpandOps.cpp | 4 ++--
mlir/test/Dialect/Math/expand-math.mlir | 8 ++++----
3 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
index 432f08b14a989..a7f5e14f74894 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -365,8 +365,9 @@ def Math_ClampFOp : Math_FloatTernaryOp<"clampf"> {
The semantics of the operation are described by:
```
- clampf(value, min, max) = minf(maxf(value, min), max)
+ clampf(value, min, max) = maxf(minf(value, max), min)
```
+ If `min>max` the resulting value is undefined.
Example:
diff --git a/mlir/lib/Dialect/Math/Transforms/ExpandOps.cpp b/mlir/lib/Dialect/Math/Transforms/ExpandOps.cpp
index cd68039d0d964..249a95cc7924a 100644
--- a/mlir/lib/Dialect/Math/Transforms/ExpandOps.cpp
+++ b/mlir/lib/Dialect/Math/Transforms/ExpandOps.cpp
@@ -669,8 +669,8 @@ static LogicalResult convertRsqrtOp(math::RsqrtOp op,
static LogicalResult convertClampfOp(math::ClampFOp op,
PatternRewriter &rewriter) {
auto minOp = arith::MinimumFOp::create(rewriter, op.getLoc(), op.getValue(),
- op.getMin(), op.getFastmath());
- rewriter.replaceOpWithNewOp<arith::MaximumFOp>(op, minOp, op.getMax(),
+ op.getMax(), op.getFastmath());
+ rewriter.replaceOpWithNewOp<arith::MaximumFOp>(op, minOp, op.getMin(),
op.getFastmath());
return success();
}
diff --git a/mlir/test/Dialect/Math/expand-math.mlir b/mlir/test/Dialect/Math/expand-math.mlir
index 615c607efc3c3..1d26c826e8d6b 100644
--- a/mlir/test/Dialect/Math/expand-math.mlir
+++ b/mlir/test/Dialect/Math/expand-math.mlir
@@ -827,8 +827,8 @@ func.func @unranked_rsqrt_op(%arg: tensor<*xf32>) -> tensor<*xf32>{
// CHECK-LABEL: func.func @clampf_scalar_op
// CHECK-SAME: (%[[ARG:.*]]: f16, %[[MIN:.*]]: f16, %[[MAX:.*]]: f16)
-// CHECK: %[[V0:.*]] = arith.minimumf %[[ARG]], %[[MIN]] : f16
-// CHECK: %[[V1:.*]] = arith.maximumf %[[V0]], %[[MAX]] : f16
+// CHECK: %[[V0:.*]] = arith.minimumf %[[ARG]], %[[MAX]] : f16
+// CHECK: %[[V1:.*]] = arith.maximumf %[[V0]], %[[MIN]] : f16
// CHECK: return %[[V1]] : f16
func.func @clampf_scalar_op(%arg: f16, %min: f16, %max: f16) -> f16 {
@@ -838,8 +838,8 @@ func.func @clampf_scalar_op(%arg: f16, %min: f16, %max: f16) -> f16 {
// CHECK-LABEL: func.func @clampf_vector_op
// CHECK-SAME: (%[[ARG:.*]]: vector<3x4xf32>, %[[MIN:.*]]: vector<3x4xf32>, %[[MAX:.*]]: vector<3x4xf32>)
-// CHECK: %[[V0:.*]] = arith.minimumf %[[ARG]], %[[MIN]] fastmath<fast> : vector<3x4xf32>
-// CHECK: %[[V1:.*]] = arith.maximumf %[[V0]], %[[MAX]] fastmath<fast> : vector<3x4xf32>
+// CHECK: %[[V0:.*]] = arith.minimumf %[[ARG]], %[[MAX]] fastmath<fast> : vector<3x4xf32>
+// CHECK: %[[V1:.*]] = arith.maximumf %[[V0]], %[[MIN]] fastmath<fast> : vector<3x4xf32>
// CHECK: return %[[V1]] : vector<3x4xf32>
func.func @clampf_vector_op(%arg: vector<3x4xf32>, %min: vector<3x4xf32>, %max: vector<3x4xf32>) -> vector<3x4xf32>{
More information about the Mlir-commits
mailing list