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/IR/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, OperationPass<>> {
27   TestMathPolynomialApproximationPass() = default;
28   TestMathPolynomialApproximationPass(
29       const TestMathPolynomialApproximationPass &pass)
30       : PassWrapper(pass) {}
31 
32   void runOnOperation() override;
33   void getDependentDialects(DialectRegistry &registry) const override {
34     registry.insert<arith::ArithmeticDialect, math::MathDialect,
35                     vector::VectorDialect>();
36     if (enableAvx2)
37       registry.insert<x86vector::X86VectorDialect>();
38   }
39   StringRef getArgument() const final {
40     return "test-math-polynomial-approximation";
41   }
42   StringRef getDescription() const final {
43     return "Test math polynomial approximations";
44   }
45 
46   Option<bool> enableAvx2{
47       *this, "enable-avx2",
48       llvm::cl::desc("Enable approximations that emit AVX2 intrinsics via the "
49                      "X86Vector dialect"),
50       llvm::cl::init(false)};
51 };
52 } // namespace
53 
54 void TestMathPolynomialApproximationPass::runOnOperation() {
55   RewritePatternSet patterns(&getContext());
56   MathPolynomialApproximationOptions approxOptions;
57   approxOptions.enableAvx2 = enableAvx2;
58   populateMathPolynomialApproximationPatterns(patterns, approxOptions);
59   (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns));
60 }
61 
62 namespace mlir {
63 namespace test {
64 void registerTestMathPolynomialApproximationPass() {
65   PassRegistration<TestMathPolynomialApproximationPass>();
66 }
67 } // namespace test
68 } // namespace mlir
69