12 #include "mlir/IR/BuiltinOps.h"
14 using namespace circt;
15 using 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 auto name = op->getAttrOfType<StringAttr>(
"sym_name");
50 return dyn_cast_or_null<hw::HierPathOp>(n);
55 return dyn_cast_or_null<FModuleLike>(n);
59 symToOp[nla.getSymNameAttr()] = nla;
60 for (
auto ent : nla.getNamepath()) {
61 if (
auto mod = dyn_cast<FlatSymbolRefAttr>(ent))
62 nodeMap[mod.getAttr()].push_back(nla);
63 else if (
auto inr = dyn_cast<hw::InnerRefAttr>(ent))
64 nodeMap[inr.getModule()].push_back(nla);
69 symToOp.erase(nla.getSymNameAttr());
70 for (
auto ent : nla.getNamepath())
71 if (
auto mod = dyn_cast<FlatSymbolRefAttr>(ent))
72 llvm::erase(
nodeMap[mod.getAttr()], nla);
73 else if (
auto inr = dyn_cast<hw::InnerRefAttr>(ent))
74 llvm::erase(
nodeMap[inr.getModule()], nla);
76 symbolTable->erase(nla);
80 StringAttr newModule) {
81 nlaOp.updateModule(oldModule, newModule);
82 auto &nlas =
nodeMap[oldModule];
83 auto *iter = std::find(nlas.begin(), nlas.end(), nlaOp);
84 if (iter != nlas.end()) {
88 nodeMap[newModule].push_back(nlaOp);
93 StringAttr newModule) {
101 auto op =
symToOp.find(oldModName);
104 auto iter =
nodeMap.find(oldModName);
107 for (
auto nla : iter->second)
108 nla.updateModule(oldModName, newModName);
109 nodeMap[newModName] = iter->second;
111 symToOp[newModName] = op->second;
116 StringAttr newModName, StringAttr oldModName,
117 const DenseMap<StringAttr, StringAttr> &innerSymRenameMap) {
119 if (newModName == oldModName)
121 for (
auto nla :
lookup(oldModName)) {
122 nla.updateModuleAndInnerRef(oldModName, newModName, innerSymRenameMap);
123 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.