Skip to content

Commit

Permalink
Internals: Fix same() called outside of sameTree (verilator#4561).
Browse files Browse the repository at this point in the history
  • Loading branch information
wsnyder committed Oct 18, 2023
1 parent 8720841 commit b7233d0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/V3Ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,9 @@ class AstNode VL_NOT_FINAL {
UASSERT_STATIC(s_cloneCntGbl, "Rollover");
}

// Use instead isSame(), this is for each Ast* class, and assumes node is of same type
virtual bool same(const AstNode*) const { return true; }

public:
// ACCESSORS
VNType type() const VL_MT_SAFE { return m_type; }
Expand Down Expand Up @@ -2137,7 +2140,10 @@ class AstNode VL_NOT_FINAL {
// statement is unlikely to be taken
virtual bool isUnlikely() const { return false; }
virtual int instrCount() const { return 0; }
virtual bool same(const AstNode*) const { return true; }
// Iff node is identical to anouther node
virtual bool isSame(const AstNode* samep) const {
return type() == samep->type() && same(samep);
}
// Iff has a data type; dtype() must be non null
virtual bool hasDType() const VL_MT_SAFE { return false; }
// Iff has a non-null childDTypep(), as generic node function
Expand Down
2 changes: 1 addition & 1 deletion src/V3AstNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@ AstNodeUOrStructDType* AstMemberDType::getChildStructp() const {

bool AstMemberSel::same(const AstNode* samep) const {
const AstMemberSel* const sp = VN_DBG_AS(samep, MemberSel);
return sp != nullptr && access() == sp->access() && fromp()->same(sp->fromp())
return sp != nullptr && access() == sp->access() && fromp()->isSame(sp->fromp())
&& name() == sp->name() && varp()->same(sp->varp());
}

Expand Down
2 changes: 1 addition & 1 deletion src/V3Const.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ class ConstVisitor final : public VNVisitor {
// Avoid comparing widthMin's, which results in lost optimization attempts
// If cleanup sameGateTree to be smarter, this can be restored.
// return node1p->sameGateTree(node2p);
return node1p->same(node2p);
return node1p->isSame(node2p);
} else {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/V3Gate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ static void eliminate(AstNode* logicp,
// Substitute in the new tree
UASSERT_OBJ(nodep->access().isReadOnly(), nodep,
"Can't replace lvalue assignments with const var");
UASSERT_OBJ(!(VN_IS(substp, NodeVarRef) && nodep->same(substp)),
UASSERT_OBJ(!(VN_IS(substp, NodeVarRef) && nodep->isSame(substp)),
// Prevent an infinite loop...
substp, "Replacing node with itself; perhaps circular logic?");
// The replacement
Expand Down
6 changes: 3 additions & 3 deletions src/V3Reloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ class ReloopVisitor final : public VNVisitor {
if (m_mgSelLp) { // Old merge
if (m_mgCfuncp == m_cfuncp // In same function
&& m_mgNextp == nodep // Consecutive node
&& m_mgVarrefLp->same(lvarrefp) // Same array on left hand side
&& m_mgVarrefLp->isSame(lvarrefp) // Same array on left hand side
&& (m_mgConstRp // On the right hand side either ...
? (rconstp && m_mgConstRp->same(rconstp)) // ... same constant
: (rselp && m_mgVarrefRp->same(rvarrefp))) // ... or same array
? (rconstp && m_mgConstRp->isSame(rconstp)) // ... same constant
: (rselp && m_mgVarrefRp->isSame(rvarrefp))) // ... or same array
&& (lindex == m_mgIndexLo - 1 || lindex == m_mgIndexHi + 1) // Left index +/- 1
&& (m_mgConstRp || lindex == rindex + m_mgOffset) // Same right index offset
) {
Expand Down
2 changes: 1 addition & 1 deletion src/V3Slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class SliceVisitor final : public VNVisitor {
const AstUnpackArrayDType* const itemDTypep
= VN_CAST(itemRawDTypep, UnpackArrayDType);
if (!itemDTypep
|| !expectedItemDTypep->same(itemDTypep->subDTypep()->skipRefp())) {
|| !expectedItemDTypep->isSame(itemDTypep->subDTypep()->skipRefp())) {
if (!m_assignError) {
itemp->v3error("Item is incompatible with the array type.");
}
Expand Down

0 comments on commit b7233d0

Please sign in to comment.