2013-11-25 13:43:57 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
#include <iterator>
|
|
|
|
|
2019-08-07 03:25:51 -05:00
|
|
|
#include "kernel/yosys.h"
|
2013-11-25 13:43:57 -06:00
|
|
|
#include "kernel/celltypes.h"
|
2016-02-04 05:26:13 -06:00
|
|
|
#include "passes/techmap/libparse.h"
|
2019-08-07 03:25:51 -05:00
|
|
|
#include "kernel/cost.h"
|
2022-07-28 07:05:34 -05:00
|
|
|
#include "libs/json11/json11.hpp"
|
2013-11-25 13:43:57 -06:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2024-02-15 17:59:19 -06:00
|
|
|
struct cell_area_t {
|
2024-02-09 17:51:00 -06:00
|
|
|
double area;
|
2024-02-15 17:59:19 -06:00
|
|
|
bool is_sequential;
|
2024-02-09 17:51:00 -06:00
|
|
|
};
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
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) \
|
2024-03-22 08:20:08 -05:00
|
|
|
X(num_ports) X(num_port_bits) X(num_memories) X(num_memory_bits) X(num_cells) \
|
|
|
|
X(num_processes)
|
2014-09-27 09:17:53 -05:00
|
|
|
|
2016-02-04 05:26:13 -06:00
|
|
|
#define STAT_NUMERIC_MEMBERS STAT_INT_MEMBERS X(area)
|
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
#define X(_name) unsigned int _name;
|
2014-09-27 09:17:53 -05:00
|
|
|
STAT_INT_MEMBERS
|
|
|
|
#undef X
|
2024-09-07 23:30:58 -05:00
|
|
|
double area = 0;
|
|
|
|
double sequential_area = 0;
|
2019-05-11 02:24:52 -05:00
|
|
|
string tech;
|
2014-09-27 09:17:53 -05:00
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
std::map<RTLIL::IdString, unsigned int, RTLIL::sort_by_id_str> num_cells_by_type;
|
2016-02-04 05:26:13 -06:00
|
|
|
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;
|
2016-02-04 05:26:13 -06:00
|
|
|
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
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
statdata_t operator*(unsigned int other) const
|
2014-09-27 09:17:53 -05:00
|
|
|
{
|
|
|
|
statdata_t sum = *this;
|
|
|
|
#define X(_name) sum._name *= other;
|
2016-02-04 05:26:13 -06:00
|
|
|
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;
|
2016-02-04 05:26:13 -06:00
|
|
|
STAT_NUMERIC_MEMBERS
|
2014-09-27 09:17:53 -05:00
|
|
|
#undef X
|
|
|
|
}
|
2013-11-25 13:43:57 -06:00
|
|
|
|
2024-02-15 18:01:44 -06:00
|
|
|
statdata_t(RTLIL::Design *design, RTLIL::Module *mod, bool width_mode, const dict<IdString, cell_area_t> &cell_area, string techname)
|
2014-09-27 09:17:53 -05:00
|
|
|
{
|
2019-05-11 02:24:52 -05:00
|
|
|
tech = techname;
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
#define X(_name) _name = 0;
|
2016-02-04 05:26:13 -06:00
|
|
|
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
|
|
|
{
|
2024-03-22 08:20:08 -05:00
|
|
|
if (wire->port_input || wire->port_output) {
|
|
|
|
num_ports++;
|
|
|
|
num_port_bits += wire->width;
|
|
|
|
}
|
|
|
|
|
2020-09-14 05:43:18 -05:00
|
|
|
if (wire->name.isPublic()) {
|
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
|
|
|
{
|
2020-04-02 11:51:32 -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),
|
2020-04-21 05:51:58 -05:00
|
|
|
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;
|
2015-10-25 13:30:49 -05:00
|
|
|
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
|
|
|
}
|
2020-04-02 11:51:32 -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)));
|
2022-01-24 09:02:29 -06:00
|
|
|
else if (cell_type == ID($bmux))
|
|
|
|
cell_type = stringf("%s_%d_%d", cell_type.c_str(), GetSize(cell->getPort(ID::Y)), GetSize(cell->getPort(ID::S)));
|
|
|
|
else if (cell_type == ID($demux))
|
|
|
|
cell_type = stringf("%s_%d_%d", cell_type.c_str(), GetSize(cell->getPort(ID::A)), GetSize(cell->getPort(ID::S)));
|
Add new builtin FF types
The new types include:
- FFs with async reset and enable (`$adffe`, `$_DFFE_[NP][NP][01][NP]_`)
- FFs with sync reset (`$sdff`, `$_SDFF_[NP][NP][01]_`)
- FFs with sync reset and enable, reset priority (`$sdffs`, `$_SDFFE_[NP][NP][01][NP]_`)
- FFs with sync reset and enable, enable priority (`$sdffce`, `$_SDFFCE_[NP][NP][01][NP]_`)
- FFs with async reset, set, and enable (`$dffsre`, `$_DFFSRE_[NP][NP][NP][NP]_`)
- latches with reset or set (`$adlatch`, `$_DLATCH_[NP][NP][01]_`)
The new FF types are not actually used anywhere yet (this is left
for future commits).
2020-04-08 14:42:50 -05:00
|
|
|
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),
|
2021-10-01 18:23:43 -05:00
|
|
|
ID($aldff), ID($aldffe), 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
|
|
|
}
|
|
|
|
|
2024-02-15 18:01:44 -06:00
|
|
|
if (!cell_area.empty()) {
|
|
|
|
if (cell_area.count(cell_type)) {
|
|
|
|
cell_area_t cell_data = cell_area.at(cell_type);
|
2024-02-15 17:59:19 -06:00
|
|
|
if (cell_data.is_sequential) {
|
2024-02-09 17:51:00 -06:00
|
|
|
sequential_area += cell_data.area;
|
|
|
|
}
|
|
|
|
area += cell_data.area;
|
|
|
|
}
|
|
|
|
else {
|
2016-02-04 05:26:13 -06:00
|
|
|
unknown_cell_area.insert(cell_type);
|
2024-02-09 17:51:00 -06:00
|
|
|
}
|
2016-02-04 05:26:13 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-09-13 16:07:19 -05:00
|
|
|
unsigned int estimate_xilinx_lc()
|
|
|
|
{
|
|
|
|
unsigned int lut6_cnt = num_cells_by_type[ID(LUT6)];
|
|
|
|
unsigned int lut5_cnt = num_cells_by_type[ID(LUT5)];
|
|
|
|
unsigned int lut4_cnt = num_cells_by_type[ID(LUT4)];
|
|
|
|
unsigned int lut3_cnt = num_cells_by_type[ID(LUT3)];
|
|
|
|
unsigned int lut2_cnt = num_cells_by_type[ID(LUT2)];
|
|
|
|
unsigned int lut1_cnt = num_cells_by_type[ID(LUT1)];
|
|
|
|
unsigned 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;
|
|
|
|
|
|
|
|
return lc_cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int cmos_transistor_count(bool *tran_cnt_exact)
|
|
|
|
{
|
|
|
|
unsigned int tran_cnt = 0;
|
|
|
|
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
|
|
|
|
*tran_cnt_exact = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tran_cnt;
|
|
|
|
}
|
|
|
|
|
2018-06-18 19:29:01 -05:00
|
|
|
void log_data(RTLIL::IdString mod_name, bool top_mod)
|
2013-11-25 13:43:57 -06:00
|
|
|
{
|
2022-07-28 07:05:34 -05:00
|
|
|
log(" Number of wires: %6u\n", num_wires);
|
|
|
|
log(" Number of wire bits: %6u\n", num_wire_bits);
|
|
|
|
log(" Number of public wires: %6u\n", num_pub_wires);
|
|
|
|
log(" Number of public wire bits: %6u\n", num_pub_wire_bits);
|
2024-03-22 08:20:08 -05:00
|
|
|
log(" Number of ports: %6u\n", num_ports);
|
|
|
|
log(" Number of port bits: %6u\n", num_port_bits);
|
2022-07-28 07:05:34 -05:00
|
|
|
log(" Number of memories: %6u\n", num_memories);
|
|
|
|
log(" Number of memory bits: %6u\n", num_memory_bits);
|
|
|
|
log(" Number of processes: %6u\n", num_processes);
|
|
|
|
log(" Number of cells: %6u\n", num_cells);
|
2013-11-25 13:43:57 -06:00
|
|
|
for (auto &it : num_cells_by_type)
|
2019-05-11 02:24:52 -05:00
|
|
|
if (it.second)
|
2022-07-28 07:05:34 -05:00
|
|
|
log(" %-26s %6u\n", log_id(it.first), it.second);
|
2016-02-04 05:26:13 -06:00
|
|
|
|
|
|
|
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");
|
2018-06-18 19:29:01 -05:00
|
|
|
log(" Chip area for %smodule '%s': %f\n", (top_mod) ? "top " : "", mod_name.c_str(), area);
|
2024-02-16 09:44:09 -06:00
|
|
|
log(" of which used for sequential elements: %f (%.2f%%)\n", sequential_area, 100.0*sequential_area/area);
|
2016-02-04 05:26:13 -06:00
|
|
|
}
|
2019-05-11 02:24:52 -05:00
|
|
|
|
|
|
|
if (tech == "xilinx")
|
|
|
|
{
|
|
|
|
log("\n");
|
2022-09-13 16:07:19 -05:00
|
|
|
log(" Estimated number of LCs: %10u\n", estimate_xilinx_lc());
|
2019-05-11 02:24:52 -05:00
|
|
|
}
|
2019-07-20 08:06:28 -05:00
|
|
|
|
|
|
|
if (tech == "cmos")
|
|
|
|
{
|
|
|
|
bool tran_cnt_exact = true;
|
2022-09-13 16:07:19 -05:00
|
|
|
unsigned int tran_cnt = cmos_transistor_count(&tran_cnt_exact);
|
2019-07-20 08:06:28 -05:00
|
|
|
|
|
|
|
log("\n");
|
2022-07-28 07:05:34 -05:00
|
|
|
log(" Estimated number of transistors: %10u%s\n", tran_cnt, tran_cnt_exact ? "" : "+");
|
2019-07-20 08:06:28 -05:00
|
|
|
}
|
2013-11-25 13:43:57 -06:00
|
|
|
}
|
2022-07-28 07:05:34 -05:00
|
|
|
|
|
|
|
void log_data_json(const char *mod_name, bool first_module)
|
|
|
|
{
|
|
|
|
if (!first_module)
|
|
|
|
log(",\n");
|
|
|
|
log(" %s: {\n", json11::Json(mod_name).dump().c_str());
|
|
|
|
log(" \"num_wires\": %u,\n", num_wires);
|
|
|
|
log(" \"num_wire_bits\": %u,\n", num_wire_bits);
|
|
|
|
log(" \"num_pub_wires\": %u,\n", num_pub_wires);
|
|
|
|
log(" \"num_pub_wire_bits\": %u,\n", num_pub_wire_bits);
|
2024-03-22 08:20:08 -05:00
|
|
|
log(" \"num_ports\": %u,\n", num_ports);
|
|
|
|
log(" \"num_port_bits\": %u,\n", num_port_bits);
|
2022-07-28 07:05:34 -05:00
|
|
|
log(" \"num_memories\": %u,\n", num_memories);
|
|
|
|
log(" \"num_memory_bits\": %u,\n", num_memory_bits);
|
|
|
|
log(" \"num_processes\": %u,\n", num_processes);
|
|
|
|
log(" \"num_cells\": %u,\n", num_cells);
|
2022-12-29 20:51:46 -06:00
|
|
|
if (area != 0) {
|
|
|
|
log(" \"area\": %f,\n", area);
|
|
|
|
}
|
2022-07-28 07:05:34 -05:00
|
|
|
log(" \"num_cells_by_type\": {\n");
|
|
|
|
bool first_line = true;
|
|
|
|
for (auto &it : num_cells_by_type)
|
|
|
|
if (it.second) {
|
|
|
|
if (!first_line)
|
|
|
|
log(",\n");
|
|
|
|
log(" %s: %u", json11::Json(log_id(it.first)).dump().c_str(), it.second);
|
|
|
|
first_line = false;
|
|
|
|
}
|
|
|
|
log("\n");
|
2022-09-13 16:07:19 -05:00
|
|
|
log(" }");
|
|
|
|
if (tech == "xilinx")
|
|
|
|
{
|
|
|
|
log(",\n");
|
2022-09-13 16:14:56 -05:00
|
|
|
log(" \"estimated_num_lc\": %u", estimate_xilinx_lc());
|
2022-09-13 16:07:19 -05:00
|
|
|
}
|
|
|
|
if (tech == "cmos")
|
|
|
|
{
|
|
|
|
bool tran_cnt_exact = true;
|
|
|
|
unsigned int tran_cnt = cmos_transistor_count(&tran_cnt_exact);
|
|
|
|
log(",\n");
|
2022-09-13 16:14:56 -05:00
|
|
|
log(" \"estimated_num_transistors\": \"%u%s\"", tran_cnt, tran_cnt_exact ? "" : "+");
|
2022-09-13 16:07:19 -05:00
|
|
|
}
|
|
|
|
log("\n");
|
2022-07-28 07:05:34 -05:00
|
|
|
log(" }");
|
|
|
|
}
|
2014-09-27 09:17:53 -05:00
|
|
|
};
|
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
statdata_t hierarchy_worker(std::map<RTLIL::IdString, statdata_t> &mod_stat, RTLIL::IdString mod, int level, bool quiet = false)
|
2014-09-27 09:17:53 -05:00
|
|
|
{
|
|
|
|
statdata_t mod_data = mod_stat.at(mod);
|
2022-07-28 07:05:34 -05:00
|
|
|
std::map<RTLIL::IdString, unsigned 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) {
|
2022-07-28 07:05:34 -05:00
|
|
|
if (!quiet)
|
|
|
|
log(" %*s%-*s %6u\n", 2*level, "", 26-2*level, log_id(it.first), it.second);
|
2023-02-28 10:12:55 -06:00
|
|
|
mod_data = mod_data + hierarchy_worker(mod_stat, it.first, level+1, quiet) * it.second;
|
2014-09-27 09:17:53 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-02-15 18:01:44 -06:00
|
|
|
void read_liberty_cellarea(dict<IdString, cell_area_t> &cell_area, string liberty_file)
|
2016-02-04 05:26:13 -06:00
|
|
|
{
|
|
|
|
std::ifstream f;
|
|
|
|
f.open(liberty_file.c_str());
|
2018-01-07 09:36:13 -06:00
|
|
|
yosys_input_files.insert(liberty_file);
|
2016-02-04 05:26:13 -06:00
|
|
|
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;
|
|
|
|
|
2024-08-13 11:43:31 -05:00
|
|
|
const LibertyAst *ar = cell->find("area");
|
2024-02-09 17:51:00 -06:00
|
|
|
bool is_flip_flop = cell->find("ff") != nullptr;
|
2020-04-05 23:36:41 -05:00
|
|
|
if (ar != nullptr && !ar->value.empty())
|
2024-02-15 18:01:44 -06:00
|
|
|
cell_area["\\" + cell->args[0]] = {/*area=*/atof(ar->value.c_str()), is_flip_flop};
|
2016-02-04 05:26:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2016-02-04 05:26:13 -06:00
|
|
|
log(" -liberty <liberty_file>\n");
|
|
|
|
log(" use cell area information from the provided liberty file\n");
|
|
|
|
log("\n");
|
2019-05-11 02:24:52 -05:00
|
|
|
log(" -tech <technology>\n");
|
2024-01-21 15:32:05 -06:00
|
|
|
log(" print area estimate for the specified technology. Currently supported\n");
|
2019-07-20 08:06:28 -05:00
|
|
|
log(" values for <technology>: xilinx, cmos\n");
|
2019-05-11 02:24:52 -05:00
|
|
|
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");
|
2022-07-28 07:05:34 -05:00
|
|
|
log(" -json\n");
|
|
|
|
log(" output the statistics in a machine-readable JSON format.\n");
|
|
|
|
log(" this is output to the console; use \"tee\" to output to a file.\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
|
|
|
{
|
2022-07-28 07:05:34 -05:00
|
|
|
bool width_mode = false, json_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;
|
2024-02-15 18:01:44 -06:00
|
|
|
dict<IdString, cell_area_t> cell_area;
|
2019-05-11 02:24:52 -05:00
|
|
|
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;
|
|
|
|
}
|
2016-02-04 05:26:13 -06:00
|
|
|
if (args[argidx] == "-liberty" && argidx+1 < args.size()) {
|
|
|
|
string liberty_file = args[++argidx];
|
|
|
|
rewrite_filename(liberty_file);
|
2024-02-15 18:01:44 -06:00
|
|
|
read_liberty_cellarea(cell_area, liberty_file);
|
2016-02-04 05:26:13 -06:00
|
|
|
continue;
|
|
|
|
}
|
2019-05-11 02:24:52 -05:00
|
|
|
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;
|
|
|
|
}
|
2022-07-28 07:05:34 -05:00
|
|
|
if (args[argidx] == "-json") {
|
|
|
|
json_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-11-25 13:43:57 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
2022-09-13 16:07:19 -05:00
|
|
|
if(!json_mode)
|
|
|
|
log_header(design, "Printing statistics.\n");
|
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
if (techname != "" && techname != "xilinx" && techname != "cmos" && !json_mode)
|
2019-05-11 02:24:52 -05:00
|
|
|
log_cmd_error("Unsupported technology: '%s'\n", techname.c_str());
|
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
if (json_mode) {
|
|
|
|
log("{\n");
|
|
|
|
log(" \"creator\": %s,\n", json11::Json(yosys_version_str).dump().c_str());
|
|
|
|
std::stringstream invocation;
|
|
|
|
std::copy(args.begin(), args.end(), std::ostream_iterator<std::string>(invocation, " "));
|
|
|
|
log(" \"invocation\": %s,\n", json11::Json(invocation.str()).dump().c_str());
|
|
|
|
log(" \"modules\": {\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool first_module = true;
|
2015-02-14 15:36:34 -06:00
|
|
|
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))
|
2015-02-14 15:36:34 -06:00
|
|
|
top_mod = mod;
|
2013-11-25 13:43:57 -06:00
|
|
|
|
2024-02-15 18:01:44 -06:00
|
|
|
statdata_t data(design, mod, width_mode, cell_area, techname);
|
2015-02-14 15:36:34 -06:00
|
|
|
mod_stat[mod->name] = data;
|
2013-11-25 13:43:57 -06:00
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
if (json_mode) {
|
|
|
|
data.log_data_json(mod->name.c_str(), first_module);
|
|
|
|
first_module = false;
|
|
|
|
} else {
|
|
|
|
log("\n");
|
|
|
|
log("=== %s%s ===\n", log_id(mod->name), design->selected_whole_module(mod->name) ? "" : " (partially selected)");
|
|
|
|
log("\n");
|
|
|
|
data.log_data(mod->name, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_mode) {
|
2013-11-25 13:43:57 -06:00
|
|
|
log("\n");
|
2022-11-28 12:05:12 -06:00
|
|
|
log(top_mod == nullptr ? " }\n" : " },\n");
|
2013-11-25 13:43:57 -06:00
|
|
|
}
|
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
if (top_mod != nullptr)
|
2013-11-25 13:43:57 -06:00
|
|
|
{
|
2022-07-28 07:05:34 -05:00
|
|
|
if (!json_mode && GetSize(mod_stat) > 1) {
|
|
|
|
log("\n");
|
|
|
|
log("=== design hierarchy ===\n");
|
|
|
|
log("\n");
|
|
|
|
log(" %-28s %6d\n", log_id(top_mod->name), 1);
|
|
|
|
}
|
2013-11-25 13:43:57 -06:00
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
statdata_t data = hierarchy_worker(mod_stat, top_mod->name, 0, /*quiet=*/json_mode);
|
|
|
|
|
2022-11-28 12:05:12 -06:00
|
|
|
if (json_mode)
|
2022-07-28 07:05:34 -05:00
|
|
|
data.log_data_json("design", true);
|
|
|
|
else if (GetSize(mod_stat) > 1) {
|
|
|
|
log("\n");
|
|
|
|
data.log_data(top_mod->name, true);
|
|
|
|
}
|
2013-11-25 13:43:57 -06:00
|
|
|
|
2022-06-01 09:01:07 -05:00
|
|
|
design->scratchpad_set_int("stat.num_wires", data.num_wires);
|
|
|
|
design->scratchpad_set_int("stat.num_wire_bits", data.num_wire_bits);
|
|
|
|
design->scratchpad_set_int("stat.num_pub_wires", data.num_pub_wires);
|
|
|
|
design->scratchpad_set_int("stat.num_pub_wire_bits", data.num_pub_wire_bits);
|
2024-03-22 08:20:08 -05:00
|
|
|
design->scratchpad_set_int("stat.num_ports", data.num_ports);
|
|
|
|
design->scratchpad_set_int("stat.num_port_bits", data.num_port_bits);
|
2022-06-01 09:01:07 -05:00
|
|
|
design->scratchpad_set_int("stat.num_memories", data.num_memories);
|
|
|
|
design->scratchpad_set_int("stat.num_memory_bits", data.num_memory_bits);
|
|
|
|
design->scratchpad_set_int("stat.num_processes", data.num_processes);
|
|
|
|
design->scratchpad_set_int("stat.num_cells", data.num_cells);
|
|
|
|
design->scratchpad_set_int("stat.area", data.area);
|
2013-11-25 13:43:57 -06:00
|
|
|
}
|
|
|
|
|
2022-07-28 07:05:34 -05:00
|
|
|
if (json_mode) {
|
|
|
|
log("\n");
|
|
|
|
log("}\n");
|
|
|
|
}
|
|
|
|
|
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
|