1 //===- PolynomialApproximation.cpp - Approximate math operations ----------===//
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 // This file implements expansion of math operations to fast approximations
10 // that do not rely on any of the library functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
15 #include "mlir/Dialect/LLVMIR/LLVMTypes.h"
16 #include "mlir/Dialect/Math/IR/Math.h"
17 #include "mlir/Dialect/Math/Transforms/Passes.h"
18 #include "mlir/Dialect/Vector/VectorOps.h"
19 #include "mlir/IR/Builders.h"
20 #include "mlir/IR/ImplicitLocOpBuilder.h"
21 #include "mlir/Transforms/Bufferize.h"
22 #include "mlir/Transforms/DialectConversion.h"
23 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
24 #include <climits>
25 
26 using namespace mlir;
27 using namespace mlir::vector;
28 
29 using TypePredicate = llvm::function_ref<bool(Type)>;
30 
31 // Returns vector width if the element type is matching the predicate (scalars
32 // that do match the predicate have width equal to `1`).
33 static Optional<int> vectorWidth(Type type, TypePredicate pred) {
34   // If the type matches the predicate then its width is `1`.
35   if (pred(type))
36     return 1;
37 
38   // Otherwise check if the type is a vector type.
39   auto vectorType = type.dyn_cast<VectorType>();
40   if (vectorType && pred(vectorType.getElementType())) {
41     assert(vectorType.getRank() == 1 && "only 1d vectors are supported");
42     return vectorType.getDimSize(0);
43   }
44 
45   return llvm::None;
46 }
47 
48 // Returns vector width of the type. If the type is a scalar returns `1`.
49 static int vectorWidth(Type type) {
50   auto vectorType = type.dyn_cast<VectorType>();
51   return vectorType ? vectorType.getDimSize(0) : 1;
52 }
53 
54 // Returns vector element type. If the type is a scalar returns the argument.
55 LLVM_ATTRIBUTE_UNUSED static Type elementType(Type type) {
56   auto vectorType = type.dyn_cast<VectorType>();
57   return vectorType ? vectorType.getElementType() : type;
58 }
59 
60 LLVM_ATTRIBUTE_UNUSED static bool isF32(Type type) { return type.isF32(); }
61 
62 LLVM_ATTRIBUTE_UNUSED static bool isI32(Type type) {
63   return type.isInteger(32);
64 }
65 
66 //----------------------------------------------------------------------------//
67 // Broadcast scalar types and values into vector types and values.
68 //----------------------------------------------------------------------------//
69 
70 // Broadcasts scalar type into vector type (iff width is greater then 1).
71 static Type broadcast(Type type, int width) {
72   assert(!type.isa<VectorType>() && "must be scalar type");
73   return width > 1 ? VectorType::get({width}, type) : type;
74 }
75 
76 // Broadcasts scalar value into vector (iff width is greater then 1).
77 static Value broadcast(ImplicitLocOpBuilder &builder, Value value, int width) {
78   assert(!value.getType().isa<VectorType>() && "must be scalar value");
79   auto type = broadcast(value.getType(), width);
80   return width > 1 ? builder.create<BroadcastOp>(type, value) : value;
81 }
82 
83 //----------------------------------------------------------------------------//
84 // Helper functions to create constants.
85 //----------------------------------------------------------------------------//
86 
87 static Value f32Cst(ImplicitLocOpBuilder &builder, float value) {
88   return builder.create<ConstantOp>(builder.getF32Type(),
89                                     builder.getF32FloatAttr(value));
90 }
91 
92 static Value i32Cst(ImplicitLocOpBuilder &builder, int32_t value) {
93   return builder.create<ConstantOp>(builder.getI32Type(),
94                                     builder.getI32IntegerAttr(value));
95 }
96 
97 static Value f32FromBits(ImplicitLocOpBuilder &builder, uint32_t bits) {
98   Value i32Value = i32Cst(builder, static_cast<int32_t>(bits));
99   return builder.create<LLVM::BitcastOp>(builder.getF32Type(), i32Value);
100 }
101 
102 //----------------------------------------------------------------------------//
103 // Helper functions to build math functions approximations.
104 //----------------------------------------------------------------------------//
105 
106 static Value min(ImplicitLocOpBuilder &builder, Value a, Value b) {
107   return builder.create<SelectOp>(
108       builder.create<CmpFOp>(CmpFPredicate::OLT, a, b), a, b);
109 }
110 
111 static Value max(ImplicitLocOpBuilder &builder, Value a, Value b) {
112   return builder.create<SelectOp>(
113       builder.create<CmpFOp>(CmpFPredicate::OGT, a, b), a, b);
114 }
115 
116 static Value clamp(ImplicitLocOpBuilder &builder, Value value, Value lowerBound,
117                    Value upperBound) {
118   return max(builder, min(builder, value, upperBound), lowerBound);
119 }
120 
121 // Decomposes given floating point value `arg` into a normalized fraction and
122 // an integral power of two (see std::frexp). Returned values have float type.
123 static std::pair<Value, Value> frexp(ImplicitLocOpBuilder &builder, Value arg,
124                                      bool is_positive = false) {
125   assert(isF32(elementType(arg.getType())) && "argument must be f32 type");
126 
127   int width = vectorWidth(arg.getType());
128 
129   auto bcast = [&](Value value) -> Value {
130     return broadcast(builder, value, width);
131   };
132 
133   auto i32 = builder.getIntegerType(32);
134   auto i32Vec = broadcast(i32, width);
135   auto f32Vec = broadcast(builder.getF32Type(), width);
136 
137   Value cst126f = f32Cst(builder, 126.0f);
138   Value cstHalf = f32Cst(builder, 0.5f);
139   Value cstInvMantMask = f32FromBits(builder, ~0x7f800000u);
140 
141   // Bitcast to i32 for bitwise operations.
142   Value i32Half = builder.create<LLVM::BitcastOp>(i32, cstHalf);
143   Value i32InvMantMask = builder.create<LLVM::BitcastOp>(i32, cstInvMantMask);
144   Value i32Arg = builder.create<LLVM::BitcastOp>(i32Vec, arg);
145 
146   // Compute normalized fraction.
147   Value tmp0 = builder.create<LLVM::AndOp>(i32Arg, bcast(i32InvMantMask));
148   Value tmp1 = builder.create<LLVM::OrOp>(tmp0, bcast(i32Half));
149   Value normalizedFraction = builder.create<LLVM::BitcastOp>(f32Vec, tmp1);
150 
151   // Compute exponent.
152   Value arg0 = is_positive ? arg : builder.create<AbsFOp>(arg);
153   Value biasedExponentBits = builder.create<UnsignedShiftRightOp>(
154       builder.create<LLVM::BitcastOp>(i32Vec, arg0),
155       bcast(i32Cst(builder, 23)));
156   Value biasedExponent = builder.create<SIToFPOp>(f32Vec, biasedExponentBits);
157   Value exponent = builder.create<SubFOp>(biasedExponent, bcast(cst126f));
158 
159   return {normalizedFraction, exponent};
160 }
161 
162 // Computes exp2 for an i32 argument.
163 static Value exp2I32(ImplicitLocOpBuilder &builder, Value arg) {
164   assert(isI32(elementType(arg.getType())) && "argument must be i32 type");
165 
166   int width = vectorWidth(arg.getType());
167 
168   auto bcast = [&](Value value) -> Value {
169     return broadcast(builder, value, width);
170   };
171 
172   auto f32Vec = broadcast(builder.getF32Type(), width);
173   // The exponent of f32 located at 23-bit.
174   auto exponetBitLocation = bcast(i32Cst(builder, 23));
175   // Set the exponent bias to zero.
176   auto bias = bcast(i32Cst(builder, 127));
177 
178   Value biasedArg = builder.create<AddIOp>(arg, bias);
179   Value exp2ValueInt =
180       builder.create<ShiftLeftOp>(biasedArg, exponetBitLocation);
181   Value exp2ValueF32 = builder.create<LLVM::BitcastOp>(f32Vec, exp2ValueInt);
182 
183   return exp2ValueF32;
184 }
185 
186 //----------------------------------------------------------------------------//
187 // TanhOp approximation.
188 //----------------------------------------------------------------------------//
189 
190 namespace {
191 struct TanhApproximation : public OpRewritePattern<math::TanhOp> {
192 public:
193   using OpRewritePattern::OpRewritePattern;
194 
195   LogicalResult matchAndRewrite(math::TanhOp op,
196                                 PatternRewriter &rewriter) const final;
197 };
198 } // namespace
199 
200 LogicalResult
201 TanhApproximation::matchAndRewrite(math::TanhOp op,
202                                    PatternRewriter &rewriter) const {
203   auto width = vectorWidth(op.operand().getType(), isF32);
204   if (!width.hasValue())
205     return rewriter.notifyMatchFailure(op, "unsupported operand type");
206 
207   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
208   auto bcast = [&](Value value) -> Value {
209     return broadcast(builder, value, *width);
210   };
211 
212   // Clamp operand into [plusClamp, minusClamp] range.
213   Value minusClamp = bcast(f32Cst(builder, -7.9053111076354980f));
214   Value plusClamp = bcast(f32Cst(builder, 7.90531110763549805f));
215   Value x = clamp(builder, op.operand(), minusClamp, plusClamp);
216 
217   // Mask for tiny values that are approximated with `operand`.
218   Value tiny = bcast(f32Cst(builder, 0.0004f));
219   Value tinyMask = builder.create<CmpFOp>(
220       CmpFPredicate::OLT, builder.create<AbsFOp>(op.operand()), tiny);
221 
222   // The monomial coefficients of the numerator polynomial (odd).
223   Value alpha1 = bcast(f32Cst(builder, 4.89352455891786e-03f));
224   Value alpha3 = bcast(f32Cst(builder, 6.37261928875436e-04f));
225   Value alpha5 = bcast(f32Cst(builder, 1.48572235717979e-05f));
226   Value alpha7 = bcast(f32Cst(builder, 5.12229709037114e-08f));
227   Value alpha9 = bcast(f32Cst(builder, -8.60467152213735e-11f));
228   Value alpha11 = bcast(f32Cst(builder, 2.00018790482477e-13f));
229   Value alpha13 = bcast(f32Cst(builder, -2.76076847742355e-16f));
230 
231   // The monomial coefficients of the denominator polynomial (even).
232   Value beta0 = bcast(f32Cst(builder, 4.89352518554385e-03f));
233   Value beta2 = bcast(f32Cst(builder, 2.26843463243900e-03f));
234   Value beta4 = bcast(f32Cst(builder, 1.18534705686654e-04f));
235   Value beta6 = bcast(f32Cst(builder, 1.19825839466702e-06f));
236 
237   // Since the polynomials are odd/even, we need x^2.
238   Value x2 = builder.create<MulFOp>(x, x);
239 
240   // Evaluate the numerator polynomial p.
241   Value p = builder.create<FmaFOp>(x2, alpha13, alpha11);
242   p = builder.create<FmaFOp>(x2, p, alpha9);
243   p = builder.create<FmaFOp>(x2, p, alpha7);
244   p = builder.create<FmaFOp>(x2, p, alpha5);
245   p = builder.create<FmaFOp>(x2, p, alpha3);
246   p = builder.create<FmaFOp>(x2, p, alpha1);
247   p = builder.create<MulFOp>(x, p);
248 
249   // Evaluate the denominator polynomial q.
250   Value q = builder.create<FmaFOp>(x2, beta6, beta4);
251   q = builder.create<FmaFOp>(x2, q, beta2);
252   q = builder.create<FmaFOp>(x2, q, beta0);
253 
254   // Divide the numerator by the denominator.
255   Value res =
256       builder.create<SelectOp>(tinyMask, x, builder.create<DivFOp>(p, q));
257 
258   rewriter.replaceOp(op, res);
259 
260   return success();
261 }
262 
263 #define LN2_VALUE                                                              \
264   0.693147180559945309417232121458176568075500134360255254120680009493393621L
265 #define LOG2E_VALUE                                                            \
266   1.442695040888963407359924681001892137426645954152985934135449406931109219L
267 
268 //----------------------------------------------------------------------------//
269 // LogOp and Log2Op approximation.
270 //----------------------------------------------------------------------------//
271 
272 namespace {
273 template <typename Op>
274 struct LogApproximationBase : public OpRewritePattern<Op> {
275   using OpRewritePattern<Op>::OpRewritePattern;
276 
277   /// Base 2 if 'base2' is set; natural logarithm (base e) otherwise.
278   LogicalResult logMatchAndRewrite(Op op, PatternRewriter &rewriter,
279                                    bool base2) const;
280 };
281 } // namespace
282 
283 // This approximation comes from Julien Pommier's SSE math library.
284 // Link: http://gruntthepeon.free.fr/ssemath
285 template <typename Op>
286 LogicalResult
287 LogApproximationBase<Op>::logMatchAndRewrite(Op op, PatternRewriter &rewriter,
288                                              bool base2) const {
289   auto width = vectorWidth(op.operand().getType(), isF32);
290   if (!width.hasValue())
291     return rewriter.notifyMatchFailure(op, "unsupported operand type");
292 
293   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
294   auto bcast = [&](Value value) -> Value {
295     return broadcast(builder, value, *width);
296   };
297 
298   Value cstZero = bcast(f32Cst(builder, 0.0f));
299   Value cstOne = bcast(f32Cst(builder, 1.0f));
300   Value cstNegHalf = bcast(f32Cst(builder, -0.5f));
301 
302   // The smallest non denormalized float number.
303   Value cstMinNormPos = bcast(f32FromBits(builder, 0x00800000u));
304   Value cstMinusInf = bcast(f32FromBits(builder, 0xff800000u));
305   Value cstPosInf = bcast(f32FromBits(builder, 0x7f800000u));
306   Value cstNan = bcast(f32FromBits(builder, 0x7fc00000));
307 
308   // Polynomial coefficients.
309   Value cstCephesSQRTHF = bcast(f32Cst(builder, 0.707106781186547524f));
310   Value cstCephesLogP0 = bcast(f32Cst(builder, 7.0376836292E-2f));
311   Value cstCephesLogP1 = bcast(f32Cst(builder, -1.1514610310E-1f));
312   Value cstCephesLogP2 = bcast(f32Cst(builder, 1.1676998740E-1f));
313   Value cstCephesLogP3 = bcast(f32Cst(builder, -1.2420140846E-1f));
314   Value cstCephesLogP4 = bcast(f32Cst(builder, +1.4249322787E-1f));
315   Value cstCephesLogP5 = bcast(f32Cst(builder, -1.6668057665E-1f));
316   Value cstCephesLogP6 = bcast(f32Cst(builder, +2.0000714765E-1f));
317   Value cstCephesLogP7 = bcast(f32Cst(builder, -2.4999993993E-1f));
318   Value cstCephesLogP8 = bcast(f32Cst(builder, +3.3333331174E-1f));
319 
320   Value x = op.operand();
321 
322   // Truncate input values to the minimum positive normal.
323   x = max(builder, x, cstMinNormPos);
324 
325   // Extract significant in the range [0.5,1) and exponent.
326   std::pair<Value, Value> pair = frexp(builder, x, /*is_positive=*/true);
327   x = pair.first;
328   Value e = pair.second;
329 
330   // Shift the inputs from the range [0.5,1) to [sqrt(1/2), sqrt(2)) and shift
331   // by -1.0. The values are then centered around 0, which improves the
332   // stability of the polynomial evaluation:
333   //
334   //   if( x < SQRTHF ) {
335   //     e -= 1;
336   //     x = x + x - 1.0;
337   //   } else { x = x - 1.0; }
338   Value mask = builder.create<CmpFOp>(CmpFPredicate::OLT, x, cstCephesSQRTHF);
339   Value tmp = builder.create<SelectOp>(mask, x, cstZero);
340 
341   x = builder.create<SubFOp>(x, cstOne);
342   e = builder.create<SubFOp>(e,
343                              builder.create<SelectOp>(mask, cstOne, cstZero));
344   x = builder.create<AddFOp>(x, tmp);
345 
346   Value x2 = builder.create<MulFOp>(x, x);
347   Value x3 = builder.create<MulFOp>(x2, x);
348 
349   // Evaluate the polynomial approximant of degree 8 in three parts.
350   Value y0, y1, y2;
351   y0 = builder.create<FmaFOp>(cstCephesLogP0, x, cstCephesLogP1);
352   y1 = builder.create<FmaFOp>(cstCephesLogP3, x, cstCephesLogP4);
353   y2 = builder.create<FmaFOp>(cstCephesLogP6, x, cstCephesLogP7);
354   y0 = builder.create<FmaFOp>(y0, x, cstCephesLogP2);
355   y1 = builder.create<FmaFOp>(y1, x, cstCephesLogP5);
356   y2 = builder.create<FmaFOp>(y2, x, cstCephesLogP8);
357   y0 = builder.create<FmaFOp>(y0, x3, y1);
358   y0 = builder.create<FmaFOp>(y0, x3, y2);
359   y0 = builder.create<MulFOp>(y0, x3);
360 
361   y0 = builder.create<FmaFOp>(cstNegHalf, x2, y0);
362   x = builder.create<AddFOp>(x, y0);
363 
364   if (base2) {
365     Value cstLog2e = bcast(f32Cst(builder, static_cast<float>(LOG2E_VALUE)));
366     x = builder.create<FmaFOp>(x, cstLog2e, e);
367   } else {
368     Value cstLn2 = bcast(f32Cst(builder, static_cast<float>(LN2_VALUE)));
369     x = builder.create<FmaFOp>(e, cstLn2, x);
370   }
371 
372   Value invalidMask =
373       builder.create<CmpFOp>(CmpFPredicate::ULT, op.operand(), cstZero);
374   Value zeroMask =
375       builder.create<CmpFOp>(CmpFPredicate::OEQ, op.operand(), cstZero);
376   Value posInfMask =
377       builder.create<CmpFOp>(CmpFPredicate::OEQ, op.operand(), cstPosInf);
378 
379   // Filter out invalid values:
380   //  • x == 0     -> -INF
381   //  • x < 0      ->  NAN
382   //  • x == +INF  -> +INF
383   Value aproximation = builder.create<SelectOp>(
384       zeroMask, cstMinusInf,
385       builder.create<SelectOp>(
386           invalidMask, cstNan,
387           builder.create<SelectOp>(posInfMask, cstPosInf, x)));
388 
389   rewriter.replaceOp(op, aproximation);
390 
391   return success();
392 }
393 
394 namespace {
395 struct LogApproximation : public LogApproximationBase<math::LogOp> {
396   using LogApproximationBase::LogApproximationBase;
397 
398   LogicalResult matchAndRewrite(math::LogOp op,
399                                 PatternRewriter &rewriter) const final {
400     return logMatchAndRewrite(op, rewriter, /*base2=*/false);
401   }
402 };
403 } // namespace
404 
405 namespace {
406 struct Log2Approximation : public LogApproximationBase<math::Log2Op> {
407   using LogApproximationBase::LogApproximationBase;
408 
409   LogicalResult matchAndRewrite(math::Log2Op op,
410                                 PatternRewriter &rewriter) const final {
411     return logMatchAndRewrite(op, rewriter, /*base2=*/true);
412   }
413 };
414 } // namespace
415 
416 //----------------------------------------------------------------------------//
417 // Exp approximation.
418 //----------------------------------------------------------------------------//
419 
420 namespace {
421 
422 struct ExpApproximation : public OpRewritePattern<math::ExpOp> {
423 public:
424   using OpRewritePattern::OpRewritePattern;
425 
426   LogicalResult matchAndRewrite(math::ExpOp op,
427                                 PatternRewriter &rewriter) const final;
428 };
429 } // namespace
430 
431 // Approximate exp(x) using its reduced range exp(y) where y is in the range
432 // [0, ln(2)], let y = x - floor(x / ln(2)) * ln(2) = x - k * ln(2), exp(x)
433 // = exp(y) * 2^k. exp(y).
434 LogicalResult
435 ExpApproximation::matchAndRewrite(math::ExpOp op,
436                                   PatternRewriter &rewriter) const {
437   auto width = vectorWidth(op.operand().getType(), isF32);
438   if (!width.hasValue())
439     return rewriter.notifyMatchFailure(op, "unsupported operand type");
440   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
441 
442   // TODO: Consider a common pattern rewriter with all methods below to
443   // write the approximations.
444   auto bcast = [&](Value value) -> Value {
445     return broadcast(builder, value, *width);
446   };
447   auto fmla = [&](Value a, Value b, Value c) {
448     return builder.create<FmaFOp>(a, b, c);
449   };
450   auto mul = [&](Value a, Value b) -> Value {
451     return builder.create<MulFOp>(a, b);
452   };
453   auto sub = [&](Value a, Value b) -> Value {
454     return builder.create<SubFOp>(a, b);
455   };
456   auto floor = [&](Value a) { return builder.create<FloorFOp>(a); };
457 
458   Value cstLn2 = bcast(f32Cst(builder, static_cast<float>(LN2_VALUE)));
459   Value cstLog2E = bcast(f32Cst(builder, static_cast<float>(LOG2E_VALUE)));
460 
461   // Polynomial coefficients.
462   Value cstCephesExpP0 = bcast(f32Cst(builder, 1.0));
463   Value cstCephesExpP1 = bcast(f32Cst(builder, 1.0));
464   Value cstCephesExpP2 = bcast(f32Cst(builder, 0.49970514590562437052f));
465   Value cstCephesExpP3 = bcast(f32Cst(builder, 0.16873890085469545053f));
466   Value cstCephesExpP4 = bcast(f32Cst(builder, 0.03668965196652099192f));
467   Value cstCephesExpP5 = bcast(f32Cst(builder, 0.01314350012789660196f));
468 
469   Value x = op.operand();
470 
471   // Reduced y = x - floor(x / ln(2)) * ln(2) = x - k * ln(2)
472   Value xL2Inv = mul(x, cstLog2E);
473   Value kF32 = floor(xL2Inv);
474   Value kLn2 = mul(kF32, cstLn2);
475   Value y = sub(x, kLn2);
476 
477   // Use Estrin's evaluation scheme with 3 independent parts:
478   // P(y)^y : (c0 + c1 y) + (c2 + c3 y) y^2 + (c4 + c5 y) y^4
479   Value y2 = mul(y, y);
480   Value y4 = mul(y2, y2);
481 
482   Value q0 = fmla(cstCephesExpP1, y, cstCephesExpP0);
483   Value q1 = fmla(cstCephesExpP3, y, cstCephesExpP2);
484   Value q2 = fmla(cstCephesExpP5, y, cstCephesExpP4);
485   Value expY = fmla(q1, y2, q0);
486   expY = fmla(q2, y4, expY);
487 
488   auto i32Vec = broadcast(builder.getI32Type(), *width);
489 
490   // exp2(k)
491   Value k = builder.create<FPToSIOp>(kF32, i32Vec);
492   Value exp2KValue = exp2I32(builder, k);
493 
494   // exp(x) = exp(y) * exp2(k)
495   expY = mul(expY, exp2KValue);
496 
497   // Handle overflow, inf and underflow of exp(x). exp(x) range is [0, inf], its
498   // partitioned as the following:
499   // exp(x) = 0, x <= -inf
500   // exp(x) = underflow (min_float), x <= -88
501   // exp(x) = inf (min_float), x >= 88
502   // Note: |k| = 127 is the value where the 8-bits exponent saturates.
503   Value zerof32Const = bcast(f32Cst(builder, 0));
504   auto constPosInfinity =
505       bcast(f32Cst(builder, std::numeric_limits<float>::infinity()));
506   auto constNegIfinity =
507       bcast(f32Cst(builder, -std::numeric_limits<float>::infinity()));
508   auto underflow = bcast(f32Cst(builder, std::numeric_limits<float>::min()));
509 
510   Value kMaxConst = bcast(i32Cst(builder, 127));
511   Value kMaxNegConst = bcast(i32Cst(builder, -127));
512   Value rightBound = builder.create<CmpIOp>(CmpIPredicate::sle, k, kMaxConst);
513   Value leftBound = builder.create<CmpIOp>(CmpIPredicate::sge, k, kMaxNegConst);
514 
515   Value isNegInfinityX =
516       builder.create<CmpFOp>(CmpFPredicate::OEQ, x, constNegIfinity);
517   Value isPostiveX =
518       builder.create<CmpFOp>(CmpFPredicate::OGT, x, zerof32Const);
519   Value isComputable = builder.create<AndOp>(rightBound, leftBound);
520 
521   expY = builder.create<SelectOp>(
522       isComputable, expY,
523       builder.create<SelectOp>(
524           isPostiveX, constPosInfinity,
525           builder.create<SelectOp>(isNegInfinityX, zerof32Const, underflow)));
526 
527   rewriter.replaceOp(op, expY);
528 
529   return success();
530 }
531 
532 //----------------------------------------------------------------------------//
533 
534 void mlir::populateMathPolynomialApproximationPatterns(
535     RewritePatternSet &patterns) {
536   patterns.add<TanhApproximation, LogApproximation, Log2Approximation,
537                ExpApproximation>(patterns.getContext());
538 }
539