From a5cae20bdb189dcd043c41e2076dc0df3a55b771 Mon Sep 17 00:00:00 2001 From: jacquesguan Date: Thu, 14 Jul 2022 15:43:16 +0800 Subject: [PATCH] [mlir][Math] Add constant folder for Log10Op. This patch adds constant folder for Log10Op which only support single and double precision floating-point. Reviewed By: Mogball Differential Revision: https://reviews.llvm.org/D129740 --- mlir/include/mlir/Dialect/Math/IR/MathOps.td | 1 + mlir/lib/Dialect/Math/IR/MathOps.cpp | 21 +++++++++++++++++++++ mlir/test/Dialect/Math/canonicalize.mlir | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td index 94103ff..163e687 100644 --- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td +++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td @@ -517,6 +517,7 @@ def Math_Log10Op : Math_FloatUnaryOp<"log10"> { %y = math.log10 %x : f64 ``` }]; + let hasFolder = 1; } //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp index ac6b1ae..b389f43 100644 --- a/mlir/lib/Dialect/Math/IR/MathOps.cpp +++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp @@ -108,6 +108,27 @@ OpFoldResult math::Log2Op::fold(ArrayRef operands) { } //===----------------------------------------------------------------------===// +// Log10Op folder +//===----------------------------------------------------------------------===// + +OpFoldResult math::Log10Op::fold(ArrayRef operands) { + return constFoldUnaryOpConditional( + operands, [](const APFloat &a) -> Optional { + if (a.isNegative()) + return {}; + + switch (a.getSizeInBits(a.getSemantics())) { + case 64: + return APFloat(log10(a.convertToDouble())); + case 32: + return APFloat(log10f(a.convertToFloat())); + default: + return {}; + } + }); +} + +//===----------------------------------------------------------------------===// // PowFOp folder //===----------------------------------------------------------------------===// diff --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir index b78e3ad3..0aabd62 100644 --- a/mlir/test/Dialect/Math/canonicalize.mlir +++ b/mlir/test/Dialect/Math/canonicalize.mlir @@ -174,3 +174,21 @@ func.func @ctpop_fold() -> i32 { %r = math.ctpop %c : i32 return %r : i32 } + +// CHECK-LABEL: @log10_fold +// CHECK: %[[cst:.+]] = arith.constant 2.000000e+00 : f32 + // CHECK: return %[[cst]] +func.func @log10_fold() -> f32 { + %c = arith.constant 100.0 : f32 + %r = math.log10 %c : f32 + return %r : f32 +} + +// CHECK-LABEL: @log10_fold_vec +// CHECK: %[[cst:.+]] = arith.constant dense<[0.000000e+00, 1.000000e+00, 2.000000e+00, 2.301030e+00]> : vector<4xf32> +// CHECK: return %[[cst]] +func.func @log10_fold_vec() -> (vector<4xf32>) { + %v1 = arith.constant dense<[1.0, 10.0, 100.0, 200.0]> : vector<4xf32> + %0 = math.log10 %v1 : vector<4xf32> + return %0 : vector<4xf32> +} -- 2.7.4