Skip to content

Commit

Permalink
dfflibmap: Add a -dont_use flag to ignore cells
Browse files Browse the repository at this point in the history
This is an alternative to setting the dont_use property in lib. This brings
dfflibmap in parity with the abc pass for dont_use.

Signed-off-by: Austin Rovinski <[email protected]>
  • Loading branch information
rovinski committed Feb 19, 2024
1 parent f8d4d71 commit d122185
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 23 deletions.
98 changes: 75 additions & 23 deletions passes/techmap/dfflibmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
#include <string.h>
#include <errno.h>

#ifdef _WIN32
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#else
#include <fnmatch.h>
#endif

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

Expand Down Expand Up @@ -115,7 +122,15 @@ static bool parse_pin(LibertyAst *cell, LibertyAst *attr, std::string &pin_name,
return false;
}

static void find_cell(LibertyAst *ast, IdString cell_type, bool clkpol, bool has_reset, bool rstpol, bool rstval)
static int glob_match(const char *pattern, const char *string) {
#ifdef _WIN32
return PathMatchSpec(string, pattern);
#else
return fnmatch(pattern, string, 0) == 0;
#endif
}

static void find_cell(LibertyAst *ast, IdString cell_type, bool clkpol, bool has_reset, bool rstpol, bool rstval, std::vector<std::string> &dont_use_cells)
{
LibertyAst *best_cell = nullptr;
std::map<std::string, char> best_cell_ports;
Expand All @@ -135,6 +150,18 @@ static void find_cell(LibertyAst *ast, IdString cell_type, bool clkpol, bool has
if (dn != nullptr && dn->value == "true")
continue;

bool dont_use = false;
for (std::string &dont_use_cell : dont_use_cells)
{
if (glob_match(dont_use_cell.c_str(), cell->args[0].c_str()))
{
dont_use = true;
break;
}
}
if (dont_use)
continue;

LibertyAst *ff = cell->find("ff");
if (ff == nullptr)
continue;
Expand Down Expand Up @@ -219,6 +246,8 @@ static void find_cell(LibertyAst *ast, IdString cell_type, bool clkpol, bool has
continue_cell_loop:;
}



if (best_cell != nullptr) {
log(" cell %s (%sinv, pins=%d, area=%.2f) is a direct match for cell type %s.\n",
best_cell->args[0].c_str(), best_cell_noninv ? "non" : "", best_cell_pins, best_cell_area, cell_type.c_str());
Expand All @@ -227,7 +256,7 @@ static void find_cell(LibertyAst *ast, IdString cell_type, bool clkpol, bool has
}
}

static void find_cell_sr(LibertyAst *ast, IdString cell_type, bool clkpol, bool setpol, bool clrpol)
static void find_cell_sr(LibertyAst *ast, IdString cell_type, bool clkpol, bool setpol, bool clrpol, std::vector<std::string> &dont_use_cells)
{
LibertyAst *best_cell = nullptr;
std::map<std::string, char> best_cell_ports;
Expand All @@ -247,6 +276,18 @@ static void find_cell_sr(LibertyAst *ast, IdString cell_type, bool clkpol, bool
if (dn != nullptr && dn->value == "true")
continue;

bool dont_use = false;
for (std::string &dont_use_cell : dont_use_cells)
{
if (glob_match(dont_use_cell.c_str(), cell->args[0].c_str()))
{
dont_use = true;
break;
}
}
if (dont_use)
continue;

LibertyAst *ff = cell->find("ff");
if (ff == nullptr)
continue;
Expand Down Expand Up @@ -414,7 +455,7 @@ struct DfflibmapPass : public Pass {
void help() override
{
log("\n");
log(" dfflibmap [-prepare] [-map-only] [-info] -liberty <file> [selection]\n");
log(" dfflibmap [-prepare] [-map-only] [-info] [-dont_use <cell_name>] -liberty <file> [selection]\n");
log("\n");
log("Map internal flip-flop cells to the flip-flop cells in the technology\n");
log("library specified in the given liberty file.\n");
Expand All @@ -435,6 +476,11 @@ struct DfflibmapPass : public Pass {
log("that would be passed to the dfflegalize pass. The design will not be\n");
log("changed.\n");
log("\n");
log("When called with -dont_use, this command will not map to the specified cell\n");
log("name as an alternative to setting the dont_use property in the Liberty file.\n");
log("This argument can be called multiple times with different cell names. This\n");
log("argument also supports simple glob patterns in the cell name.\n");
log("\n");
}
void execute(std::vector<std::string> args, RTLIL::Design *design) override
{
Expand All @@ -446,6 +492,8 @@ struct DfflibmapPass : public Pass {
bool map_only_mode = false;
bool info_mode = false;

std::vector<std::string> dont_use_cells;

size_t argidx;
for (argidx = 1; argidx < args.size(); argidx++)
{
Expand All @@ -467,6 +515,10 @@ struct DfflibmapPass : public Pass {
info_mode = true;
continue;
}
if (arg == "-dont_use" && argidx+1 < args.size()) {
dont_use_cells.push_back(args[++argidx]);
continue;
}
break;
}
extra_args(args, argidx, design);
Expand All @@ -491,26 +543,26 @@ struct DfflibmapPass : public Pass {
LibertyParser libparser(f);
f.close();

find_cell(libparser.ast, ID($_DFF_N_), false, false, false, false);
find_cell(libparser.ast, ID($_DFF_P_), true, false, false, false);

find_cell(libparser.ast, ID($_DFF_NN0_), false, true, false, false);
find_cell(libparser.ast, ID($_DFF_NN1_), false, true, false, true);
find_cell(libparser.ast, ID($_DFF_NP0_), false, true, true, false);
find_cell(libparser.ast, ID($_DFF_NP1_), false, true, true, true);
find_cell(libparser.ast, ID($_DFF_PN0_), true, true, false, false);
find_cell(libparser.ast, ID($_DFF_PN1_), true, true, false, true);
find_cell(libparser.ast, ID($_DFF_PP0_), true, true, true, false);
find_cell(libparser.ast, ID($_DFF_PP1_), true, true, true, true);

find_cell_sr(libparser.ast, ID($_DFFSR_NNN_), false, false, false);
find_cell_sr(libparser.ast, ID($_DFFSR_NNP_), false, false, true);
find_cell_sr(libparser.ast, ID($_DFFSR_NPN_), false, true, false);
find_cell_sr(libparser.ast, ID($_DFFSR_NPP_), false, true, true);
find_cell_sr(libparser.ast, ID($_DFFSR_PNN_), true, false, false);
find_cell_sr(libparser.ast, ID($_DFFSR_PNP_), true, false, true);
find_cell_sr(libparser.ast, ID($_DFFSR_PPN_), true, true, false);
find_cell_sr(libparser.ast, ID($_DFFSR_PPP_), true, true, true);
find_cell(libparser.ast, ID($_DFF_N_), false, false, false, false, dont_use_cells);
find_cell(libparser.ast, ID($_DFF_P_), true, false, false, false, dont_use_cells);

find_cell(libparser.ast, ID($_DFF_NN0_), false, true, false, false, dont_use_cells);
find_cell(libparser.ast, ID($_DFF_NN1_), false, true, false, true, dont_use_cells);
find_cell(libparser.ast, ID($_DFF_NP0_), false, true, true, false, dont_use_cells);
find_cell(libparser.ast, ID($_DFF_NP1_), false, true, true, true, dont_use_cells);
find_cell(libparser.ast, ID($_DFF_PN0_), true, true, false, false, dont_use_cells);
find_cell(libparser.ast, ID($_DFF_PN1_), true, true, false, true, dont_use_cells);
find_cell(libparser.ast, ID($_DFF_PP0_), true, true, true, false, dont_use_cells);
find_cell(libparser.ast, ID($_DFF_PP1_), true, true, true, true, dont_use_cells);

find_cell_sr(libparser.ast, ID($_DFFSR_NNN_), false, false, false, dont_use_cells);
find_cell_sr(libparser.ast, ID($_DFFSR_NNP_), false, false, true, dont_use_cells);
find_cell_sr(libparser.ast, ID($_DFFSR_NPN_), false, true, false, dont_use_cells);
find_cell_sr(libparser.ast, ID($_DFFSR_NPP_), false, true, true, dont_use_cells);
find_cell_sr(libparser.ast, ID($_DFFSR_PNN_), true, false, false, dont_use_cells);
find_cell_sr(libparser.ast, ID($_DFFSR_PNP_), true, false, true, dont_use_cells);
find_cell_sr(libparser.ast, ID($_DFFSR_PPN_), true, true, false, dont_use_cells);
find_cell_sr(libparser.ast, ID($_DFFSR_PPP_), true, true, true, dont_use_cells);

log(" final dff cell mappings:\n");
logmap_all();
Expand Down
7 changes: 7 additions & 0 deletions tests/techmap/dfflibmap.ys
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ select -assert-count 4 t:$_NOT_
select -assert-count 1 t:dffn
select -assert-count 4 t:dffsr
select -assert-none t:dffn t:dffsr t:$_NOT_ %% %n t:* %i

design -load orig
dfflibmap -liberty dfflibmap.lib -dont_use *ffn
clean

select -assert-count 0 t:dffn
select -assert-count 5 t:dffsr

0 comments on commit d122185

Please sign in to comment.