From 16cb6ce554b7ed7b6f4df216ea4efcd782b00187 Mon Sep 17 00:00:00 2001 From: jacquesguan Date: Tue, 26 Jul 2022 21:56:43 +0800 Subject: [mlir][Math] Add constant folder for ExpM1Op. This patch adds constant folder for ExpM1Op which only supports single and double precision floating-point. Differential Revision: https://reviews.llvm.org/D130567 --- mlir/lib/Dialect/Math/IR/MathOps.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'mlir/lib/Dialect') diff --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp index 829db9160638..2ba640b796b2 100644 --- a/mlir/lib/Dialect/Math/IR/MathOps.cpp +++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp @@ -246,6 +246,24 @@ OpFoldResult math::Exp2Op::fold(ArrayRef operands) { }); } +//===----------------------------------------------------------------------===// +// ExpM1Op folder +//===----------------------------------------------------------------------===// + +OpFoldResult math::ExpM1Op::fold(ArrayRef operands) { + return constFoldUnaryOpConditional( + operands, [](const APFloat &a) -> Optional { + switch (a.getSizeInBits(a.getSemantics())) { + case 64: + return APFloat(expm1(a.convertToDouble())); + case 32: + return APFloat(expm1f(a.convertToFloat())); + default: + return {}; + } + }); +} + /// Materialize an integer or floating point constant. Operation *math::MathDialect::materializeConstant(OpBuilder &builder, Attribute value, Type type, -- cgit v1.2.3