Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] [Comb][Canonicalize] Best effort dialect attribute propagation #6768

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/Dialect/Comb/CombFolds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,21 @@ static void replaceOpAndCopyName(PatternRewriter &rewriter, Operation *op,
/// A wrapper of `PatternRewriter::replaceOpWithNewOp` to propagate
/// "sv.namehint" attribute. If a replaced op has a "sv.namehint" attribute,
/// this function propagates the name to the new value.
template <typename OpTy, typename... Args>
static OpTy replaceOpWithNewOpAndCopyName(PatternRewriter &rewriter,
Operation *op, Args &&...args) {
auto name = op->getAttrOfType<StringAttr>("sv.namehint");
template <typename NewOpTy, typename OldOpTy, typename... Args>
static NewOpTy replaceOpWithNewOpAndCopyName(PatternRewriter &rewriter,
OldOpTy op, Args &&...args) {
auto newOp =
rewriter.replaceOpWithNewOp<OpTy>(op, std::forward<Args>(args)...);
if (name && !newOp->hasAttr("sv.namehint"))
rewriter.modifyOpInPlace(newOp,
[&] { newOp->setAttr("sv.namehint", name); });
rewriter.replaceOpWithNewOp<NewOpTy>(op, std::forward<Args>(args)...);

if constexpr (std::is_same<NewOpTy, OldOpTy>::value) {
rewriter.modifyOpInPlace(
newOp, [&] { newOp->setDialectAttrs(op->getDialectAttrs()); });
} else {
auto name = op->template getAttrOfType<StringAttr>("sv.namehint");
if (name && !newOp->hasAttr("sv.namehint"))
rewriter.modifyOpInPlace(newOp,
[&] { newOp->setAttr("sv.namehint", name); });
}

return newOp;
}
Expand Down
9 changes: 9 additions & 0 deletions test/Dialect/Comb/canonicalization.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1662,3 +1662,12 @@ hw.module @cantCombineOppositeNonBinCmpIntoConstant(in %tag_0: i4, in %tag_1: i4
%opposite_xor_or, %opposite_xor_and :
i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i4, i4
}

// https://github.com/llvm/circt/issues/6767
// CHECK-LABEL: @Issue6767
hw.module @Issue6767(in %arg0 : i64, out out: i32) {
// CHECK: %0 = comb.extract %arg0 from 0 {custom_dialect.attr = "test"} : (i64) -> i32
%0 = comb.extract %arg0 from 0 : (i64) -> i42
%1 = comb.extract %0 from 0 {custom_dialect.attr = "test"} : (i42) -> i32
hw.output %1 : i32
}
Loading