CIRCT  20.0.0git
GenericReductions.cpp
Go to the documentation of this file.
1 //===- GenericReductions.cpp - Generic Reduction patterns -----------------===//
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 
11 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
12 #include "mlir/Transforms/Passes.h"
13 
14 using namespace mlir;
15 using namespace circt;
16 
17 //===----------------------------------------------------------------------===//
18 // Reduction Patterns
19 //===----------------------------------------------------------------------===//
20 
21 /// A sample reduction pattern that removes operations which either produce no
22 /// results or their results have no users.
23 struct OperationPruner : public Reduction {
24  void beforeReduction(mlir::ModuleOp module) override {
25  userMap = std::make_unique<SymbolUserMap>(table, module);
26  }
27  uint64_t match(Operation *op) override {
28  if (op->hasTrait<OpTrait::IsTerminator>())
29  return false;
30 
31  return !isa<ModuleOp>(op) &&
32  (op->getNumResults() == 0 || op->use_empty()) &&
33  userMap->useEmpty(op);
34  }
35  LogicalResult rewrite(Operation *op) override {
36  reduce::pruneUnusedOps(op, *this);
37  return success();
38  }
39  std::string getName() const override { return "operation-pruner"; }
40 
41  SymbolTableCollection table;
42  std::unique_ptr<SymbolUserMap> userMap;
43 };
44 
45 //===----------------------------------------------------------------------===//
46 // Reduction Registration
47 //===----------------------------------------------------------------------===//
48 
49 static std::unique_ptr<Pass> createSimpleCanonicalizerPass() {
50  GreedyRewriteConfig config;
51  config.useTopDownTraversal = true;
52  config.enableRegionSimplification = mlir::GreedySimplifyRegionLevel::Disabled;
53  return createCanonicalizerPass(config);
54 }
55 
56 void circt::populateGenericReducePatterns(MLIRContext *context,
58  patterns.add<PassReduction, 3>(context, createCSEPass());
60  patterns.add<OperationPruner, 1>();
61 }
static std::unique_ptr< Pass > createSimpleCanonicalizerPass()
void pruneUnusedOps(Operation *initialOp, Reduction &reduction)
Starting at the given op, traverse through it and its operands and erase operations that have no more...
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
void populateGenericReducePatterns(MLIRContext *context, ReducePatternSet &patterns)
Populate reduction patterns that are not specific to certain operations or dialects.
A sample reduction pattern that removes operations which either produce no results or their results h...
LogicalResult rewrite(Operation *op) override
Apply the reduction to a specific operation.
uint64_t match(Operation *op) override
Check if the reduction can apply to a specific operation.
std::unique_ptr< SymbolUserMap > userMap
void beforeReduction(mlir::ModuleOp module) override
Called before the reduction is applied to a new subset of operations.
std::string getName() const override
Return a human-readable name for this reduction pattern.
SymbolTableCollection table
A reduction pattern that applies an mlir::Pass.
Definition: Reduction.h:99
An abstract reduction pattern.
Definition: Reduction.h:24