CIRCT  20.0.0git
InnerSymbolDCE.cpp
Go to the documentation of this file.
1 //===- InnerSymbolDCE.cpp - Delete Unused Inner Symbols----------*- C++ -*-===//
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 // This pass removes inner symbols which have no uses.
9 //
10 //===----------------------------------------------------------------------===//
11 
15 #include "mlir/IR/BuiltinOps.h"
16 #include "mlir/IR/Threading.h"
17 #include "mlir/Pass/Pass.h"
18 #include "llvm/Support/Debug.h"
19 
20 #define DEBUG_TYPE "firrtl-inner-symbol-dce"
21 
22 namespace circt {
23 namespace firrtl {
24 #define GEN_PASS_DEF_INNERSYMBOLDCE
25 #include "circt/Dialect/FIRRTL/Passes.h.inc"
26 } // namespace firrtl
27 } // namespace circt
28 
29 using namespace mlir;
30 using namespace circt;
31 using namespace firrtl;
32 using namespace hw;
33 
34 /// Drop the specified symbol.
35 /// Belongs in InnerSymbolTable, need to move port symbols accessors
36 /// into HW(ModuleLike) or perhaps a new inner-symbol interface that
37 /// dialects can optionally elect to use on their "ModuleLike"s.
38 /// For now, since InnerSymbolDCE is FIRRTL-only, define this here.
39 static void dropSymbol(const InnerSymTarget &target) {
40  assert(target);
41  assert(InnerSymbolTable::getInnerSymbol(target));
42 
43  if (target.isPort()) {
44  auto mod = cast<FModuleLike>(target.getOp());
45  assert(target.getPort() < mod.getNumPorts());
46  auto base = mod.getPortSymbolAttr(target.getPort());
47  cast<firrtl::FModuleLike>(*mod).setPortSymbolsAttr(
48  target.getPort(), base.erase(target.getField()));
49  return;
50  }
51 
52  auto symOp = cast<InnerSymbolOpInterface>(target.getOp());
53  auto base = symOp.getInnerSymAttr();
54  symOp.setInnerSymbolAttr(base.erase(target.getField()));
55 }
56 
58  : public circt::firrtl::impl::InnerSymbolDCEBase<InnerSymbolDCEPass> {
59  void runOnOperation() override;
60 
61 private:
62  void findInnerRefs(Attribute attr);
63  void insertInnerRef(InnerRefAttr innerRef);
64  void removeInnerSyms(FModuleLike mod);
65 
66  DenseSet<std::pair<StringAttr, StringAttr>> innerRefs;
67 };
68 
69 /// Find all InnerRefAttrs inside a given Attribute.
70 void InnerSymbolDCEPass::findInnerRefs(Attribute attr) {
71  // Check if this Attribute or any sub-Attributes are InnerRefAttrs.
72  attr.walk([&](Attribute subAttr) {
73  if (auto innerRef = dyn_cast<InnerRefAttr>(subAttr))
74  insertInnerRef(innerRef);
75  });
76 }
77 
78 /// Add an InnerRefAttr to the set of all InnerRefAttrs.
79 void InnerSymbolDCEPass::insertInnerRef(InnerRefAttr innerRef) {
80  StringAttr moduleName = innerRef.getModule();
81  StringAttr symName = innerRef.getName();
82 
83  // Track total inner refs found.
84  ++numInnerRefsFound;
85 
86  auto [iter, inserted] = innerRefs.insert({moduleName, symName});
87  if (!inserted)
88  return;
89 
90  LLVM_DEBUG(llvm::dbgs() << DEBUG_TYPE << ": found reference to " << moduleName
91  << "::" << symName << '\n';);
92 }
93 
94 /// Remove all dead inner symbols from the specified module.
95 void InnerSymbolDCEPass::removeInnerSyms(FModuleLike mod) {
96  auto moduleName = mod.getModuleNameAttr();
97 
98  // Walk inner symbols, removing any not referenced.
99  InnerSymbolTable::walkSymbols(
100  mod, [&](StringAttr name, const InnerSymTarget &target) {
101  ++numInnerSymbolsFound;
102 
103  // Check if the name is referenced by any InnerRef.
104  if (innerRefs.contains({moduleName, name}))
105  return;
106 
107  dropSymbol(target);
108  ++numInnerSymbolsRemoved;
109 
110  LLVM_DEBUG(llvm::dbgs() << DEBUG_TYPE << ": removed " << moduleName
111  << "::" << name << '\n';);
112  });
113 }
114 
116  // Run on the top-level ModuleOp to include any VerbatimOps that aren't
117  // wrapped in a CircuitOp.
118  ModuleOp topModule = getOperation();
119 
120  // Traverse the entire IR once.
121  SmallVector<FModuleLike> modules;
122  topModule.walk([&](Operation *op) {
123  // Find all InnerRefAttrs.
124  for (NamedAttribute namedAttr : op->getAttrs())
125  findInnerRefs(namedAttr.getValue());
126 
127  // Collect all FModuleLike operations.
128  if (auto mod = dyn_cast<FModuleLike>(op))
129  modules.push_back(mod);
130  });
131 
132  // Traverse all FModuleOps in parallel, removing any InnerSymAttrs that are
133  // dead code.
134  parallelForEach(&getContext(), modules,
135  [&](FModuleLike mod) { removeInnerSyms(mod); });
136 }
137 
138 std::unique_ptr<mlir::Pass> circt::firrtl::createInnerSymbolDCEPass() {
139  return std::make_unique<InnerSymbolDCEPass>();
140 }
assert(baseType &&"element must be base type")
static void dropSymbol(const InnerSymTarget &target)
Drop the specified symbol.
#define DEBUG_TYPE
The target of an inner symbol, the entity the symbol is a handle for.
Operation * getOp() const
Return the target's base operation. For ports, this is the module.
auto getField() const
Return the target's fieldID.
auto getPort() const
Return the target's port, if valid. Check "isPort()".
bool isPort() const
Return if this targets a port.
std::unique_ptr< mlir::Pass > createInnerSymbolDCEPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: hw.py:1
void removeInnerSyms(FModuleLike mod)
Remove all dead inner symbols from the specified module.
void findInnerRefs(Attribute attr)
Find all InnerRefAttrs inside a given Attribute.
void runOnOperation() override
void insertInnerRef(InnerRefAttr innerRef)
Add an InnerRefAttr to the set of all InnerRefAttrs.
DenseSet< std::pair< StringAttr, StringAttr > > innerRefs