mirror of
https://github.com/YosysHQ/yosys
synced 2026-07-21 06:35:49 +00:00
Merge upstream yosys
This commit is contained in:
commit
5f5ed1b29e
41 changed files with 920 additions and 237 deletions
|
|
@ -54,6 +54,7 @@ OBJS += passes/techmap/flowmap.o
|
|||
OBJS += passes/techmap/extractinv.o
|
||||
OBJS += passes/techmap/cellmatch.o
|
||||
OBJS += passes/techmap/clockgate.o
|
||||
OBJS += passes/techmap/constmap.o
|
||||
endif
|
||||
|
||||
ifeq ($(DISABLE_SPAWN),0)
|
||||
|
|
@ -62,5 +63,5 @@ EXTRA_OBJS += passes/techmap/filterlib.o
|
|||
|
||||
$(PROGRAM_PREFIX)yosys-filterlib$(EXE): passes/techmap/filterlib.o
|
||||
$(Q) mkdir -p $(dir $@)
|
||||
$(P) $(CXX) -o $(PROGRAM_PREFIX)yosys-filterlib$(EXE) $(LINKFLAGS) $^ $(LIBS)
|
||||
$(P) $(CXX) -o $(PROGRAM_PREFIX)yosys-filterlib$(EXE) $(LINKFLAGS) $^ $(EXE_LIBS) $(LIBS)
|
||||
endif
|
||||
|
|
|
|||
|
|
@ -142,7 +142,7 @@ struct AlumaccWorker
|
|||
log(" creating $macc model for %s (%s).\n", log_id(cell), log_id(cell->type));
|
||||
|
||||
maccnode_t *n = new maccnode_t;
|
||||
Macc::port_t new_port;
|
||||
Macc::term_t new_term;
|
||||
|
||||
n->cell = cell;
|
||||
n->y = sigmap(cell->getPort(ID::Y));
|
||||
|
|
@ -153,32 +153,32 @@ struct AlumaccWorker
|
|||
|
||||
if (cell->type.in(ID($pos), ID($neg)))
|
||||
{
|
||||
new_port.in_a = sigmap(cell->getPort(ID::A));
|
||||
new_port.is_signed = cell->getParam(ID::A_SIGNED).as_bool();
|
||||
new_port.do_subtract = cell->type == ID($neg);
|
||||
n->macc.ports.push_back(new_port);
|
||||
new_term.in_a = sigmap(cell->getPort(ID::A));
|
||||
new_term.is_signed = cell->getParam(ID::A_SIGNED).as_bool();
|
||||
new_term.do_subtract = cell->type == ID($neg);
|
||||
n->macc.terms.push_back(new_term);
|
||||
}
|
||||
|
||||
if (cell->type.in(ID($add), ID($sub)))
|
||||
{
|
||||
new_port.in_a = sigmap(cell->getPort(ID::A));
|
||||
new_port.is_signed = cell->getParam(ID::A_SIGNED).as_bool();
|
||||
new_port.do_subtract = false;
|
||||
n->macc.ports.push_back(new_port);
|
||||
new_term.in_a = sigmap(cell->getPort(ID::A));
|
||||
new_term.is_signed = cell->getParam(ID::A_SIGNED).as_bool();
|
||||
new_term.do_subtract = false;
|
||||
n->macc.terms.push_back(new_term);
|
||||
|
||||
new_port.in_a = sigmap(cell->getPort(ID::B));
|
||||
new_port.is_signed = cell->getParam(ID::B_SIGNED).as_bool();
|
||||
new_port.do_subtract = cell->type == ID($sub);
|
||||
n->macc.ports.push_back(new_port);
|
||||
new_term.in_a = sigmap(cell->getPort(ID::B));
|
||||
new_term.is_signed = cell->getParam(ID::B_SIGNED).as_bool();
|
||||
new_term.do_subtract = cell->type == ID($sub);
|
||||
n->macc.terms.push_back(new_term);
|
||||
}
|
||||
|
||||
if (cell->type.in(ID($mul)))
|
||||
{
|
||||
new_port.in_a = sigmap(cell->getPort(ID::A));
|
||||
new_port.in_b = sigmap(cell->getPort(ID::B));
|
||||
new_port.is_signed = cell->getParam(ID::A_SIGNED).as_bool();
|
||||
new_port.do_subtract = false;
|
||||
n->macc.ports.push_back(new_port);
|
||||
new_term.in_a = sigmap(cell->getPort(ID::A));
|
||||
new_term.in_b = sigmap(cell->getPort(ID::B));
|
||||
new_term.is_signed = cell->getParam(ID::A_SIGNED).as_bool();
|
||||
new_term.do_subtract = false;
|
||||
n->macc.terms.push_back(new_term);
|
||||
}
|
||||
|
||||
log_assert(sig_macc.count(n->y) == 0);
|
||||
|
|
@ -190,7 +190,7 @@ struct AlumaccWorker
|
|||
{
|
||||
std::vector<int> port_sizes;
|
||||
|
||||
for (auto &port : macc.ports) {
|
||||
for (auto &port : macc.terms) {
|
||||
if (port.is_signed != is_signed)
|
||||
return true;
|
||||
if (!port.is_signed && port.do_subtract)
|
||||
|
|
@ -235,9 +235,9 @@ struct AlumaccWorker
|
|||
if (delete_nodes.count(n))
|
||||
continue;
|
||||
|
||||
for (int i = 0; i < GetSize(n->macc.ports); i++)
|
||||
for (int i = 0; i < GetSize(n->macc.terms); i++)
|
||||
{
|
||||
auto &port = n->macc.ports[i];
|
||||
auto &port = n->macc.terms[i];
|
||||
|
||||
if (GetSize(port.in_b) > 0 || sig_macc.count(port.in_a) == 0)
|
||||
continue;
|
||||
|
|
@ -253,13 +253,13 @@ struct AlumaccWorker
|
|||
log(" merging $macc model for %s into %s.\n", log_id(other_n->cell), log_id(n->cell));
|
||||
|
||||
bool do_subtract = port.do_subtract;
|
||||
for (int j = 0; j < GetSize(other_n->macc.ports); j++) {
|
||||
for (int j = 0; j < GetSize(other_n->macc.terms); j++) {
|
||||
if (do_subtract)
|
||||
other_n->macc.ports[j].do_subtract = !other_n->macc.ports[j].do_subtract;
|
||||
other_n->macc.terms[j].do_subtract = !other_n->macc.terms[j].do_subtract;
|
||||
if (j == 0)
|
||||
n->macc.ports[i--] = other_n->macc.ports[j];
|
||||
n->macc.terms[i--] = other_n->macc.terms[j];
|
||||
else
|
||||
n->macc.ports.push_back(other_n->macc.ports[j]);
|
||||
n->macc.terms.push_back(other_n->macc.terms[j]);
|
||||
}
|
||||
|
||||
delete_nodes.insert(other_n);
|
||||
|
|
@ -288,7 +288,7 @@ struct AlumaccWorker
|
|||
bool subtract_b = false;
|
||||
alunode_t *alunode;
|
||||
|
||||
for (auto &port : n->macc.ports)
|
||||
for (auto &port : n->macc.terms)
|
||||
if (GetSize(port.in_b) > 0) {
|
||||
goto next_macc;
|
||||
} else if (GetSize(port.in_a) == 1 && !port.is_signed && !port.do_subtract) {
|
||||
|
|
|
|||
|
|
@ -227,9 +227,9 @@ struct BoothPassWorker {
|
|||
continue;
|
||||
}
|
||||
|
||||
A = macc.ports[0].in_a;
|
||||
B = macc.ports[0].in_b;
|
||||
is_signed = macc.ports[0].is_signed;
|
||||
A = macc.terms[0].in_a;
|
||||
B = macc.terms[0].in_b;
|
||||
is_signed = macc.terms[0].is_signed;
|
||||
Y = cell->getPort(ID::Y);
|
||||
} else {
|
||||
continue;
|
||||
|
|
|
|||
106
passes/techmap/constmap.cc
Normal file
106
passes/techmap/constmap.cc
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* yosys -- Yosys Open SYnthesis Suite
|
||||
*
|
||||
* Copyright (C) 2025 King Lok Chung <king.chung@manchester.ac.uk>
|
||||
*
|
||||
* 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/register.h"
|
||||
#include "kernel/rtlil.h"
|
||||
#include "kernel/log.h"
|
||||
|
||||
USING_YOSYS_NAMESPACE
|
||||
PRIVATE_NAMESPACE_BEGIN
|
||||
|
||||
static std::string celltype, cell_portname, cell_paramname;
|
||||
|
||||
static RTLIL::Module *module;
|
||||
static RTLIL::SigChunk value;
|
||||
|
||||
void constmap_worker(RTLIL::SigSpec &sig)
|
||||
{
|
||||
if (sig.is_fully_const()){
|
||||
value = module->addWire(NEW_ID, sig.size());
|
||||
RTLIL::Cell *cell = module->addCell(NEW_ID, celltype);
|
||||
cell->setParam(cell_paramname, sig.as_const());
|
||||
cell->setPort(cell_portname, value);
|
||||
sig = value;
|
||||
}
|
||||
}
|
||||
|
||||
struct ConstmapPass : public Pass {
|
||||
ConstmapPass() : Pass("constmap", "technology mapping of coarse constant value") { }
|
||||
void help() override
|
||||
{
|
||||
log("\n");
|
||||
log(" constmap [options] [selection]\n");
|
||||
log("\n");
|
||||
log("Map constants to a driver cell.\n");
|
||||
log("\n");
|
||||
log(" -cell <celltype> <portname> <paramname>\n");
|
||||
log(" Replace constant bits with this cell.\n");
|
||||
log(" The value of the constant will be stored to the parameter specified.\n");
|
||||
log("\n");
|
||||
}
|
||||
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
||||
{
|
||||
log_header(design, "Executing CONSTMAP pass (mapping to constant driver).\n");
|
||||
|
||||
size_t argidx;
|
||||
for (argidx = 1; argidx < args.size(); argidx++)
|
||||
{
|
||||
if (args[argidx] == "-cell" && argidx+3 < args.size()){
|
||||
celltype = RTLIL::escape_id(args[++argidx]);
|
||||
cell_portname = RTLIL::escape_id(args[++argidx]);
|
||||
cell_paramname = RTLIL::escape_id(args[++argidx]);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
extra_args(args, argidx, design);
|
||||
|
||||
|
||||
if (design->has(celltype)) {
|
||||
Module *existing = design->module(celltype);
|
||||
bool has_port = false;
|
||||
for (auto &p : existing->ports){
|
||||
if (p == cell_portname){
|
||||
has_port = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!has_port)
|
||||
log_cmd_error("Cell type '%s' does not have port '%s'.\n", celltype.c_str(), cell_portname.c_str());
|
||||
|
||||
bool has_param = false;
|
||||
for (auto &p : existing->avail_parameters){
|
||||
if (p == cell_paramname)
|
||||
has_param = true;
|
||||
}
|
||||
|
||||
if (!has_param)
|
||||
log_cmd_error("Cell type '%s' does not have parameter '%s'.\n", celltype.c_str(), cell_paramname.c_str());
|
||||
}
|
||||
|
||||
|
||||
for (auto mod : design->selected_modules())
|
||||
{
|
||||
module = mod;
|
||||
module->rewrite_sigspecs(constmap_worker);
|
||||
}
|
||||
}
|
||||
} ConstmapPass;
|
||||
|
||||
PRIVATE_NAMESPACE_END
|
||||
|
|
@ -75,7 +75,7 @@ bool LibertyInputStream::extend_buffer_once()
|
|||
buffer.resize(buf_end + chunk_size);
|
||||
}
|
||||
|
||||
size_t read_size = f.rdbuf()->sgetn(buffer.data() + buf_end, chunk_size);
|
||||
size_t read_size = f.rdbuf()->sgetn((char *)buffer.data() + buf_end, chunk_size);
|
||||
buf_end += read_size;
|
||||
if (read_size < chunk_size)
|
||||
eof = true;
|
||||
|
|
@ -436,6 +436,9 @@ void LibertyParser::report_unexpected_token(int tok)
|
|||
eReport += "'.";
|
||||
error(eReport);
|
||||
break;
|
||||
case EOF:
|
||||
error("Unexpected end of file");
|
||||
break;
|
||||
default:
|
||||
eReport = "Unexpected token: ";
|
||||
eReport += static_cast<char>(tok);
|
||||
|
|
@ -484,7 +487,7 @@ void LibertyParser::parse_vector_range(int tok)
|
|||
}
|
||||
}
|
||||
|
||||
LibertyAst *LibertyParser::parse()
|
||||
LibertyAst *LibertyParser::parse(bool top_level)
|
||||
{
|
||||
std::string str;
|
||||
|
||||
|
|
@ -498,7 +501,13 @@ LibertyAst *LibertyParser::parse()
|
|||
while ((tok == 'n') || (tok == ';'))
|
||||
tok = lexer(str);
|
||||
|
||||
if (tok == '}' || tok < 0)
|
||||
if (tok == EOF) {
|
||||
if (top_level)
|
||||
return NULL;
|
||||
report_unexpected_token(tok);
|
||||
}
|
||||
|
||||
if (tok == '}')
|
||||
return NULL;
|
||||
|
||||
if (tok != 'v') {
|
||||
|
|
@ -571,12 +580,18 @@ LibertyAst *LibertyParser::parse()
|
|||
}
|
||||
|
||||
if (tok == '{') {
|
||||
bool terminated = false;
|
||||
while (1) {
|
||||
LibertyAst *child = parse();
|
||||
if (child == NULL)
|
||||
LibertyAst *child = parse(false);
|
||||
if (child == NULL) {
|
||||
terminated = true;
|
||||
break;
|
||||
}
|
||||
ast->children.push_back(child);
|
||||
}
|
||||
if (!terminated) {
|
||||
report_unexpected_token(EOF);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ namespace Yosys
|
|||
|
||||
class LibertyInputStream {
|
||||
std::istream &f;
|
||||
std::vector<char> buffer;
|
||||
std::vector<unsigned char> buffer;
|
||||
size_t buf_pos = 0;
|
||||
size_t buf_end = 0;
|
||||
bool eof = false;
|
||||
|
|
@ -107,7 +107,7 @@ namespace Yosys
|
|||
LibertyInputStream(std::istream &f) : f(f) {}
|
||||
|
||||
size_t buffered_size() { return buf_end - buf_pos; }
|
||||
const char *buffered_data() { return buffer.data() + buf_pos; }
|
||||
const unsigned char *buffered_data() { return buffer.data() + buf_pos; }
|
||||
|
||||
int get() {
|
||||
if (buf_pos == buf_end)
|
||||
|
|
@ -165,7 +165,7 @@ namespace Yosys
|
|||
|
||||
void report_unexpected_token(int tok);
|
||||
void parse_vector_range(int tok);
|
||||
LibertyAst *parse();
|
||||
LibertyAst *parse(bool top_level);
|
||||
void error() const;
|
||||
void error(const std::string &str) const;
|
||||
|
||||
|
|
@ -174,18 +174,29 @@ namespace Yosys
|
|||
const LibertyAst *ast = nullptr;
|
||||
|
||||
LibertyParser(std::istream &f) : f(f), line(1) {
|
||||
shared_ast.reset(parse());
|
||||
shared_ast.reset(parse(true));
|
||||
ast = shared_ast.get();
|
||||
if (!ast) {
|
||||
#ifdef FILTERLIB
|
||||
fprintf(stderr, "No entries found in liberty file.\n");
|
||||
exit(1);
|
||||
#else
|
||||
log_error("No entries found in liberty file.\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef FILTERLIB
|
||||
LibertyParser(std::istream &f, const std::string &fname) : f(f), line(1) {
|
||||
shared_ast = LibertyAstCache::instance.cached_ast(fname);
|
||||
if (!shared_ast) {
|
||||
shared_ast.reset(parse());
|
||||
shared_ast.reset(parse(true));
|
||||
LibertyAstCache::instance.parsed_ast(fname, shared_ast);
|
||||
}
|
||||
ast = shared_ast.get();
|
||||
if (!ast) {
|
||||
log_error("No entries found in liberty file `%s'.\n", fname.c_str());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
|
|
|||
|
|
@ -278,42 +278,42 @@ void maccmap(RTLIL::Module *module, RTLIL::Cell *cell, bool unmap)
|
|||
return;
|
||||
}
|
||||
|
||||
for (auto &port : macc.ports)
|
||||
if (GetSize(port.in_b) == 0)
|
||||
log(" %s %s (%d bits, %s)\n", port.do_subtract ? "sub" : "add", log_signal(port.in_a),
|
||||
GetSize(port.in_a), port.is_signed ? "signed" : "unsigned");
|
||||
for (auto &term : macc.terms)
|
||||
if (GetSize(term.in_b) == 0)
|
||||
log(" %s %s (%d bits, %s)\n", term.do_subtract ? "sub" : "add", log_signal(term.in_a),
|
||||
GetSize(term.in_a), term.is_signed ? "signed" : "unsigned");
|
||||
else
|
||||
log(" %s %s * %s (%dx%d bits, %s)\n", port.do_subtract ? "sub" : "add", log_signal(port.in_a), log_signal(port.in_b),
|
||||
GetSize(port.in_a), GetSize(port.in_b), port.is_signed ? "signed" : "unsigned");
|
||||
log(" %s %s * %s (%dx%d bits, %s)\n", term.do_subtract ? "sub" : "add", log_signal(term.in_a), log_signal(term.in_b),
|
||||
GetSize(term.in_a), GetSize(term.in_b), term.is_signed ? "signed" : "unsigned");
|
||||
|
||||
if (unmap)
|
||||
{
|
||||
typedef std::pair<RTLIL::SigSpec, bool> summand_t;
|
||||
std::vector<summand_t> summands;
|
||||
|
||||
RTLIL::SigSpec bit_ports;
|
||||
RTLIL::SigSpec bit_terms;
|
||||
|
||||
for (auto &port : macc.ports) {
|
||||
for (auto &term : macc.terms) {
|
||||
summand_t this_summand;
|
||||
if (GetSize(port.in_b)) {
|
||||
if (GetSize(term.in_b)) {
|
||||
this_summand.first = module->addWire(NEW_ID, width);
|
||||
module->addMul(NEW_ID, port.in_a, port.in_b, this_summand.first, port.is_signed);
|
||||
} else if (GetSize(port.in_a) == 1 && GetSize(port.in_b) == 0 && !port.is_signed && !port.do_subtract) {
|
||||
// Mimic old 'bit_ports' treatment in case it's relevant for performance,
|
||||
module->addMul(NEW_ID, term.in_a, term.in_b, this_summand.first, term.is_signed);
|
||||
} else if (GetSize(term.in_a) == 1 && GetSize(term.in_b) == 0 && !term.is_signed && !term.do_subtract) {
|
||||
// Mimic old 'bit_terms' treatment in case it's relevant for performance,
|
||||
// i.e. defer single-bit summands to be the last ones
|
||||
bit_ports.append(port.in_a);
|
||||
bit_terms.append(term.in_a);
|
||||
continue;
|
||||
} else if (GetSize(port.in_a) != width) {
|
||||
} else if (GetSize(term.in_a) != width) {
|
||||
this_summand.first = module->addWire(NEW_ID, width);
|
||||
module->addPos(NEW_ID, port.in_a, this_summand.first, port.is_signed);
|
||||
module->addPos(NEW_ID, term.in_a, this_summand.first, term.is_signed);
|
||||
} else {
|
||||
this_summand.first = port.in_a;
|
||||
this_summand.first = term.in_a;
|
||||
}
|
||||
this_summand.second = port.do_subtract;
|
||||
this_summand.second = term.do_subtract;
|
||||
summands.push_back(this_summand);
|
||||
}
|
||||
|
||||
for (auto &bit : bit_ports)
|
||||
for (auto &bit : bit_terms)
|
||||
summands.push_back(summand_t(bit, false));
|
||||
|
||||
if (GetSize(summands) == 0)
|
||||
|
|
@ -350,20 +350,20 @@ void maccmap(RTLIL::Module *module, RTLIL::Cell *cell, bool unmap)
|
|||
else
|
||||
{
|
||||
MaccmapWorker worker(module, width);
|
||||
RTLIL::SigSpec bit_ports;
|
||||
RTLIL::SigSpec bit_terms;
|
||||
|
||||
for (auto &port : macc.ports) {
|
||||
// Mimic old 'bit_ports' treatment in case it's relevant for performance,
|
||||
for (auto &term : macc.terms) {
|
||||
// Mimic old 'bit_terms' treatment in case it's relevant for performance,
|
||||
// i.e. defer single-bit summands to be the last ones
|
||||
if (GetSize(port.in_a) == 1 && GetSize(port.in_b) == 0 && !port.is_signed && !port.do_subtract)
|
||||
bit_ports.append(port.in_a);
|
||||
else if (GetSize(port.in_b) == 0)
|
||||
worker.add(port.in_a, port.is_signed, port.do_subtract);
|
||||
if (GetSize(term.in_a) == 1 && GetSize(term.in_b) == 0 && !term.is_signed && !term.do_subtract)
|
||||
bit_terms.append(term.in_a);
|
||||
else if (GetSize(term.in_b) == 0)
|
||||
worker.add(term.in_a, term.is_signed, term.do_subtract);
|
||||
else
|
||||
worker.add(port.in_a, port.in_b, port.is_signed, port.do_subtract);
|
||||
worker.add(term.in_a, term.in_b, term.is_signed, term.do_subtract);
|
||||
}
|
||||
|
||||
for (auto bit : bit_ports)
|
||||
for (auto bit : bit_terms)
|
||||
worker.add(bit, 0);
|
||||
|
||||
module->connect(cell->getPort(ID::Y), worker.synth());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue