2026-01-17 01:32:04 +01:00
|
|
|
/*
|
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
|
|
|
|
* 2026 Stan Lee <stan@silimate.com>
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2026-01-21 00:35:13 +01:00
|
|
|
#include "kernel/fstdata.h"
|
2026-01-29 02:00:46 +01:00
|
|
|
#include "kernel/yosys.h"
|
2026-04-09 01:18:55 +02:00
|
|
|
#include "passes/silimate/reg_rename.h"
|
2026-01-19 20:20:11 +01:00
|
|
|
#include <regex>
|
2026-01-17 01:32:04 +01:00
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
2026-01-29 02:00:46 +01:00
|
|
|
struct RegRenameInstance {
|
|
|
|
|
std::string vcd_scope;
|
|
|
|
|
Module *module;
|
2026-04-15 20:50:35 +02:00
|
|
|
bool debug;
|
2026-01-29 02:20:19 +01:00
|
|
|
dict<Cell*, RegRenameInstance *> children;
|
2026-01-29 02:00:46 +01:00
|
|
|
|
|
|
|
|
// Constructor
|
2026-01-29 02:20:19 +01:00
|
|
|
// When constructing, it will recursively build the
|
|
|
|
|
// module hierarchy with correct VCD scope mapping
|
2026-04-15 20:50:35 +02:00
|
|
|
RegRenameInstance(std::string scope, Module *mod, bool dbg = false) : vcd_scope(scope), module(mod), debug(dbg)
|
2026-01-29 02:00:46 +01:00
|
|
|
{
|
|
|
|
|
// Loop through all cells in the module
|
|
|
|
|
for (auto cell : module->cells()) {
|
|
|
|
|
Module *child = module->design->module(cell->type);
|
|
|
|
|
if (child == nullptr) {
|
|
|
|
|
continue; // skip non-module cells
|
|
|
|
|
}
|
2026-01-29 02:20:19 +01:00
|
|
|
// Construct the child's scope in VCD format,
|
|
|
|
|
// which is the parent scope plus the instance name
|
2026-01-29 02:00:46 +01:00
|
|
|
std::string child_scope = vcd_scope + "." + RTLIL::unescape_id(cell->name);
|
2026-04-15 20:50:35 +02:00
|
|
|
children[cell] = new RegRenameInstance(child_scope, child, debug);
|
2026-01-29 02:00:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 02:20:19 +01:00
|
|
|
// Destructor
|
2026-01-29 02:00:46 +01:00
|
|
|
~RegRenameInstance()
|
|
|
|
|
{
|
|
|
|
|
for (auto &it : children)
|
|
|
|
|
delete it.second;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 02:20:19 +01:00
|
|
|
// Processes registers in a given module hierarchy
|
|
|
|
|
// and renames to allow for correct register annotation
|
2026-01-29 02:00:46 +01:00
|
|
|
void process_registers(dict<std::pair<std::string, std::string>, int> &vcd_reg_widths)
|
|
|
|
|
{
|
2026-04-15 20:50:35 +02:00
|
|
|
if (debug)
|
|
|
|
|
log("Processing registers in scope: %s (module: %s)\n",
|
|
|
|
|
vcd_scope.c_str(), log_id(module->name));
|
|
|
|
|
|
2026-01-29 02:00:46 +01:00
|
|
|
pool<Wire *> wiresToRemove;
|
|
|
|
|
|
|
|
|
|
// Loop through all cells in the module
|
|
|
|
|
for (auto cell : module->cells()) {
|
|
|
|
|
|
|
|
|
|
// Skip non-register cells
|
|
|
|
|
if (!RTLIL::builtin_ff_cell_types().count(cell->type)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 20:50:35 +02:00
|
|
|
// We know it is a reg with _reg suffix with all brackets removed
|
|
|
|
|
std::string searchName = cell->name.c_str();
|
|
|
|
|
if (auto pos = searchName.find('['); pos != std::string::npos)
|
|
|
|
|
searchName.erase(pos);
|
|
|
|
|
|
|
|
|
|
// If register name with no brackets ends with _reg, we can process it
|
|
|
|
|
size_t reg_pos = searchName.find("_reg");
|
|
|
|
|
if (reg_pos != std::string::npos && reg_pos == searchName.size() - 4) {
|
2026-01-29 02:00:46 +01:00
|
|
|
|
2026-04-15 20:50:35 +02:00
|
|
|
// Remove "_reg" to get the target wire specification
|
|
|
|
|
std::string cellName = cell->name.c_str();
|
|
|
|
|
cellName.erase(reg_pos, 4);
|
2026-01-29 02:00:46 +01:00
|
|
|
|
2026-04-15 20:50:35 +02:00
|
|
|
// Index comes from the right-most brackets
|
|
|
|
|
std::string wireName;
|
|
|
|
|
int bitIndex = 0;
|
|
|
|
|
size_t last_open = cellName.rfind('[');
|
|
|
|
|
size_t last_close = cellName.rfind(']');
|
|
|
|
|
if (last_open != std::string::npos && last_close != std::string::npos && last_close > last_open) {
|
|
|
|
|
wireName = cellName.substr(0, last_open);
|
|
|
|
|
bitIndex = std::stoi(cellName.substr(last_open + 1, last_close - last_open - 1));
|
|
|
|
|
} else {
|
|
|
|
|
wireName = cellName;
|
|
|
|
|
bitIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process Q output connection for the cell
|
|
|
|
|
for (auto &conn : cell->connections()) {
|
|
|
|
|
if (conn.first != ID::Q || !conn.second.is_wire()) continue;
|
2026-01-29 02:00:46 +01:00
|
|
|
|
|
|
|
|
Wire *oldWire = conn.second.as_wire();
|
2026-04-15 20:50:35 +02:00
|
|
|
if (oldWire->port_input || oldWire->port_output) continue;
|
2026-01-29 02:00:46 +01:00
|
|
|
|
2026-04-15 20:50:35 +02:00
|
|
|
// Lookup wire width from VCD
|
|
|
|
|
int wireWidth = vcd_reg_widths[{vcd_scope, wireName}];
|
|
|
|
|
if (wireWidth == 0) {
|
|
|
|
|
if (debug)
|
|
|
|
|
log("Wire '%s' not found in VCD scope '%s' (cell: %s)\n",
|
|
|
|
|
wireName.c_str(), vcd_scope.c_str(), cellName.c_str());
|
2026-01-29 02:00:46 +01:00
|
|
|
continue;
|
2026-04-15 20:50:35 +02:00
|
|
|
}
|
2026-01-29 02:00:46 +01:00
|
|
|
|
2026-04-15 20:50:35 +02:00
|
|
|
// Validate bit index
|
|
|
|
|
if (bitIndex >= wireWidth) {
|
|
|
|
|
log_warning("Bit index %d exceeds wire width %d for '%s'\n",
|
|
|
|
|
bitIndex, wireWidth, wireName.c_str());
|
2026-01-29 02:00:46 +01:00
|
|
|
continue;
|
2026-04-15 20:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IdString wireId = RTLIL::escape_id(wireName);
|
|
|
|
|
|
|
|
|
|
// Single-bit wire requires only simple renaming
|
|
|
|
|
if (wireWidth == 1 && bitIndex == 0) {
|
|
|
|
|
if (oldWire->name != wireId && !module->wire(wireId)) {
|
|
|
|
|
if (debug)
|
|
|
|
|
log("Renaming %s to %s\n", log_id(oldWire), wireName.c_str());
|
|
|
|
|
module->rename(oldWire, wireId);
|
|
|
|
|
}
|
2026-03-31 01:34:20 +02:00
|
|
|
continue;
|
2026-01-29 02:00:46 +01:00
|
|
|
}
|
2026-04-15 20:50:35 +02:00
|
|
|
|
|
|
|
|
// Multi-bit wire requires creating a new wire and rewiring connections
|
|
|
|
|
Wire *targetWire = module->wire(wireId);
|
|
|
|
|
|
|
|
|
|
if (!targetWire) {
|
|
|
|
|
if (debug)
|
|
|
|
|
log("Creating wire %s[%d:0] in scope %s\n",
|
|
|
|
|
wireName.c_str(), wireWidth - 1, vcd_scope.c_str());
|
|
|
|
|
targetWire = module->addWire(wireId, wireWidth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
|
log("Connecting %s to %s[%d]\n",
|
|
|
|
|
log_id(oldWire), wireName.c_str(), bitIndex);
|
|
|
|
|
|
|
|
|
|
auto rewriter = [&](SigSpec &sig) {
|
|
|
|
|
sig.replace(SigBit(oldWire), SigSpec(targetWire, bitIndex, 1));
|
|
|
|
|
};
|
|
|
|
|
module->rewrite_sigspecs(rewriter);
|
|
|
|
|
|
|
|
|
|
wiresToRemove.insert(oldWire);
|
2026-01-29 02:00:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Delete the old unused wires
|
|
|
|
|
module->remove(wiresToRemove);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void process_all(dict<std::pair<std::string, std::string>, int> &vcd_reg_widths)
|
|
|
|
|
{
|
|
|
|
|
process_registers(vcd_reg_widths);
|
|
|
|
|
for (auto &it : children)
|
|
|
|
|
it.second->process_all(vcd_reg_widths);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-17 01:32:04 +01:00
|
|
|
struct RegRenamePass : public Pass {
|
2026-01-29 02:00:46 +01:00
|
|
|
RegRenamePass()
|
2026-01-29 02:40:57 +01:00
|
|
|
: Pass("reg_rename", "renames register output wires to the correct "
|
|
|
|
|
"register name and creates new wires for multi-bit registers for "
|
|
|
|
|
"correct VCD register annotations.")
|
2026-01-29 02:00:46 +01:00
|
|
|
{
|
|
|
|
|
}
|
2026-01-17 01:32:04 +01:00
|
|
|
void help() override
|
|
|
|
|
{
|
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
|
log("\n");
|
2026-01-21 00:35:13 +01:00
|
|
|
log(" reg_rename [options]\n");
|
|
|
|
|
log("\n");
|
|
|
|
|
log(" -vcd <filename>\n");
|
|
|
|
|
log(" vcd file to extract original register width from\n");
|
2026-01-29 02:00:46 +01:00
|
|
|
log(" -scope <scope>\n");
|
|
|
|
|
log(" scope to process in vcd file\n");
|
2026-01-17 01:32:04 +01:00
|
|
|
log("\n");
|
2026-04-15 20:50:35 +02:00
|
|
|
log(" -d\n");
|
|
|
|
|
log(" enable debug output\n");
|
|
|
|
|
log("\n");
|
2026-01-17 01:32:04 +01:00
|
|
|
}
|
|
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
|
|
|
|
{
|
|
|
|
|
log_header(design, "Executing reg_rename pass\n");
|
|
|
|
|
|
2026-01-29 02:00:46 +01:00
|
|
|
// Argument parsing
|
2026-01-21 00:35:13 +01:00
|
|
|
std::string vcd_filename;
|
2026-01-29 02:00:46 +01:00
|
|
|
std::string scope;
|
2026-04-15 20:50:35 +02:00
|
|
|
bool debug = false;
|
2026-01-17 01:32:04 +01:00
|
|
|
size_t argidx;
|
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
2026-01-29 02:00:46 +01:00
|
|
|
if (args[argidx] == "-vcd" && argidx + 1 < args.size()) {
|
2026-01-21 00:35:13 +01:00
|
|
|
vcd_filename = args[++argidx];
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-01-29 02:00:46 +01:00
|
|
|
if (args[argidx] == "-scope" && argidx + 1 < args.size()) {
|
2026-04-09 01:18:55 +02:00
|
|
|
scope = normalize_scope(args[++argidx]);
|
2026-01-29 02:00:46 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
2026-04-15 20:50:35 +02:00
|
|
|
if (args[argidx] == "-d") {
|
|
|
|
|
debug = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-01-17 01:32:04 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
2026-03-02 00:39:35 +01:00
|
|
|
// Extract top module
|
|
|
|
|
Module *topmod = design->top_module();
|
|
|
|
|
if (!topmod)
|
|
|
|
|
log_error("No top module found!\n");
|
|
|
|
|
|
2026-01-29 02:00:46 +01:00
|
|
|
// Extract pre-optimization register widths from VCD file
|
|
|
|
|
dict<std::pair<std::string, std::string>, int> vcd_reg_widths;
|
2026-01-21 00:35:13 +01:00
|
|
|
if (!vcd_filename.empty()) {
|
|
|
|
|
log("Reading VCD file: %s\n", vcd_filename.c_str());
|
|
|
|
|
try {
|
|
|
|
|
FstData fst(vcd_filename);
|
2026-03-02 00:39:35 +01:00
|
|
|
if (scope.empty()) {
|
|
|
|
|
scope = fst.autoScope(topmod);
|
|
|
|
|
if (scope.empty()) {
|
2026-03-02 20:05:44 +01:00
|
|
|
log_error("No scope found for module '%s'. Please specify -scope explicitly.\n",
|
2026-03-02 00:39:35 +01:00
|
|
|
RTLIL::unescape_id(topmod->name).c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log("Using scope: \"%s\"\n", scope.c_str());
|
2026-01-21 00:35:13 +01:00
|
|
|
for (auto &var : fst.getVars()) {
|
|
|
|
|
if (var.is_reg) {
|
2026-01-29 02:00:46 +01:00
|
|
|
std::string reg_vcd_scope = var.scope;
|
2026-01-21 00:35:13 +01:00
|
|
|
std::string reg_name = var.name;
|
2026-01-29 02:00:46 +01:00
|
|
|
|
|
|
|
|
// Remove bracket notation if present to preserve register name
|
2026-04-15 20:50:35 +02:00
|
|
|
if (auto pos = reg_name.rfind('['); pos != std::string::npos)
|
2026-01-21 00:35:13 +01:00
|
|
|
reg_name.erase(pos);
|
2026-01-29 02:00:46 +01:00
|
|
|
|
2026-01-29 02:20:19 +01:00
|
|
|
// Map the register's vcd scope and name to
|
|
|
|
|
// its original width for later lookup.
|
2026-01-29 02:00:46 +01:00
|
|
|
vcd_reg_widths[{reg_vcd_scope, reg_name}] = var.width;
|
2026-04-15 20:50:35 +02:00
|
|
|
if (debug)
|
|
|
|
|
log("Found register '%s' in scope '%s' with width %d\n",
|
|
|
|
|
reg_name.c_str(), reg_vcd_scope.c_str(), var.width);
|
2026-01-21 00:35:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-29 02:00:46 +01:00
|
|
|
log("Extracted %d register widths from VCD\n", GetSize(vcd_reg_widths));
|
2026-01-21 00:35:13 +01:00
|
|
|
} catch (const std::exception &e) {
|
2026-01-29 02:20:19 +01:00
|
|
|
log_error("Failed to read VCD file '%s': %s\n",
|
|
|
|
|
vcd_filename.c_str(), e.what());
|
2026-01-21 00:35:13 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-01-29 02:00:46 +01:00
|
|
|
log_error("No VCD file provided. Use -vcd option.\n");
|
2026-01-21 00:35:13 +01:00
|
|
|
}
|
2026-01-19 20:20:11 +01:00
|
|
|
|
2026-01-29 02:00:46 +01:00
|
|
|
// STEP 2: Build hierarchy and process
|
|
|
|
|
log("Building hierarchy from scope: %s\n", scope.c_str());
|
|
|
|
|
|
|
|
|
|
// Build hierarchy and process register renamings
|
2026-04-15 20:50:35 +02:00
|
|
|
RegRenameInstance *root = new RegRenameInstance(scope, topmod, debug);
|
2026-01-29 02:00:46 +01:00
|
|
|
root->process_all(vcd_reg_widths);
|
|
|
|
|
delete root;
|
2026-01-19 21:10:48 +01:00
|
|
|
|
2026-01-17 01:32:04 +01:00
|
|
|
log_flush();
|
|
|
|
|
}
|
|
|
|
|
} RegRenamePass;
|
|
|
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|