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