12#include "mlir/IR/BuiltinOps.h"
15using namespace firrtl;
18 if (
auto mod = dyn_cast<mlir::ModuleOp>(operation))
19 for (
auto &op : *mod.getBody())
20 if ((operation = dyn_cast<CircuitOp>(&op)))
23 auto circuit = cast<CircuitOp>(operation);
26 for (
auto &op : *circuit.getBodyBlock()) {
27 if (
auto module = dyn_cast<FModuleLike>(op))
28 symToOp[module.getModuleNameAttr()] =
module;
29 if (
auto nla = dyn_cast<hw::HierPathOp>(op))
42 if (
auto symOp = dyn_cast<mlir::SymbolOpInterface>(op))
43 return lookup(symOp.getNameAttr());
49 return dyn_cast_or_null<hw::HierPathOp>(n);
54 return dyn_cast_or_null<FModuleLike>(n);
58 symToOp[nla.getSymNameAttr()] = nla;
59 for (
auto ent : nla.getNamepath()) {
60 if (
auto mod = dyn_cast<FlatSymbolRefAttr>(ent))
61 nodeMap[mod.getAttr()].push_back(nla);
62 else if (
auto inr = dyn_cast<hw::InnerRefAttr>(ent))
63 nodeMap[inr.getModule()].push_back(nla);
68 symToOp.erase(nla.getSymNameAttr());
69 for (
auto ent : nla.getNamepath())
70 if (
auto mod = dyn_cast<FlatSymbolRefAttr>(ent))
71 llvm::erase(
nodeMap[mod.getAttr()], nla);
72 else if (
auto inr = dyn_cast<hw::InnerRefAttr>(ent))
73 llvm::erase(
nodeMap[inr.getModule()], nla);
75 symbolTable->erase(nla);
79 StringAttr newModule) {
80 nlaOp.updateModule(oldModule, newModule);
81 auto &nlas =
nodeMap[oldModule];
82 auto *iter = std::find(nlas.begin(), nlas.end(), nlaOp);
83 if (iter != nlas.end()) {
87 nodeMap[newModule].push_back(nlaOp);
92 StringAttr newModule) {
100 auto op =
symToOp.find(oldModName);
103 auto iter =
nodeMap.find(oldModName);
106 for (
auto nla : iter->second)
107 nla.updateModule(oldModName, newModName);
108 nodeMap[newModName] = iter->second;
110 symToOp[newModName] = op->second;
115 StringAttr newModName, StringAttr oldModName,
116 const DenseMap<StringAttr, StringAttr> &innerSymRenameMap) {
118 if (newModName == oldModName)
120 for (
auto nla :
lookup(oldModName)) {
121 nla.updateModuleAndInnerRef(oldModName, newModName, innerSymRenameMap);
122 nodeMap[newModName].push_back(nla);
ArrayRef< hw::HierPathOp > lookup(Operation *op)
Lookup all NLAs an operation participates in.
void updateModuleInNLA(StringAttr nlaName, StringAttr oldModule, StringAttr newModule)
Replace the module oldModule with newModule in the namepath of the nla nlaName.
llvm::DenseMap< StringAttr, SmallVector< hw::HierPathOp, 4 > > nodeMap
Map modules to the NLA's that target them.
void renameModule(StringAttr oldModName, StringAttr newModName)
Rename a module, this updates the name to module tracking and the name to NLA tracking.
void addNLA(hw::HierPathOp nla)
Insert a new NLA.
void renameModuleAndInnerRef(StringAttr newModName, StringAttr oldModName, const DenseMap< StringAttr, StringAttr > &innerSymRenameMap)
Replace the module oldModName with newModName in the namepath of any NLA.
void erase(hw::HierPathOp nlaOp, SymbolTable *symbolTable=nullptr)
Remove the NLA from the analysis.
NLATable(Operation *operation)
Create a new NLA table of a circuit.
hw::HierPathOp getNLA(StringAttr name)
Resolve a symbol to an NLA.
llvm::DenseMap< StringAttr, Operation * > symToOp
Map symbol names to module and NLA operations.
FModuleLike getModule(StringAttr name)
Resolve a symbol to a Module.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.