diff --git a/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td b/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td --- a/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td +++ b/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td @@ -814,6 +814,68 @@ }]; } +//===----------------------------------------------------------------------===// +// ClampFOp +//===----------------------------------------------------------------------===// + +def ClampFOp : Std_Op<"clampf", + [NoSideEffect, SameOperandsAndResultType]> { + let summary = "floating-point clamp operation"; + let description = [{ + The `clampf` operation clamps value inside a region. If target compares less + than lowerBound, lowerBound is returned. If target compares more than + upperBound, uppperBound is returned. Otherwise, target is returned. + + Example: + ``` + clampf %0 in (%1, %2) : f32 + "std.clampf"(%0, %1, %2) : (f32, f32, f32) -> f32 + ``` + }]; + let arguments = (ins + FloatLike:$target, + FloatLike:$lowerBound, + FloatLike:$upperBound + ); + let results = (outs FloatLike:$result); + + let verifier = [{ return success(); }]; + + let assemblyFormat = "$target `in` `(` $lowerBound `,` $upperBound `)`" + " attr-dict `:` type($result)"; +} + +//===----------------------------------------------------------------------===// +// ClampIOp +//===----------------------------------------------------------------------===// + +def ClampIOp : Std_Op<"clampi", + [NoSideEffect, SameOperandsAndResultType]> { + let summary = "integer clamp operation"; + let description = [{ + The `clampi` operation clamps value inside a region. If target compares less + than lowerBound, lowerBound is returned. If target compares more than + upperBound, uppperBound is returned. Otherwise, target is returned. + + Example: + ``` + clampi %0 in (%1, %2) : i32 + "std.clampi"(%0, %1, %2) : (i32, i32, i32) -> i32 + ``` + }]; + let arguments = (ins + SignlessIntegerLike:$target, + SignlessIntegerLike:$lowerBound, + SignlessIntegerLike:$upperBound + ); + let results = (outs SignlessIntegerLike:$result); + + let verifier = [{ return success(); }]; + + let assemblyFormat = "$target `in` `(` $lowerBound `,` $upperBound `)`" + " attr-dict `:` type($result)"; +} + //===----------------------------------------------------------------------===// // CmpFOp //===----------------------------------------------------------------------===// diff --git a/mlir/test/Dialect/Standard/ops.mlir b/mlir/test/Dialect/Standard/ops.mlir --- a/mlir/test/Dialect/Standard/ops.mlir +++ b/mlir/test/Dialect/Standard/ops.mlir @@ -22,3 +22,13 @@ assert %arg, "Some message in case this assertion fails." return } + +func @test_clampi(%arg0 : i32, %arg1 : i32, %arg2 : i32) { + clampi %arg0 in (%arg1, %arg2) : i32 + return +} + +func @test_clampf(%arg0 : f32, %arg1 : f32, %arg2 : f32) { + clampf %arg0 in (%arg1, %arg2) : f32 + return +}