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: Collect and print statistics
|
|
|
|
|
//
|
2019-11-08 04:33:59 +01:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2022-01-01 14:26:40 +01:00
|
|
|
// Copyright 2005-2022 by Wilson Snyder. This program is free software; you
|
2020-03-21 16:24:24 +01:00
|
|
|
// 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
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2019-10-05 02:17:11 +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 "V3Stats.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
#include "V3Ast.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
#include "V3Global.h"
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2014-04-01 02:06:16 +02:00
|
|
|
// This visitor does not edit nodes, and is called at error-exit, so should use constant iterators
|
|
|
|
|
#include "V3AstConstOnly.h"
|
|
|
|
|
|
2018-10-14 19:43:24 +02:00
|
|
|
#include <iomanip>
|
|
|
|
|
#include <map>
|
|
|
|
|
|
2022-09-18 21:53:42 +02:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
//######################################################################
|
|
|
|
|
// Stats class functions
|
|
|
|
|
|
2022-01-02 19:56:40 +01:00
|
|
|
class StatsVisitor final : public VNVisitor {
|
2006-08-26 13:35:28 +02:00
|
|
|
private:
|
|
|
|
|
// NODE STATE/TYPES
|
2014-12-20 14:28:31 +01:00
|
|
|
|
2021-03-13 00:10:45 +01:00
|
|
|
using NameMap = std::map<const std::string, int>; // Number of times a name appears
|
2014-12-20 14:28:31 +01:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
// STATE
|
2021-11-26 23:55:36 +01:00
|
|
|
const string m_stage; // Name of the stage we are scanning
|
2017-10-31 03:38:47 +01:00
|
|
|
/// m_fast = true: Counting only critical branch of fastpath
|
|
|
|
|
/// m_fast = false: Counting every node, ignoring structure of program
|
2021-11-26 23:55:36 +01:00
|
|
|
const bool m_fast;
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2021-11-26 23:55:36 +01:00
|
|
|
const AstCFunc* m_cfuncp; // Current CFUNC
|
2020-04-14 04:51:35 +02:00
|
|
|
bool m_counting; // Currently counting
|
|
|
|
|
double m_instrs; // Current instr count (for determining branch direction)
|
|
|
|
|
bool m_tracingCall; // Iterating into a CCall to a CFunc
|
2008-06-10 03:25:10 +02:00
|
|
|
|
2020-04-14 04:51:35 +02:00
|
|
|
std::vector<VDouble0> m_statTypeCount; // Nodes of given type
|
2022-01-02 19:56:40 +01:00
|
|
|
VDouble0 m_statAbove[VNType::_ENUM_END][VNType::_ENUM_END]; // Nodes of given type
|
2020-11-15 22:21:26 +01:00
|
|
|
std::array<VDouble0, VBranchPred::_ENUM_END> m_statPred; // Nodes of given type
|
2020-04-14 04:51:35 +02:00
|
|
|
VDouble0 m_statInstr; // Instruction count
|
|
|
|
|
VDouble0 m_statInstrFast; // Instruction count, non-slow() eval functions only
|
|
|
|
|
std::vector<VDouble0> m_statVarWidths; // Variables of given width
|
|
|
|
|
std::vector<NameMap> m_statVarWidthNames; // Var names of given width
|
|
|
|
|
VDouble0 m_statVarArray; // Statistic tracking
|
|
|
|
|
VDouble0 m_statVarBytes; // Statistic tracking
|
|
|
|
|
VDouble0 m_statVarClock; // Statistic tracking
|
|
|
|
|
VDouble0 m_statVarScpBytes; // Statistic tracking
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
// METHODS
|
2017-10-31 03:38:47 +01:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
void allNodes(AstNode* nodep) {
|
2019-05-19 22:13:13 +02:00
|
|
|
m_instrs += nodep->instrCount();
|
|
|
|
|
if (m_counting) {
|
|
|
|
|
++m_statTypeCount[nodep->type()];
|
|
|
|
|
if (nodep->firstAbovep()) { // Grab only those above, not those "back"
|
|
|
|
|
++m_statAbove[nodep->firstAbovep()->type()][nodep->type()];
|
|
|
|
|
}
|
|
|
|
|
m_statInstr += nodep->instrCount();
|
|
|
|
|
if (m_cfuncp && !m_cfuncp->slow()) m_statInstrFast += nodep->instrCount();
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeModule* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
allNodes(nodep);
|
|
|
|
|
if (!m_fast) {
|
2017-12-01 00:53:57 +01:00
|
|
|
// Count all CFuncs below this module
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildrenConst(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2017-12-01 00:53:57 +01:00
|
|
|
// Else we recursively trace fast CFuncs from the top _eval
|
|
|
|
|
// func, see visit(AstNetlist*)
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstVar* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
allNodes(nodep);
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildrenConst(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
if (m_counting && nodep->dtypep()) {
|
|
|
|
|
if (nodep->isUsedClock()) ++m_statVarClock;
|
2020-04-14 04:51:35 +02:00
|
|
|
if (VN_IS(nodep->dtypeSkipRefp(), UnpackArrayDType)) {
|
|
|
|
|
++m_statVarArray;
|
|
|
|
|
} else {
|
|
|
|
|
m_statVarBytes += nodep->dtypeSkipRefp()->widthTotalBytes();
|
|
|
|
|
}
|
2019-05-19 22:13:13 +02:00
|
|
|
if (int(m_statVarWidths.size()) <= nodep->width()) {
|
2020-04-14 04:51:35 +02:00
|
|
|
m_statVarWidths.resize(nodep->width() + 5);
|
|
|
|
|
if (v3Global.opt.statsVars()) m_statVarWidthNames.resize(nodep->width() + 5);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2020-04-14 04:51:35 +02:00
|
|
|
++m_statVarWidths.at(nodep->width());
|
2021-06-21 00:32:57 +02:00
|
|
|
const string pn = nodep->prettyName();
|
2019-05-19 22:13:13 +02:00
|
|
|
if (v3Global.opt.statsVars()) {
|
|
|
|
|
NameMap& nameMapr = m_statVarWidthNames.at(nodep->width());
|
|
|
|
|
if (nameMapr.find(pn) != nameMapr.end()) {
|
|
|
|
|
nameMapr[pn]++;
|
|
|
|
|
} else {
|
|
|
|
|
nameMapr[pn] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstVarScope* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
allNodes(nodep);
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildrenConst(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
if (m_counting) {
|
2018-02-02 03:32:58 +01:00
|
|
|
if (VN_IS(nodep->varp()->dtypeSkipRefp(), BasicDType)) {
|
2019-05-19 22:13:13 +02:00
|
|
|
m_statVarScpBytes += nodep->varp()->dtypeSkipRefp()->widthTotalBytes();
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeIf* nodep) override {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(4, " IF i=" << m_instrs << " " << nodep << endl);
|
2019-05-19 22:13:13 +02:00
|
|
|
allNodes(nodep);
|
|
|
|
|
// Condition is part of cost allocated to PREVIOUS block
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateAndNextConstNull(nodep->condp());
|
2019-05-19 22:13:13 +02:00
|
|
|
// Track prediction
|
2020-04-14 04:51:35 +02:00
|
|
|
if (m_counting) ++m_statPred[nodep->branchPred()];
|
2019-05-19 22:13:13 +02:00
|
|
|
if (!m_fast) {
|
|
|
|
|
// Count everything
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildrenConst(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
} else {
|
|
|
|
|
// See which path we want to take
|
|
|
|
|
// Need to do even if !m_counting because maybe determining upstream if/else
|
|
|
|
|
double ifInstrs = 0.0;
|
|
|
|
|
double elseInstrs = 0.0;
|
2020-12-20 02:46:10 +01:00
|
|
|
if (!nodep->branchPred().unlikely()) { // Check if
|
2020-08-25 03:10:43 +02:00
|
|
|
VL_RESTORER(m_instrs);
|
|
|
|
|
VL_RESTORER(m_counting);
|
2019-05-19 22:13:13 +02:00
|
|
|
{
|
|
|
|
|
m_counting = false;
|
|
|
|
|
m_instrs = 0.0;
|
2022-09-15 20:43:56 +02:00
|
|
|
iterateAndNextConstNull(nodep->thensp());
|
2019-05-19 22:13:13 +02:00
|
|
|
ifInstrs = m_instrs;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-20 02:46:10 +01:00
|
|
|
if (!nodep->branchPred().likely()) { // Check else
|
2020-08-25 03:10:43 +02:00
|
|
|
VL_RESTORER(m_instrs);
|
|
|
|
|
VL_RESTORER(m_counting);
|
2019-05-19 22:13:13 +02:00
|
|
|
{
|
|
|
|
|
m_counting = false;
|
|
|
|
|
m_instrs = 0.0;
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateAndNextConstNull(nodep->elsesp());
|
2019-05-19 22:13:13 +02:00
|
|
|
elseInstrs = m_instrs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Now collect the stats
|
|
|
|
|
if (m_counting) {
|
|
|
|
|
if (ifInstrs >= elseInstrs) {
|
2022-09-15 20:43:56 +02:00
|
|
|
iterateAndNextConstNull(nodep->thensp());
|
2019-05-19 22:13:13 +02:00
|
|
|
} else {
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateAndNextConstNull(nodep->elsesp());
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
// While's we assume evaluate once.
|
2022-09-16 12:22:11 +02:00
|
|
|
// void visit(AstWhile* nodep) override {
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeCCall* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
allNodes(nodep);
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildrenConst(nodep);
|
2017-11-29 00:38:19 +01:00
|
|
|
if (m_fast && !nodep->funcp()->entryPoint()) {
|
2019-05-19 22:13:13 +02:00
|
|
|
// Enter the function and trace it
|
2017-11-29 00:38:19 +01:00
|
|
|
m_tracingCall = true;
|
2018-05-11 02:55:37 +02:00
|
|
|
iterate(nodep->funcp());
|
2017-11-29 00:38:19 +01:00
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstCFunc* nodep) override {
|
2017-11-29 00:38:19 +01:00
|
|
|
if (m_fast) {
|
|
|
|
|
if (!m_tracingCall && !nodep->entryPoint()) return;
|
|
|
|
|
m_tracingCall = false;
|
|
|
|
|
}
|
2020-10-31 13:59:35 +01:00
|
|
|
VL_RESTORER(m_cfuncp);
|
|
|
|
|
{
|
|
|
|
|
m_cfuncp = nodep;
|
|
|
|
|
allNodes(nodep);
|
|
|
|
|
iterateChildrenConst(nodep);
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNode* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
allNodes(nodep);
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildrenConst(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNetlist* nodep) override {
|
2017-12-01 00:53:57 +01:00
|
|
|
if (m_fast && nodep->evalp()) {
|
|
|
|
|
m_instrs = 0;
|
|
|
|
|
m_counting = true;
|
2020-02-04 05:21:56 +01:00
|
|
|
iterateChildrenConst(nodep->evalp());
|
2017-12-01 00:53:57 +01:00
|
|
|
m_counting = false;
|
|
|
|
|
}
|
|
|
|
|
allNodes(nodep);
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildrenConst(nodep);
|
2017-12-01 00:53:57 +01:00
|
|
|
}
|
2020-04-14 04:51:35 +02:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
public:
|
|
|
|
|
// CONSTRUCTORS
|
|
|
|
|
StatsVisitor(AstNetlist* nodep, const string& stage, bool fast)
|
2020-08-16 15:55:36 +02:00
|
|
|
: m_stage{stage}
|
|
|
|
|
, m_fast{fast} {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(9, "Starting stats, fast=" << fast << endl);
|
2020-08-15 16:12:55 +02:00
|
|
|
m_cfuncp = nullptr;
|
2019-05-19 22:13:13 +02:00
|
|
|
m_counting = !m_fast;
|
|
|
|
|
m_instrs = 0;
|
2017-11-29 00:38:19 +01:00
|
|
|
m_tracingCall = false;
|
2019-05-19 22:13:13 +02:00
|
|
|
// Initialize arrays
|
2022-01-02 19:56:40 +01:00
|
|
|
m_statTypeCount.resize(VNType::_ENUM_END);
|
2019-05-19 22:13:13 +02:00
|
|
|
// Process
|
2018-05-11 02:55:37 +02:00
|
|
|
iterate(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
~StatsVisitor() override {
|
2019-05-19 22:13:13 +02:00
|
|
|
// Done. Publish statistics
|
|
|
|
|
V3Stats::addStat(m_stage, "Instruction count, TOTAL", m_statInstr);
|
2020-04-14 04:51:35 +02:00
|
|
|
V3Stats::addStat(m_stage, "Instruction count, fast critical", m_statInstrFast);
|
2019-05-19 22:13:13 +02:00
|
|
|
// Vars
|
|
|
|
|
V3Stats::addStat(m_stage, "Vars, unpacked arrayed", m_statVarArray);
|
|
|
|
|
V3Stats::addStat(m_stage, "Vars, clock attribute", m_statVarClock);
|
|
|
|
|
V3Stats::addStat(m_stage, "Var space, non-arrays, bytes", m_statVarBytes);
|
2020-04-14 04:51:35 +02:00
|
|
|
if (m_statVarScpBytes != 0.0) {
|
2019-05-19 22:13:13 +02:00
|
|
|
V3Stats::addStat(m_stage, "Var space, scoped, bytes", m_statVarScpBytes);
|
|
|
|
|
}
|
2020-04-14 04:51:35 +02:00
|
|
|
for (unsigned i = 0; i < m_statVarWidths.size(); i++) {
|
2022-09-16 01:58:01 +02:00
|
|
|
const double count{m_statVarWidths.at(i)};
|
2019-05-19 22:13:13 +02:00
|
|
|
if (count != 0.0) {
|
|
|
|
|
if (v3Global.opt.statsVars()) {
|
2021-11-26 23:55:36 +01:00
|
|
|
const NameMap& nameMapr = m_statVarWidthNames.at(i);
|
2020-11-11 04:10:38 +01:00
|
|
|
for (const auto& itr : nameMapr) {
|
2020-04-14 04:51:35 +02:00
|
|
|
std::ostringstream os;
|
2020-11-11 04:10:38 +01:00
|
|
|
os << "Vars, width " << std::setw(5) << std::dec << i << " " << itr.first;
|
|
|
|
|
V3Stats::addStat(m_stage, os.str(), itr.second);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-04-14 04:51:35 +02:00
|
|
|
std::ostringstream os;
|
|
|
|
|
os << "Vars, width " << std::setw(5) << std::dec << i;
|
2019-05-19 22:13:13 +02:00
|
|
|
V3Stats::addStat(m_stage, os.str(), count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Node types
|
2022-01-02 19:56:40 +01:00
|
|
|
for (int type = 0; type < VNType::_ENUM_END; type++) {
|
2022-09-16 01:58:01 +02:00
|
|
|
const double count{m_statTypeCount.at(type)};
|
2019-05-19 22:13:13 +02:00
|
|
|
if (count != 0.0) {
|
2022-08-30 07:02:39 +02:00
|
|
|
V3Stats::addStat(m_stage, std::string{"Node count, "} + VNType{type}.ascii(),
|
|
|
|
|
count);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-02 19:56:40 +01:00
|
|
|
for (int type = 0; type < VNType::_ENUM_END; type++) {
|
|
|
|
|
for (int type2 = 0; type2 < VNType::_ENUM_END; type2++) {
|
2022-09-16 01:58:01 +02:00
|
|
|
const double count{m_statAbove[type][type2]};
|
2019-05-19 22:13:13 +02:00
|
|
|
if (count != 0.0) {
|
2020-04-14 04:51:35 +02:00
|
|
|
V3Stats::addStat(m_stage,
|
2022-08-30 07:02:39 +02:00
|
|
|
(std::string{"Node pairs, "} + VNType{type}.ascii() + "_"
|
2022-09-16 01:58:01 +02:00
|
|
|
+ VNType{type2}.ascii()),
|
2019-05-19 22:13:13 +02:00
|
|
|
count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Branch pred
|
2020-04-14 04:51:35 +02:00
|
|
|
for (int type = 0; type < VBranchPred::_ENUM_END; type++) {
|
2022-09-16 01:58:01 +02:00
|
|
|
const double count{m_statPred[type]};
|
2019-05-19 22:13:13 +02:00
|
|
|
if (count != 0.0) {
|
2022-08-30 07:02:39 +02:00
|
|
|
V3Stats::addStat(m_stage,
|
|
|
|
|
(std::string{"Branch prediction, "} + VBranchPred{type}.ascii()),
|
|
|
|
|
count);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Top Stats class
|
|
|
|
|
|
|
|
|
|
void V3Stats::statsStageAll(AstNetlist* nodep, const string& stage, bool fast) {
|
2021-11-26 16:52:36 +01:00
|
|
|
{ StatsVisitor{nodep, stage, fast}; }
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void V3Stats::statsFinalAll(AstNetlist* nodep) {
|
|
|
|
|
statsStageAll(nodep, "Final");
|
|
|
|
|
statsStageAll(nodep, "Final_Fast", true);
|
|
|
|
|
}
|