The greedy rewriter is used in many different flows and it has a lot of
convenience (work list management, debugging actions, tracing, etc). But
it combines two kinds of greedy behavior 1) how ops are matched, 2)
folding wherever it can.
These are independent forms of greedy and leads to inefficiency. E.g.,
cases where one need to create different phases in lowering and is
required to applying patterns in specific order split across different
passes. Using the driver one ends up needlessly retrying folding/having
multiple rounds of folding attempts, where one final run would have
sufficed.
Of course folks can locally avoid this behavior by just building their
own, but this is also a common requested feature that folks keep on
working around locally in suboptimal ways.
For downstream users, there should be no behavioral change. Updating
from the deprecated should just be a find and replace (e.g., `find ./
-type f -exec sed -i
's|applyPatternsAndFoldGreedily|applyPatternsGreedily|g' {} \;` variety)
as the API arguments hasn't changed between the two.
53 lines
1.9 KiB
C++
53 lines
1.9 KiB
C++
//===- AlgebraicSimplification.cpp - Simplify algebraic expressions -------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
// This file defines a pass that applies algebraic simplifications
|
|
// to operations of Math/Complex/etc. dialects that are used by Flang.
|
|
// It is done as a Flang specific pass, because we may want to tune
|
|
// the parameters of the patterns for Fortran programs.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "flang/Optimizer/Transforms/Passes.h"
|
|
#include "mlir/Dialect/Math/IR/Math.h"
|
|
#include "mlir/Dialect/Math/Transforms/Passes.h"
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
|
|
|
|
namespace fir {
|
|
#define GEN_PASS_DEF_ALGEBRAICSIMPLIFICATION
|
|
#include "flang/Optimizer/Transforms/Passes.h.inc"
|
|
} // namespace fir
|
|
|
|
using namespace mlir;
|
|
|
|
namespace {
|
|
struct AlgebraicSimplification
|
|
: public fir::impl::AlgebraicSimplificationBase<AlgebraicSimplification> {
|
|
AlgebraicSimplification(const GreedyRewriteConfig &rewriteConfig) {
|
|
config = rewriteConfig;
|
|
}
|
|
|
|
void runOnOperation() override;
|
|
|
|
mlir::GreedyRewriteConfig config;
|
|
};
|
|
} // namespace
|
|
|
|
void AlgebraicSimplification::runOnOperation() {
|
|
RewritePatternSet patterns(&getContext());
|
|
populateMathAlgebraicSimplificationPatterns(patterns);
|
|
(void)applyPatternsGreedily(getOperation(), std::move(patterns), config);
|
|
}
|
|
|
|
std::unique_ptr<mlir::Pass> fir::createAlgebraicSimplificationPass() {
|
|
return std::make_unique<AlgebraicSimplification>(GreedyRewriteConfig());
|
|
}
|
|
|
|
std::unique_ptr<mlir::Pass> fir::createAlgebraicSimplificationPass(
|
|
const mlir::GreedyRewriteConfig &config) {
|
|
return std::make_unique<AlgebraicSimplification>(config);
|
|
}
|