Improve emitted code to use a reference for VlSelf (#5254)

This commit is contained in:
Yangyu Chen 2024-07-15 21:44:01 +08:00 committed by GitHub
parent 67ea819d82
commit 164a7a7c5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 2 deletions

View File

@ -210,6 +210,7 @@ William D. Jones
Wilson Snyder
Xi Zhang
Yan Xu
Yangyu Chen
Yinan Xu
Yoda Lee
Yossi Nivin

View File

@ -445,8 +445,12 @@ void EmitCFunc::emitDereference(AstNode* nodep, const string& pointer) {
putns(nodep, pointer.substr(2, pointer.length() - 3));
puts(".");
} else {
putns(nodep, pointer);
puts("->");
if (pointer == "vlSelf" && m_usevlSelfRef) {
puts("vlSelfRef.");
} else {
putns(nodep, pointer);
puts("->");
}
}
}

View File

@ -148,6 +148,7 @@ class EmitCFunc VL_NOT_FINAL : public EmitCConstInit {
protected:
EmitCLazyDecls m_lazyDecls; // Visitor for emitting lazy declarations
bool m_useSelfForThis = false; // Replace "this" with "vlSelf"
bool m_usevlSelfRef = false; // Use vlSelfRef reference instead of vlSelf pointer
const AstNodeModule* m_modp = nullptr; // Current module being emitted
const AstCFunc* m_cfuncp = nullptr; // Current function being emitted
bool m_instantiatesOwnProcess = false;
@ -343,6 +344,21 @@ public:
}
}
if (m_useSelfForThis) {
m_usevlSelfRef = true;
/*
* Using reference to the vlSelf pointer will help the C++
* compiler to have dereferenceable hints, which can help to
* reduce the need for branch instructions in the generated
* code to allow the compiler to generate load store after the
* if condition (including short-circuit evaluation)
* speculatively and also reduce the data cache pollution when
* executing in the wrong path to make verilator-generated code
* run faster.
*/
puts("auto &vlSelfRef = std::ref(*vlSelf).get();\n");
}
if (nodep->initsp()) {
putsDecoration(nodep, "// Init\n");
iterateAndNextConstNull(nodep->initsp());
@ -358,6 +374,8 @@ public:
iterateAndNextConstNull(nodep->finalsp());
}
m_usevlSelfRef = false;
puts("}\n");
if (nodep->ifdef() != "") puts("#endif // " + nodep->ifdef() + "\n");
}