44 HWExportModuleHierarchyPass> {
49 void printHierarchy(hw::InstanceOp &inst, SymbolTable &symbolTable,
50 llvm::json::OStream &j,
51 SmallVectorImpl<Attribute> &symbols,
unsigned &
id);
54 llvm::raw_ostream &os,
55 SmallVectorImpl<Attribute> &symbols);
62 hw::InstanceOp &inst, SymbolTable &symbolTable, llvm::json::OStream &j,
63 SmallVectorImpl<Attribute> &symbols,
unsigned &
id) {
65 auto innerSym = inst.getInnerSymAttr();
68 innerSym = hw::InnerSymAttr::get(
69 StringAttr::get(inst.getContext(), ns.newName(inst.getInstanceName())));
70 inst->setAttr(
"inner_sym", innerSym);
74 j.attribute(
"instance_name", (
"{{" + Twine(
id++) +
"}}").str());
75 symbols.push_back(hw::InnerRefAttr::get(moduleOp.getModuleNameAttr(),
76 innerSym.getSymName()));
77 j.attribute(
"module_name", (
"{{" + Twine(
id++) +
"}}").str());
78 symbols.push_back(inst.getModuleNameAttr());
79 j.attributeArray(
"instances", [&] {
83 symbolTable.lookup(inst.getModuleNameAttr().getValue());
84 if (
auto module = dyn_cast<hw::HWModuleOp>(nextModuleOp)) {
85 for (
auto op :
module.getOps<hw::InstanceOp>()) {
86 if (!op.getDoNotPrint())
87 printHierarchy(op, symbolTable, j, symbols, id);
97 hw::HWModuleOp op, SymbolTable &symbolTable, llvm::raw_ostream &os,
98 SmallVectorImpl<Attribute> &symbols) {
99 llvm::json::OStream j(os, 2);
104 j.attribute(
"instance_name",
"{{0}}");
105 j.attribute(
"module_name",
"{{0}}");
106 symbols.push_back(FlatSymbolRefAttr::get(op.getNameAttr()));
107 j.attributeArray(
"instances", [&] {
109 for (
auto op : op.getOps<hw::InstanceOp>())
118 mlir::ModuleOp mlirModule = getOperation();
119 std::optional<SymbolTable *> symbolTable;
122 auto attr = op->getAttrOfType<ArrayAttr>(
"firrtl.moduleHierarchyFile");
125 for (
auto file : attr.getAsRange<hw::OutputFileAttr>()) {
127 symbolTable = &getAnalysis<SymbolTable>();
129 std::string jsonBuffer;
130 llvm::raw_string_ostream os(jsonBuffer);
131 SmallVector<Attribute> symbols;
135 auto builder = ImplicitLocOpBuilder::atBlockEnd(
136 UnknownLoc::get(mlirModule.getContext()), mlirModule.getBody());
137 builder.create<emit::FileOp>(file.getFilename(), [&] {
138 builder.create<sv::VerbatimOp>(jsonBuffer, ValueRange{},
139 builder.getArrayAttr(symbols));
144 markAllAnalysesPreserved();
void printHierarchy(hw::InstanceOp &inst, SymbolTable &symbolTable, llvm::json::OStream &j, SmallVectorImpl< Attribute > &symbols, unsigned &id)
Recursively print the module hierarchy as serialized as JSON.
void extractHierarchyFromTop(hw::HWModuleOp op, SymbolTable &symbolTable, llvm::raw_ostream &os, SmallVectorImpl< Attribute > &symbols)
Return the JSON-serialized module hierarchy for the given module as the top of the hierarchy.