yosys/passes/cmds/stat.cc

387 lines
11 KiB
C++
Raw Normal View History

2013-11-25 13:43:57 -06:00
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
2015-07-02 04:14:30 -05:00
*
2013-11-25 13:43:57 -06:00
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
2015-07-02 04:14:30 -05:00
*
2013-11-25 13:43:57 -06:00
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "kernel/yosys.h"
2013-11-25 13:43:57 -06:00
#include "kernel/celltypes.h"
#include "passes/techmap/libparse.h"
#include "kernel/cost.h"
2013-11-25 13:43:57 -06:00
2014-09-27 09:17:53 -05:00
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
struct statdata_t
2013-11-25 13:43:57 -06:00
{
2014-09-27 09:17:53 -05:00
#define STAT_INT_MEMBERS X(num_wires) X(num_wire_bits) X(num_pub_wires) X(num_pub_wire_bits) \
X(num_memories) X(num_memory_bits) X(num_cells) X(num_processes)
#define STAT_NUMERIC_MEMBERS STAT_INT_MEMBERS X(area)
2014-09-27 09:17:53 -05:00
#define X(_name) int _name;
STAT_INT_MEMBERS
#undef X
double area;
string tech;
2014-09-27 09:17:53 -05:00
std::map<RTLIL::IdString, int> techinfo;
std::map<RTLIL::IdString, int, RTLIL::sort_by_id_str> num_cells_by_type;
std::set<RTLIL::IdString> unknown_cell_area;
2014-09-27 09:17:53 -05:00
statdata_t operator+(const statdata_t &other) const
2013-11-25 13:43:57 -06:00
{
2014-09-27 09:17:53 -05:00
statdata_t sum = other;
#define X(_name) sum._name += _name;
STAT_NUMERIC_MEMBERS
2014-09-27 09:17:53 -05:00
#undef X
for (auto &it : num_cells_by_type)
sum.num_cells_by_type[it.first] += it.second;
return sum;
}
2013-11-25 13:43:57 -06:00
2014-09-27 09:17:53 -05:00
statdata_t operator*(int other) const
{
statdata_t sum = *this;
#define X(_name) sum._name *= other;
STAT_NUMERIC_MEMBERS
2014-09-27 09:17:53 -05:00
#undef X
for (auto &it : sum.num_cells_by_type)
it.second *= other;
return sum;
}
2013-11-25 13:43:57 -06:00
2014-09-27 09:17:53 -05:00
statdata_t()
{
#define X(_name) _name = 0;
STAT_NUMERIC_MEMBERS
2014-09-27 09:17:53 -05:00
#undef X
}
2013-11-25 13:43:57 -06:00
statdata_t(RTLIL::Design *design, RTLIL::Module *mod, bool width_mode, const dict<IdString, double> &cell_area, string techname)
2014-09-27 09:17:53 -05:00
{
tech = techname;
2014-09-27 09:17:53 -05:00
#define X(_name) _name = 0;
STAT_NUMERIC_MEMBERS
2014-09-27 09:17:53 -05:00
#undef X
2013-11-25 13:43:57 -06:00
2020-04-05 23:36:41 -05:00
for (auto wire : mod->selected_wires())
2013-11-25 13:43:57 -06:00
{
2020-04-05 23:36:41 -05:00
if (wire->name[0] == '\\') {
2014-09-27 09:17:53 -05:00
num_pub_wires++;
2020-04-05 23:36:41 -05:00
num_pub_wire_bits += wire->width;
2014-09-27 09:17:53 -05:00
}
num_wires++;
2020-04-05 23:36:41 -05:00
num_wire_bits += wire->width;
2013-11-25 13:43:57 -06:00
}
2014-09-27 09:17:53 -05:00
for (auto &it : mod->memories) {
if (!design->selected(mod, it.second))
continue;
num_memories++;
num_memory_bits += it.second->width * it.second->size;
2013-11-25 13:43:57 -06:00
}
2020-04-05 23:36:41 -05:00
for (auto cell : mod->selected_cells())
2013-11-25 13:43:57 -06:00
{
2020-04-05 23:36:41 -05:00
RTLIL::IdString cell_type = cell->type;
2013-11-25 13:43:57 -06:00
2014-09-27 09:17:53 -05:00
if (width_mode)
2014-08-22 10:20:28 -05:00
{
if (cell_type.in(ID($not), ID($pos), ID($neg),
ID($logic_not), ID($logic_and), ID($logic_or),
ID($reduce_and), ID($reduce_or), ID($reduce_xor), ID($reduce_xnor), ID($reduce_bool),
ID($lut), ID($and), ID($or), ID($xor), ID($xnor),
ID($shl), ID($shr), ID($sshl), ID($sshr), ID($shift), ID($shiftx),
ID($lt), ID($le), ID($eq), ID($ne), ID($eqx), ID($nex), ID($ge), ID($gt),
ID($add), ID($sub), ID($mul), ID($div), ID($mod), ID($divfloor), ID($modfloor), ID($pow), ID($alu))) {
2020-04-05 23:36:41 -05:00
int width_a = cell->hasPort(ID::A) ? GetSize(cell->getPort(ID::A)) : 0;
int width_b = cell->hasPort(ID::B) ? GetSize(cell->getPort(ID::B)) : 0;
int width_y = cell->hasPort(ID::Y) ? GetSize(cell->getPort(ID::Y)) : 0;
cell_type = stringf("%s_%d", cell_type.c_str(), max<int>({width_a, width_b, width_y}));
2014-08-22 10:20:28 -05:00
}
else if (cell_type.in(ID($mux), ID($pmux)))
2020-04-05 23:36:41 -05:00
cell_type = stringf("%s_%d", cell_type.c_str(), GetSize(cell->getPort(ID::Y)));
else if (cell_type.in(
ID($sr), ID($ff), ID($dff), ID($dffe), ID($dffsr), ID($dffsre),
ID($adff), ID($adffe), ID($sdff), ID($sdffe), ID($sdffce),
ID($dlatch), ID($adlatch), ID($dlatchsr)))
2020-04-05 23:36:41 -05:00
cell_type = stringf("%s_%d", cell_type.c_str(), GetSize(cell->getPort(ID::Q)));
2013-11-25 13:43:57 -06:00
}
if (!cell_area.empty()) {
if (cell_area.count(cell_type))
area += cell_area.at(cell_type);
else
unknown_cell_area.insert(cell_type);
}
2014-09-27 09:17:53 -05:00
num_cells++;
num_cells_by_type[cell_type]++;
2013-11-25 13:43:57 -06:00
}
2014-09-27 09:17:53 -05:00
for (auto &it : mod->processes) {
if (!design->selected(mod, it.second))
continue;
num_processes++;
2013-11-25 13:43:57 -06:00
}
2014-09-27 09:17:53 -05:00
}
2013-11-25 13:43:57 -06:00
void log_data(RTLIL::IdString mod_name, bool top_mod)
2013-11-25 13:43:57 -06:00
{
2014-09-27 09:17:53 -05:00
log(" Number of wires: %6d\n", num_wires);
log(" Number of wire bits: %6d\n", num_wire_bits);
log(" Number of public wires: %6d\n", num_pub_wires);
log(" Number of public wire bits: %6d\n", num_pub_wire_bits);
log(" Number of memories: %6d\n", num_memories);
log(" Number of memory bits: %6d\n", num_memory_bits);
log(" Number of processes: %6d\n", num_processes);
log(" Number of cells: %6d\n", num_cells);
2013-11-25 13:43:57 -06:00
for (auto &it : num_cells_by_type)
if (it.second)
2020-04-05 23:36:41 -05:00
log(" %-26s %6d\n", log_id(it.first), it.second);
if (!unknown_cell_area.empty()) {
log("\n");
for (auto cell_type : unknown_cell_area)
log(" Area for cell type %s is unknown!\n", cell_type.c_str());
}
if (area != 0) {
log("\n");
log(" Chip area for %smodule '%s': %f\n", (top_mod) ? "top " : "", mod_name.c_str(), area);
}
if (tech == "xilinx")
{
int lut6_cnt = num_cells_by_type[ID(LUT6)];
int lut5_cnt = num_cells_by_type[ID(LUT5)];
int lut4_cnt = num_cells_by_type[ID(LUT4)];
int lut3_cnt = num_cells_by_type[ID(LUT3)];
int lut2_cnt = num_cells_by_type[ID(LUT2)];
int lut1_cnt = num_cells_by_type[ID(LUT1)];
int lc_cnt = 0;
lc_cnt += lut6_cnt;
lc_cnt += lut5_cnt;
if (lut1_cnt) {
int cnt = std::min(lut5_cnt, lut1_cnt);
lut5_cnt -= cnt;
lut1_cnt -= cnt;
}
lc_cnt += lut4_cnt;
if (lut1_cnt) {
int cnt = std::min(lut4_cnt, lut1_cnt);
lut4_cnt -= cnt;
lut1_cnt -= cnt;
}
if (lut2_cnt) {
int cnt = std::min(lut4_cnt, lut2_cnt);
lut4_cnt -= cnt;
lut2_cnt -= cnt;
}
lc_cnt += lut3_cnt;
if (lut1_cnt) {
int cnt = std::min(lut3_cnt, lut1_cnt);
lut3_cnt -= cnt;
lut1_cnt -= cnt;
}
if (lut2_cnt) {
int cnt = std::min(lut3_cnt, lut2_cnt);
lut3_cnt -= cnt;
lut2_cnt -= cnt;
}
if (lut3_cnt) {
int cnt = (lut3_cnt + 1) / 2;
lut3_cnt -= cnt;
}
lc_cnt += (lut2_cnt + lut1_cnt + 1) / 2;
log("\n");
log(" Estimated number of LCs: %10d\n", lc_cnt);
}
if (tech == "cmos")
{
int tran_cnt = 0;
bool tran_cnt_exact = true;
auto &gate_costs = CellCosts::cmos_gate_cost();
for (auto it : num_cells_by_type) {
auto ctype = it.first;
auto cnum = it.second;
if (gate_costs.count(ctype))
tran_cnt += cnum * gate_costs.at(ctype);
else if (ctype.in(ID($_DFF_P_), ID($_DFF_N_)))
tran_cnt += cnum * 16;
else
tran_cnt_exact = false;
}
log("\n");
log(" Estimated number of transistors: %10d%s\n", tran_cnt, tran_cnt_exact ? "" : "+");
}
2013-11-25 13:43:57 -06:00
}
2014-09-27 09:17:53 -05:00
};
statdata_t hierarchy_worker(std::map<RTLIL::IdString, statdata_t> &mod_stat, RTLIL::IdString mod, int level)
{
statdata_t mod_data = mod_stat.at(mod);
std::map<RTLIL::IdString, int, RTLIL::sort_by_id_str> num_cells_by_type;
2014-09-27 09:17:53 -05:00
num_cells_by_type.swap(mod_data.num_cells_by_type);
for (auto &it : num_cells_by_type)
if (mod_stat.count(it.first) > 0) {
2020-04-05 23:36:41 -05:00
log(" %*s%-*s %6d\n", 2*level, "", 26-2*level, log_id(it.first), it.second);
2014-09-27 09:17:53 -05:00
mod_data = mod_data + hierarchy_worker(mod_stat, it.first, level+1) * it.second;
mod_data.num_cells -= it.second;
} else {
mod_data.num_cells_by_type[it.first] += it.second;
}
return mod_data;
2013-11-25 13:43:57 -06:00
}
void read_liberty_cellarea(dict<IdString, double> &cell_area, string liberty_file)
{
std::ifstream f;
f.open(liberty_file.c_str());
yosys_input_files.insert(liberty_file);
if (f.fail())
log_cmd_error("Can't open liberty file `%s': %s\n", liberty_file.c_str(), strerror(errno));
LibertyParser libparser(f);
f.close();
for (auto cell : libparser.ast->children)
{
if (cell->id != "cell" || cell->args.size() != 1)
continue;
LibertyAst *ar = cell->find("area");
2020-04-05 23:36:41 -05:00
if (ar != nullptr && !ar->value.empty())
cell_area["\\" + cell->args[0]] = atof(ar->value.c_str());
}
}
2013-11-25 13:43:57 -06:00
struct StatPass : public Pass {
StatPass() : Pass("stat", "print some statistics") { }
2020-06-18 18:34:52 -05:00
void help() override
2013-11-25 13:43:57 -06:00
{
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
log("\n");
log(" stat [options] [selection]\n");
log("\n");
log("Print some statistics (number of objects) on the selected portion of the\n");
log("design.\n");
log("\n");
log(" -top <module>\n");
log(" print design hierarchy with this module as top. if the design is fully\n");
log(" selected and a module has the 'top' attribute set, this module is used\n");
log(" default value for this option.\n");
log("\n");
log(" -liberty <liberty_file>\n");
log(" use cell area information from the provided liberty file\n");
log("\n");
log(" -tech <technology>\n");
log(" print area estemate for the specified technology. Currently supported\n");
log(" values for <technology>: xilinx, cmos\n");
log("\n");
2014-08-22 10:20:28 -05:00
log(" -width\n");
log(" annotate internal cell types with their word width.\n");
log(" e.g. $add_8 for an 8 bit wide $add cell.\n");
log("\n");
2013-11-25 13:43:57 -06:00
}
2020-06-18 18:34:52 -05:00
void execute(std::vector<std::string> args, RTLIL::Design *design) override
2013-11-25 13:43:57 -06:00
{
2016-04-21 16:28:37 -05:00
log_header(design, "Printing statistics.\n");
2013-11-25 13:43:57 -06:00
2014-08-22 10:20:28 -05:00
bool width_mode = false;
2020-04-05 23:36:41 -05:00
RTLIL::Module *top_mod = nullptr;
2013-11-25 13:43:57 -06:00
std::map<RTLIL::IdString, statdata_t> mod_stat;
dict<IdString, double> cell_area;
string techname;
2013-11-25 13:43:57 -06:00
size_t argidx;
for (argidx = 1; argidx < args.size(); argidx++)
{
2014-08-22 10:20:28 -05:00
if (args[argidx] == "-width") {
width_mode = true;
continue;
}
if (args[argidx] == "-liberty" && argidx+1 < args.size()) {
string liberty_file = args[++argidx];
rewrite_filename(liberty_file);
read_liberty_cellarea(cell_area, liberty_file);
continue;
}
if (args[argidx] == "-tech" && argidx+1 < args.size()) {
techname = args[++argidx];
continue;
}
2013-11-25 13:43:57 -06:00
if (args[argidx] == "-top" && argidx+1 < args.size()) {
2020-04-05 23:36:41 -05:00
if (design->module(RTLIL::escape_id(args[argidx+1])) == nullptr)
2013-11-25 13:43:57 -06:00
log_cmd_error("Can't find module %s.\n", args[argidx+1].c_str());
2020-04-05 23:36:41 -05:00
top_mod = design->module(RTLIL::escape_id(args[++argidx]));
2013-11-25 13:43:57 -06:00
continue;
}
break;
}
extra_args(args, argidx, design);
if (techname != "" && techname != "xilinx" && techname != "cmos")
log_cmd_error("Unsupported technology: '%s'\n", techname.c_str());
for (auto mod : design->selected_modules())
2013-11-25 13:43:57 -06:00
{
if (!top_mod && design->full_selection())
2020-03-12 14:57:01 -05:00
if (mod->get_bool_attribute(ID::top))
top_mod = mod;
2013-11-25 13:43:57 -06:00
statdata_t data(design, mod, width_mode, cell_area, techname);
mod_stat[mod->name] = data;
2013-11-25 13:43:57 -06:00
log("\n");
2020-04-05 23:36:41 -05:00
log("=== %s%s ===\n", log_id(mod->name), design->selected_whole_module(mod->name) ? "" : " (partially selected)");
2013-11-25 13:43:57 -06:00
log("\n");
data.log_data(mod->name, false);
2013-11-25 13:43:57 -06:00
}
2020-04-05 23:36:41 -05:00
if (top_mod != nullptr && GetSize(mod_stat) > 1)
2013-11-25 13:43:57 -06:00
{
log("\n");
log("=== design hierarchy ===\n");
log("\n");
2020-04-05 23:36:41 -05:00
log(" %-28s %6d\n", log_id(top_mod->name), 1);
2013-11-25 13:43:57 -06:00
statdata_t data = hierarchy_worker(mod_stat, top_mod->name, 0);
log("\n");
data.log_data(top_mod->name, true);
2013-11-25 13:43:57 -06:00
}
log("\n");
}
} StatPass;
2015-07-02 04:14:30 -05:00
2014-09-27 09:17:53 -05:00
PRIVATE_NAMESPACE_END