rm split-bb-files.py

This commit is contained in:
joey0320
2023-04-29 18:21:48 -07:00
parent 30ec9806e0
commit 3f80507ce4
5 changed files with 15 additions and 103 deletions

View File

@@ -1,82 +0,0 @@
#!/usr/bin/env python3
import json
import argparse
from collections import defaultdict
# Schema of *.f emitted by circt
"""
<gen-src-dir>/<long-name>/gen-collateral/SimUART.cc
<gen-src-dir>/<long-name>/gen-collateral/AsyncQueueSource.sv
<gen-src-dir>/<long-name>/gen-collateral/AsyncQueueSink.sv
<gen-src-dir>/<long-name>/gen-collateral/AsyncQueueSource_1.sv
<gen-src-dir>/<long-name>/gen-collateral/AsyncQueueSink_1.sv
<gen-src-dir>/<long-name>/gen-collateral/AsyncQueueSource_2.sv
<gen-src-dir>/<long-name>/gen-collateral/AsyncQueueSink_2.sv
<gen-src-dir>/<long-name>/gen-collateral/AsyncResetSynchronizerShiftReg_w4_d3_i0.sv
"""
def bfs_collect_submodules(tree):
output = set()
q = [(tree['instance_name'], tree['module_name'], tree['instances'])]
while len(q) != 0:
front = q[0]
q.pop(0)
(inst, mod, child) = front
output.add(mod)
for c in child:
q.append((c['instance_name'], c['module_name'], c['instances']))
return output
def write_lines_to_file(lines, file_path):
with open(file_path, "w") as fp:
for line in lines:
fp.write("%s\n" % line)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Create *.model.bb.f and *.top.bb.f blackbox filelists')
parser.add_argument('--in-bb-f', type=str, required=True, help='All blackbox files filelist (includes both MODEL/TOP files)')
parser.add_argument('--in-top-hrchy-json', type=str, required=True, help='List containing hierarchy of top modules (top-module-hierarchy.json)')
parser.add_argument('--in-anno-json', type=str, required=True, help='Anno. file with blackbox annotations')
parser.add_argument('--out-top-bb-f', type=str, required=True, help='List of blackbox files for TOP')
parser.add_argument('--out-model-bb-f', type=str, required=True, help='List of blackbox files for MODEL')
args = parser.parse_args()
# module_path -> list of bb paths (not fully resolved paths)
mod_bb_dict = defaultdict(list)
with open(args.in_anno_json, "r") as f:
anno_data = json.load(f)
for anno in anno_data:
if 'BlackBoxInlineAnno' in anno['class']:
mod_bb_dict[anno['target']].append(anno['name'])
if 'BlackBoxPathAnno' in anno['class']:
mod_bb_dict[anno['target']].append(anno['path'])
with open(args.in_top_hrchy_json) as ihj:
ihj_data = json.load(ihj)
top_inner_modules = bfs_collect_submodules(ihj_data)
with open(args.in_bb_f) as ibf:
lines = ibf.read().splitlines()
tbfs = set()
for mod_path, bb_files in mod_bb_dict.items():
leaf_mod = mod_path.split('.')[-1]
# if matched, add the fully resolved path to the top bb filelist
if leaf_mod in top_inner_modules:
for line in lines:
for bb_file in bb_files:
if bb_file in line:
tbfs.add(line)
# now tbfs should be complete (need to remove tbf files from original bb file for model bb)
mbfs = set()
for line in lines:
if not line in tbfs:
mbfs.add(line)
write_lines_to_file(tbfs, args.out_top_bb_f)
write_lines_to_file(mbfs, args.out_model_bb_f)

View File

@@ -54,14 +54,14 @@ def generate_copy(c, sfx):
bash(f"sed -i s/\"module {cur_name}\"/\"module {new_name}\"/ {new_file}")
return new_file
def dfs_update_modules(tree, common_fnames, visited, ext_dict):
def dfs_update_modules(tree, common_fnames, visited):
# List of direct submodules to update
childs_to_update = list()
for child in tree['instances']:
# We don't have to change stuff that are under the dut
if (child['module_name'] == args.dut):
continue
if dfs_update_modules(child, common_fnames, visited, ext_dict):
if dfs_update_modules(child, common_fnames, visited):
childs_to_update.append(child['module_name'])
if (child['module_name']) in common_fnames:
child['module_name'] = child['module_name'] + "_" + MODEL_SFX
@@ -87,7 +87,7 @@ def bfs_update(tree, common_fnames, ext_dict, filelist):
(inst, mod, child, parent) = front
try:
cur_file = mod + "." + ext_dict[mod]
cur_file = mod + "." + ext_dict[mod][0]
except:
cur_file = mod + ".sv"
@@ -99,7 +99,7 @@ def bfs_update(tree, common_fnames, ext_dict, filelist):
new_file = generate_copy(cur_file, MODEL_SFX)
filelist.append((mod, new_file))
if parent is not None and ((parent, mod) not in updated_submodule):
parent_file = os.path.join(args.gcpath, parent + "." + ext_dict[parent])
parent_file = os.path.join(args.gcpath, parent + "." + ext_dict[parent][0])
bash(f"sed -i s/\"{mod} \"/\"{mod}_{MODEL_SFX} \"/ {parent_file}")
updated_submodule.add((parent, mod))
mod_updated = True
@@ -165,6 +165,10 @@ def write_filelist_model(modules, out_file, ext_dict):
else:
df.write(f"{fname}\n")
if len(ext_dict[m]) > 1:
assert(len(ext_dict[m]) == 2)
df.write(f"{args.target_dir}/{m}.{ext_dict[m][1]}\n")
def get_file_ext(all_filelist):
ext_dict = dict()
with open(all_filelist) as fl:
@@ -174,7 +178,9 @@ def get_file_ext(all_filelist):
ext = fname_strip[-1]
fname_strip.pop()
module = ".".join(fname_strip)
ext_dict[module] = ext
if module not in ext_dict.keys():
ext_dict[module] = list()
ext_dict[module].append(ext)
return ext_dict
def main():
@@ -197,7 +203,7 @@ def main():
visited = set()
filelist = list()
bfs_update(imhj_data, common_modules, ext_dict, filelist)
dfs_update_modules(imhj_data, common_modules, visited, ext_dict)
dfs_update_modules(imhj_data, common_modules, visited)
json.dump(imhj_data, out_file, indent=2)
write_filelist_model(set(filelist), args.out_model_filelist, ext_dict)