CIRCT  20.0.0git
Reduction.cpp
Go to the documentation of this file.
1 //===- Reduction.cpp - Reductions for circt-reduce ------------------------===//
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 //
9 // This file defines datastructures to handle reduction patterns.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "circt/Reduce/Reduction.h"
15 #include "circt/Dialect/HW/HWOps.h"
16 #include "mlir/Transforms/Passes.h"
17 #include "llvm/Support/Debug.h"
18 
19 #define DEBUG_TYPE "circt-reduce"
20 
21 using namespace circt;
22 
23 //===----------------------------------------------------------------------===//
24 // Reduction
25 //===----------------------------------------------------------------------===//
26 
27 Reduction::~Reduction() = default;
28 
29 //===----------------------------------------------------------------------===//
30 // Pass Reduction
31 //===----------------------------------------------------------------------===//
32 
33 PassReduction::PassReduction(MLIRContext *context, std::unique_ptr<Pass> pass,
34  bool canIncreaseSize, bool oneShot)
35  : context(context), canIncreaseSize(canIncreaseSize), oneShot(oneShot) {
36  passName = pass->getArgument();
37  if (passName.empty())
38  passName = pass->getName();
39 
40  pm = std::make_unique<mlir::PassManager>(
41  context, "builtin.module", mlir::OpPassManager::Nesting::Explicit);
42  auto opName = pass->getOpName();
43  if (opName && *opName == "firrtl.circuit")
44  pm->nest<firrtl::CircuitOp>().addPass(std::move(pass));
45  else if (opName && *opName == "firrtl.module")
46  pm->nest<firrtl::CircuitOp>().nest<firrtl::FModuleOp>().addPass(
47  std::move(pass));
48  else if (opName && *opName == "hw.module")
49  pm->nest<hw::HWModuleOp>().addPass(std::move(pass));
50  else
51  pm->addPass(std::move(pass));
52 }
53 
54 uint64_t PassReduction::match(Operation *op) {
55  return op->getName() == pm->getOpName(*context);
56 }
57 
58 LogicalResult PassReduction::rewrite(Operation *op) { return pm->run(op); }
59 
60 std::string PassReduction::getName() const { return passName.str(); }
61 
62 //===----------------------------------------------------------------------===//
63 // ReducePatternSet
64 //===----------------------------------------------------------------------===//
65 
67  const std::function<bool(const Reduction &)> &pred) {
68  for (auto *iter = reducePatternsWithBenefit.begin();
69  iter != reducePatternsWithBenefit.end(); ++iter) {
70  if (!pred(*iter->first))
71  reducePatternsWithBenefit.erase(iter--);
72  }
73 }
74 
76  llvm::stable_sort(reducePatternsWithBenefit,
77  [](const auto &pairA, const auto &pairB) {
78  return pairA.second > pairB.second;
79  });
80 }
81 
82 size_t ReducePatternSet::size() const {
83  return reducePatternsWithBenefit.size();
84 }
85 
87  return *reducePatternsWithBenefit[idx].first;
88 }
89 
90 //===----------------------------------------------------------------------===//
91 // ReducePatternInterfaceCollection
92 //===----------------------------------------------------------------------===//
93 
95  ReducePatternSet &patterns) const {
96  for (const ReducePatternDialectInterface &interface : *this)
98 }
SmallVector< std::pair< std::unique_ptr< Reduction >, unsigned > > reducePatternsWithBenefit
Definition: Reduction.h:132
void filter(const std::function< bool(const Reduction &)> &pred)
Definition: Reduction.cpp:66
size_t size() const
Definition: Reduction.cpp:82
Reduction & operator[](size_t idx) const
Definition: Reduction.cpp:86
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
StringRef passName
Definition: Reduction.h:111
LogicalResult rewrite(Operation *op) override
Apply the reduction to a specific operation.
Definition: Reduction.cpp:58
uint64_t match(Operation *op) override
Check if the reduction can apply to a specific operation.
Definition: Reduction.cpp:54
MLIRContext *const context
Definition: Reduction.h:109
std::string getName() const override
Return a human-readable name for this reduction pattern.
Definition: Reduction.cpp:60
std::unique_ptr< mlir::PassManager > pm
Definition: Reduction.h:110
PassReduction(MLIRContext *context, std::unique_ptr< Pass > pass, bool canIncreaseSize=false, bool oneShot=false)
Definition: Reduction.cpp:33
A dialect interface to provide reduction patterns to a reducer tool.
Definition: Reduction.h:137
virtual void populateReducePatterns(ReducePatternSet &patterns) const =0
void populateReducePatterns(ReducePatternSet &patterns) const
Definition: Reduction.cpp:94
An abstract reduction pattern.
Definition: Reduction.h:24
virtual ~Reduction()