Skip to content

Commit

Permalink
checker: --cleanup_blif functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-rs committed Oct 23, 2024
1 parent 1aed94c commit 22acc3c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 29 deletions.
72 changes: 56 additions & 16 deletions planning/src/RS/rsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ using std::vector;
using std::string;
using std::endl;

// in 'cleanup' mode checker may modify the blif
bool do_check_blif(CStr cfn,
vector<string>& badInputs,
vector<string>& badOutputs) {
vector<string>& badOutputs,
vector<uspair>& corrected,
bool cleanup) {
assert(cfn);
uint16_t tr = ltrace();
auto& ls = lout();
badInputs.clear();
badOutputs.clear();
corrected.clear();
flush_out(false);

BLIF_file bfile(string{cfn});

Expand All @@ -25,26 +30,26 @@ bool do_check_blif(CStr cfn,

bool exi = false;
exi = bfile.fileExists();
if (tr >= 7)
ls << int(exi) << endl;
if (not exi) {
err_puts(); flush_out(true);
lprintf2("[Error] file '%s' does not exist\n", cfn);
flush_out(true); err_puts();
return false;
}

exi = bfile.fileAccessible();
if (tr >= 7)
ls << int(exi) << endl;
if (not exi) {
err_puts(); flush_out(true);
lprintf2("[Error] file '%s' is not accessible\n", cfn);
flush_out(true); err_puts();
return false;
}

bool rd_ok = bfile.readBlif();
if (tr >= 7)
ls << int(rd_ok) << endl;
if (not rd_ok) {
err_puts(); flush_out(true);
lprintf2("[Error] failed reading file '%s'\n", cfn);
flush_out(true); err_puts();
return false;
}

Expand Down Expand Up @@ -122,14 +127,32 @@ bool do_check_blif(CStr cfn,
std::filesystem::path base_path = full_path.filename();
std::string base = base_path.string();
//lputs9();
string outFn = str::concat("PLN_W", std::to_string(numWarn), "_", base);

string wr_ok = bfile.writeBlif(outFn, numWarn);

if (wr_ok.empty())
string outFn;
if (cleanup) {
flush_out(true);
lprintf("[PLANNER BLIF-CLEANER] : replacing file '%s' ...\n", cfn);
flush_out(true);
// cannot write to the currently mem-mapped file,
// write to a temproray and rename later.
outFn = str::concat(full_path.lexically_normal().string(),
"+BLIF_CLEANER.tmp_", std::to_string(get_PID()));
} else {
outFn = str::concat("PLN_WARN", std::to_string(numWarn), "_", base);
}

string wr_ok = bfile.writeBlif(outFn, bool(numWarn), corrected);

if (wr_ok.empty()) {
lprintf("---!! FAILED writeBlif to '%s'\n", outFn.c_str());
else
if (cleanup) {
lprintf("[PLANNER BLIF-CLEANER] : FAILED\n");
}
} else {
lprintf("+++++ WRITTEN '%s'\n", wr_ok.c_str());
if (cleanup) {
lprintf("[PLANNER BLIF-CLEANER] : replaced file '%s'\n", cfn);
}
}
}
}
lprintf("===== passed: %s\n", chk_ok ? "YES" : "NO");
Expand All @@ -156,11 +179,27 @@ bool do_check_blif(CStr cfn,
}

// 'corrected' : (lnum, removed_net)
bool do_cleanup_blif(CStr cfn, std::vector<uspair>& corrected) {
bool do_cleanup_blif(CStr cfn, vector<uspair>& corrected) {
assert(cfn);
corrected.clear();

return false;
vector<string> badInp, badOut;
bool status = do_check_blif(cfn, badInp, badOut, corrected, true);

size_t cor_sz = corrected.size();
if (status and cor_sz) {
flush_out(true);
lprintf("[PLANNER BLIF-CLEANER] : corrected netlist '%s'\n", cfn);
lprintf("-- removed dangling nets (%zu):\n", cor_sz);
for (size_t i = 0; i < cor_sz; i++) {
const uspair& cor = corrected[i];
lprintf(" line %u net %s\n", cor.first, cor.second.c_str());
}
lprintf("-- removed dangling nets (%zu).\n", cor_sz);
flush_out(true);
}

return status;
}

static bool do_check_csv(CStr cfn) {
Expand Down Expand Up @@ -230,7 +269,8 @@ bool do_check(const rsOpts& opts, bool blif_vs_csv) {
bool status;
if (blif_vs_csv) {
vector<string> badInp, badOut;
status = do_check_blif(cfn, badInp, badOut);
vector<uspair> corrected;
status = do_check_blif(cfn, badInp, badOut, corrected, false);
} else {
status = do_check_csv(cfn);
}
Expand Down
16 changes: 12 additions & 4 deletions planning/src/RS/rsCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ namespace pln {

bool do_check(const rsOpts& opts, bool blif_vs_csv);

bool do_check_blif(CStr cfn,
std::vector<std::string>& badInputs,
std::vector<std::string>& badOutputs);
bool do_check_blif(
CStr fileName,
std::vector<std::string>& badInputs,
std::vector<std::string>& badOutputs,
std::vector<uspair>& corrected,

bool cleanup = false // cleanup => checker may modify the blif

);


bool do_cleanup_blif(
CStr cfn,
CStr fileName,
std::vector<uspair>& corrected // (lnum, removed_net)
);


}

#endif
Expand Down
15 changes: 10 additions & 5 deletions planning/src/file_io/pln_blif_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2501,7 +2501,10 @@ string BLIF_file::writePinGraph(CStr fn0) const noexcept {
return {};
}

string BLIF_file::writeBlif(const string& toFn, bool cleanUp) noexcept {

string BLIF_file::writeBlif(const string& toFn, bool cleanUp,
vector<uspair>& corrected) noexcept {
corrected.clear();
if (toFn.empty())
return {};
if (!fsz_ || !sz_ || !buf_ || fnm_.empty())
Expand All @@ -2513,15 +2516,16 @@ string BLIF_file::writeBlif(const string& toFn, bool cleanUp) noexcept {

if (trace_ >= 4) {
lout() << "pln_blif_file: writing BLIF to " << fn2
<< "\n CWD= " << get_CWD() << endl;
<< "\n CWD= " << get_CWD()
<< "\n clean-up mode : " << int(cleanUp) << endl;
}

CStr cnm = fn2.c_str();
FILE* f = ::fopen(cnm, "w");
if (!f) {
if (trace_ >= 3) {
flush_out(true);
lprintf("ERROR writeBlif() could not open file for writing: %s\n", cnm);
lprintf2("[Error] writeBlif() could not open file for writing: %s\n", cnm);
flush_out(true);
}
return {};
Expand All @@ -2540,7 +2544,7 @@ string BLIF_file::writeBlif(const string& toFn, bool cleanUp) noexcept {
if (::ferror(f)) {
if (trace_ >= 3) {
flush_out(true);
lprintf("ERROR writeBlif() error during writing: %s\n", cnm);
lprintf2("ERROR writeBlif() error during writing: %s\n", cnm);
flush_out(true);
}
error = true;
Expand Down Expand Up @@ -2578,6 +2582,7 @@ string BLIF_file::writeBlif(const string& toFn, bool cleanUp) noexcept {
if (dNode.isDanglingTerm(i)) {
if (trace_ >= 6)
lprintf("\t (wrBlif) skipping dangling term %zu\n", i);
corrected.emplace_back(lineNum, dNode.realData_[i]);
skipCnt++;
continue;
}
Expand All @@ -2598,7 +2603,7 @@ string BLIF_file::writeBlif(const string& toFn, bool cleanUp) noexcept {
if (::ferror(f)) {
if (trace_ >= 3) {
flush_out(true);
lprintf("ERROR writeBlif() error during writing: %s\n", cnm);
lprintf2("ERROR writeBlif() error during writing: %s\n", cnm);
flush_out(true);
}
error = true;
Expand Down
3 changes: 2 additions & 1 deletion planning/src/file_io/pln_blif_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ struct BLIF_file : public fio::MMapReader

bool readBlif() noexcept;

string writeBlif(const string& toFn, bool cleanUp) noexcept;
string writeBlif(const string& toFn, bool cleanUp,
std::vector<uspair>& corrected) noexcept;

bool checkBlif(vector<string>& badInputs, vector<string>& badOutputs) noexcept;

Expand Down
5 changes: 3 additions & 2 deletions planning/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
static const char* _pln_VERSION_STR = "pln0357";
static const char* _pln_VERSION_STR = "pln0358";

#include "RS/rsEnv.h"
#include "util/pln_log.h"
Expand Down Expand Up @@ -87,7 +87,8 @@ static bool deal_cleanup(const rsOpts& opts) {
status = do_cleanup_blif(opts.input_, corrected);

if (tr >= 3) {
lprintf(" deal_cleanup status: %s\n", status ? "TRUE" : "FALSE");
flush_out(true);
lprintf(" deal_cleanup: status= %s\n", status ? "TRUE" : "FALSE");
if (corrected.empty()) {
if (status)
lprintf(" deal_cleanup: BLIF was not modified (NOP)\n");
Expand Down
3 changes: 2 additions & 1 deletion planning/src/pin_loc/read_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1118,11 +1118,12 @@ bool PinPlacer::BlifReader::read_blif(const string& blif_fn, bool& checked_ok) n
}

vector<string> badInputs, badOutputs;
vector<uspair> corrected;

if (not ::getenv("pinc_dont_check_blif")) {
lprintf("____ BEGIN pinc_check_blif: %s\n", cfn);
flush_out(true);
checked_ok = do_check_blif(cfn, badInputs, badOutputs);
checked_ok = do_check_blif(cfn, badInputs, badOutputs, corrected, false);
flush_out(true);
lprintf(" pinc_check_blif STATUS = %s\n\n",
checked_ok ? "PASS" : "FAIL");
Expand Down

0 comments on commit 22acc3c

Please sign in to comment.