1 | //===- AlgebraicSimplification.cpp - Simplify algebraic expressions -------===// |
---|---|
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 | // This file defines a pass that applies algebraic simplifications |
9 | // to operations of Math/Complex/etc. dialects that are used by Flang. |
10 | // It is done as a Flang specific pass, because we may want to tune |
11 | // the parameters of the patterns for Fortran programs. |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #include "flang/Optimizer/Transforms/Passes.h" |
15 | #include "mlir/Dialect/Math/IR/Math.h" |
16 | #include "mlir/Dialect/Math/Transforms/Passes.h" |
17 | #include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
18 | |
19 | namespace fir { |
20 | #define GEN_PASS_DEF_ALGEBRAICSIMPLIFICATION |
21 | #include "flang/Optimizer/Transforms/Passes.h.inc" |
22 | } // namespace fir |
23 | |
24 | using namespace mlir; |
25 | |
26 | namespace { |
27 | struct AlgebraicSimplification |
28 | : public fir::impl::AlgebraicSimplificationBase<AlgebraicSimplification> { |
29 | AlgebraicSimplification(const GreedyRewriteConfig &rewriteConfig) { |
30 | config = rewriteConfig; |
31 | } |
32 | |
33 | void runOnOperation() override; |
34 | |
35 | mlir::GreedyRewriteConfig config; |
36 | }; |
37 | } // namespace |
38 | |
39 | void AlgebraicSimplification::runOnOperation() { |
40 | RewritePatternSet patterns(&getContext()); |
41 | populateMathAlgebraicSimplificationPatterns(patterns); |
42 | (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns), |
43 | config); |
44 | } |
45 | |
46 | std::unique_ptr<mlir::Pass> fir::createAlgebraicSimplificationPass() { |
47 | return std::make_unique<AlgebraicSimplification>(args: GreedyRewriteConfig()); |
48 | } |
49 | |
50 | std::unique_ptr<mlir::Pass> fir::createAlgebraicSimplificationPass( |
51 | const mlir::GreedyRewriteConfig &config) { |
52 | return std::make_unique<AlgebraicSimplification>(args: config); |
53 | } |
54 |