Skip to content

Commit 214a824

Browse files
authored
Merge pull request #903 from os-fpga/checker_cleanup_functionality
checker: --cleanup_blif functionality
2 parents 1aed94c + 22acc3c commit 214a824

File tree

6 files changed

+85
-29
lines changed

6 files changed

+85
-29
lines changed

planning/src/RS/rsCheck.cpp

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ using std::vector;
99
using std::string;
1010
using std::endl;
1111

12+
// in 'cleanup' mode checker may modify the blif
1213
bool do_check_blif(CStr cfn,
1314
vector<string>& badInputs,
14-
vector<string>& badOutputs) {
15+
vector<string>& badOutputs,
16+
vector<uspair>& corrected,
17+
bool cleanup) {
1518
assert(cfn);
1619
uint16_t tr = ltrace();
1720
auto& ls = lout();
1821
badInputs.clear();
1922
badOutputs.clear();
23+
corrected.clear();
24+
flush_out(false);
2025

2126
BLIF_file bfile(string{cfn});
2227

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

2631
bool exi = false;
2732
exi = bfile.fileExists();
28-
if (tr >= 7)
29-
ls << int(exi) << endl;
3033
if (not exi) {
34+
err_puts(); flush_out(true);
3135
lprintf2("[Error] file '%s' does not exist\n", cfn);
36+
flush_out(true); err_puts();
3237
return false;
3338
}
3439

3540
exi = bfile.fileAccessible();
36-
if (tr >= 7)
37-
ls << int(exi) << endl;
3841
if (not exi) {
42+
err_puts(); flush_out(true);
3943
lprintf2("[Error] file '%s' is not accessible\n", cfn);
44+
flush_out(true); err_puts();
4045
return false;
4146
}
4247

4348
bool rd_ok = bfile.readBlif();
44-
if (tr >= 7)
45-
ls << int(rd_ok) << endl;
4649
if (not rd_ok) {
50+
err_puts(); flush_out(true);
4751
lprintf2("[Error] failed reading file '%s'\n", cfn);
52+
flush_out(true); err_puts();
4853
return false;
4954
}
5055

@@ -122,14 +127,32 @@ bool do_check_blif(CStr cfn,
122127
std::filesystem::path base_path = full_path.filename();
123128
std::string base = base_path.string();
124129
//lputs9();
125-
string outFn = str::concat("PLN_W", std::to_string(numWarn), "_", base);
126-
127-
string wr_ok = bfile.writeBlif(outFn, numWarn);
128-
129-
if (wr_ok.empty())
130+
string outFn;
131+
if (cleanup) {
132+
flush_out(true);
133+
lprintf("[PLANNER BLIF-CLEANER] : replacing file '%s' ...\n", cfn);
134+
flush_out(true);
135+
// cannot write to the currently mem-mapped file,
136+
// write to a temproray and rename later.
137+
outFn = str::concat(full_path.lexically_normal().string(),
138+
"+BLIF_CLEANER.tmp_", std::to_string(get_PID()));
139+
} else {
140+
outFn = str::concat("PLN_WARN", std::to_string(numWarn), "_", base);
141+
}
142+
143+
string wr_ok = bfile.writeBlif(outFn, bool(numWarn), corrected);
144+
145+
if (wr_ok.empty()) {
130146
lprintf("---!! FAILED writeBlif to '%s'\n", outFn.c_str());
131-
else
147+
if (cleanup) {
148+
lprintf("[PLANNER BLIF-CLEANER] : FAILED\n");
149+
}
150+
} else {
132151
lprintf("+++++ WRITTEN '%s'\n", wr_ok.c_str());
152+
if (cleanup) {
153+
lprintf("[PLANNER BLIF-CLEANER] : replaced file '%s'\n", cfn);
154+
}
155+
}
133156
}
134157
}
135158
lprintf("===== passed: %s\n", chk_ok ? "YES" : "NO");
@@ -156,11 +179,27 @@ bool do_check_blif(CStr cfn,
156179
}
157180

