Skip to content

Commit

Permalink
Add extra triggers for virtual interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Krzysztof Bieganski <[email protected]>
  • Loading branch information
kbieganski committed Nov 16, 2023
1 parent dc10118 commit 54fab1d
Show file tree
Hide file tree
Showing 24 changed files with 463 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ set(HEADERS
V3Unknown.h
V3Unroll.h
V3VariableOrder.h
V3VirtIface.h
V3Waiver.h
V3Width.h
V3WidthCommit.h
Expand Down Expand Up @@ -302,6 +303,7 @@ set(COMMON_SOURCES
V3Undriven.cpp
V3Unknown.cpp
V3Unroll.cpp
V3VirtIface.cpp
V3VariableOrder.cpp
V3Waiver.cpp
V3Width.cpp
Expand Down
1 change: 1 addition & 0 deletions src/Makefile_obj.in
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ RAW_OBJS_PCH_ASTNOMT = \
V3Undriven.o \
V3Unknown.o \
V3Unroll.o \
V3VirtIface.o \
V3VariableOrder.o \
V3Width.o \
V3WidthCommit.o \
Expand Down
3 changes: 3 additions & 0 deletions src/V3AstNodeDType.h
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ class AstIfaceRefDType final : public AstNodeDType {
// Reference to an interface, either for a port, or inside parent cell
// @astgen op1 := paramsp : List[AstPin]
private:
bool m_virtual = false; // True if virtual interface
FileLine* m_modportFileline; // Where modport token was
string m_cellName; // "" = no cell, such as when connects to 'input' iface
string m_ifaceName; // Interface name
Expand Down Expand Up @@ -861,6 +862,8 @@ class AstIfaceRefDType final : public AstNodeDType {
bool similarDType(const AstNodeDType* samep) const override { return this == samep; }
int widthAlignBytes() const override { return 1; }
int widthTotalBytes() const override { return 1; }
void isVirtual(bool flag) { m_virtual = flag; }
bool isVirtual() const { return m_virtual; }
FileLine* modportFileline() const { return m_modportFileline; }
string cellName() const { return m_cellName; }
void cellName(const string& name) { m_cellName = name; }
Expand Down
10 changes: 6 additions & 4 deletions src/V3AstNodeOther.h
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,8 @@ class AstNetlist final : public AstNode {
AstCFunc* m_evalp = nullptr; // The '_eval' function
AstCFunc* m_evalNbap = nullptr; // The '_eval__nba' function
AstVarScope* m_dpiExportTriggerp = nullptr; // The DPI export trigger variable
std::map<AstIface*, AstVarScope*>
m_virtIfaceTriggerps; // The virtual interface trigger variables
AstVar* m_delaySchedulerp = nullptr; // The delay scheduler variable
AstVarScope* m_nbaEventp = nullptr; // The NBA event variable
AstVarScope* m_nbaEventTriggerp = nullptr; // If set to 1, the NBA event should get triggered
Expand Down Expand Up @@ -1228,6 +1230,7 @@ class AstNetlist final : public AstNode {
void evalNbap(AstCFunc* funcp) { m_evalNbap = funcp; }
AstVarScope* dpiExportTriggerp() const { return m_dpiExportTriggerp; }
void dpiExportTriggerp(AstVarScope* varScopep) { m_dpiExportTriggerp = varScopep; }
std::map<AstIface*, AstVarScope*>& virtIfaceTriggerps() { return m_virtIfaceTriggerps; }
AstVar* delaySchedulerp() const { return m_delaySchedulerp; }
void delaySchedulerp(AstVar* const varScopep) { m_delaySchedulerp = varScopep; }
AstVarScope* nbaEventp() const { return m_nbaEventp; }
Expand Down Expand Up @@ -1680,6 +1683,7 @@ class AstVar final : public AstNode {
VLifetime m_lifetime; // Lifetime
VVarAttrClocker m_attrClocker;
MTaskIdSet m_mtaskIds; // MTaskID's that read or write this var
AstIface* m_sensIfacep = nullptr; // Interface type to which reads from this var are sensitive
int m_pinNum = 0; // For XML, if non-zero the connection pin number
bool m_ansi : 1; // Params or pins declared in the module header, rather than the body
bool m_declTyped : 1; // Declared as type (for dedup check)
Expand All @@ -1695,7 +1699,6 @@ class AstVar final : public AstNode {
bool m_usedClock : 1; // Signal used as a clock
bool m_usedParam : 1; // Parameter is referenced (on link; later signals not setup)
bool m_usedLoopIdx : 1; // Variable subject of for unrolling
bool m_usedVirtIface : 1; // Signal used through a virtual interface
bool m_funcLocal : 1; // Local variable for a function
bool m_funcLocalSticky : 1; // As m_funcLocal but remains set if var is moved to a static
bool m_funcReturn : 1; // Return variable for a function
Expand Down Expand Up @@ -1737,7 +1740,6 @@ class AstVar final : public AstNode {
m_usedClock = false;
m_usedParam = false;
m_usedLoopIdx = false;
m_usedVirtIface = false;
m_sigPublic = false;
m_sigModPublic = false;
m_sigUserRdPublic = false;
Expand Down Expand Up @@ -1866,6 +1868,7 @@ class AstVar final : public AstNode {
}
void ansi(bool flag) { m_ansi = flag; }
void declTyped(bool flag) { m_declTyped = flag; }
void sensIfacep(AstIface* nodep) { m_sensIfacep = nodep; }
void attrClocker(VVarAttrClocker flag) { m_attrClocker = flag; }
void attrFileDescr(bool flag) { m_fileDescr = flag; }
void attrScClocked(bool flag) { m_scClocked = flag; }
Expand All @@ -1876,7 +1879,6 @@ class AstVar final : public AstNode {
void usedClock(bool flag) { m_usedClock = flag; }
void usedParam(bool flag) { m_usedParam = flag; }
void usedLoopIdx(bool flag) { m_usedLoopIdx = flag; }
void usedVirtIface(bool flag) { m_usedVirtIface = flag; }
void sigPublic(bool flag) { m_sigPublic = flag; }
void sigModPublic(bool flag) { m_sigModPublic = flag; }
void sigUserRdPublic(bool flag) {
Expand Down Expand Up @@ -1969,7 +1971,6 @@ class AstVar final : public AstNode {
bool isUsedClock() const { return m_usedClock; }
bool isUsedParam() const { return m_usedParam; }
bool isUsedLoopIdx() const { return m_usedLoopIdx; }
bool isUsedVirtIface() const { return m_usedVirtIface; }
bool isSc() const VL_MT_SAFE { return m_sc; }
bool isScQuad() const;
bool isScBv() const;
Expand Down Expand Up @@ -1997,6 +1998,7 @@ class AstVar final : public AstNode {
bool attrSFormat() const { return m_attrSFormat; }
bool attrSplitVar() const { return m_attrSplitVar; }
bool attrIsolateAssign() const { return m_attrIsolateAssign; }
AstIface* sensIfacep() const { return m_sensIfacep; }
VVarAttrClocker attrClocker() const { return m_attrClocker; }
string verilogKwd() const override;
void lifetime(const VLifetime& flag) { m_lifetime = flag; }
Expand Down
1 change: 1 addition & 0 deletions src/V3AstNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,7 @@ const char* AstNetlist::broken() const {
BROKEN_RTN(m_dollarUnitPkgp && !m_dollarUnitPkgp->brokeExists());
BROKEN_RTN(m_evalp && !m_evalp->brokeExists());
BROKEN_RTN(m_dpiExportTriggerp && !m_dpiExportTriggerp->brokeExists());
//BROKEN_RTN(m_virtIfaceTriggerp && !m_virtIfaceTriggerp->brokeExists());
BROKEN_RTN(m_topScopep && !m_topScopep->brokeExists());
BROKEN_RTN(m_delaySchedulerp && !m_delaySchedulerp->brokeExists());
BROKEN_RTN(m_nbaEventp && !m_nbaEventp->brokeExists());
Expand Down
2 changes: 1 addition & 1 deletion src/V3Const.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2692,7 +2692,7 @@ class ConstVisitor final : public VNVisitor {
&& m_doNConst
&& v3Global.opt.fConst()
// Default value, not a "known" constant for this usage
&& !nodep->varp()->isClassMember() && !nodep->varp()->isUsedVirtIface()
&& !nodep->varp()->isClassMember() && !nodep->varp()->sensIfacep()
&& !(nodep->varp()->isFuncLocal() && nodep->varp()->isNonOutput())
&& !nodep->varp()->noSubst() && !nodep->varp()->isSigPublic())
|| nodep->varp()->isParam())) {
Expand Down
2 changes: 1 addition & 1 deletion src/V3Dead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ class DeadVisitor final : public VNVisitor {
}
bool mightElimVar(AstVar* nodep) const {
if (nodep->isSigPublic()) return false; // Can't elim publics!
if (nodep->isIO() || nodep->isClassMember() || nodep->isUsedVirtIface()) return false;
if (nodep->isIO() || nodep->isClassMember() || nodep->sensIfacep()) return false;
if (nodep->isTemp() && !nodep->isTrace()) return true;
return m_elimUserVars; // Post-Trace can kill most anything
}
Expand Down
2 changes: 1 addition & 1 deletion src/V3Gate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class GateVisitor final : public VNVisitor {
UINFO(6, "New vertex " << varscp << endl);
vertexp = new GateVarVertex{&m_graph, m_scopep, varscp};
varscp->user1p(vertexp);
if (varscp->varp()->isUsedVirtIface()) {
if (varscp->varp()->sensIfacep()) {
// Can be used in a class method, which cannot be tracked statically
vertexp->clearReducibleAndDedupable("VirtIface");
vertexp->setConsumed("VirtIface");
Expand Down
3 changes: 3 additions & 0 deletions src/V3Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class V3Global final {
bool m_dpi = false; // Need __Dpi include files
bool m_hasEvents = false; // Design uses SystemVerilog named events
bool m_hasClasses = false; // Design uses SystemVerilog classes
bool m_hasVirtIfaces = false; // Design uses virtual interfaces
bool m_usesProbDist = false; // Uses $dist_*
bool m_usesStdPackage = false; // Design uses the std package
bool m_usesTiming = false; // Design uses timing constructs
Expand Down Expand Up @@ -162,6 +163,8 @@ class V3Global final {
void setHasEvents() { m_hasEvents = true; }
bool hasClasses() const { return m_hasClasses; }
void setHasClasses() { m_hasClasses = true; }
bool hasVirtIfaces() const { return m_hasVirtIfaces; }
void setHasVirtIfaces() { m_hasVirtIfaces = true; }
bool usesProbDist() const { return m_usesProbDist; }
void setUsesProbDist() { m_usesProbDist = true; }
bool usesStdPackage() const { return m_usesStdPackage; }
Expand Down
4 changes: 2 additions & 2 deletions src/V3Life.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class LifeBlock final {
void checkRemoveAssign(const LifeMap::iterator& it) {
const AstVar* const varp = it->first->varp();
LifeVarEntry* const entp = &(it->second);
if (!varp->isSigPublic() && !varp->isUsedVirtIface()) {
if (!varp->isSigPublic() && !varp->sensIfacep()) {
// Rather than track what sigs AstUCFunc/AstUCStmt may change,
// we just don't optimize any public sigs
// Check the var entry, and remove if appropriate
Expand Down Expand Up @@ -178,7 +178,7 @@ class LifeBlock final {
const auto pair = m_map.emplace(nodep, LifeVarEntry::CONSUMED{});
if (!pair.second) {
if (AstConst* const constp = pair.first->second.constNodep()) {
if (!varrefp->varp()->isSigPublic() && !varrefp->varp()->isUsedVirtIface()) {
if (!varrefp->varp()->isSigPublic() && !varrefp->varp()->sensIfacep()) {
// Aha, variable is constant; substitute in.
// We'll later constant propagate
UINFO(4, " replaceconst: " << varrefp << endl);
Expand Down
2 changes: 1 addition & 1 deletion src/V3Localize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class LocalizeVisitor final : public VNVisitor {
&& !nodep->varp()->isFuncLocal() // Not already a function local (e.g.: argument)
&& !nodep->varp()->isStatic() // Not a static variable
&& !nodep->varp()->isClassMember() // Statically exists in design hierarchy
&& !nodep->varp()->isUsedVirtIface() // Not used through a virtual interface
&& !nodep->varp()->sensIfacep() // Not sensitive to an interface
&& !nodep->varp()->valuep() // Does not have an initializer
) {
UINFO(4, "Consider for localization: " << nodep << endl);
Expand Down
43 changes: 36 additions & 7 deletions src/V3Sched.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,16 +507,16 @@ struct TriggerKit {
m_funcp->stmtsp()->addHereThisAsNext(callp->makeStmt());
}

// Utility to set then clear the dpiExportTrigger trigger
void addDpiExportTriggerAssignment(AstVarScope* dpiExportTriggerVscp, uint32_t index) const {
FileLine* const flp = dpiExportTriggerVscp->fileline();
// Utility to set then clear an extra trigger
void addExtraTriggerAssignment(AstVarScope* extraTriggerVscp, uint32_t index) const {
FileLine* const flp = extraTriggerVscp->fileline();
AstVarRef* const vrefp = new AstVarRef{flp, m_vscp, VAccess::WRITE};
AstCMethodHard* const callp = new AstCMethodHard{flp, vrefp, "set"};
callp->addPinsp(new AstConst{flp, index});
callp->addPinsp(new AstVarRef{flp, dpiExportTriggerVscp, VAccess::READ});
callp->addPinsp(new AstVarRef{flp, extraTriggerVscp, VAccess::READ});
callp->dtypeSetVoid();
AstNode* const stmtp = callp->makeStmt();
stmtp->addNext(new AstAssign{flp, new AstVarRef{flp, dpiExportTriggerVscp, VAccess::WRITE},
stmtp->addNext(new AstAssign{flp, new AstVarRef{flp, extraTriggerVscp, VAccess::WRITE},
new AstConst{flp, AstConst::BitFalse{}}});
m_funcp->stmtsp()->addHereThisAsNext(stmtp);
}
Expand Down Expand Up @@ -840,7 +840,7 @@ AstNode* createInputCombLoop(AstNetlist* netlistp, AstCFunc* const initFuncp,
= createTriggers(netlistp, initFuncp, senExprBuilder, senTreeps, "ico", extraTriggers);

if (dpiExportTriggerVscp) {
trig.addDpiExportTriggerAssignment(dpiExportTriggerVscp, dpiExportTriggerIndex);
trig.addExtraTriggerAssignment(dpiExportTriggerVscp, dpiExportTriggerIndex);
}

// Remap sensitivities
Expand Down Expand Up @@ -1150,13 +1150,19 @@ void schedule(AstNetlist* netlistp) {

// Step 8: Create the pre/act/nba triggers
AstVarScope* const dpiExportTriggerVscp = netlistp->dpiExportTriggerp();
std::vector<std::pair<AstIface*, size_t>> virtIfaceTriggerIndices;

// We may have an extra trigger for variable updated in DPI exports
ExtraTriggers extraTriggers;
const size_t dpiExportTriggerIndex = dpiExportTriggerVscp
? extraTriggers.allocate("DPI export trigger")
: std::numeric_limits<unsigned>::max();

for (auto p : netlistp->virtIfaceTriggerps()) {
const size_t virtIfaceTriggerIndex = extraTriggers.allocate("Virtual interface trigger");
virtIfaceTriggerIndices.push_back(std::make_pair(p.first, virtIfaceTriggerIndex));
}

const auto& senTreeps = getSenTreesUsedBy({&logicRegions.m_pre, //
&logicRegions.m_act, //
&logicRegions.m_nba, //
Expand All @@ -1170,7 +1176,10 @@ void schedule(AstNetlist* netlistp) {
if (timingKit.m_postUpdates) actTrig.m_funcp->addStmtsp(timingKit.m_postUpdates);

if (dpiExportTriggerVscp) {
actTrig.addDpiExportTriggerAssignment(dpiExportTriggerVscp, dpiExportTriggerIndex);
actTrig.addExtraTriggerAssignment(dpiExportTriggerVscp, dpiExportTriggerIndex);
}
for (auto p : virtIfaceTriggerIndices) {
actTrig.addExtraTriggerAssignment(netlistp->virtIfaceTriggerps().at(p.first), p.second);
}

AstVarScope* const actTrigVscp = actTrig.m_vscp;
Expand Down Expand Up @@ -1223,12 +1232,22 @@ void schedule(AstNetlist* netlistp) {
? createTriggerSenTree(netlistp, actTrig.m_vscp, dpiExportTriggerIndex)
: nullptr;

std::map<AstIface*, AstSenTree*> virtIfaceTriggeredAct;
for (const auto& p : virtIfaceTriggerIndices) {
virtIfaceTriggeredAct.insert(
std::make_pair(p.first, createTriggerSenTree(netlistp, actTrig.m_vscp, p.second)));
}

AstCFunc* const actFuncp = V3Order::order(
netlistp, {&logicRegions.m_pre, &logicRegions.m_act, &logicReplicas.m_act}, trigToSenAct,
"act", false, false, [&](const AstVarScope* vscp, std::vector<AstSenTree*>& out) {
auto it = actTimingDomains.find(vscp);
if (it != actTimingDomains.end()) out = it->second;
if (vscp->varp()->isWrittenByDpi()) out.push_back(dpiExportTriggeredAct);
if (vscp->varp()->sensIfacep()) {
auto it = virtIfaceTriggeredAct.find(vscp->varp()->sensIfacep());
if (it != virtIfaceTriggeredAct.end()) out.push_back(it->second);
}
});
splitCheck(actFuncp);
if (v3Global.opt.stats()) V3Stats::statsStage("sched-create-act");
Expand All @@ -1252,6 +1271,11 @@ void schedule(AstNetlist* netlistp) {
= dpiExportTriggerVscp
? createTriggerSenTree(netlistp, trigVscp, dpiExportTriggerIndex)
: nullptr;
std::map<AstIface*, AstSenTree*> virtIfaceTriggered;
for (const auto& p : virtIfaceTriggerIndices) {
virtIfaceTriggered.insert(
std::make_pair(p.first, createTriggerSenTree(netlistp, trigVscp, p.second)));
}

const auto& timingDomains = timingKit.remapDomains(trigMap);
AstCFunc* const funcp = V3Order::order(
Expand All @@ -1260,6 +1284,10 @@ void schedule(AstNetlist* netlistp) {
auto it = timingDomains.find(vscp);
if (it != timingDomains.end()) out = it->second;
if (vscp->varp()->isWrittenByDpi()) out.push_back(dpiExportTriggered);
if (vscp->varp()->sensIfacep()) {
auto it = virtIfaceTriggeredAct.find(vscp->varp()->sensIfacep());
if (it != virtIfaceTriggeredAct.end()) out.push_back(it->second);
}
});

// Create the trigger dumping function, which is the same as act trigger
Expand Down Expand Up @@ -1314,6 +1342,7 @@ void schedule(AstNetlist* netlistp) {
splitCheck(initp);

netlistp->dpiExportTriggerp(nullptr);
netlistp->virtIfaceTriggerps().clear();

V3Global::dumpCheckGlobalTree("sched", 0, dumpTreeLevel() >= 3);
}
Expand Down
1 change: 1 addition & 0 deletions src/V3SchedReplicate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class SchedReplicateVarVertex final : public SchedReplicateVertex {
// the act region, which means combinational logic driven from a suspendable
// processes must be present in the 'act' region
if (varp()->isWrittenBySuspendable()) addDrivingRegions(ACTIVE);
if (varp()->sensIfacep()) addDrivingRegions(ACTIVE);
}
AstVarScope* vscp() const { return m_vscp; }
AstVar* varp() const { return m_vscp->varp(); }
Expand Down
Loading

0 comments on commit 54fab1d

Please sign in to comment.