From 987eed67c2b14ba1e02cf192f1eed6fa55fa5ffe Mon Sep 17 00:00:00 2001 From: Nishanth Date: Mon, 2 Feb 2026 11:20:36 +0530 Subject: [PATCH] mem_dump: add summary of inferred memory usage Signed-off-by: Nishanth --- flow/scripts/mem_dump.py | 76 ++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/flow/scripts/mem_dump.py b/flow/scripts/mem_dump.py index 36956bece8..aac74f9e56 100644 --- a/flow/scripts/mem_dump.py +++ b/flow/scripts/mem_dump.py @@ -4,8 +4,6 @@ def find_top_modules(data): - # There can be some cruft in the modules list so that - # we have multiple top level candidates. top_module = [] instantiations = set( [ @@ -14,7 +12,7 @@ def find_top_modules(data): for cell in minfo2["cells"].values() ] ) - for mname, minfo in data["modules"].items(): + for mname in data["modules"].keys(): if mname not in instantiations: top_module.append(mname) return top_module @@ -23,16 +21,6 @@ def find_top_modules(data): def find_cells_by_type_in_module( module_name, data, target_type, current_path, matching_cells ): - """ - Searches through hierarchy starting at module_name to find all instances of - the given module/type in the hierarchy. - - Returns list of cell paths, which are constructed as: - - .(.+). - - where the child_inst_name/child_module_name pairs are repeated for each level of the hierarchy. - """ for cell_name, cell in data["modules"][module_name]["cells"].items(): cell_path = ( f"{current_path}.{module_name}.{cell_name}" @@ -42,18 +30,15 @@ def find_cells_by_type_in_module( if cell["type"] == target_type: matching_cells.append(cell_path) elif cell["type"] in data["modules"]: - # Recursively search within the module matching_cells.extend( find_cells_by_type_in_module( cell["type"], data, target_type, cell_path, [] ) ) - return matching_cells def find_cells_by_type(top_modules, data, module_name, current_path=""): - # first find top module, the module without any submodules names = [] for top_module in top_modules: names.extend( @@ -69,13 +54,12 @@ def format_ram_table_from_json(data, max_bits=None): formatting = "{:>5} | {:>5} | {:>6} | {:<20} | {:<80}\n" table = formatting.format("Rows", "Width", "Bits", "Module", "Instances") table += "-" * len(table) + "\n" + max_ok = True entries = [] - # Collect the entries in a list for module_name, module_info in data["modules"].items(): - cells = module_info["cells"] - for cell in cells.values(): + for cell in module_info["cells"].values(): if not cell["type"].startswith("$mem"): continue parameters = cell["parameters"] @@ -84,18 +68,38 @@ def format_ram_table_from_json(data, max_bits=None): instances = find_cells_by_type(top_modules, data, module_name) instance_bits = size * width bits = instance_bits * len(instances) + entries.append((size, width, bits, module_name, ", ".join(instances))) + if max_bits is not None and instance_bits > max_bits: max_ok = False - # Sort the entries by descending bits entries.sort(key=lambda x: x[2], reverse=True) - # Format the sorted entries into the table for entry in entries: table += formatting.format(*entry) - return table, max_ok + # ---- Summary statistics ---- + total_bits = sum(e[2] for e in entries) + + largest_instance_bits = 0 + largest_instance_module = None + + for size, width, _, module, _ in entries: + instance_bits = size * width + if instance_bits > largest_instance_bits: + largest_instance_bits = instance_bits + largest_instance_module = module + + summary = { + "memory_count": len(entries), + "largest_instance_bits": largest_instance_bits, + "largest_instance_module": largest_instance_module, + "largest_total_bits": entries[0][2] if entries else 0, + "total_bits": total_bits, + } + + return table, max_ok, summary if __name__ == "__main__": @@ -119,9 +123,27 @@ def format_ram_table_from_json(data, max_bits=None): print(" " + "\n ".join(src_files)) print("Memories found in the design:") - formatted_table, max_ok = format_ram_table_from_json(json_data, args.max_bits) + formatted_table, max_ok, summary = format_ram_table_from_json( + json_data, args.max_bits + ) + print(formatted_table) - if not max_ok: - sys.exit( - f"Error: Synthesized memory size {args.max_bits} exceeds SYNTH_MEMORY_MAX_BITS" - ) + + print("Summary:") + print(f"- Total inferred memories: {summary['memory_count']}") + print( + f"- Largest single memory instance: " + f"{summary['largest_instance_bits']} bits " + f"(module {summary['largest_instance_module']})" + ) + print(f"- Total inferred memory bits (all instances): {summary['total_bits']}") + + if args.max_bits is not None: + status = "OK" if max_ok else "FAIL" + print(f"- SYNTH_MEMORY_MAX_BITS: {args.max_bits}") + print(f"- Status: {status}") + + if not max_ok: + sys.exit( + f"Error: Synthesized memory size {args.max_bits} exceeds SYNTH_MEMORY_MAX_BITS" + )