abc_prep: refactor utilities into new namespace

This commit is contained in:
Emil J. Tywoniak 2025-01-13 12:40:32 +01:00
parent ee0b91417c
commit 0885780460
4 changed files with 67 additions and 63 deletions

View File

@ -48,7 +48,6 @@
#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>
@ -64,6 +63,7 @@
#endif
#include "frontends/blif/blifparse.h"
#include "passes/techmap/abc_prep.h"
#ifdef YOSYS_LINK_ABC
namespace abc {
@ -72,51 +72,6 @@ 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 {
@ -833,7 +788,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 = tmp_base(cleanup) + "yosys-abc-XXXXXX";
std::string tempdir_name = AbcPrep::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());
@ -2082,9 +2037,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";
std::string lib_tempdir_name = AbcPrep::tmp_base(cleanup) + "yosys-abc-lib-XXXXXX";
lib_tempdir_name = make_temp_dir(lib_tempdir_name);
lib_to_tmp(lib_tempdir_name, liberty_files);
AbcPrep::lib_to_tmp(lib_tempdir_name, liberty_files);
for (auto mod : design->selected_modules())
{

View File

@ -27,6 +27,8 @@
#include "kernel/rtlil.h"
#include "kernel/log.h"
#include "passes/techmap/abc_prep.h"
// abc9_exe.cc
std::string fold_abc9_cmd(std::string str);
@ -405,12 +407,8 @@ struct Abc9Pass : public ScriptPass
if (!active_design->selected_whole_module(mod))
log_error("Can't handle partially selected module %s!\n", log_id(mod));
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 = AbcPrep::tmp_base(cleanup);
tempdir_name += "yosys-abc-XXXXXX";
tempdir_name = make_temp_dir(tempdir_name);
if (!lut_mode)

View File

@ -21,13 +21,10 @@
#include "kernel/rtlil.h"
#include "kernel/utils.h"
#include "kernel/io.h"
#include "kernel/gzip.h"
#include "passes/techmap/abc_prep.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)
@ -118,9 +115,9 @@ struct AbcNewPass : public ScriptPass {
log_header(d, "Executing ABC_NEW pass.\n");
log_push();
std::string lib_tmpdir = tmp_base(cleanup) + "yosys-abc-lib-XXXXXX";
std::string lib_tmpdir = AbcPrep::tmp_base(cleanup) + "yosys-abc-lib-XXXXXX";
lib_tmpdir = make_temp_dir(lib_tmpdir);
lib_to_tmp(lib_tmpdir, liberty_files);
AbcPrep::lib_to_tmp(lib_tmpdir, liberty_files);
run_script(d, run_from, run_to);
if (cleanup)
remove_directory(lib_tmpdir);
@ -167,7 +164,7 @@ struct AbcNewPass : public ScriptPass {
std::string modname = "<module>";
std::string exe_options = "[options]";
if (!help_mode) {
tmpdir = tmp_base(cleanup) + "yosys-abc-XXXXXX";
tmpdir = AbcPrep::tmp_base(cleanup) + "yosys-abc-XXXXXX";
tmpdir = make_temp_dir(tmpdir);
modname = mod->name.str();
exe_options = abc_exe_options;

54
passes/techmap/abc_prep.h Normal file
View File

@ -0,0 +1,54 @@
#ifndef ABC_PREP_H
#define ABC_PREP_H
#include "kernel/gzip.h"
USING_YOSYS_NAMESPACE
namespace AbcPrep {
inline 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
*/
inline 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;
}
}
}
};
#endif /* ABC_PREP_H */