1 //===- TestPolynomialApproximation.cpp - Test math ops approximations -----===//
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 contains test passes for expanding math operations into
10 // polynomial approximations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
15 #include "mlir/Dialect/Math/IR/Math.h"
16 #include "mlir/Dialect/Math/Transforms/Passes.h"
17 #include "mlir/Dialect/Vector/VectorOps.h"
18 #include "mlir/Dialect/X86Vector/X86VectorDialect.h"
19 #include "mlir/Pass/Pass.h"
20 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
21 
22 using namespace mlir;
23 
24 namespace {
25 struct TestMathPolynomialApproximationPass
26     : public PassWrapper<TestMathPolynomialApproximationPass, FunctionPass> {
27   TestMathPolynomialApproximationPass() = default;
28   TestMathPolynomialApproximationPass(
29       const TestMathPolynomialApproximationPass &pass) {}
30 
31   void runOnFunction() override;
32   void getDependentDialects(DialectRegistry &registry) const override {
33     registry.insert<arith::ArithmeticDialect, math::MathDialect,
34                     vector::VectorDialect>();
35     if (enableAvx2)
36       registry.insert<x86vector::X86VectorDialect>();
37   }
38   StringRef getArgument() const final {
39     return "test-math-polynomial-approximation";
40   }
41   StringRef getDescription() const final {
42     return "Test math polynomial approximations";
43   }
44 
45   Option<bool> enableAvx2{
46       *this, "enable-avx2",
47       llvm::cl::desc("Enable approximations that emit AVX2 intrinsics via the "
48                      "X86Vector dialect"),
49       llvm::cl::init(false)};
50 };
51 } // namespace
52 
53 void TestMathPolynomialApproximationPass::runOnFunction() {
54   RewritePatternSet patterns(&getContext());
55   MathPolynomialApproximationOptions approx_options;
56   approx_options.enableAvx2 = enableAvx2;
57   populateMathPolynomialApproximationPatterns(patterns, approx_options);
58   (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns));
59 }
60 
61 namespace mlir {
62 namespace test {
63 void registerTestMathPolynomialApproximationPass() {
64   PassRegistration<TestMathPolynomialApproximationPass>();
65 }
66 } // namespace test
67 } // namespace mlir
68