From 98a3bfaf875431eeaabe6743a7e068666743d5a0 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Sat, 12 Jul 2025 22:48:39 -0700 Subject: [PATCH] add baseline signi/signf ops --- mlir/include/mlir/Dialect/Math/IR/MathOps.td | 39 ++++++++++++++++++++ mlir/lib/Dialect/Math/IR/MathOps.cpp | 28 ++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td index 56370388dea87..50992fea6f09f 100644 --- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td +++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td @@ -437,6 +437,45 @@ def Math_CoshOp : Math_FloatUnaryOp<"cosh"> { let hasFolder = 1; } +//===----------------------------------------------------------------------===// +// SignIOp +//===----------------------------------------------------------------------===// + +def Math_SignIOp : Math_IntegerUnaryOp<"signi"> { + let summary = "ternary-valued sign of an integer"; + let description = [{ + The `signi` operation computes -1 if the operand is negative, 0 if it is + zero, and 1 if it is positive. + + Example: + + ```mlir + %a = math.signi %b : i64 + ``` + }]; + let hasFolder = 1; +} + + +//===----------------------------------------------------------------------===// +// SignFOp +//===----------------------------------------------------------------------===// + +def Math_SignFOp : Math_FloatUnaryOp<"signf"> { + let summary = "ternary-valued sign of a floating point value"; + let description = [{ + The `signf` operation computes -1.0 if the operand is negative, 0.0 if it + is zero, 1.0 if it is positive, and `NaN` if the operand is `NaN`. + + Example: + + ```mlir + %a = math.signf %b : f32 + ``` + }]; + let hasFolder = 1; +} + //===----------------------------------------------------------------------===// // SinOp //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp index 26441a9d78658..fb3cd7daf2f27 100644 --- a/mlir/lib/Dialect/Math/IR/MathOps.cpp +++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp @@ -266,6 +266,34 @@ OpFoldResult math::SinOp::fold(FoldAdaptor adaptor) { }); } +//===----------------------------------------------------------------------===// +// SignIOp folder +//===----------------------------------------------------------------------===// + +OpFoldResult math::SignIOp::fold(FoldAdaptor adaptor) { + return constFoldUnaryOp( + adaptor.getOperands(), [](const APInt &a) { + return a.isNegative() ? APInt(a.getBitWidth(), -1) + : (a.isZero() ? APInt(a.getBitWidth(), 0) + : APInt(a.getBitWidth(), 1)); + }); +} + +//===----------------------------------------------------------------------===// +// SignFOp folder +//===----------------------------------------------------------------------===// + +OpFoldResult math::SignFOp::fold(FoldAdaptor adaptor) { + return constFoldUnaryOp( + adaptor.getOperands(), [](const APFloat &a) { + if (a.isNaN()) + return APFloat::getNaN(a.getSemantics()); + return a.isNegative() ? APFloat(a.getSemantics(), -1) + : (a.isZero() ? APFloat(a.getSemantics(), 0) + : APFloat(a.getSemantics(), 1)); + }); +} + //===----------------------------------------------------------------------===// // SinhOp folder //===----------------------------------------------------------------------===//