CIRCT 22.0.0git
Loading...
Searching...
No Matches
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
14using namespace mlir;
15using namespace circt;
16
17//===----------------------------------------------------------------------===//
18// Reduction Patterns
19//===----------------------------------------------------------------------===//
20
21namespace {
22
23/// A sample reduction pattern that removes operations which either produce no
24/// results or their results have no users.
25struct OperationPruner : public Reduction {
26 void beforeReduction(mlir::ModuleOp module) override {
27 userMap = std::make_unique<SymbolUserMap>(table, module);
28 }
29 uint64_t match(Operation *op) override {
30 if (op->hasTrait<OpTrait::IsTerminator>())
31 return false;
32
33 return !isa<ModuleOp>(op) &&
34 (op->getNumResults() == 0 || op->use_empty()) &&
35 userMap->useEmpty(op);
36 }
37 LogicalResult rewrite(Operation *op) override {
38 reduce::pruneUnusedOps(op, *this);
39 return success();
40 }
41 std::string getName() const override { return "operation-pruner"; }
42
43 SymbolTableCollection table;
44 std::unique_ptr<SymbolUserMap> userMap;
45};
46
47/// Remove unused symbol ops.
48struct UnusedSymbolPruner : public Reduction {
49 void beforeReduction(mlir::ModuleOp op) override {
50 symbolTables = std::make_unique<SymbolTableCollection>();
51 symbolUsers = std::make_unique<SymbolUserMap>(*symbolTables, op);
52 }
53
54 uint64_t match(Operation *op) override {
55 return isa<SymbolOpInterface>(op) && symbolUsers->useEmpty(op);
56 }
57
58 LogicalResult rewrite(Operation *op) override {
59 op->erase();
60 return success();
61 }
62
63 std::string getName() const override { return "unused-symbol-pruner"; }
64
65 std::unique_ptr<SymbolTableCollection> symbolTables;
66 std::unique_ptr<SymbolUserMap> symbolUsers;
67};
68
69} // namespace
70
71//===----------------------------------------------------------------------===//
72// Reduction Registration
73//===----------------------------------------------------------------------===//
74
75static std::unique_ptr<Pass> createSimpleCanonicalizerPass() {
76 GreedyRewriteConfig config;
77 config.setUseTopDownTraversal(true);
78 config.setRegionSimplificationLevel(
79 mlir::GreedySimplifyRegionLevel::Disabled);
80 return createCanonicalizerPass(config);
81}
82
83void circt::populateGenericReducePatterns(MLIRContext *context,
85 patterns.add<UnusedSymbolPruner, 40>();
86 patterns.add<PassReduction, 3>(context, createCSEPass());
88 patterns.add<OperationPruner, 1>();
89}
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.
void populateGenericReducePatterns(MLIRContext *context, ReducePatternSet &patterns)
Populate reduction patterns that are not specific to certain operations or dialects.
std::unique_ptr< Pass > createSimpleCanonicalizerPass()
Create a simple canonicalizer pass.
Definition Passes.cpp:15
A reduction pattern that applies an mlir::Pass.
Definition Reduction.h:123
An abstract reduction pattern.
Definition Reduction.h:24
virtual LogicalResult rewrite(Operation *op)
Apply the reduction to a specific operation.
Definition Reduction.h:58
virtual uint64_t match(Operation *op)
Check if the reduction can apply to a specific operation.
Definition Reduction.h:41
virtual std::string getName() const =0
Return a human-readable name for this reduction pattern.
virtual void beforeReduction(mlir::ModuleOp)
Called before the reduction is applied to a new subset of operations.
Definition Reduction.h:30