Skip to content

Commit

Permalink
[LowerToHW] Pass through attributes on FModuleOp (#6400)
Browse files Browse the repository at this point in the history
This commit allows users to propagate attributes on modules to HW dialects. External passes hooked by pass plugin might want to control the passes based on annotation values but currently unhandled annotations or unknown attributes are not attached to lowered hw modules.
  • Loading branch information
uenoku authored and mikeurbach committed Dec 21, 2023
1 parent c823db7 commit 168bae9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
33 changes: 24 additions & 9 deletions lib/Conversion/FIRRTLToHW/LowerToHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,18 +1130,33 @@ FIRRTLModuleLowering::lowerModule(FModuleOp oldModule, Block *topLevelModule,
auto nameAttr = builder.getStringAttr(oldModule.getName());
auto newModule =
builder.create<hw::HWModuleOp>(oldModule.getLoc(), nameAttr, ports);
if (auto outputFile = oldModule->getAttr("output_file"))
newModule->setAttr("output_file", outputFile);

if (auto comment = oldModule->getAttrOfType<StringAttr>("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<StringRef, 12> attrNames = {"annotations",
"convention",
"portNames",
"sym_name",
"portDirections",
"portTypes",
"portAnnotations",
"portSyms",
"portLocations",
"parameters",
SymbolTable::getVisibilityAttrName()};

DenseSet<StringRef> attrSet(attrNames.begin(), attrNames.end());
SmallVector<NamedAttribute> 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.
Expand Down
9 changes: 6 additions & 3 deletions test/Conversion/FIRRTLToHW/lower-to-hw.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 168bae9

Please sign in to comment.