yosys/passes/silimate/reg_rename.cc

255 lines
8.5 KiB
C++
Raw Normal View History

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.
*
*/
#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-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-01-29 02:00:46 +01:00
RegRenameInstance(std::string scope, Module *mod) : vcd_scope(scope), module(mod)
{
// 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);
children[cell] = new RegRenameInstance(child_scope, child);
}
}
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)
{
std::regex reg_regex("(.*)_reg(?:\\[(\\d+)\\])?$");
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;
}
// Extract the register name from the cell name
std::smatch match;
std::string name = cell->name.c_str();
if (!std::regex_match(name, match, reg_regex)) {
log_warning("Unable to extract register name from cell %s\n", name.c_str());
continue;
}
// Register name
std::string baseName = RTLIL::unescape_id(match[1].str());
bool isMultiBit = match.size() > 2 && match[2].matched;
for (auto conn : cell->connections()) {
// Rename wires from the register output
if (conn.first == ID::Q && conn.second.is_wire()) {
Wire *oldWire = conn.second.as_wire();
// Skip wires that are inputs or outputs
if (oldWire->port_input || oldWire->port_output)
continue;
// If the register is multi-bit, we must create a new wire
if (isMultiBit) {
int index = std::stoi(match[2].str());
2026-01-29 02:20:19 +01:00
// Lookup the original register width using the VCD scope
// and netlist-extracted register name
2026-01-29 02:00:46 +01:00
int origRegWidth = vcd_reg_widths[{vcd_scope, baseName}];
if (origRegWidth == 0) { // if not found, log a warning and skip
2026-01-29 03:05:21 +01:00
log_debug("Register '%s' with extracted name '%s' in scope '%s' not found in VCD\n",
2026-01-29 02:00:46 +01:00
cell->name.c_str(), baseName.c_str(), vcd_scope.c_str());
continue;
}
// Create a new wire for the multi-bit register if it doesn't exist already
Wire *newWire = module->wire(RTLIL::escape_id(baseName));
if (newWire == nullptr) {
2026-01-29 03:05:21 +01:00
log_debug("Creating wire %s[%d:0] in scope %s\n", baseName.c_str(), origRegWidth - 1,
2026-01-29 02:00:46 +01:00
vcd_scope.c_str());
newWire = module->addWire(RTLIL::escape_id(baseName), origRegWidth);
}
2026-03-31 01:34:20 +02:00
// Check if the bit index exceeds the actual wire width before creating SigSpec
if (index >= newWire->width) {
log_warning("Register bit index %d exceeds wire width %d for '%s' in scope '%s'. Skipping.\n",
index, newWire->width, baseName.c_str(), vcd_scope.c_str());
continue;
}
2026-01-29 02:00:46 +01:00
// Log the connection of the new wire to the register
2026-01-29 03:05:21 +01:00
log_debug("Connecting register wire %s[%d] to bit %d of %s in module %s\n",
2026-01-29 02:20:19 +01:00
newWire->name.c_str(), index, index, log_id(newWire), log_id(module));
2026-01-29 02:00:46 +01:00
2026-03-31 01:34:20 +02:00
// Replace old connection with a new one even at the input ports of subsequent cells from the register output
2026-01-29 02:00:46 +01:00
auto rewriter = [&](SigSpec &sig) { sig.replace(SigBit(oldWire), SigSpec(newWire, index, 1)); };
module->rewrite_sigspecs(rewriter);
// Add the old wires to the list of wires to delete after processing
wiresToRemove.insert(oldWire);
} else {
// Single-bit register rename
IdString target_name = RTLIL::escape_id(baseName);
if (oldWire->name != target_name && !module->wire(target_name)) {
2026-01-29 03:05:21 +01:00
log_debug("Renaming %s to %s in scope %s\n", oldWire->name.c_str(), target_name.c_str(),
2026-01-29 02:00:46 +01:00
vcd_scope.c_str());
module->rename(oldWire, target_name);
}
}
}
}
}
// 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");
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");
}
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
std::string vcd_filename;
2026-01-29 02:00:46 +01:00
std::string scope;
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()) {
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-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;
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());
for (auto &var : fst.getVars()) {
if (var.is_reg) {
2026-01-29 02:00:46 +01:00
std::string reg_vcd_scope = var.scope;
std::string reg_name = var.name;
2026-01-29 02:00:46 +01:00
// Remove bracket notation if present to preserve register name
if (auto pos = reg_name.find('['); pos != std::string::npos)
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-01-29 03:05:21 +01:00
log_debug("Found register '%s' in scope '%s' with width %d\n",
2026-01-29 02:20:19 +01:00
reg_name.c_str(), reg_vcd_scope.c_str(), var.width);
}
}
2026-01-29 02:00:46 +01:00
log("Extracted %d register widths from VCD\n", GetSize(vcd_reg_widths));
} 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());
}
} else {
2026-01-29 02:00:46 +01:00
log_error("No VCD file provided. Use -vcd option.\n");
}
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
RegRenameInstance *root = new RegRenameInstance(scope, topmod);
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