CIRCT 22.0.0git
Loading...
Searching...
No Matches
EmitReductions.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
14#include "mlir/IR/SymbolTable.h"
15#include "llvm/Support/Debug.h"
16
17#define DEBUG_TYPE "emit-reductions"
18
19using namespace circt;
20using namespace emit;
21
22//===----------------------------------------------------------------------===//
23// Reduction patterns
24//===----------------------------------------------------------------------===//
25
26namespace {
27
28/// A reduction pattern that erases emit dialect operations.
29struct EmitOpEraser : public Reduction {
30 void beforeReduction(mlir::ModuleOp op) override {
31 innerSymUses = reduce::InnerSymbolUses(op);
32 }
33
34 uint64_t match(Operation *op) override {
35 if (!isa<emit::EmitDialect>(op->getDialect()))
36 return 0;
37 if (innerSymUses.hasRef(op))
38 return 0;
39 return 1;
40 }
41
42 LogicalResult rewrite(Operation *op) override {
43 op->erase();
44 return success();
45 }
46
47 std::string getName() const override { return "emit-op-eraser"; }
48 bool acceptSizeIncrease() const override { return true; }
49
50 reduce::InnerSymbolUses innerSymUses;
51};
52
53} // namespace
54
55//===----------------------------------------------------------------------===//
56// Reduction Registration
57//===----------------------------------------------------------------------===//
58
59namespace {
60/// A dialect interface to provide reduction patterns to a reducer tool.
61struct EmitReducePatternDialectInterface
65 patterns.add<EmitOpEraser, 1000>();
66 }
67};
68} // namespace
69
70void emit::registerReducePatternDialectInterface(
71 mlir::DialectRegistry &registry) {
72 registry.addExtension(+[](MLIRContext *ctx, EmitDialect *dialect) {
73 dialect->addInterfaces<EmitReducePatternDialectInterface>();
74 });
75}
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition emit.py:1
A dialect interface to provide reduction patterns to a reducer tool.
Definition Reduction.h:180
ReducePatternDialectInterface(Dialect *dialect)
Definition Reduction.h:181
virtual void populateReducePatterns(ReducePatternSet &patterns) const =0
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 bool acceptSizeIncrease() const
Return true if the tool should accept the transformation this reduction performs on the module even i...
Definition Reduction.h:79
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
A helper struct that scans a root operation and all its nested operations for InnerRefAttrs.