1 //===- MathOps.cpp - MLIR operations for math implementation --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
10 #include "mlir/Dialect/CommonFolders.h"
11 #include "mlir/Dialect/Math/IR/Math.h"
12 #include "mlir/IR/Builders.h"
13 
14 using namespace mlir;
15 using namespace mlir::math;
16 
17 //===----------------------------------------------------------------------===//
18 // TableGen'd op method definitions
19 //===----------------------------------------------------------------------===//
20 
21 #define GET_OP_CLASSES
22 #include "mlir/Dialect/Math/IR/MathOps.cpp.inc"
23 
24 //===----------------------------------------------------------------------===//
25 // AbsOp folder
26 //===----------------------------------------------------------------------===//
27 
28 OpFoldResult math::AbsOp::fold(ArrayRef<Attribute> operands) {
29   return constFoldUnaryOp<FloatAttr>(operands, [](const APFloat &a) {
30     const APFloat &result(a);
31     return abs(result);
32   });
33 }
34 
35 //===----------------------------------------------------------------------===//
36 // CeilOp folder
37 //===----------------------------------------------------------------------===//
38 
39 OpFoldResult math::CeilOp::fold(ArrayRef<Attribute> operands) {
40   return constFoldUnaryOp<FloatAttr>(operands, [](const APFloat &a) {
41     APFloat result(a);
42     result.roundToIntegral(llvm::RoundingMode::TowardPositive);
43     return result;
44   });
45 }
46 
47 //===----------------------------------------------------------------------===//
48 // CopySignOp folder
49 //===----------------------------------------------------------------------===//
50 
51 OpFoldResult math::CopySignOp::fold(ArrayRef<Attribute> operands) {
52   return constFoldBinaryOp<FloatAttr>(operands,
53                                       [](const APFloat &a, const APFloat &b) {
54                                         APFloat result(a);
55                                         result.copySign(b);
56                                         return result;
57                                       });
58 }
59 
60 //===----------------------------------------------------------------------===//
61 // CountLeadingZerosOp folder
62 //===----------------------------------------------------------------------===//
63 
64 OpFoldResult math::CountLeadingZerosOp::fold(ArrayRef<Attribute> operands) {
65   return constFoldUnaryOp<IntegerAttr>(operands, [](const APInt &a) {
66     return APInt(a.getBitWidth(), a.countLeadingZeros());
67   });
68 }
69 
70 //===----------------------------------------------------------------------===//
71 // CountTrailingZerosOp folder
72 //===----------------------------------------------------------------------===//
73 
74 OpFoldResult math::CountTrailingZerosOp::fold(ArrayRef<Attribute> operands) {
75   return constFoldUnaryOp<IntegerAttr>(operands, [](const APInt &a) {
76     return APInt(a.getBitWidth(), a.countTrailingZeros());
77   });
78 }
79 
80 //===----------------------------------------------------------------------===//
81 // CtPopOp folder
82 //===----------------------------------------------------------------------===//
83 
84 OpFoldResult math::CtPopOp::fold(ArrayRef<Attribute> operands) {
85   return constFoldUnaryOp<IntegerAttr>(operands, [](const APInt &a) {
86     return APInt(a.getBitWidth(), a.countPopulation());
87   });
88 }
89 
90 //===----------------------------------------------------------------------===//
91 // Log2Op folder
92 //===----------------------------------------------------------------------===//
93 
94 OpFoldResult math::Log2Op::fold(ArrayRef<Attribute> operands) {
95   auto constOperand = operands.front();
96   if (!constOperand)
97     return {};
98 
99   auto attr = constOperand.dyn_cast<FloatAttr>();
100   if (!attr)
101     return {};
102 
103   auto ft = getType().cast<FloatType>();
104 
105   APFloat apf = attr.getValue();
106 
107   if (apf.isNegative())
108     return {};
109 
110   if (ft.getWidth() == 64)
111     return FloatAttr::get(getType(), log2(apf.convertToDouble()));
112 
113   if (ft.getWidth() == 32)
114     return FloatAttr::get(getType(), log2f(apf.convertToFloat()));
115 
116   return {};
117 }
118 
119 //===----------------------------------------------------------------------===//
120 // PowFOp folder
121 //===----------------------------------------------------------------------===//
122 
123 OpFoldResult math::PowFOp::fold(ArrayRef<Attribute> operands) {
124   return constFoldBinaryOpConditional<FloatAttr>(
125       operands, [](const APFloat &a, const APFloat &b) -> Optional<APFloat> {
126         if (a.getSizeInBits(a.getSemantics()) == 64 &&
127             b.getSizeInBits(b.getSemantics()) == 64)
128           return APFloat(pow(a.convertToDouble(), b.convertToDouble()));
129 
130         if (a.getSizeInBits(a.getSemantics()) == 32 &&
131             b.getSizeInBits(b.getSemantics()) == 32)
132           return APFloat(powf(a.convertToFloat(), b.convertToFloat()));
133 
134         return {};
135       });
136 }
137 
138 OpFoldResult math::SqrtOp::fold(ArrayRef<Attribute> operands) {
139   auto constOperand = operands.front();
140   if (!constOperand)
141     return {};
142 
143   auto attr = constOperand.dyn_cast<FloatAttr>();
144   if (!attr)
145     return {};
146 
147   auto ft = getType().cast<FloatType>();
148 
149   APFloat apf = attr.getValue();
150 
151   if (apf.isNegative())
152     return {};
153 
154   if (ft.getWidth() == 64)
155     return FloatAttr::get(getType(), sqrt(apf.convertToDouble()));
156 
157   if (ft.getWidth() == 32)
158     return FloatAttr::get(getType(), sqrtf(apf.convertToFloat()));
159 
160   return {};
161 }
162 
163 /// Materialize an integer or floating point constant.
164 Operation *math::MathDialect::materializeConstant(OpBuilder &builder,
165                                                   Attribute value, Type type,
166                                                   Location loc) {
167   return builder.create<arith::ConstantOp>(loc, value, type);
168 }
169