diff --git a/lib/Conversion/FIRRTLToHW/LowerToHW.cpp b/lib/Conversion/FIRRTLToHW/LowerToHW.cpp index a9c3706420b7..3f6f6cb79358 100644 --- a/lib/Conversion/FIRRTLToHW/LowerToHW.cpp +++ b/lib/Conversion/FIRRTLToHW/LowerToHW.cpp @@ -1130,18 +1130,33 @@ FIRRTLModuleLowering::lowerModule(FModuleOp oldModule, Block *topLevelModule, auto nameAttr = builder.getStringAttr(oldModule.getName()); auto newModule = builder.create(oldModule.getLoc(), nameAttr, ports); - if (auto outputFile = oldModule->getAttr("output_file")) - newModule->setAttr("output_file", outputFile); + if (auto comment = oldModule->getAttrOfType("comment")) newModule.setCommentAttr(comment); - // Move SV attributes. - if (auto svAttrs = sv::getSVAttributes(oldModule)) - sv::setSVAttributes(newModule, svAttrs); - - // Pass along the number of random initialization bits needed for this module. - if (auto randomWidth = oldModule->getAttr("firrtl.random_init_width")) - newModule->setAttr("firrtl.random_init_width", randomWidth); + // Copy over any attributes which are not required for FModuleOp. + SmallVector attrNames = {"annotations", + "convention", + "portNames", + "sym_name", + "portDirections", + "portTypes", + "portAnnotations", + "portSyms", + "portLocations", + "parameters", + SymbolTable::getVisibilityAttrName()}; + + DenseSet attrSet(attrNames.begin(), attrNames.end()); + SmallVector newAttrs(newModule->getAttrs()); + for (auto i : + llvm::make_filter_range(oldModule->getAttrs(), [&](auto namedAttr) { + return !attrSet.count(namedAttr.getName()) && + !newModule->getAttrDictionary().contains(namedAttr.getName()); + })) + newAttrs.push_back(i); + + newModule->setAttrs(newAttrs); // If the circuit has an entry point, set all other modules private. // Otherwise, mark all modules as public. diff --git a/test/Conversion/FIRRTLToHW/lower-to-hw.mlir b/test/Conversion/FIRRTLToHW/lower-to-hw.mlir index 0c6f6b3df4ef..2eec85e3f4f2 100644 --- a/test/Conversion/FIRRTLToHW/lower-to-hw.mlir +++ b/test/Conversion/FIRRTLToHW/lower-to-hw.mlir @@ -609,10 +609,13 @@ firrtl.circuit "Simple" attributes {annotations = [{class = } - // CHECK-LABEL: hw.module private @output_fileTest + // CHECK-LABEL: hw.module private @attributes_preservation + // CHECK-SAME: firrtl.foo = "bar" // CHECK-SAME: output_file = #hw.output_file<"output_fileTest.sv", excludeFromFileList> - firrtl.module private @output_fileTest() attributes { - output_file = #hw.output_file<"output_fileTest.sv", excludeFromFileList >} { + firrtl.module private @attributes_preservation() attributes { + firrtl.foo = "bar", + output_file = #hw.output_file<"output_fileTest.sv", excludeFromFileList > + } { } // https://github.com/llvm/circt/issues/314