abc: centralize gzipped Liberty extraction, use a separate directory, still allow per-module tmp dirs

This commit is contained in:
Emil J. Tywoniak 2025-01-13 12:34:01 +01:00
parent 4d6f16567c
commit ee0b91417c
2 changed files with 70 additions and 49 deletions
passes/techmap

View File

@ -48,6 +48,7 @@
#include "kernel/ff.h"
#include "kernel/cost.h"
#include "kernel/log.h"
#include "kernel/gzip.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -71,6 +72,51 @@ namespace abc {
#endif
USING_YOSYS_NAMESPACE
std::string tmp_base(bool cleanup)
{
std::string base;
if (cleanup)
base = get_base_tmpdir() + "/";
else
base = "_tmp_";
return base + proc_program_prefix();
}
/**
* Extracts gzipped liberty_files and rewrites their paths
* to the new temporary file paths
*/
void lib_to_tmp(std::string top_tmpdir, std::vector<std::string>& liberty_files)
{
for (std::string& f : liberty_files) {
bool ends_gz = false;
auto dot_pos = f.find_last_of(".");
if(dot_pos != std::string::npos)
ends_gz = f.substr(dot_pos+1) == "gz";
log_debug("Does %s end with .gz? %d\n", f.c_str(), ends_gz);
if (ends_gz) {
auto filename_pos = f.find_last_of("/");
if(filename_pos == std::string::npos)
filename_pos = f.find_last_of("\\");
if(filename_pos == std::string::npos) {
filename_pos = 0;
} else {
filename_pos++;
}
std::istream* s = uncompressed(f);
std::string base = f.substr(filename_pos, dot_pos - filename_pos);
log_debug("base %s\n", base.c_str());
std::string tmp_f = top_tmpdir + "/" + base + "-XXXXXX";
tmp_f = make_temp_file(tmp_f);
log_debug("tmp_f %s\n", tmp_f.c_str());
std::ofstream out(tmp_f);
out << s->rdbuf();
f = tmp_f;
}
}
}
PRIVATE_NAMESPACE_BEGIN
enum class gate_type_t {
@ -787,12 +833,7 @@ void abc_module(RTLIL::Design *design, RTLIL::Module *current_module, std::strin
if (dff_mode && clk_sig.empty())
log_cmd_error("Clock domain %s not found.\n", clk_str.c_str());
std::string tempdir_name;
if (cleanup)
tempdir_name = get_base_tmpdir() + "/";
else
tempdir_name = "_tmp_";
tempdir_name += proc_program_prefix() + "yosys-abc-XXXXXX";
std::string tempdir_name = tmp_base(cleanup) + "yosys-abc-XXXXXX";
tempdir_name = make_temp_dir(tempdir_name);
log_header(design, "Extracting gate netlist of module `%s' to `%s/input.blif'..\n",
module->name.c_str(), replace_tempdir(tempdir_name, tempdir_name, show_tempdir).c_str());
@ -2041,6 +2082,9 @@ struct AbcPass : public Pass {
enabled_gates.insert("MUX");
// enabled_gates.insert("NMUX");
}
std::string lib_tempdir_name = tmp_base(cleanup) + "yosys-abc-lib-XXXXXX";
lib_tempdir_name = make_temp_dir(lib_tempdir_name);
lib_to_tmp(lib_tempdir_name, liberty_files);
for (auto mod : design->selected_modules())
{
@ -2221,6 +2265,9 @@ struct AbcPass : public Pass {
}
}
if (cleanup)
remove_directory(lib_tempdir_name);
assign_map.clear();
signal_list.clear();
signal_map.clear();

View File

@ -24,6 +24,10 @@
#include "kernel/gzip.h"
USING_YOSYS_NAMESPACE
void lib_to_tmp(std::string top_tmpdir, std::vector<std::string>& liberty_files);
std::string tmp_base(bool cleanup);
PRIVATE_NAMESPACE_BEGIN
std::vector<Module*> order_modules(Design *design, std::vector<Module *> modules)
@ -82,7 +86,6 @@ struct AbcNewPass : public ScriptPass {
bool cleanup;
std::string abc_exe_options;
std::string top_tmpdir;
std::vector<std::string> liberty_files;
void execute(std::vector<std::string> args, RTLIL::Design *d) override
@ -115,13 +118,12 @@ struct AbcNewPass : public ScriptPass {
log_header(d, "Executing ABC_NEW pass.\n");
log_push();
top_tmpdir = cleanup ? (get_base_tmpdir() + "/") : "_tmp_";
top_tmpdir += proc_program_prefix() + "yosys-abc-XXXXXX";
top_tmpdir = make_temp_dir(top_tmpdir);
lib_to_tmp();
std::string lib_tmpdir = tmp_base(cleanup) + "yosys-abc-lib-XXXXXX";
lib_tmpdir = make_temp_dir(lib_tmpdir);
lib_to_tmp(lib_tmpdir, liberty_files);
run_script(d, run_from, run_to);
if (cleanup)
remove_directory(top_tmpdir);
remove_directory(lib_tmpdir);
log_pop();
}
@ -157,12 +159,15 @@ struct AbcNewPass : public ScriptPass {
run("foreach module in selection");
}
for (auto f : liberty_files)
abc_exe_options += stringf(" -liberty %s", f.c_str());
for (auto mod : selected_modules) {
std::string tmpdir = "<abc-temp-dir>/<mod-dir>";
std::string modname = "<module>";
std::string exe_options = "[options]";
if (!help_mode) {
tmpdir = top_tmpdir + "/XXXXXX";
tmpdir = tmp_base(cleanup) + "yosys-abc-XXXXXX";
tmpdir = make_temp_dir(tmpdir);
modname = mod->name.str();
exe_options = abc_exe_options;
@ -202,6 +207,11 @@ struct AbcNewPass : public ScriptPass {
run("abc9_ops -prep_box");
}
}
if (cleanup)
{
log("Removing temp directory.\n");
remove_directory(tmpdir);
}
}
if (!help_mode) {
@ -209,44 +219,8 @@ struct AbcNewPass : public ScriptPass {
}
}
if (cleanup)
{
log("Removing temp directory.\n");
remove_directory(top_tmpdir);
}
}
void lib_to_tmp()
{
for (auto f : liberty_files) {
bool ends_gz = false;
auto dot_pos = f.find_last_of(".");
if(dot_pos != std::string::npos)
ends_gz = f.substr(dot_pos+1) == "gz";
log_debug("Does %s end with .gz? %d\n", f.c_str(), ends_gz);
if (ends_gz) {
auto filename_pos = f.find_last_of("/");
if(filename_pos == std::string::npos)
filename_pos = f.find_last_of("\\");
if(filename_pos == std::string::npos) {
filename_pos = 0;
} else {
filename_pos++;
}
std::istream* s = uncompressed(f);
std::string base = f.substr(filename_pos, dot_pos - filename_pos);
log_debug("base %s\n", base.c_str());
std::string tmp_f = top_tmpdir + "/" + base + "-XXXXXX";
tmp_f = make_temp_file(tmp_f);
log_debug("tmp_f %s\n", tmp_f.c_str());
std::ofstream out(tmp_f);
out << s->rdbuf();
abc_exe_options += stringf(" -liberty %s", tmp_f.c_str());
} else {
abc_exe_options += stringf(" -liberty %s", f.c_str());
}
}
}
} AbcNewPass;
PRIVATE_NAMESPACE_END