From 77b6dc44b80e5d50db92f5263ece908acaeb18ff Mon Sep 17 00:00:00 2001 From: Sergey Dolgov Date: Wed, 23 Oct 2024 00:41:13 -0700 Subject: [PATCH] checker: finished --cleanup_blif --- planning/src/RS/rsCheck.cpp | 88 +++++++++++++++++++++++--- planning/src/RS/rsCheck.h | 4 +- planning/src/RS/rsOpts.cpp | 3 +- planning/src/file_io/pln_blif_file.cpp | 2 +- planning/src/main.cpp | 2 +- planning/src/pin_loc/read_ports.cpp | 4 +- 6 files changed, 88 insertions(+), 15 deletions(-) diff --git a/planning/src/RS/rsCheck.cpp b/planning/src/RS/rsCheck.cpp index 24c9298b..3a693279 100644 --- a/planning/src/RS/rsCheck.cpp +++ b/planning/src/RS/rsCheck.cpp @@ -10,10 +10,12 @@ using std::string; using std::endl; // in 'cleanup' mode checker may modify the blif +// if checker wrote blif, 'outFn' is the file name bool do_check_blif(CStr cfn, vector& badInputs, vector& badOutputs, vector& corrected, + string& outFn, bool cleanup) { assert(cfn); uint16_t tr = ltrace(); @@ -21,6 +23,7 @@ bool do_check_blif(CStr cfn, badInputs.clear(); badOutputs.clear(); corrected.clear(); + outFn.clear(); flush_out(false); BLIF_file bfile(string{cfn}); @@ -127,13 +130,12 @@ bool do_check_blif(CStr cfn, std::filesystem::path base_path = full_path.filename(); std::string base = base_path.string(); //lputs9(); - 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. + // write to a temporary and rename later. outFn = str::concat(full_path.lexically_normal().string(), "+BLIF_CLEANER.tmp_", std::to_string(get_PID())); } else { @@ -144,9 +146,9 @@ bool do_check_blif(CStr cfn, if (wr_ok.empty()) { lprintf("---!! FAILED writeBlif to '%s'\n", outFn.c_str()); - if (cleanup) { + if (cleanup) lprintf("[PLANNER BLIF-CLEANER] : FAILED\n"); - } + outFn.clear(); } else { lprintf("+++++ WRITTEN '%s'\n", wr_ok.c_str()); if (cleanup) { @@ -180,23 +182,88 @@ bool do_check_blif(CStr cfn, // 'corrected' : (lnum, removed_net) bool do_cleanup_blif(CStr cfn, vector& corrected) { + using namespace fio; + using namespace std; assert(cfn); corrected.clear(); + uint16_t tr = ltrace(); vector badInp, badOut; - bool status = do_check_blif(cfn, badInp, badOut, corrected, true); + string outFn; + + bool status = do_check_blif(cfn, badInp, badOut, corrected, outFn, 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()); + if (tr >= 3) { + 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()); + if (tr < 4 and i > 40 and cor_sz > 50) { + lputs(" ..."); + break; + } + } + lprintf("-- removed dangling nets (%zu).\n", cor_sz); } - lprintf("-- removed dangling nets (%zu).\n", cor_sz); flush_out(true); + + bool outFn_exists = Fio::nonEmptyFileExists(outFn); + bool cfn_exists = Fio::nonEmptyFileExists(cfn); + //assert(cfn_exists); + if (outFn_exists and cfn_exists) { + + bool error1 = false, error2 = false; + + // -- 1. add prefix 'orig_' to the original BLIF + { + string newName = str::concat("orig_", cfn); + filesystem::path newPath{newName}; + filesystem::path oldPath{cfn}; + error_code ec; + filesystem::rename(oldPath, newPath, ec); + if (ec) { + error1 = true; + lout() << endl + << "BLIF-CLEANER: [Error] renaming original BLIF to " + << newPath << endl + << " ERROR: " << ec.message() << '\n' << endl; + } + else if (tr >= 3) { + lprintf("[PLANNER BLIF-CLEANER] : original BLIF saved as '%s'\n", + newName.c_str()); + } + } + + // -- 2. rename 'outFn' to 'cfn' + if (not error1) { + filesystem::path oldPath{outFn}; + filesystem::path newPath{cfn}; + error_code ec; + filesystem::rename(oldPath, newPath, ec); + if (ec) { + error2 = true; + lout() << endl + << "BLIF-CLEANER: [Error] renaming temporary BLIF to " + << newPath << endl + << " ERROR: " << ec.message() << '\n' << endl; + } + else if (tr >= 3) { + string newName = newPath.lexically_normal().string(); + lprintf("[PLANNER BLIF-CLEANER] : corrected BLIF written : %s\n", + newName.c_str()); + } + } + + if (error1 or error2) { + status = false; + lputs("[PLANNER BLIF-CLEANER] : FAILED due to filesystem errors\n"); + } + } } return status; @@ -270,7 +337,8 @@ bool do_check(const rsOpts& opts, bool blif_vs_csv) { if (blif_vs_csv) { vector badInp, badOut; vector corrected; - status = do_check_blif(cfn, badInp, badOut, corrected, false); + string outFn; + status = do_check_blif(cfn, badInp, badOut, corrected, outFn, false); } else { status = do_check_csv(cfn); } diff --git a/planning/src/RS/rsCheck.h b/planning/src/RS/rsCheck.h index 764fd631..36a5cfe0 100644 --- a/planning/src/RS/rsCheck.h +++ b/planning/src/RS/rsCheck.h @@ -15,7 +15,9 @@ bool do_check_blif( std::vector& badOutputs, std::vector& corrected, - bool cleanup = false // cleanup => checker may modify the blif + std::string& outFn, // if checker wrote blif, outFn is the file name + + bool cleanup = false // cleanup => checker may modify the blif ); diff --git a/planning/src/RS/rsOpts.cpp b/planning/src/RS/rsOpts.cpp index f67c246e..7cfe4dd1 100644 --- a/planning/src/RS/rsOpts.cpp +++ b/planning/src/RS/rsOpts.cpp @@ -28,7 +28,8 @@ static CStr _check_[] = { "CH", "CHECK", "ch", "CC", "cc", "che", "chec", "check", "check_blif", nullptr }; static CStr _clean_[] = { "CLEAN", "CLEANUP", "clean", "cleanup", "clean_up", - "cleanup_blif", "cleanupblif", "clean_up_blif", nullptr }; + "cleanup_blif", "cleanupblif", "clean_up_blif", + "cle", "clea", nullptr }; static CStr _csv_[] = {"CSV", "cs", "csv", "Csv", nullptr}; diff --git a/planning/src/file_io/pln_blif_file.cpp b/planning/src/file_io/pln_blif_file.cpp index c7733db3..7b8df24b 100644 --- a/planning/src/file_io/pln_blif_file.cpp +++ b/planning/src/file_io/pln_blif_file.cpp @@ -2540,7 +2540,7 @@ string BLIF_file::writeBlif(const string& toFn, bool cleanUp, } bool error = false; - ::fprintf(f, "### written by PLN %s\n\n", pln_get_version()); + ::fprintf(f, "### written by PLANNER %s\n\n", pln_get_version()); if (::ferror(f)) { if (trace_ >= 3) { flush_out(true); diff --git a/planning/src/main.cpp b/planning/src/main.cpp index 3c832839..17accb88 100644 --- a/planning/src/main.cpp +++ b/planning/src/main.cpp @@ -1,4 +1,4 @@ -static const char* _pln_VERSION_STR = "pln0358"; +static const char* _pln_VERSION_STR = "pln0359"; #include "RS/rsEnv.h" #include "util/pln_log.h" diff --git a/planning/src/pin_loc/read_ports.cpp b/planning/src/pin_loc/read_ports.cpp index 22fb4ea6..b57aa0ff 100644 --- a/planning/src/pin_loc/read_ports.cpp +++ b/planning/src/pin_loc/read_ports.cpp @@ -1119,11 +1119,13 @@ bool PinPlacer::BlifReader::read_blif(const string& blif_fn, bool& checked_ok) n vector badInputs, badOutputs; vector corrected; + string outFn; 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, corrected, false); + checked_ok = do_check_blif(cfn, badInputs, badOutputs, + corrected, outFn, false); flush_out(true); lprintf(" pinc_check_blif STATUS = %s\n\n", checked_ok ? "PASS" : "FAIL");