Skip to content

Commit

Permalink
Associate trace codes with function indices
Browse files Browse the repository at this point in the history
For each traced variable, also register the trace function index that
will write it.
  • Loading branch information
gezalore committed Oct 23, 2023
1 parent 1bd3174 commit eb9a93c
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 83 deletions.
29 changes: 16 additions & 13 deletions include/verilated_fst_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,35 +222,38 @@ void VerilatedFst::declare(uint32_t code, const char* name, int dtypenum, fstVar
}
}

void VerilatedFst::declEvent(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum) {
void VerilatedFst::declEvent(uint32_t code, uint32_t fidx, const char* name, int dtypenum,
fstVarDir vardir, fstVarType vartype, bool array, int arraynum) {
declare(code, name, dtypenum, vardir, vartype, array, arraynum, false, 0, 0);
}
void VerilatedFst::declBit(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum) {
void VerilatedFst::declBit(uint32_t code, uint32_t fidx, const char* name, int dtypenum,
fstVarDir vardir, fstVarType vartype, bool array, int arraynum) {
declare(code, name, dtypenum, vardir, vartype, array, arraynum, false, 0, 0);
}
void VerilatedFst::declBus(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum, int msb, int lsb) {
void VerilatedFst::declBus(uint32_t code, uint32_t fidx, const char* name, int dtypenum,
fstVarDir vardir, fstVarType vartype, bool array, int arraynum, int msb,
int lsb) {
declare(code, name, dtypenum, vardir, vartype, array, arraynum, true, msb, lsb);
}
void VerilatedFst::declQuad(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum, int msb, int lsb) {
void VerilatedFst::declQuad(uint32_t code, uint32_t fidx, const char* name, int dtypenum,
fstVarDir vardir, fstVarType vartype, bool array, int arraynum,
int msb, int lsb) {
declare(code, name, dtypenum, vardir, vartype, array, arraynum, true, msb, lsb);
}
void VerilatedFst::declArray(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum, int msb, int lsb) {
void VerilatedFst::declArray(uint32_t code, uint32_t fidx, const char* name, int dtypenum,
fstVarDir vardir, fstVarType vartype, bool array, int arraynum,
int msb, int lsb) {
declare(code, name, dtypenum, vardir, vartype, array, arraynum, true, msb, lsb);
}
void VerilatedFst::declDouble(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum) {
void VerilatedFst::declDouble(uint32_t code, uint32_t fidx, const char* name, int dtypenum,
fstVarDir vardir, fstVarType vartype, bool array, int arraynum) {
declare(code, name, dtypenum, vardir, vartype, array, arraynum, false, 63, 0);
}

//=============================================================================
// Get/commit trace buffer

VerilatedFst::Buffer* VerilatedFst::getTraceBuffer() {
VerilatedFst::Buffer* VerilatedFst::getTraceBuffer(uint32_t fidx) {
if (offload()) return new OffloadBuffer{*this};
return new Buffer{*this};
}
Expand Down
14 changes: 7 additions & 7 deletions include/verilated_fst_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class VerilatedFst final : public VerilatedTrace<VerilatedFst, VerilatedFstBuffe
bool preChangeDump() override { return isOpen(); }

// Trace buffer management
Buffer* getTraceBuffer() override;
Buffer* getTraceBuffer(uint32_t fidx) override;
void commitTraceBuffer(Buffer*) override;

// Configure sub-class
Expand All @@ -101,17 +101,17 @@ class VerilatedFst final : public VerilatedTrace<VerilatedFst, VerilatedFstBuffe
//=========================================================================
// Internal interface to Verilator generated code

void declEvent(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
void declEvent(uint32_t code, uint32_t fidx, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum);
void declBit(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
void declBit(uint32_t code, uint32_t fidx, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum);
void declBus(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
void declBus(uint32_t code, uint32_t fidx, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum, int msb, int lsb);
void declQuad(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
void declQuad(uint32_t code, uint32_t fidx, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum, int msb, int lsb);
void declArray(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
void declArray(uint32_t code, uint32_t fidx, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum, int msb, int lsb);
void declDouble(uint32_t code, const char* name, int dtypenum, fstVarDir vardir,
void declDouble(uint32_t code, uint32_t fidx, const char* name, int dtypenum, fstVarDir vardir,
fstVarType vartype, bool array, int arraynum);

void declDTypeEnum(int dtypenum, const char* name, uint32_t elements, unsigned int minValbits,
Expand Down
27 changes: 16 additions & 11 deletions include/verilated_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,29 @@ class VerilatedTrace VL_NOT_FINAL {
// (the one in Ubuntu 14.04 with GCC 4.8.4 in particular) use the
// assignment operator on inserting into collections, so they don't work
// with const fields...
union { // The callback
const union { // The callback
initCb_t m_initCb;
dumpCb_t m_dumpCb;
dumpOffloadCb_t m_dumpOffloadCb;
cleanupCb_t m_cleanupCb;
};
void* m_userp; // The user pointer to pass to the callback (the symbol table)
const uint32_t m_fidx; // The index of the tracing function
void* const m_userp; // The user pointer to pass to the callback (the symbol table)
CallbackRecord(initCb_t cb, void* userp)
: m_initCb{cb}
, m_fidx{0}
, m_userp{userp} {}
CallbackRecord(dumpCb_t cb, void* userp)
CallbackRecord(dumpCb_t cb, uint32_t fidx, void* userp)
: m_dumpCb{cb}
, m_fidx{fidx}
, m_userp{userp} {}
CallbackRecord(dumpOffloadCb_t cb, void* userp)
CallbackRecord(dumpOffloadCb_t cb, uint32_t fidx, void* userp)
: m_dumpOffloadCb{cb}
, m_fidx{fidx}
, m_userp{userp} {}
CallbackRecord(cleanupCb_t cb, void* userp)
: m_cleanupCb{cb}
, m_fidx{0}
, m_userp{userp} {}
};

Expand Down Expand Up @@ -329,7 +334,7 @@ class VerilatedTrace VL_NOT_FINAL {
virtual bool preChangeDump() = 0;

// Trace buffer management
virtual Buffer* getTraceBuffer() = 0;
virtual Buffer* getTraceBuffer(uint32_t fidx) = 0;
virtual void commitTraceBuffer(Buffer*) = 0;

// Configure sub-class
Expand Down Expand Up @@ -363,12 +368,12 @@ class VerilatedTrace VL_NOT_FINAL {

void addModel(VerilatedModel*) VL_MT_SAFE_EXCLUDES(m_mutex);
void addInitCb(initCb_t cb, void* userp) VL_MT_SAFE;
void addConstCb(dumpCb_t cb, void* userp) VL_MT_SAFE;
void addConstCb(dumpOffloadCb_t cb, void* userp) VL_MT_SAFE;
void addFullCb(dumpCb_t cb, void* userp) VL_MT_SAFE;
void addFullCb(dumpOffloadCb_t cb, void* userp) VL_MT_SAFE;
void addChgCb(dumpCb_t cb, void* userp) VL_MT_SAFE;
void addChgCb(dumpOffloadCb_t cb, void* userp) VL_MT_SAFE;
void addConstCb(dumpCb_t cb, uint32_t fidx, void* userp) VL_MT_SAFE;
void addConstCb(dumpOffloadCb_t cb, uint32_t fidx, void* userp) VL_MT_SAFE;
void addFullCb(dumpCb_t cb, uint32_t fidx, void* userp) VL_MT_SAFE;
void addFullCb(dumpOffloadCb_t cb, uint32_t fidx, void* userp) VL_MT_SAFE;
void addChgCb(dumpCb_t cb, uint32_t fidx, void* userp) VL_MT_SAFE;
void addChgCb(dumpOffloadCb_t cb, uint32_t fidx, void* userp) VL_MT_SAFE;
void addCleanupCb(cleanupCb_t cb, void* userp) VL_MT_SAFE;

void scopeEscape(char flag) { m_scopeEscape = flag; }
Expand Down
41 changes: 23 additions & 18 deletions include/verilated_trace_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,16 +498,15 @@ void VerilatedTrace<VL_SUB_T, VL_BUF_T>::runCallbacks(const std::vector<Callback
// Main thread executes all jobs with index % threads == 0
std::vector<ParallelWorkerData*> mainThreadWorkerData;
// Enqueue all the jobs
for (unsigned i = 0; i < cbVec.size(); ++i) {
const CallbackRecord& cbr = cbVec[i];
for (const CallbackRecord& cbr : cbVec) {
// Always get the trace buffer on the main thread
Buffer* const bufp = getTraceBuffer();
Buffer* const bufp = getTraceBuffer(cbr.m_fidx);
// Create new work item
workerData.emplace_back(cbr.m_dumpCb, cbr.m_userp, bufp);
// Grab the new work item
ParallelWorkerData* const itemp = &workerData.back();
// Enqueue task to thread pool, or main thread
if (unsigned rem = i % threads) {
if (unsigned rem = cbr.m_fidx % threads) {
threadPoolp->workerp(rem - 1)->addTask(parallelWorkerTask, itemp);
} else {
mainThreadWorkerData.push_back(itemp);
Expand All @@ -530,7 +529,7 @@ void VerilatedTrace<VL_SUB_T, VL_BUF_T>::runCallbacks(const std::vector<Callback
}
// Fall back on sequential execution
for (const CallbackRecord& cbr : cbVec) {
Buffer* const traceBufferp = getTraceBuffer();
Buffer* const traceBufferp = getTraceBuffer(cbr.m_fidx);
cbr.m_dumpCb(cbr.m_userp, traceBufferp);
commitTraceBuffer(traceBufferp);
}
Expand All @@ -541,7 +540,7 @@ void VerilatedTrace<VL_SUB_T, VL_BUF_T>::runOffloadedCallbacks(
const std::vector<CallbackRecord>& cbVec) {
// Fall back on sequential execution
for (const CallbackRecord& cbr : cbVec) {
Buffer* traceBufferp = getTraceBuffer();
Buffer* traceBufferp = getTraceBuffer(cbr.m_fidx);
cbr.m_dumpOffloadCb(cbr.m_userp, static_cast<OffloadBuffer*>(traceBufferp));
commitTraceBuffer(traceBufferp);
}
Expand Down Expand Up @@ -704,28 +703,34 @@ void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addInitCb(initCb_t cb, void* userp) VL_
addCallbackRecord(m_initCbs, CallbackRecord{cb, userp});
}
template <>
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addConstCb(dumpCb_t cb, void* userp) VL_MT_SAFE {
addCallbackRecord(m_constCbs, CallbackRecord{cb, userp});
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addConstCb(dumpCb_t cb, uint32_t fidx,
void* userp) VL_MT_SAFE {
addCallbackRecord(m_constCbs, CallbackRecord{cb, fidx, userp});
}
template <>
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addConstCb(dumpOffloadCb_t cb, void* userp) VL_MT_SAFE {
addCallbackRecord(m_constOffloadCbs, CallbackRecord{cb, userp});
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addConstCb(dumpOffloadCb_t cb, uint32_t fidx,
void* userp) VL_MT_SAFE {
addCallbackRecord(m_constOffloadCbs, CallbackRecord{cb, fidx, userp});
}
template <>
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addFullCb(dumpCb_t cb, void* userp) VL_MT_SAFE {
addCallbackRecord(m_fullCbs, CallbackRecord{cb, userp});
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addFullCb(dumpCb_t cb, uint32_t fidx,
void* userp) VL_MT_SAFE {
addCallbackRecord(m_fullCbs, CallbackRecord{cb, fidx, userp});
}
template <>
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addFullCb(dumpOffloadCb_t cb, void* userp) VL_MT_SAFE {
addCallbackRecord(m_fullOffloadCbs, CallbackRecord{cb, userp});
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addFullCb(dumpOffloadCb_t cb, uint32_t fidx,
void* userp) VL_MT_SAFE {
addCallbackRecord(m_fullOffloadCbs, CallbackRecord{cb, fidx, userp});
}
template <>
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addChgCb(dumpCb_t cb, void* userp) VL_MT_SAFE {
addCallbackRecord(m_chgCbs, CallbackRecord{cb, userp});
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addChgCb(dumpCb_t cb, uint32_t fidx,
void* userp) VL_MT_SAFE {
addCallbackRecord(m_chgCbs, CallbackRecord{cb, fidx, userp});
}
template <>
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addChgCb(dumpOffloadCb_t cb, void* userp) VL_MT_SAFE {
addCallbackRecord(m_chgOffloadCbs, CallbackRecord{cb, userp});
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addChgCb(dumpOffloadCb_t cb, uint32_t fidx,
void* userp) VL_MT_SAFE {
addCallbackRecord(m_chgOffloadCbs, CallbackRecord{cb, fidx, userp});
}
template <>
void VerilatedTrace<VL_SUB_T, VL_BUF_T>::addCleanupCb(cleanupCb_t cb, void* userp) VL_MT_SAFE {
Expand Down
23 changes: 13 additions & 10 deletions include/verilated_vcd_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,32 +520,35 @@ void VerilatedVcd::declare(uint32_t code, const char* name, const char* wirep, b
m_namemapp->emplace(hiername, decl);
}

void VerilatedVcd::declEvent(uint32_t code, const char* name, bool array, int arraynum) {
void VerilatedVcd::declEvent(uint32_t code, uint32_t fidx, const char* name, bool array,
int arraynum) {
declare(code, name, "event", array, arraynum, false, false, 0, 0);
}
void VerilatedVcd::declBit(uint32_t code, const char* name, bool array, int arraynum) {
void VerilatedVcd::declBit(uint32_t code, uint32_t fidx, const char* name, bool array,
int arraynum) {
declare(code, name, "wire", array, arraynum, false, false, 0, 0);
}
void VerilatedVcd::declBus(uint32_t code, const char* name, bool array, int arraynum, int msb,
int lsb) {
void VerilatedVcd::declBus(uint32_t code, uint32_t fidx, const char* name, bool array,
int arraynum, int msb, int lsb) {
declare(code, name, "wire", array, arraynum, false, true, msb, lsb);
}
void VerilatedVcd::declQuad(uint32_t code, const char* name, bool array, int arraynum, int msb,
int lsb) {
void VerilatedVcd::declQuad(uint32_t code, uint32_t fidx, const char* name, bool array,
int arraynum, int msb, int lsb) {
declare(code, name, "wire", array, arraynum, false, true, msb, lsb);
}
void VerilatedVcd::declArray(uint32_t code, const char* name, bool array, int arraynum, int msb,
int lsb) {
void VerilatedVcd::declArray(uint32_t code, uint32_t fidx, const char* name, bool array,
int arraynum, int msb, int lsb) {
declare(code, name, "wire", array, arraynum, false, true, msb, lsb);
}
void VerilatedVcd::declDouble(uint32_t code, const char* name, bool array, int arraynum) {
void VerilatedVcd::declDouble(uint32_t code, uint32_t fidx, const char* name, bool array,
int arraynum) {
declare(code, name, "real", array, arraynum, false, false, 63, 0);
}

//=============================================================================
// Get/commit trace buffer

VerilatedVcd::Buffer* VerilatedVcd::getTraceBuffer() {
VerilatedVcd::Buffer* VerilatedVcd::getTraceBuffer(uint32_t fidx) {
VerilatedVcd::Buffer* const bufp = new Buffer{*this};
if (parallel()) {
// Note: This is called from VerilatedVcd::dump, which already holds the lock
Expand Down
17 changes: 10 additions & 7 deletions include/verilated_vcd_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class VerilatedVcd VL_NOT_FINAL : public VerilatedTrace<VerilatedVcd, VerilatedV
bool preChangeDump() override;

// Trace buffer management
Buffer* getTraceBuffer() override;
Buffer* getTraceBuffer(uint32_t fidx) override;
void commitTraceBuffer(Buffer*) override;

// Configure sub-class
Expand Down Expand Up @@ -140,12 +140,15 @@ class VerilatedVcd VL_NOT_FINAL : public VerilatedTrace<VerilatedVcd, VerilatedV
//=========================================================================
// Internal interface to Verilator generated code

void declEvent(uint32_t code, const char* name, bool array, int arraynum);
void declBit(uint32_t code, const char* name, bool array, int arraynum);
void declBus(uint32_t code, const char* name, bool array, int arraynum, int msb, int lsb);
void declQuad(uint32_t code, const char* name, bool array, int arraynum, int msb, int lsb);
void declArray(uint32_t code, const char* name, bool array, int arraynum, int msb, int lsb);
void declDouble(uint32_t code, const char* name, bool array, int arraynum);
void declEvent(uint32_t code, uint32_t fidx, const char* name, bool array, int arraynum);
void declBit(uint32_t code, uint32_t fidx, const char* name, bool array, int arraynum);
void declBus(uint32_t code, uint32_t fidx, const char* name, bool array, int arraynum, int msb,
int lsb);
void declQuad(uint32_t code, uint32_t fidx, const char* name, bool array, int arraynum,
int msb, int lsb);
void declArray(uint32_t code, uint32_t fidx, const char* name, bool array, int arraynum,
int msb, int lsb);
void declDouble(uint32_t code, uint32_t fidx, const char* name, bool array, int arraynum);
};

#ifndef DOXYGEN
Expand Down
5 changes: 4 additions & 1 deletion src/V3AstNodeOther.h
Original file line number Diff line number Diff line change
Expand Up @@ -3192,7 +3192,8 @@ class AstTraceDecl final : public AstNodeStmt {
// Expression being traced - Moved to AstTraceInc by V3Trace
// @astgen op1 := valuep : Optional[AstNodeExpr]
private:
uint32_t m_code = 0; // Trace identifier code; converted to ASCII by trace routines
uint32_t m_code{0}; // Trace identifier code
uint32_t m_fidx{0}; // Trace function index
const string m_showname; // Name of variable
const VNumRange m_bitRange; // Property of var the trace details
const VNumRange m_arrayRange; // Property of var the trace details
Expand Down Expand Up @@ -3228,6 +3229,8 @@ class AstTraceDecl final : public AstNodeStmt {
// Details on what we're tracing
uint32_t code() const { return m_code; }
void code(uint32_t code) { m_code = code; }
uint32_t fidx() const { return m_fidx; }
void fidx(uint32_t fidx) { m_fidx = fidx; }
uint32_t codeInc() const { return m_codeInc; }
const VNumRange& bitRange() const { return m_bitRange; }
const VNumRange& arrayRange() const { return m_arrayRange; }
Expand Down
2 changes: 2 additions & 0 deletions src/V3EmitCImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ class EmitCTrace final : EmitCFunc {
puts("(c+" + cvtToStr(nodep->code()));
if (nodep->arrayRange().ranged()) puts("+i*" + cvtToStr(nodep->widthWords()));
puts(",");
puts(cvtToStr(nodep->fidx()));
puts(",");
putsQuoted(VIdProtect::protectWordsIf(nodep->showname(), nodep->protect()));
// Direction
if (v3Global.opt.traceFormat().fst()) {
Expand Down
Loading

0 comments on commit eb9a93c

Please sign in to comment.