2012-04-13 03:08:20 +02:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 13:35:28 +02:00
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Waves tracing
|
|
|
|
|
//
|
2019-11-08 04:33:59 +01:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2020-03-21 16:24:24 +01:00
|
|
|
// Copyright 2003-2020 by Wilson Snyder. This program is free software; you
|
|
|
|
|
// can redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 23:07:57 +02:00
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2020-03-21 16:24:24 +01:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3Trace's Transformations:
|
2020-05-09 00:42:34 +02:00
|
|
|
//
|
|
|
|
|
// Examine whole design and build a graph describing which function call
|
|
|
|
|
// may result in a write to a traced variable. This is done in 2 passes:
|
|
|
|
|
//
|
2006-08-26 13:35:28 +02:00
|
|
|
// Pass 1:
|
2020-05-09 00:42:34 +02:00
|
|
|
// Add vertices for TraceDecl, CFunc, CCall and VarRef nodes, add
|
|
|
|
|
// edges from CCall -> CFunc, VarRef -> TraceDecl, also add edges
|
|
|
|
|
// for public entry points to CFuncs (these are like a spontaneous
|
|
|
|
|
// call)
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
2020-05-09 00:42:34 +02:00
|
|
|
// Pass 2:
|
|
|
|
|
// Add edges from CFunc -> VarRef being written
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
2020-05-09 00:42:34 +02:00
|
|
|
// Finally:
|
|
|
|
|
// Process graph to determine when traced variables can change, allocate
|
|
|
|
|
// activity flags, insert nodes to set activity flags, allocate signal
|
|
|
|
|
// numbers (codes), and construct the full and incremental trace
|
|
|
|
|
// functions, together with all other trace support functions.
|
2008-06-10 03:25:10 +02:00
|
|
|
//
|
2006-08-26 13:35:28 +02:00
|
|
|
//*************************************************************************
|
|
|
|
|
|
2006-12-18 20:20:45 +01:00
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3Trace.h"
|
|
|
|
|
#include "V3EmitCBase.h"
|
|
|
|
|
#include "V3Graph.h"
|
|
|
|
|
#include "V3Hashed.h"
|
|
|
|
|
#include "V3Stats.h"
|
|
|
|
|
|
2018-10-14 19:43:24 +02:00
|
|
|
#include <map>
|
|
|
|
|
#include <set>
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
//######################################################################
|
|
|
|
|
// Graph vertexes
|
|
|
|
|
|
|
|
|
|
class TraceActivityVertex : public V3GraphVertex {
|
2020-05-17 14:53:02 +02:00
|
|
|
AstNode* const m_insertp;
|
2020-04-14 04:51:35 +02:00
|
|
|
vlsint32_t m_activityCode;
|
|
|
|
|
bool m_slow; // If always slow, we can use the same code
|
2006-08-26 13:35:28 +02:00
|
|
|
public:
|
2020-04-14 04:51:35 +02:00
|
|
|
enum { ACTIVITY_NEVER = ((1UL << 31) - 1) };
|
|
|
|
|
enum { ACTIVITY_ALWAYS = ((1UL << 31) - 2) };
|
|
|
|
|
enum { ACTIVITY_SLOW = 0 };
|
2006-08-26 13:35:28 +02:00
|
|
|
TraceActivityVertex(V3Graph* graphp, AstNode* nodep, bool slow)
|
2020-04-14 04:51:35 +02:00
|
|
|
: V3GraphVertex(graphp)
|
|
|
|
|
, m_insertp(nodep) {
|
2019-05-19 22:13:13 +02:00
|
|
|
m_activityCode = 0;
|
|
|
|
|
m_slow = slow;
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
TraceActivityVertex(V3Graph* graphp, vlsint32_t code)
|
2020-04-14 04:51:35 +02:00
|
|
|
: V3GraphVertex(graphp)
|
|
|
|
|
, m_insertp(NULL) {
|
2019-05-19 22:13:13 +02:00
|
|
|
m_activityCode = code;
|
|
|
|
|
m_slow = false;
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
virtual ~TraceActivityVertex() {}
|
2017-11-28 02:11:34 +01:00
|
|
|
// ACCESSORS
|
2006-08-26 13:35:28 +02:00
|
|
|
AstNode* insertp() const {
|
2019-05-19 22:13:13 +02:00
|
|
|
if (!m_insertp) v3fatalSrc("Null insertp; probably called on a special always/slow.");
|
|
|
|
|
return m_insertp;
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
virtual string name() const {
|
2020-04-14 04:51:35 +02:00
|
|
|
if (activityAlways()) {
|
|
|
|
|
return "*ALWAYS*";
|
|
|
|
|
} else {
|
|
|
|
|
return (string(slow() ? "*SLOW* " : "")) + insertp()->name();
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2020-04-14 04:51:35 +02:00
|
|
|
virtual string dotColor() const { return slow() ? "yellowGreen" : "green"; }
|
2006-08-26 13:35:28 +02:00
|
|
|
vlsint32_t activityCode() const { return m_activityCode; }
|
2020-04-14 04:51:35 +02:00
|
|
|
bool activityAlways() const { return activityCode() == ACTIVITY_ALWAYS; }
|
2020-05-17 14:53:02 +02:00
|
|
|
void activityCode(vlsint32_t code) { m_activityCode = code; }
|
2006-08-26 13:35:28 +02:00
|
|
|
bool slow() const { return m_slow; }
|
2020-04-14 04:51:35 +02:00
|
|
|
void slow(bool flag) {
|
|
|
|
|
if (!flag) m_slow = false;
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TraceCFuncVertex : public V3GraphVertex {
|
2019-05-19 22:13:13 +02:00
|
|
|
AstCFunc* m_nodep;
|
2020-04-14 04:51:35 +02:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
public:
|
|
|
|
|
TraceCFuncVertex(V3Graph* graphp, AstCFunc* nodep)
|
2020-04-14 04:51:35 +02:00
|
|
|
: V3GraphVertex(graphp)
|
|
|
|
|
, m_nodep(nodep) {}
|
2006-08-26 13:35:28 +02:00
|
|
|
virtual ~TraceCFuncVertex() {}
|
2017-11-28 02:11:34 +01:00
|
|
|
// ACCESSORS
|
2006-08-26 13:35:28 +02:00
|
|
|
AstCFunc* nodep() const { return m_nodep; }
|
|
|
|
|
virtual string name() const { return nodep()->name(); }
|
|
|
|
|
virtual string dotColor() const { return "yellow"; }
|
2018-07-15 00:45:06 +02:00
|
|
|
virtual FileLine* fileline() const { return nodep()->fileline(); }
|
2006-08-26 13:35:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TraceTraceVertex : public V3GraphVertex {
|
2020-05-09 00:42:34 +02:00
|
|
|
AstTraceDecl* const m_nodep; // TRACEINC this represents
|
2020-04-14 04:51:35 +02:00
|
|
|
// NULL, or other vertex with the real code() that duplicates this one
|
|
|
|
|
TraceTraceVertex* m_duplicatep;
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
public:
|
2020-05-09 00:42:34 +02:00
|
|
|
TraceTraceVertex(V3Graph* graphp, AstTraceDecl* nodep)
|
2020-04-14 04:51:35 +02:00
|
|
|
: V3GraphVertex(graphp)
|
|
|
|
|
, m_nodep(nodep)
|
|
|
|
|
, m_duplicatep(NULL) {}
|
2006-08-26 13:35:28 +02:00
|
|
|
virtual ~TraceTraceVertex() {}
|
2017-11-28 02:11:34 +01:00
|
|
|
// ACCESSORS
|
2020-05-09 00:42:34 +02:00
|
|
|
AstTraceDecl* nodep() const { return m_nodep; }
|
|
|
|
|
virtual string name() const { return nodep()->name(); }
|
2006-08-26 13:35:28 +02:00
|
|
|
virtual string dotColor() const { return "red"; }
|
2018-07-15 00:45:06 +02:00
|
|
|
virtual FileLine* fileline() const { return nodep()->fileline(); }
|
2006-08-26 13:35:28 +02:00
|
|
|
TraceTraceVertex* duplicatep() const { return m_duplicatep; }
|
|
|
|
|
void duplicatep(TraceTraceVertex* dupp) {
|
2020-04-14 04:51:35 +02:00
|
|
|
UASSERT_OBJ(!duplicatep(), nodep(), "Assigning duplicatep() to already duplicated node");
|
2019-05-19 22:13:13 +02:00
|
|
|
m_duplicatep = dupp;
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TraceVarVertex : public V3GraphVertex {
|
2020-04-14 04:51:35 +02:00
|
|
|
AstVarScope* m_nodep;
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
public:
|
|
|
|
|
TraceVarVertex(V3Graph* graphp, AstVarScope* nodep)
|
2020-04-14 04:51:35 +02:00
|
|
|
: V3GraphVertex(graphp)
|
|
|
|
|
, m_nodep(nodep) {}
|
2006-08-26 13:35:28 +02:00
|
|
|
virtual ~TraceVarVertex() {}
|
2017-11-28 02:11:34 +01:00
|
|
|
// ACCESSORS
|
2006-08-26 13:35:28 +02:00
|
|
|
AstVarScope* nodep() const { return m_nodep; }
|
|
|
|
|
virtual string name() const { return nodep()->name(); }
|
|
|
|
|
virtual string dotColor() const { return "skyblue"; }
|
2018-07-15 00:45:06 +02:00
|
|
|
virtual FileLine* fileline() const { return nodep()->fileline(); }
|
2006-08-26 13:35:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Trace state, as a visitor of each AstNode
|
|
|
|
|
|
|
|
|
|
class TraceVisitor : public EmitCBaseVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// NODE STATE
|
|
|
|
|
// V3Hashed
|
2019-05-19 22:13:13 +02:00
|
|
|
// Ast*::user4() // V3Hashed calculation
|
2008-06-10 03:25:10 +02:00
|
|
|
// Cleared entire netlist
|
2019-05-19 22:13:13 +02:00
|
|
|
// AstCFunc::user1() // V3GraphVertex* for this node
|
2020-05-09 00:42:34 +02:00
|
|
|
// AstTraceDecl::user1() // V3GraphVertex* for this node
|
2019-05-19 22:13:13 +02:00
|
|
|
// AstVarScope::user1() // V3GraphVertex* for this node
|
|
|
|
|
// AstCCall::user2() // bool; walked next list for other ccalls
|
|
|
|
|
// Ast*::user3() // TraceActivityVertex* for this node
|
2020-04-14 04:51:35 +02:00
|
|
|
AstUser1InUse m_inuser1;
|
|
|
|
|
AstUser2InUse m_inuser2;
|
|
|
|
|
AstUser3InUse m_inuser3;
|
|
|
|
|
// AstUser4InUse In V3Hashed
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
// STATE
|
2020-04-14 04:51:35 +02:00
|
|
|
AstNodeModule* m_topModp; // Module to add variables to
|
2020-05-09 12:38:32 +02:00
|
|
|
AstScope* m_topScopep; // Scope to add variables to
|
2020-04-14 04:51:35 +02:00
|
|
|
AstCFunc* m_funcp; // C function adding to graph
|
2020-05-09 00:42:34 +02:00
|
|
|
AstTraceDecl* m_tracep; // Trace function adding to graph
|
2020-04-14 04:51:35 +02:00
|
|
|
AstVarScope* m_activityVscp; // Activity variable
|
|
|
|
|
uint32_t m_activityNumber; // Count of fields in activity variable
|
|
|
|
|
uint32_t m_code; // Trace ident code# being assigned
|
|
|
|
|
V3Graph m_graph; // Var/CFunc tracking
|
2020-05-09 12:38:32 +02:00
|
|
|
TraceActivityVertex* const m_alwaysVtxp; // "Always trace" vertex
|
2020-04-14 04:51:35 +02:00
|
|
|
bool m_finding; // Pass one of algorithm?
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2019-10-05 13:54:14 +02:00
|
|
|
VDouble0 m_statChgSigs; // Statistic tracking
|
|
|
|
|
VDouble0 m_statUniqSigs; // Statistic tracking
|
|
|
|
|
VDouble0 m_statUniqCodes; // Statistic tracking
|
2008-11-17 23:13:57 +01:00
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
// All activity numbers applying to a given trace
|
|
|
|
|
typedef std::set<uint32_t> ActCodeSet;
|
|
|
|
|
// For activity set, what traces apply
|
2020-05-17 14:53:02 +02:00
|
|
|
typedef std::multimap<ActCodeSet, TraceTraceVertex*> TraceVec;
|
2020-05-09 00:42:34 +02:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
// METHODS
|
2018-05-14 12:50:47 +02:00
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2009-01-21 22:56:50 +01:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
void detectDuplicates() {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(9, "Finding duplicates\n");
|
2019-05-19 22:13:13 +02:00
|
|
|
// Note uses user4
|
|
|
|
|
V3Hashed hashed; // Duplicate code detection
|
|
|
|
|
// Hash all of the values the traceIncs need
|
2020-05-09 12:38:32 +02:00
|
|
|
for (const V3GraphVertex* itp = m_graph.verticesBeginp(); itp;
|
|
|
|
|
itp = itp->verticesNextp()) {
|
|
|
|
|
if (const TraceTraceVertex* const vvertexp
|
|
|
|
|
= dynamic_cast<const TraceTraceVertex*>(itp)) {
|
2020-05-09 00:42:34 +02:00
|
|
|
const AstTraceDecl* const nodep = vvertexp->nodep();
|
2019-05-19 22:13:13 +02:00
|
|
|
if (nodep->valuep()) {
|
2019-07-06 18:57:50 +02:00
|
|
|
UASSERT_OBJ(nodep->valuep()->backp() == nodep, nodep,
|
|
|
|
|
"Trace duplicate back needs consistency,"
|
|
|
|
|
" so we can map duplicates back to TRACEINCs");
|
2019-05-19 22:13:13 +02:00
|
|
|
hashed.hash(nodep->valuep());
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(8, " Hashed " << std::hex << hashed.nodeHash(nodep->valuep()) << " "
|
|
|
|
|
<< nodep << endl);
|
2019-05-19 22:13:13 +02:00
|
|
|
|
|
|
|
|
// Just keep one node in the map and point all duplicates to this node
|
|
|
|
|
if (hashed.findDuplicate(nodep->valuep()) == hashed.end()) {
|
|
|
|
|
hashed.hashAndInsert(nodep->valuep());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Find if there are any duplicates
|
2020-04-14 04:51:35 +02:00
|
|
|
for (V3GraphVertex* itp = m_graph.verticesBeginp(); itp; itp = itp->verticesNextp()) {
|
2020-05-09 12:38:32 +02:00
|
|
|
if (TraceTraceVertex* const vvertexp = dynamic_cast<TraceTraceVertex*>(itp)) {
|
2020-05-09 00:42:34 +02:00
|
|
|
AstTraceDecl* const nodep = vvertexp->nodep();
|
2019-05-19 22:13:13 +02:00
|
|
|
if (nodep->valuep() && !vvertexp->duplicatep()) {
|
|
|
|
|
V3Hashed::iterator dupit = hashed.findDuplicate(nodep->valuep());
|
|
|
|
|
if (dupit != hashed.end()) {
|
2020-05-09 00:42:34 +02:00
|
|
|
const AstTraceDecl* const dupDeclp
|
|
|
|
|
= VN_CAST_CONST(hashed.iteratorNodep(dupit)->backp(), TraceDecl);
|
|
|
|
|
UASSERT_OBJ(dupDeclp, nodep, "Trace duplicate of wrong type");
|
2020-05-09 12:38:32 +02:00
|
|
|
TraceTraceVertex* const dupvertexp
|
2020-05-09 00:42:34 +02:00
|
|
|
= dynamic_cast<TraceTraceVertex*>(dupDeclp->user1u().toGraphVertex());
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(8, " Orig " << nodep << endl);
|
2020-05-09 00:42:34 +02:00
|
|
|
UINFO(8, " dup " << dupDeclp << endl);
|
2019-05-19 22:13:13 +02:00
|
|
|
// Mark the hashed node as the original and our
|
|
|
|
|
// iterating node as duplicated
|
|
|
|
|
vvertexp->duplicatep(dupvertexp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
hashed.clear();
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-17 14:53:02 +02:00
|
|
|
void graphSimplify(bool initial) {
|
|
|
|
|
if (initial) {
|
|
|
|
|
// Remove all variable nodes
|
|
|
|
|
for (V3GraphVertex *nextp, *itp = m_graph.verticesBeginp(); itp; itp = nextp) {
|
|
|
|
|
nextp = itp->verticesNextp();
|
|
|
|
|
if (TraceVarVertex* const vvertexp = dynamic_cast<TraceVarVertex*>(itp)) {
|
|
|
|
|
vvertexp->rerouteEdges(&m_graph);
|
|
|
|
|
vvertexp->unlinkDelete(&m_graph);
|
|
|
|
|
}
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2020-05-17 14:53:02 +02:00
|
|
|
// Remove multiple variables connecting funcs to traces
|
|
|
|
|
// We do this twice, as then we have fewer edges to multiply out in the below
|
|
|
|
|
// expansion.
|
|
|
|
|
m_graph.removeRedundantEdges(&V3GraphEdge::followAlwaysTrue);
|
|
|
|
|
// Remove all Cfunc nodes
|
|
|
|
|
for (V3GraphVertex *nextp, *itp = m_graph.verticesBeginp(); itp; itp = nextp) {
|
|
|
|
|
nextp = itp->verticesNextp();
|
|
|
|
|
if (TraceCFuncVertex* const vvertexp = dynamic_cast<TraceCFuncVertex*>(itp)) {
|
|
|
|
|
vvertexp->rerouteEdges(&m_graph);
|
|
|
|
|
vvertexp->unlinkDelete(&m_graph);
|
|
|
|
|
}
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove multiple variables connecting funcs to traces
|
|
|
|
|
m_graph.removeRedundantEdges(&V3GraphEdge::followAlwaysTrue);
|
|
|
|
|
|
|
|
|
|
// If there are any edges from a always, keep only the always
|
2020-05-09 12:38:32 +02:00
|
|
|
for (const V3GraphVertex* itp = m_graph.verticesBeginp(); itp;
|
|
|
|
|
itp = itp->verticesNextp()) {
|
|
|
|
|
if (const TraceTraceVertex* const vvertexp
|
|
|
|
|
= dynamic_cast<const TraceTraceVertex*>(itp)) {
|
|
|
|
|
// Search for the incoming always edge
|
|
|
|
|
const V3GraphEdge* alwaysEdgep = NULL;
|
|
|
|
|
for (const V3GraphEdge* edgep = vvertexp->inBeginp(); edgep;
|
|
|
|
|
edgep = edgep->inNextp()) {
|
|
|
|
|
const TraceActivityVertex* const actVtxp
|
|
|
|
|
= dynamic_cast<const TraceActivityVertex*>(edgep->fromp());
|
2019-07-06 18:57:50 +02:00
|
|
|
UASSERT_OBJ(actVtxp, vvertexp->nodep(),
|
|
|
|
|
"Tracing a node with FROM non activity");
|
2019-05-19 22:13:13 +02:00
|
|
|
if (actVtxp->activityAlways()) {
|
|
|
|
|
alwaysEdgep = edgep;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-09 12:38:32 +02:00
|
|
|
// If always edge exists, remove all other edges
|
2019-05-19 22:13:13 +02:00
|
|
|
if (alwaysEdgep) {
|
2020-04-14 04:51:35 +02:00
|
|
|
for (V3GraphEdge *nextp, *edgep = vvertexp->inBeginp(); edgep; edgep = nextp) {
|
2019-05-19 22:13:13 +02:00
|
|
|
nextp = edgep->inNextp();
|
2020-04-14 04:51:35 +02:00
|
|
|
if (edgep != alwaysEdgep) edgep->unlinkDelete();
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Activity points with no outputs can be removed
|
2020-04-14 04:51:35 +02:00
|
|
|
for (V3GraphVertex *nextp, *itp = m_graph.verticesBeginp(); itp; itp = nextp) {
|
2019-05-19 22:13:13 +02:00
|
|
|
nextp = itp->verticesNextp();
|
2020-05-17 14:53:02 +02:00
|
|
|
if (TraceActivityVertex* const vtxp = dynamic_cast<TraceActivityVertex*>(itp)) {
|
|
|
|
|
// Leave in the always vertex for later use.
|
|
|
|
|
if (vtxp != m_alwaysVtxp && !vtxp->outBeginp()) { vtxp->unlinkDelete(&m_graph); }
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-17 14:53:02 +02:00
|
|
|
uint32_t assignactivityNumbers() {
|
|
|
|
|
uint32_t activityNumber = 1; // Note 0 indicates "slow"
|
2020-04-14 04:51:35 +02:00
|
|
|
for (V3GraphVertex* itp = m_graph.verticesBeginp(); itp; itp = itp->verticesNextp()) {
|
2020-05-09 12:38:32 +02:00
|
|
|
if (TraceActivityVertex* const vvertexp = dynamic_cast<TraceActivityVertex*>(itp)) {
|
2020-05-17 14:53:02 +02:00
|
|
|
if (vvertexp != m_alwaysVtxp) {
|
2019-05-19 22:13:13 +02:00
|
|
|
if (vvertexp->slow()) {
|
|
|
|
|
vvertexp->activityCode(TraceActivityVertex::ACTIVITY_SLOW);
|
|
|
|
|
} else {
|
2020-05-17 14:53:02 +02:00
|
|
|
vvertexp->activityCode(activityNumber++);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-17 14:53:02 +02:00
|
|
|
return activityNumber;
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2020-05-17 14:53:02 +02:00
|
|
|
void sortTraces(TraceVec& traces, uint32_t& nFullCodes, uint32_t& nChgCodes) {
|
|
|
|
|
// Populate sort structure
|
|
|
|
|
traces.clear();
|
|
|
|
|
nFullCodes = 0;
|
|
|
|
|
nChgCodes = 0;
|
|
|
|
|
for (V3GraphVertex* itp = m_graph.verticesBeginp(); itp; itp = itp->verticesNextp()) {
|
|
|
|
|
if (TraceTraceVertex* const vtxp = dynamic_cast<TraceTraceVertex*>(itp)) {
|
|
|
|
|
ActCodeSet actSet;
|
|
|
|
|
UINFO(9, " Add to sort: " << vtxp << endl);
|
|
|
|
|
if (debug() >= 9) vtxp->nodep()->dumpTree(cout, "- trnode: ");
|
|
|
|
|
for (const V3GraphEdge* edgep = vtxp->inBeginp(); edgep;
|
|
|
|
|
edgep = edgep->inNextp()) {
|
|
|
|
|
const TraceActivityVertex* const cfvertexp
|
|
|
|
|
= dynamic_cast<const TraceActivityVertex*>(edgep->fromp());
|
|
|
|
|
UASSERT_OBJ(cfvertexp, vtxp->nodep(),
|
|
|
|
|
"Should have been function pointing to this trace");
|
|
|
|
|
UINFO(9, " Activity: " << cfvertexp << endl);
|
|
|
|
|
if (cfvertexp->activityAlways()) {
|
|
|
|
|
// If code 0, we always trace; ignore other codes
|
|
|
|
|
actSet.insert(TraceActivityVertex::ACTIVITY_ALWAYS);
|
|
|
|
|
} else {
|
|
|
|
|
actSet.insert(cfvertexp->activityCode());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
UASSERT_OBJ(actSet.count(TraceActivityVertex::ACTIVITY_ALWAYS) == 0
|
|
|
|
|
|| actSet.size() == 1,
|
|
|
|
|
vtxp->nodep(), "Always active trace has further triggers");
|
|
|
|
|
// Count nodes
|
|
|
|
|
if (!vtxp->duplicatep()) {
|
|
|
|
|
const uint32_t inc = vtxp->nodep()->codeInc();
|
|
|
|
|
nFullCodes += inc;
|
|
|
|
|
if (!actSet.empty()) nChgCodes += inc;
|
|
|
|
|
}
|
|
|
|
|
// If a trace doesn't have activity, it's constant, and we
|
|
|
|
|
// don't need to track changes on it.
|
|
|
|
|
// We put constants and non-changers last, as then the
|
|
|
|
|
// prevvalue vector is more compacted
|
|
|
|
|
if (actSet.empty()) actSet.insert(TraceActivityVertex::ACTIVITY_NEVER);
|
|
|
|
|
traces.insert(make_pair(actSet, vtxp));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-09 12:38:32 +02:00
|
|
|
|
2020-05-17 14:53:02 +02:00
|
|
|
void graphOptimize() {
|
|
|
|
|
// Assign initial activity numbers to activity vertices
|
|
|
|
|
assignactivityNumbers();
|
|
|
|
|
|
|
|
|
|
// Sort the traces by activity sets
|
|
|
|
|
TraceVec traces;
|
|
|
|
|
uint32_t unused1, unused2;
|
|
|
|
|
sortTraces(traces, unused1, unused2);
|
|
|
|
|
|
|
|
|
|
// For each activity set with only a small number of signals, make those
|
|
|
|
|
// signals always traced, as it's cheaper to check a few value changes
|
|
|
|
|
// than to test a lot of activity flags
|
|
|
|
|
TraceVec::iterator it = traces.begin();
|
|
|
|
|
const TraceVec::iterator end = traces.end();
|
|
|
|
|
while (it != end) {
|
|
|
|
|
TraceVec::iterator head = it;
|
|
|
|
|
// Count the value comparisons in all traces under this activity set
|
|
|
|
|
uint32_t valueComparisons = 0;
|
|
|
|
|
const ActCodeSet& actSet = it->first;
|
|
|
|
|
for (; it != end && it->first == actSet; ++it) {
|
|
|
|
|
valueComparisons += it->second->nodep()->codeInc();
|
|
|
|
|
}
|
|
|
|
|
// Leave alone always changing, never changing and signals only set in slow code
|
|
|
|
|
if (actSet.count(TraceActivityVertex::ACTIVITY_ALWAYS)) continue;
|
|
|
|
|
if (actSet.count(TraceActivityVertex::ACTIVITY_NEVER)) continue;
|
|
|
|
|
if (actSet.count(TraceActivityVertex::ACTIVITY_SLOW) && actSet.size() == 1) continue;
|
|
|
|
|
// If the value comparisons are cheaper to perform than checking the
|
|
|
|
|
// activity flags make the signals always traced. Note this cost
|
|
|
|
|
// equation is heuristic.
|
|
|
|
|
if (valueComparisons < actSet.size() * 2) {
|
|
|
|
|
for (; head != it; ++head) {
|
|
|
|
|
new V3GraphEdge(&m_graph, m_alwaysVtxp, head->second, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-23 02:54:28 +02:00
|
|
|
}
|
2020-05-17 14:53:02 +02:00
|
|
|
|
|
|
|
|
graphSimplify(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AstNode* selectActivity(FileLine* flp, uint32_t acode, bool lvalue) {
|
|
|
|
|
return new AstArraySel(flp, new AstVarRef(flp, m_activityVscp, lvalue), acode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void createActivityFlags() {
|
|
|
|
|
// Assign final activity numbers
|
|
|
|
|
m_activityNumber = assignactivityNumbers();
|
|
|
|
|
|
|
|
|
|
// Create an array of bytes, not a bit vector, as they can be set
|
|
|
|
|
// atomically by mtasks, and are cheaper to set (not need for
|
|
|
|
|
// read-modify-write on the C type), and the speed of the tracing code
|
|
|
|
|
// is the same on largish designs.
|
|
|
|
|
FileLine* const flp = m_topScopep->fileline();
|
|
|
|
|
AstNodeDType* const newScalarDtp = new AstBasicDType(flp, VFlagLogicPacked(), 1);
|
|
|
|
|
v3Global.rootp()->typeTablep()->addTypesp(newScalarDtp);
|
|
|
|
|
AstRange* const newArange = new AstRange(flp, VNumRange(m_activityNumber - 1, 0, false));
|
|
|
|
|
AstNodeDType* const newArrDtp = new AstUnpackArrayDType(flp, newScalarDtp, newArange);
|
|
|
|
|
v3Global.rootp()->typeTablep()->addTypesp(newArrDtp);
|
|
|
|
|
AstVar* const newvarp
|
|
|
|
|
= new AstVar(flp, AstVarType::MODULETEMP, "__Vm_traceActivity", newArrDtp);
|
2019-05-19 22:13:13 +02:00
|
|
|
m_topModp->addStmtp(newvarp);
|
2020-05-09 12:38:32 +02:00
|
|
|
AstVarScope* const newvscp = new AstVarScope(flp, m_topScopep, newvarp);
|
|
|
|
|
m_topScopep->addVarp(newvscp);
|
2019-05-19 22:13:13 +02:00
|
|
|
m_activityVscp = newvscp;
|
|
|
|
|
|
2020-05-17 14:53:02 +02:00
|
|
|
// Insert activity setters
|
2020-05-09 12:38:32 +02:00
|
|
|
for (const V3GraphVertex* itp = m_graph.verticesBeginp(); itp;
|
|
|
|
|
itp = itp->verticesNextp()) {
|
2020-05-17 14:53:02 +02:00
|
|
|
if (const TraceActivityVertex* const vtxp
|
2020-05-09 12:38:32 +02:00
|
|
|
= dynamic_cast<const TraceActivityVertex*>(itp)) {
|
2020-05-17 14:53:02 +02:00
|
|
|
if (!vtxp->activityAlways()) {
|
|
|
|
|
FileLine* const fl = vtxp->insertp()->fileline();
|
|
|
|
|
const uint32_t acode = vtxp->activityCode();
|
|
|
|
|
AstAssign* const setterp
|
|
|
|
|
= new AstAssign(fl, selectActivity(fl, acode, true),
|
|
|
|
|
new AstConst(fl, AstConst::LogicTrue()));
|
|
|
|
|
if (AstCCall* const callp = VN_CAST(vtxp->insertp(), CCall)) {
|
|
|
|
|
callp->addNextHere(setterp);
|
|
|
|
|
} else if (AstCFunc* const funcp = VN_CAST(vtxp->insertp(), CFunc)) {
|
|
|
|
|
funcp->addStmtsp(setterp);
|
|
|
|
|
} else {
|
|
|
|
|
vtxp->insertp()->v3fatalSrc("Bad trace activity vertex");
|
|
|
|
|
}
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
AstCFunc* newCFunc(AstCFuncType type, AstCFunc* callfromp, AstCFunc* regp, int& funcNump) {
|
|
|
|
|
// Create new function
|
|
|
|
|
string name;
|
|
|
|
|
switch (type) {
|
|
|
|
|
case AstCFuncType::TRACE_FULL: name = "traceFullTop"; break;
|
|
|
|
|
case AstCFuncType::TRACE_FULL_SUB: name = "traceFullSub"; break;
|
|
|
|
|
case AstCFuncType::TRACE_CHANGE: name = "traceChgTop"; break;
|
|
|
|
|
case AstCFuncType::TRACE_CHANGE_SUB: name = "traceChgSub"; break;
|
|
|
|
|
default: m_topScopep->v3fatalSrc("Bad trace function type");
|
2018-07-23 02:54:28 +02:00
|
|
|
}
|
2020-05-09 00:42:34 +02:00
|
|
|
name += cvtToStr(funcNump++);
|
|
|
|
|
FileLine* const flp = m_topScopep->fileline();
|
|
|
|
|
AstCFunc* const funcp = new AstCFunc(flp, name, m_topScopep);
|
|
|
|
|
const string argTypes("void* userp, " + v3Global.opt.traceClassBase() + "* tracep");
|
|
|
|
|
funcp->argTypes(argTypes);
|
2019-05-19 22:13:13 +02:00
|
|
|
funcp->funcType(type);
|
2020-05-09 00:42:34 +02:00
|
|
|
funcp->slow(type == AstCFuncType::TRACE_FULL || type == AstCFuncType::TRACE_FULL_SUB);
|
2019-05-19 22:13:13 +02:00
|
|
|
funcp->symProlog(true);
|
2020-05-09 00:42:34 +02:00
|
|
|
funcp->declPrivate(true);
|
|
|
|
|
// Add it to top scope
|
|
|
|
|
m_topScopep->addActivep(funcp);
|
|
|
|
|
// Add call to new function
|
|
|
|
|
if (callfromp) {
|
|
|
|
|
AstCCall* callp = new AstCCall(funcp->fileline(), funcp);
|
|
|
|
|
callp->argTypes("userp, tracep");
|
|
|
|
|
callfromp->addStmtsp(callp);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2020-05-09 00:42:34 +02:00
|
|
|
// Register function
|
|
|
|
|
if (regp) {
|
|
|
|
|
string registration = "tracep->add";
|
|
|
|
|
if (type == AstCFuncType::TRACE_FULL) {
|
|
|
|
|
registration += "Full";
|
|
|
|
|
} else if (type == AstCFuncType::TRACE_CHANGE) {
|
|
|
|
|
registration += "Chg";
|
|
|
|
|
} else {
|
|
|
|
|
funcp->v3fatal("Don't know how to register this type of function");
|
|
|
|
|
}
|
|
|
|
|
registration += "Cb(&" + protect(name) + ", __VlSymsp);\n";
|
|
|
|
|
AstCStmt* const stmtp = new AstCStmt(flp, registration);
|
|
|
|
|
regp->addStmtsp(stmtp);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2020-05-09 00:42:34 +02:00
|
|
|
// Add global activity check to TRACE_CHANGE functions
|
|
|
|
|
if (type == AstCFuncType::TRACE_CHANGE) {
|
|
|
|
|
funcp->addInitsp(
|
|
|
|
|
new AstCStmt(flp, string("if (VL_UNLIKELY(!vlSymsp->__Vm_activity)) return;\n")));
|
|
|
|
|
}
|
|
|
|
|
// Done
|
|
|
|
|
UINFO(5, " newCFunc " << funcp << endl);
|
2019-05-19 22:13:13 +02:00
|
|
|
return funcp;
|
2008-11-17 23:13:57 +01:00
|
|
|
}
|
2020-05-09 12:38:32 +02:00
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
void assignDeclCode(AstTraceDecl* nodep) {
|
2020-05-09 12:38:32 +02:00
|
|
|
if (!nodep->code()) {
|
|
|
|
|
nodep->code(m_code);
|
|
|
|
|
m_code += nodep->codeInc();
|
|
|
|
|
m_statUniqCodes += nodep->codeInc();
|
|
|
|
|
++m_statUniqSigs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
void createFullTraceFunction(const TraceVec& traces, uint32_t nAllCodes, uint32_t parallelism,
|
|
|
|
|
AstCFunc* regFuncp) {
|
|
|
|
|
const int splitLimit = v3Global.opt.outputSplitCTrace() ? v3Global.opt.outputSplitCTrace()
|
|
|
|
|
: std::numeric_limits<int>::max();
|
|
|
|
|
|
|
|
|
|
int topFuncNum = 0;
|
|
|
|
|
int subFuncNum = 0;
|
|
|
|
|
TraceVec::const_iterator it = traces.begin();
|
|
|
|
|
while (it != traces.end()) {
|
|
|
|
|
AstCFunc* topFuncp = NULL;
|
|
|
|
|
AstCFunc* subFuncp = NULL;
|
|
|
|
|
int subStmts = 0;
|
|
|
|
|
const uint32_t maxCodes = (nAllCodes + parallelism - 1) / parallelism;
|
|
|
|
|
uint32_t nCodes = 0;
|
|
|
|
|
for (; nCodes < maxCodes && it != traces.end(); ++it) {
|
|
|
|
|
const TraceTraceVertex* const vtxp = it->second;
|
|
|
|
|
AstTraceDecl* const declp = vtxp->nodep();
|
|
|
|
|
if (const TraceTraceVertex* const canonVtxp = vtxp->duplicatep()) {
|
|
|
|
|
// This is a duplicate trace node. We will assign the signal
|
|
|
|
|
// number to the canonical node, and emit this as an alias, so
|
|
|
|
|
// no need to create a TraceInc node.
|
|
|
|
|
AstTraceDecl* const canonDeclp = canonVtxp->nodep();
|
|
|
|
|
UASSERT_OBJ(!canonVtxp->duplicatep(), canonDeclp,
|
|
|
|
|
"Canonical node is a duplicate");
|
|
|
|
|
assignDeclCode(canonDeclp);
|
|
|
|
|
declp->code(canonDeclp->code());
|
2019-05-19 22:13:13 +02:00
|
|
|
} else {
|
2020-05-09 00:42:34 +02:00
|
|
|
// This is a canonical trace node. Assign signal number and
|
|
|
|
|
// add a TraceInc node to the full dump function.
|
|
|
|
|
assignDeclCode(declp);
|
|
|
|
|
|
|
|
|
|
// Create top function if not yet created
|
|
|
|
|
if (!topFuncp) {
|
|
|
|
|
topFuncp = newCFunc(AstCFuncType::TRACE_FULL, NULL, regFuncp, topFuncNum);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2008-11-17 23:13:57 +01:00
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
// Crate new sub function if required
|
|
|
|
|
if (!subFuncp || subStmts > splitLimit) {
|
|
|
|
|
subStmts = 0;
|
|
|
|
|
subFuncp
|
|
|
|
|
= newCFunc(AstCFuncType::TRACE_FULL_SUB, topFuncp, NULL, subFuncNum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add TraceInc node
|
|
|
|
|
AstTraceInc* const incp = new AstTraceInc(declp->fileline(), declp, true);
|
|
|
|
|
subFuncp->addStmtsp(incp);
|
|
|
|
|
subStmts += EmitCBaseCounterVisitor(incp).count();
|
|
|
|
|
|
|
|
|
|
// Track partitioning
|
|
|
|
|
nCodes += declp->codeInc();
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-05-09 00:42:34 +02:00
|
|
|
if (topFuncp) { // might be NULL if all trailing entries were duplicates
|
|
|
|
|
UINFO(5, "traceFullTop" << topFuncNum - 1 << " codes: " << nCodes << "/"
|
|
|
|
|
<< maxCodes << endl);
|
|
|
|
|
}
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2020-05-09 00:42:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void createChgTraceFunctions(const TraceVec& traces, uint32_t nAllCodes, uint32_t parallelism,
|
|
|
|
|
AstCFunc* regFuncp) {
|
|
|
|
|
const int splitLimit = v3Global.opt.outputSplitCTrace() ? v3Global.opt.outputSplitCTrace()
|
|
|
|
|
: std::numeric_limits<int>::max();
|
|
|
|
|
int topFuncNum = 0;
|
|
|
|
|
int subFuncNum = 0;
|
|
|
|
|
TraceVec::const_iterator it = traces.begin();
|
|
|
|
|
while (it != traces.end()) {
|
|
|
|
|
AstCFunc* topFuncp = NULL;
|
|
|
|
|
AstCFunc* subFuncp = NULL;
|
|
|
|
|
int subStmts = 0;
|
|
|
|
|
const uint32_t maxCodes = (nAllCodes + parallelism - 1) / parallelism;
|
|
|
|
|
uint32_t nCodes = 0;
|
|
|
|
|
const ActCodeSet* prevActSet = NULL;
|
|
|
|
|
AstIf* ifp = NULL;
|
|
|
|
|
for (; nCodes < maxCodes && it != traces.end(); ++it) {
|
|
|
|
|
const TraceTraceVertex* const vtxp = it->second;
|
|
|
|
|
// This is a duplicate decl, no need to add it to incremental dump
|
|
|
|
|
if (vtxp->duplicatep()) continue;
|
|
|
|
|
const ActCodeSet& actSet = it->first;
|
|
|
|
|
// Traced value never changes, no need to add it to incremental dump
|
|
|
|
|
if (actSet.count(TraceActivityVertex::ACTIVITY_NEVER)) continue;
|
|
|
|
|
|
|
|
|
|
// Create top function if not yet created
|
|
|
|
|
if (!topFuncp) {
|
|
|
|
|
topFuncp = newCFunc(AstCFuncType::TRACE_CHANGE, NULL, regFuncp, topFuncNum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Crate new sub function if required
|
|
|
|
|
if (!subFuncp || subStmts > splitLimit) {
|
|
|
|
|
subStmts = 0;
|
|
|
|
|
subFuncp
|
|
|
|
|
= newCFunc(AstCFuncType::TRACE_CHANGE_SUB, topFuncp, NULL, subFuncNum);
|
|
|
|
|
prevActSet = NULL;
|
|
|
|
|
ifp = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If required, create the conditional node checking the activity flags
|
|
|
|
|
if (!prevActSet || actSet != *prevActSet) {
|
|
|
|
|
FileLine* const flp = m_topScopep->fileline();
|
|
|
|
|
bool always = actSet.count(TraceActivityVertex::ACTIVITY_ALWAYS) != 0;
|
|
|
|
|
AstNode* condp = NULL;
|
|
|
|
|
if (always) {
|
|
|
|
|
condp = new AstConst(flp, 1); // Always true, will be folded later
|
|
|
|
|
} else {
|
|
|
|
|
for (ActCodeSet::iterator it = actSet.begin(); it != actSet.end(); ++it) {
|
|
|
|
|
const uint32_t actCode = *it;
|
|
|
|
|
AstNode* const selp = selectActivity(flp, actCode, false);
|
|
|
|
|
condp = condp ? new AstOr(flp, condp, selp) : selp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ifp = new AstIf(flp, condp, NULL, NULL);
|
|
|
|
|
if (!always) { ifp->branchPred(VBranchPred::BP_UNLIKELY); }
|
|
|
|
|
subFuncp->addStmtsp(ifp);
|
|
|
|
|
subStmts += EmitCBaseCounterVisitor(ifp).count();
|
|
|
|
|
prevActSet = &actSet;
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
// Add TraceInc node
|
|
|
|
|
AstTraceDecl* const declp = vtxp->nodep();
|
|
|
|
|
AstTraceInc* const incp = new AstTraceInc(declp->fileline(), declp, false);
|
|
|
|
|
ifp->addIfsp(incp);
|
|
|
|
|
subStmts += EmitCBaseCounterVisitor(incp).count();
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
// Track partitioning
|
|
|
|
|
nCodes += declp->codeInc();
|
|
|
|
|
}
|
|
|
|
|
if (topFuncp) { // might be NULL if all trailing entries were duplicates/constants
|
|
|
|
|
UINFO(5, "traceChgTop" << topFuncNum - 1 << " codes: " << nCodes << "/" << maxCodes
|
|
|
|
|
<< endl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void createCleanupFunction(AstCFunc* regFuncp) {
|
|
|
|
|
FileLine* const fl = m_topScopep->fileline();
|
|
|
|
|
AstCFunc* const cleanupFuncp = new AstCFunc(fl, "traceCleanup", m_topScopep);
|
|
|
|
|
const string argTypes("void* userp, " + v3Global.opt.traceClassBase() + "* /*unused*/");
|
|
|
|
|
cleanupFuncp->argTypes(argTypes);
|
|
|
|
|
cleanupFuncp->funcType(AstCFuncType::TRACE_CLEANUP);
|
|
|
|
|
cleanupFuncp->slow(false);
|
|
|
|
|
cleanupFuncp->symProlog(true);
|
|
|
|
|
cleanupFuncp->declPrivate(true);
|
|
|
|
|
m_topScopep->addActivep(cleanupFuncp);
|
|
|
|
|
|
|
|
|
|
// Register it
|
|
|
|
|
regFuncp->addStmtsp(new AstCStmt(
|
|
|
|
|
fl, string("tracep->addCleanupCb(&" + protect("traceCleanup") + ", __VlSymsp);\n")));
|
|
|
|
|
|
|
|
|
|
// Clear global activity flag
|
|
|
|
|
cleanupFuncp->addStmtsp(
|
|
|
|
|
new AstCStmt(m_topScopep->fileline(), string("vlSymsp->__Vm_activity = false;\n")));
|
|
|
|
|
|
|
|
|
|
// Clear fine grained activity flags
|
2020-05-17 14:53:02 +02:00
|
|
|
for (uint32_t i = 0; i < m_activityNumber; ++i) {
|
|
|
|
|
AstNode* const clrp = new AstAssign(fl, selectActivity(fl, i, true),
|
|
|
|
|
new AstConst(fl, AstConst::LogicFalse()));
|
2020-05-09 00:42:34 +02:00
|
|
|
cleanupFuncp->addStmtsp(clrp);
|
2018-07-23 02:54:28 +02:00
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
void createTraceFunctions() {
|
2020-05-17 14:53:02 +02:00
|
|
|
// Detect and remove duplicate values
|
|
|
|
|
detectDuplicates();
|
2020-05-09 00:42:34 +02:00
|
|
|
|
2020-05-17 14:53:02 +02:00
|
|
|
// Simplify & optimize the graph
|
|
|
|
|
if (debug() >= 6) m_graph.dumpDotFilePrefixed("trace_pre");
|
|
|
|
|
graphSimplify(true);
|
|
|
|
|
if (debug() >= 6) m_graph.dumpDotFilePrefixed("trace_simplified");
|
|
|
|
|
graphOptimize();
|
|
|
|
|
if (debug() >= 6) m_graph.dumpDotFilePrefixed("trace_optimized");
|
|
|
|
|
|
|
|
|
|
// Create the fine grained activity flags
|
|
|
|
|
createActivityFlags();
|
|
|
|
|
|
|
|
|
|
// Form a sorted list of the traces we are interested in
|
2020-05-09 00:42:34 +02:00
|
|
|
TraceVec traces; // The sorted traces
|
2020-05-17 14:53:02 +02:00
|
|
|
// We will split functions such that each have to dump roughly the same amount of data
|
|
|
|
|
// for this we need to keep tack of the number of codes used by the trace functions.
|
2020-05-09 00:42:34 +02:00
|
|
|
uint32_t nFullCodes = 0; // Number of non-duplicate codes (need to go into full* dump)
|
|
|
|
|
uint32_t nChgCodes = 0; // Number of non-consant codes (need to go in to chg* dump)
|
|
|
|
|
sortTraces(traces, nFullCodes, nChgCodes);
|
|
|
|
|
|
|
|
|
|
UINFO(5, "nFullCodes: " << nFullCodes << " nChgCodes: " << nChgCodes << endl);
|
|
|
|
|
|
|
|
|
|
// Our keys are now sorted to have same activity number adjacent, then
|
|
|
|
|
// by trace order. (Better would be execution order for cache
|
|
|
|
|
// efficiency....) Last are constants and non-changers, as then the
|
|
|
|
|
// last value vector is more compact
|
|
|
|
|
|
|
|
|
|
// Create the trace registration function
|
|
|
|
|
AstCFunc* const regFuncp
|
|
|
|
|
= new AstCFunc(m_topScopep->fileline(), "traceRegister", m_topScopep);
|
|
|
|
|
regFuncp->argTypes(v3Global.opt.traceClassBase() + "* tracep");
|
|
|
|
|
regFuncp->funcType(AstCFuncType::TRACE_REGISTER);
|
|
|
|
|
regFuncp->slow(true);
|
|
|
|
|
regFuncp->isStatic(false);
|
|
|
|
|
regFuncp->declPrivate(true);
|
|
|
|
|
m_topScopep->addActivep(regFuncp);
|
|
|
|
|
|
|
|
|
|
const int parallelism = 1; // Note: will bump this later, code below works for any value
|
|
|
|
|
|
|
|
|
|
// Create the full dump functions, also allocates signal numbers
|
|
|
|
|
createFullTraceFunction(traces, nFullCodes, parallelism, regFuncp);
|
|
|
|
|
|
|
|
|
|
// Create the incremental dump functions
|
|
|
|
|
createChgTraceFunctions(traces, nChgCodes, parallelism, regFuncp);
|
|
|
|
|
|
2020-05-17 14:53:02 +02:00
|
|
|
// Remove refs to traced values from TraceDecl nodes, these have now moved under
|
|
|
|
|
// TraceInc
|
2020-05-09 00:42:34 +02:00
|
|
|
for (TraceVec::iterator it = traces.begin(); it != traces.end(); ++it) {
|
|
|
|
|
AstNode* const valuep = it->second->nodep()->valuep();
|
|
|
|
|
valuep->unlinkFrBack();
|
|
|
|
|
valuep->deleteTree();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the trace cleanup function clearing the activity flags
|
|
|
|
|
createCleanupFunction(regFuncp);
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
TraceCFuncVertex* getCFuncVertexp(AstCFunc* nodep) {
|
2019-05-19 22:13:13 +02:00
|
|
|
TraceCFuncVertex* vertexp
|
|
|
|
|
= dynamic_cast<TraceCFuncVertex*>(nodep->user1u().toGraphVertex());
|
|
|
|
|
if (!vertexp) {
|
|
|
|
|
vertexp = new TraceCFuncVertex(&m_graph, nodep);
|
|
|
|
|
nodep->user1p(vertexp);
|
|
|
|
|
}
|
|
|
|
|
return vertexp;
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
TraceActivityVertex* getActivityVertexp(AstNode* nodep, bool slow) {
|
2019-05-19 22:13:13 +02:00
|
|
|
TraceActivityVertex* vertexp
|
|
|
|
|
= dynamic_cast<TraceActivityVertex*>(nodep->user3u().toGraphVertex());
|
|
|
|
|
if (!vertexp) {
|
|
|
|
|
vertexp = new TraceActivityVertex(&m_graph, nodep, slow);
|
|
|
|
|
nodep->user3p(vertexp);
|
|
|
|
|
}
|
|
|
|
|
vertexp->slow(slow);
|
|
|
|
|
return vertexp;
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2020-01-21 23:35:56 +01:00
|
|
|
virtual void visit(AstNetlist* nodep) VL_OVERRIDE {
|
2019-05-19 22:13:13 +02:00
|
|
|
m_code = 1; // Multiple TopScopes will require fixing how code#s
|
|
|
|
|
// are assigned as duplicate varscopes must result in the same tracing code#.
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
// Add vertexes for all TraceDecl, and edges from VARs each trace looks at
|
2019-05-19 22:13:13 +02:00
|
|
|
m_finding = false;
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2019-05-19 22:13:13 +02:00
|
|
|
// Add vertexes for all CFUNCs, and edges to VARs the func sets
|
|
|
|
|
m_finding = true;
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2020-05-09 12:38:32 +02:00
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
// Create the trace functions and insert them into the tree
|
|
|
|
|
createTraceFunctions();
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2020-01-21 23:35:56 +01:00
|
|
|
virtual void visit(AstNodeModule* nodep) VL_OVERRIDE {
|
2019-05-19 22:13:13 +02:00
|
|
|
if (nodep->isTop()) m_topModp = nodep;
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2020-01-21 23:35:56 +01:00
|
|
|
virtual void visit(AstTopScope* nodep) VL_OVERRIDE {
|
2020-05-09 12:38:32 +02:00
|
|
|
AstScope* const scopep = nodep->scopep();
|
2019-07-06 18:57:50 +02:00
|
|
|
UASSERT_OBJ(scopep, nodep, "No scope found on top level");
|
2020-05-09 12:38:32 +02:00
|
|
|
m_topScopep = scopep;
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2020-01-21 23:35:56 +01:00
|
|
|
virtual void visit(AstCCall* nodep) VL_OVERRIDE {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(8, " CCALL " << nodep << endl);
|
2019-05-19 22:13:13 +02:00
|
|
|
if (!m_finding && !nodep->user2()) {
|
|
|
|
|
// See if there are other calls in same statement list;
|
|
|
|
|
// If so, all funcs might share the same activity code
|
2020-05-09 12:38:32 +02:00
|
|
|
TraceActivityVertex* const activityVtxp
|
|
|
|
|
= getActivityVertexp(nodep, nodep->funcp()->slow());
|
2020-04-14 04:51:35 +02:00
|
|
|
for (AstNode* nextp = nodep; nextp; nextp = nextp->nextp()) {
|
2020-05-09 12:38:32 +02:00
|
|
|
if (AstCCall* const ccallp = VN_CAST(nextp, CCall)) {
|
2019-05-19 22:13:13 +02:00
|
|
|
ccallp->user2(true); // Processed
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(8, " SubCCALL " << ccallp << endl);
|
2020-05-09 12:38:32 +02:00
|
|
|
V3GraphVertex* const ccallFuncVtxp = getCFuncVertexp(ccallp->funcp());
|
2019-05-19 22:13:13 +02:00
|
|
|
activityVtxp->slow(ccallp->funcp()->slow());
|
2018-08-25 15:52:45 +02:00
|
|
|
new V3GraphEdge(&m_graph, activityVtxp, ccallFuncVtxp, 1);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2020-01-21 23:35:56 +01:00
|
|
|
virtual void visit(AstCFunc* nodep) VL_OVERRIDE {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(8, " CFUNC " << nodep << endl);
|
2020-05-09 12:38:32 +02:00
|
|
|
V3GraphVertex* const funcVtxp = getCFuncVertexp(nodep);
|
2020-05-17 14:53:02 +02:00
|
|
|
if (!m_finding) { // If public, we need a unique activity code to allow for sets
|
|
|
|
|
// directly in this func
|
2020-04-14 04:51:35 +02:00
|
|
|
if (nodep->funcPublic() || nodep->dpiExport() || nodep == v3Global.rootp()->evalp()) {
|
2020-05-17 14:53:02 +02:00
|
|
|
V3GraphVertex* const activityVtxp = getActivityVertexp(nodep, nodep->slow());
|
2018-08-25 15:52:45 +02:00
|
|
|
new V3GraphEdge(&m_graph, activityVtxp, funcVtxp, 1);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_funcp = nodep;
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
m_funcp = NULL;
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2020-05-09 00:42:34 +02:00
|
|
|
virtual void visit(AstTraceDecl* nodep) VL_OVERRIDE {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(8, " TRACE " << nodep << endl);
|
2020-05-09 00:42:34 +02:00
|
|
|
if (!m_finding) {
|
|
|
|
|
V3GraphVertex* const vertexp = new TraceTraceVertex(&m_graph, nodep);
|
|
|
|
|
nodep->user1p(vertexp);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2020-05-09 00:42:34 +02:00
|
|
|
UASSERT_OBJ(m_funcp, nodep, "Trace not under func");
|
|
|
|
|
m_tracep = nodep;
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
m_tracep = NULL;
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2020-01-21 23:35:56 +01:00
|
|
|
virtual void visit(AstVarRef* nodep) VL_OVERRIDE {
|
2019-05-19 22:13:13 +02:00
|
|
|
if (m_tracep) {
|
2019-07-06 18:57:50 +02:00
|
|
|
UASSERT_OBJ(nodep->varScopep(), nodep, "No var scope?");
|
|
|
|
|
UASSERT_OBJ(!nodep->lvalue(), nodep, "Lvalue in trace? Should be const.");
|
2019-05-19 22:13:13 +02:00
|
|
|
V3GraphVertex* varVtxp = nodep->varScopep()->user1u().toGraphVertex();
|
|
|
|
|
if (!varVtxp) {
|
|
|
|
|
varVtxp = new TraceVarVertex(&m_graph, nodep->varScopep());
|
|
|
|
|
nodep->varScopep()->user1p(varVtxp);
|
|
|
|
|
}
|
2020-05-09 12:38:32 +02:00
|
|
|
V3GraphVertex* const traceVtxp = m_tracep->user1u().toGraphVertex();
|
2019-05-19 22:13:13 +02:00
|
|
|
new V3GraphEdge(&m_graph, varVtxp, traceVtxp, 1);
|
2018-10-27 23:29:00 +02:00
|
|
|
if (nodep->varp()->isPrimaryInish() // Always need to trace primary inputs
|
|
|
|
|
|| nodep->varp()->isSigPublic()) { // Or ones user can change
|
|
|
|
|
new V3GraphEdge(&m_graph, m_alwaysVtxp, traceVtxp, 1);
|
|
|
|
|
}
|
2020-04-14 04:51:35 +02:00
|
|
|
} else if (m_funcp && m_finding && nodep->lvalue()) {
|
2019-07-06 18:57:50 +02:00
|
|
|
UASSERT_OBJ(nodep->varScopep(), nodep, "No var scope?");
|
2020-05-09 12:38:32 +02:00
|
|
|
V3GraphVertex* const funcVtxp = getCFuncVertexp(m_funcp);
|
|
|
|
|
V3GraphVertex* const varVtxp = nodep->varScopep()->user1u().toGraphVertex();
|
2019-05-19 22:13:13 +02:00
|
|
|
if (varVtxp) { // else we're not tracing this signal
|
|
|
|
|
new V3GraphEdge(&m_graph, funcVtxp, varVtxp, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
//--------------------
|
2020-04-04 14:31:14 +02:00
|
|
|
virtual void visit(AstNode* nodep) VL_OVERRIDE { iterateChildren(nodep); }
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
public:
|
2019-09-12 13:22:22 +02:00
|
|
|
// CONSTRUCTORS
|
2020-05-09 12:38:32 +02:00
|
|
|
explicit TraceVisitor(AstNetlist* nodep)
|
|
|
|
|
: m_alwaysVtxp(new TraceActivityVertex(&m_graph, TraceActivityVertex::ACTIVITY_ALWAYS)) {
|
2019-05-19 22:13:13 +02:00
|
|
|
m_funcp = NULL;
|
|
|
|
|
m_tracep = NULL;
|
|
|
|
|
m_topModp = NULL;
|
2020-05-09 12:38:32 +02:00
|
|
|
m_topScopep = NULL;
|
2019-05-19 22:13:13 +02:00
|
|
|
m_finding = false;
|
|
|
|
|
m_activityVscp = NULL;
|
2018-07-23 02:54:28 +02:00
|
|
|
m_activityNumber = 0;
|
2018-06-15 00:59:24 +02:00
|
|
|
m_code = 0;
|
2018-05-11 02:55:37 +02:00
|
|
|
iterate(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
virtual ~TraceVisitor() {
|
2019-05-19 22:13:13 +02:00
|
|
|
V3Stats::addStat("Tracing, Unique changing signals", m_statChgSigs);
|
|
|
|
|
V3Stats::addStat("Tracing, Unique traced signals", m_statUniqSigs);
|
|
|
|
|
V3Stats::addStat("Tracing, Unique trace codes", m_statUniqCodes);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Trace class functions
|
|
|
|
|
|
|
|
|
|
void V3Trace::traceAll(AstNetlist* nodep) {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
|
|
|
|
{ TraceVisitor visitor(nodep); } // Destruct before checking
|
2017-09-18 04:52:57 +02:00
|
|
|
V3Global::dumpCheckGlobalTree("trace", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 3);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|