158181
// 'corrected' : (lnum, removed_net)
159-
bool do_cleanup_blif(CStr cfn, std::vector<uspair>& corrected) {
182+
bool do_cleanup_blif(CStr cfn, vector<uspair>& corrected) {
160183
assert(cfn);
161184
corrected.clear();
162185

163-
return false;
186+
vector<string> badInp, badOut;
187+
bool status = do_check_blif(cfn, badInp, badOut, corrected, true);
188+
189+
size_t cor_sz = corrected.size();
190+
if (status and cor_sz) {
191+
flush_out(true);
192+
lprintf("[PLANNER BLIF-CLEANER] : corrected netlist '%s'\n", cfn);
193+
lprintf("-- removed dangling nets (%zu):\n", cor_sz);
194+
for (size_t i = 0; i < cor_sz; i++) {
195+
const uspair& cor = corrected[i];
196+
lprintf(" line %u net %s\n", cor.first, cor.second.c_str());
197+
}
198+
lprintf("-- removed dangling nets (%zu).\n", cor_sz);
199+
flush_out(true);
200+
}
201+
202+
return status;
164203
}
165204

166205
static bool do_check_csv(CStr cfn) {
@@ -230,7 +269,8 @@ bool do_check(const rsOpts& opts, bool blif_vs_csv) {
230269
bool status;
231270
if (blif_vs_csv) {
232271
vector<string> badInp, badOut;
233-
status = do_check_blif(cfn, badInp, badOut);
272+
vector<uspair> corrected;
273+
status = do_check_blif(cfn, badInp, badOut, corrected, false);
234274
} else {
235275
status = do_check_csv(cfn);
236276
}

planning/src/RS/rsCheck.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ namespace pln {
99

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

12-
bool do_check_blif(CStr cfn,
13-
std::vector<std::string>& badInputs,
14-
std::vector<std::string>& badOutputs);
12+
bool do_check_blif(
13+
CStr fileName,
14+
std::vector<std::string>& badInputs,
15+
std::vector<std::string>& badOutputs,
16+
std::vector<uspair>& corrected,
17+
18+
bool cleanup = false // cleanup => checker may modify the blif
19+
20+
);
21+
1522

1623
bool do_cleanup_blif(
17-
CStr cfn,
24+
CStr fileName,
1825
std::vector<uspair>& corrected // (lnum, removed_net)
1926
);
2027

28+
2129
}
2230

2331
#endif

planning/src/file_io/pln_blif_file.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,7 +2501,10 @@ string BLIF_file::writePinGraph(CStr fn0) const noexcept {
25012501
return {};
25022502
}
25032503

2504-
string BLIF_file::writeBlif(const string& toFn, bool cleanUp) noexcept {
2504+
2505+
string BLIF_file::writeBlif(const string& toFn, bool cleanUp,
2506+
vector<uspair>& corrected) noexcept {
2507+
corrected.clear();
25052508
if (toFn.empty())
25062509
return {};
25072510
if (!fsz_ || !sz_ || !buf_ || fnm_.empty())
@@ -2513,15 +2516,16 @@ string BLIF_file::writeBlif(const string& toFn, bool cleanUp) noexcept {
25132516

25142517
if (trace_ >= 4) {
25152518
lout() << "pln_blif_file: writing BLIF to " << fn2
2516-
<< "\n CWD= " << get_CWD() << endl;
2519+
<< "\n CWD= " << get_CWD()
2520+
<< "\n clean-up mode : " << int(cleanUp) << endl;
25172521
}
25182522

25192523
CStr cnm = fn2.c_str();
25202524
FILE* f = ::fopen(cnm, "w");
25212525
if (!f) {
25222526
if (trace_ >= 3) {
25232527
flush_out(true);
2524-
lprintf("ERROR writeBlif() could not open file for writing: %s\n", cnm);
2528+
lprintf2("[Error] writeBlif() could not open file for writing: %s\n", cnm);
25252529
flush_out(true);
25262530
}
25272531
return {};
@@ -2540,7 +2544,7 @@ string BLIF_file::writeBlif(const string& toFn, bool cleanUp) noexcept {
25402544
if (::ferror(f)) {
25412545
if (trace_ >= 3) {
25422546
flush_out(true);
2543-
lprintf("ERROR writeBlif() error during writing: %s\n", cnm);
2547+
lprintf2("ERROR writeBlif() error during writing: %s\n", cnm);
25442548
flush_out(true);
25452549
}
25462550
error = true;
@@ -2578,6 +2582,7 @@ string BLIF_file::writeBlif(const string& toFn, bool cleanUp) noexcept {
25782582
if (dNode.isDanglingTerm(i)) {
25792583
if (trace_ >= 6)
25802584
lprintf("\t (wrBlif) skipping dangling term %zu\n", i);
2585+
corrected.emplace_back(lineNum, dNode.realData_[i]);
25812586
skipCnt++;
25822587
continue;
25832588
}
@@ -2598,7 +2603,7 @@ string BLIF_file::writeBlif(const string& toFn, bool cleanUp) noexcept {
25982603
if (::ferror(f)) {
25992604
if (trace_ >= 3) {
26002605
flush_out(true);
2601-
lprintf("ERROR writeBlif() error during writing: %s\n", cnm);
2606+
lprintf2("ERROR writeBlif() error during writing: %s\n", cnm);
26022607
flush_out(true);
26032608
}
26042609
error = true;

planning/src/file_io/pln_blif_file.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ struct BLIF_file : public fio::MMapReader
290290

291291
bool readBlif() noexcept;
292292

293-
string writeBlif(const string& toFn, bool cleanUp) noexcept;
293+
string writeBlif(const string& toFn, bool cleanUp,
294+
std::vector<uspair>& corrected) noexcept;
294295

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

planning/src/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
static const char* _pln_VERSION_STR = "pln0357";
1+
static const char* _pln_VERSION_STR = "pln0358";
22

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

8989
if (tr >= 3) {
90-
lprintf(" deal_cleanup status: %s\n", status ? "TRUE" : "FALSE");
90+
flush_out(true);
91+
lprintf(" deal_cleanup: status= %s\n", status ? "TRUE" : "FALSE");
9192
if (corrected.empty()) {
9293
if (status)
9394
lprintf(" deal_cleanup: BLIF was not modified (NOP)\n");

planning/src/pin_loc/read_ports.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,11 +1118,12 @@ bool PinPlacer::BlifReader::read_blif(const string& blif_fn, bool& checked_ok) n
11181118
}
11191119

11201120
vector<string> badInputs, badOutputs;
1121+
vector<uspair> corrected;
11211122

11221123
if (not ::getenv("pinc_dont_check_blif")) {
11231124
lprintf("____ BEGIN pinc_check_blif: %s\n", cfn);
11241125
flush_out(true);
1125-
checked_ok = do_check_blif(cfn, badInputs, badOutputs);
1126+
checked_ok = do_check_blif(cfn, badInputs, badOutputs, corrected, false);
11261127
flush_out(true);
11271128
lprintf(" pinc_check_blif STATUS = %s\n\n",
11281129
checked_ok ? "PASS" : "FAIL");

0 commit comments

Comments
 (0)