Skip to content

Commit

Permalink
[LowerTypes] Copy discardable attributes when cloning operations (#7948)
Browse files Browse the repository at this point in the history
This PR changes to clone discardable attributes when operation is cloned. 

Fix #7934. AttributeAnnotation adds `sv.attributes` in LowerAnnotation as a discardable attribute which previously LowerTypes dropped.
  • Loading branch information
uenoku authored Dec 5, 2024
1 parent 79c2d41 commit d912e07
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
14 changes: 4 additions & 10 deletions lib/Dialect/FIRRTL/Transforms/LowerTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,9 @@ bool TypeLoweringVisitor::lowerProducer(
newOp->setAttr(cache.nameAttr, StringAttr::get(context, loweredName));
if (nameKindAttr)
newOp->setAttr(cache.nameKindAttr, nameKindAttr);

// Clone discardable attributes as well.
newOp->setDiscardableAttrs(op->getDiscardableAttrDictionary());
}
lowered.push_back(newVal);
}
Expand Down Expand Up @@ -1502,16 +1505,7 @@ bool TypeLoweringVisitor::visitDecl(InstanceOp op) {
op.getLowerToBindAttr(),
sym ? hw::InnerSymAttr::get(sym) : hw::InnerSymAttr());

// Copy over any attributes which have not already been copied over by
// arguments to the builder.
auto attrNames = InstanceOp::getAttributeNames();
DenseSet<StringRef> attrSet(attrNames.begin(), attrNames.end());
SmallVector<NamedAttribute> newAttrs(newInstance->getAttrs());
for (auto i : llvm::make_filter_range(op->getAttrs(), [&](auto namedAttr) {
return !attrSet.count(namedAttr.getName());
}))
newAttrs.push_back(i);
newInstance->setAttrs(newAttrs);
newInstance->setDiscardableAttrs(op->getDiscardableAttrDictionary());

SmallVector<Value> lowered;
for (size_t aggIndex = 0, eAgg = op.getNumResults(); aggIndex != eAgg;
Expand Down
15 changes: 15 additions & 0 deletions test/Dialect/FIRRTL/lower-types.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1422,3 +1422,18 @@ firrtl.circuit "MemoryPrefixCopying" {
} : !firrtl.bundle<addr: uint<6>, en: uint<1>, clk: clock, data flip: vector<uint<8>, 1>>
}
}


firrtl.circuit "DiscardableAttributes" {
// COMMON-LABEL: firrtl.module @DiscardableAttributes
firrtl.module @DiscardableAttributes(in %clock: !firrtl.clock, in %reset: !firrtl.uint<1>, in %a: !firrtl.bundle<a: uint<1>>) {
// CHECK-NEXT: %node_a = firrtl.node %a_a {foo}
// CHECK-NEXT: %wire_a = firrtl.wire {foo = "bar"}
// CHECK-NEXT: %reg_a = firrtl.reg %clock {foo}
// CHECK-NEXT: %regreset_a = firrtl.regreset %clock, %reset, %a_a {foo}
%node = firrtl.node %a {foo}: !firrtl.bundle<a: uint<1>>
%wire = firrtl.wire {foo = "bar"} : !firrtl.bundle<a: uint<1>>
%reg = firrtl.reg %clock {foo}: !firrtl.clock, !firrtl.bundle<a: uint<1>>
%regreset = firrtl.regreset %clock, %reset, %a {foo}: !firrtl.clock, !firrtl.uint<1>, !firrtl.bundle<a: uint<1>>, !firrtl.bundle<a: uint<1>>
}
}

0 comments on commit d912e07

Please sign in to comment.