Repository navigation
Conversation
No need to keep it separate anymore. Signed-off-by: Matt Liberty <mliberty@precisioninno.com>
|
@oharboe @jeffng-or FYI |
There was a problem hiding this comment.
Code Review
This pull request introduces FakeRAM2.0, a fake memory compiler that generates PDK-compatible memory models (LEF, Liberty, and Verilog) from configuration files or custom spreadsheets. The feedback highlights several critical issues, including potential TypeError and KeyError exceptions in the process and port organizer utilities, syntax errors in the generated Verilog/SystemVerilog outputs, and risky overridable method calls in the Memory constructor. Additionally, the reviewer suggested various style and encapsulation improvements, such as using cls for class methods, avoiding implicit boolean-to-integer conversions, adhering to public getters in TimingData, and using array-based execution in bash scripts to prevent word splitting.
| contacted_poly_pitch_um = self.contacted_poly_pitch_nm / 1000 | ||
| fin_pitch_um = self.fin_pitch_nm / 1000 |
There was a problem hiding this comment.
If bitcell_width_um and bitcell_height_um are not defined, and either contacted_poly_pitch_nm or fin_pitch_nm is missing from the configuration, this code will raise a TypeError when attempting to divide None by 1000. It is safer to check if these values are defined and raise a descriptive ValueError instead.
else:
if self.contacted_poly_pitch_nm is None or self.fin_pitch_nm is None:
raise ValueError("contacted_poly_pitch_nm and fin_pitch_nm must be defined if bitcell size is not specified.")
contacted_poly_pitch_um = self.contacted_poly_pitch_nm / 1000
fin_pitch_um = self.fin_pitch_nm / 1000| last_char = port_data["name"][-1] | ||
| self._rw_groups[last_char][port_data["type"]] = port_data |
There was a problem hiding this comment.
If len(address_busses) > 1, this loop attempts to group various port types by their suffix (last_char). However, if a port name does not end with one of the address bus suffixes (e.g., shared or unsuffixed ports like toggle_power), last_char will not be present in self._rw_groups, causing a KeyError. Consider checking if last_char exists in self._rw_groups first, and falling back to misc ports if it does not.
| last_char = port_data["name"][-1] | |
| self._rw_groups[last_char][port_data["type"]] = port_data | |
| last_char = port_data["name"][-1] | |
| if last_char in self._rw_groups: | |
| self._rw_groups[last_char][port_data["type"]] = port_data | |
| else: | |
| if "msb" in port_data: | |
| self._misc_busses.append(port_data) | |
| else: | |
| self._misc_ports.append(port_data) |
| if index != 0: | ||
| out_fh.write(",\n") | ||
| if rw_port_group.get_write_enable_name(): | ||
| out_fh.write(f" {rw_port_group.get_write_enable_name()},\n") | ||
| if rw_port_group.get_address_bus_name(): | ||
| out_fh.write(f" {rw_port_group.get_address_bus_name()},\n") | ||
| if rw_port_group.get_data_input_bus_name(): | ||
| out_fh.write(f" {rw_port_group.get_data_input_bus_name()},\n") | ||
| if rw_port_group.get_data_output_bus_name(): | ||
| out_fh.write(f" {rw_port_group.get_data_output_bus_name()},\n") | ||
| if rw_port_group.get_clock_name(): | ||
| out_fh.write(f" {rw_port_group.get_clock_name()}") | ||
| for port_name in rw_port_group.get_related_pins(): | ||
| out_fh.write(f" {port_name}") | ||
| for bus_name in rw_port_group.get_related_busses(): | ||
| out_fh.write(f" {bus_name}") |
There was a problem hiding this comment.
When related_pins or related_busses are present, they are written directly after the clock pin without any separator (comma or newline), resulting in syntactically invalid Verilog (e.g., clk_a related_pin). Refactoring this method to collect all active port declarations in a list and joining them with ",\n" is much cleaner and avoids syntax errors.
| if index != 0: | |
| out_fh.write(",\n") | |
| if rw_port_group.get_write_enable_name(): | |
| out_fh.write(f" {rw_port_group.get_write_enable_name()},\n") | |
| if rw_port_group.get_address_bus_name(): | |
| out_fh.write(f" {rw_port_group.get_address_bus_name()},\n") | |
| if rw_port_group.get_data_input_bus_name(): | |
| out_fh.write(f" {rw_port_group.get_data_input_bus_name()},\n") | |
| if rw_port_group.get_data_output_bus_name(): | |
| out_fh.write(f" {rw_port_group.get_data_output_bus_name()},\n") | |
| if rw_port_group.get_clock_name(): | |
| out_fh.write(f" {rw_port_group.get_clock_name()}") | |
| for port_name in rw_port_group.get_related_pins(): | |
| out_fh.write(f" {port_name}") | |
| for bus_name in rw_port_group.get_related_busses(): | |
| out_fh.write(f" {bus_name}") | |
| ports = [] | |
| if rw_port_group.get_write_enable_name(): | |
| ports.append(f" {rw_port_group.get_write_enable_name()}") | |
| if rw_port_group.get_address_bus_name(): | |
| ports.append(f" {rw_port_group.get_address_bus_name()}") | |
| if rw_port_group.get_data_input_bus_name(): | |
| ports.append(f" {rw_port_group.get_data_input_bus_name()}") | |
| if rw_port_group.get_data_output_bus_name(): | |
| ports.append(f" {rw_port_group.get_data_output_bus_name()}") | |
| if rw_port_group.get_clock_name(): | |
| ports.append(f" {rw_port_group.get_clock_name()}") | |
| for port_name in rw_port_group.get_related_pins(): | |
| ports.append(f" {port_name}") | |
| for bus_name in rw_port_group.get_related_busses(): | |
| ports.append(f" {bus_name}") | |
| if ports: | |
| if index != 0: | |
| out_fh.write(",\n") | |
| out_fh.write(",\n".join(ports)) |
| def export_bb_port_decl_set(self, rw_port_group, out_fh, index): | ||
| """Writes the SystemVerilog port declarations""" | ||
|
|
||
| mem = self.get_memory() | ||
| addr_bus_msb = mem.get_addr_bus_msb() | ||
| data_bus_msb = mem.get_data_bus_msb() | ||
| if index != 0: | ||
| out_fh.write(",\n") | ||
| if rw_port_group.get_write_enable_name(): | ||
| out_fh.write(f" input {rw_port_group.get_write_enable_name()},\n") | ||
| if rw_port_group.get_address_bus_name(): | ||
| out_fh.write( | ||
| f" input [{addr_bus_msb}:0] {rw_port_group.get_address_bus_name()},\n" | ||
| ) | ||
| if rw_port_group.get_data_input_bus_name(): | ||
| out_fh.write( | ||
| f" input [{data_bus_msb}:0] {rw_port_group.get_data_input_bus_name()},\n" | ||
| ) | ||
| if rw_port_group.get_data_output_bus_name(): | ||
| out_fh.write( | ||
| f" output reg [{data_bus_msb}:0] {rw_port_group.get_data_output_bus_name()},\n" | ||
| ) | ||
| for pin_name in rw_port_group.get_related_pins(): | ||
| out_fh.write( | ||
| f" input {pin_name},\n" | ||
| ) | ||
| for bus_name, bus_data in rw_port_group.get_related_busses().items(): | ||
| out_fh.write( | ||
| f" input [{bus_data['msb']}:{bus_data['lsb']}] {bus_name},\n" | ||
| ) | ||
| if rw_port_group.get_clock_name(): | ||
| out_fh.write(f" input {rw_port_group.get_clock_name()}") |
There was a problem hiding this comment.
If clock_name is None, the last item written by export_bb_port_decl_set will have a trailing comma. When followed by misc ports in export_blackbox (which write leading commas), this results in a syntax error due to double commas ,,. Refactoring export_bb_port_decl_set to return a list of strings allows export_blackbox to cleanly join all declarations with ",\n".
def export_bb_port_decl_set(self, rw_port_group):
"""Returns a list of SystemVerilog port declarations"""
mem = self.get_memory()
addr_bus_msb = mem.get_addr_bus_msb()
data_bus_msb = mem.get_data_bus_msb()
ports = []
if rw_port_group.get_write_enable_name():
ports.append(f" input {rw_port_group.get_write_enable_name()}")
if rw_port_group.get_address_bus_name():
ports.append(f" input [{addr_bus_msb}:0] {rw_port_group.get_address_bus_name()}")
if rw_port_group.get_data_input_bus_name():
ports.append(f" input [{data_bus_msb}:0] {rw_port_group.get_data_input_bus_name()}")
if rw_port_group.get_data_output_bus_name():
ports.append(f" output reg [{data_bus_msb}:0] {rw_port_group.get_data_output_bus_name()}")
for pin_name in rw_port_group.get_related_pins():
ports.append(f" input {pin_name}")
for bus_name, bus_data in rw_port_group.get_related_busses().items():
ports.append(f" input [{bus_data['msb']}:{bus_data['lsb']}] {bus_name}")
if rw_port_group.get_clock_name():
ports.append(f" input {rw_port_group.get_clock_name()}")
return ports| """Writes the blackbox content to the output stream""" | ||
|
|
||
| self.export_bb_header(out_fh) | ||
| for index, rw_port_group in enumerate(self.get_memory().get_rw_port_groups()): | ||
| self.export_bb_port_decl_set(rw_port_group, out_fh, index) | ||
| for bus_name, bus_data in self.get_memory().get_misc_busses().items(): | ||
| out_fh.write( | ||
| f",\n input [{bus_data['msb']}:{bus_data['lsb']}] {bus_name}" | ||
| ) | ||
| for pin in sorted(self.get_memory().get_misc_ports()): | ||
| out_fh.write(f",\n input {pin}") | ||
| out_fh.write("\n") | ||
| self.export_bb_footer(out_fh) |
There was a problem hiding this comment.
Refactor export_blackbox to collect all port declarations in a list and join them with ",\n" to avoid potential double comma syntax errors when clock_name is None.
| """Writes the blackbox content to the output stream""" | |
| self.export_bb_header(out_fh) | |
| for index, rw_port_group in enumerate(self.get_memory().get_rw_port_groups()): | |
| self.export_bb_port_decl_set(rw_port_group, out_fh, index) | |
| for bus_name, bus_data in self.get_memory().get_misc_busses().items(): | |
| out_fh.write( | |
| f",\n input [{bus_data['msb']}:{bus_data['lsb']}] {bus_name}" | |
| ) | |
| for pin in sorted(self.get_memory().get_misc_ports()): | |
| out_fh.write(f",\n input {pin}") | |
| out_fh.write("\n") | |
| self.export_bb_footer(out_fh) | |
| def export_blackbox(self, out_fh): | |
| """Writes the blackbox content to the output stream""" | |
| self.export_bb_header(out_fh) | |
| ports = [] | |
| for rw_port_group in self.get_memory().get_rw_port_groups(): | |
| ports.extend(self.export_bb_port_decl_set(rw_port_group)) | |
| for bus_name, bus_data in self.get_memory().get_misc_busses().items(): | |
| ports.append(f" input [{bus_data['msb']}:{bus_data['lsb']}] {bus_name}") | |
| for pin in sorted(self.get_memory().get_misc_ports()): | |
| ports.append(f" input {pin}") | |
| out_fh.write(",\n".join(ports)) | |
| out_fh.write("\n") | |
| self.export_bb_footer(out_fh) |
| self.physical.snap_to_grid( | ||
| self.process.snap_width_nm, self.process.snap_height_nm | ||
| ) | ||
| num_pins = self.get_num_pins() |
There was a problem hiding this comment.
Calling the overridable method self.get_num_pins() inside the base class Memory.__init__ constructor is risky. If a subclass overrides get_num_pins() and accesses any subclass-specific attributes initialized in its own __init__ (which runs after super().__init__()), it will raise an AttributeError. It is safer to perform physical dimension calculations in a separate initialization method called after the object is fully constructed.
| else: | ||
| fid.write(f" LAYER {layer_name} ;\n") | ||
| for rect in layer_data["rects"]: | ||
| fid.write( |
There was a problem hiding this comment.
| timing_data = self.get_memory().get_timing_data() | ||
| max_load = timing_data.max_load | ||
| slew_indices = timing_data.slew_indices | ||
| load_indices = timing_data.load_indices | ||
| min_slew = timing_data.min_slew | ||
| max_slew = timing_data.max_slew |
There was a problem hiding this comment.
Directly accessing protected/private-like attributes of TimingData (e.g., timing_data.max_load, timing_data.slew_indices, etc.) violates encapsulation. Use the public getter methods defined in TimingData instead.
| timing_data = self.get_memory().get_timing_data() | |
| max_load = timing_data.max_load | |
| slew_indices = timing_data.slew_indices | |
| load_indices = timing_data.load_indices | |
| min_slew = timing_data.min_slew | |
| max_slew = timing_data.max_slew | |
| max_load = timing_data.get_max_load() | |
| slew_indices = timing_data.get_slew_indices_str() | |
| load_indices = timing_data.get_load_indices_str() | |
| min_slew = timing_data.get_min_slew() | |
| max_slew = timing_data.get_max_slew() | |
| tcq = timing_data.get_access_time() |
| if [[ -v COVERAGE_RUN ]]; then | ||
| rm -rf .coverage* | ||
| cmd_preamble="coverage run --parallel-mode" | ||
| else | ||
| cmd_preamble="" | ||
| fi | ||
|
|
||
| SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
|
|
||
| for test_file in $SCRIPT_DIR/*_test.py; do | ||
| $cmd_preamble $test_file | ||
| done |
There was a problem hiding this comment.
Using unquoted variables like $cmd_preamble and $test_file can lead to word splitting and globbing issues. It is much safer and more robust to use a bash array for cmd_preamble and execute it with "${cmd_preamble[@]}".
| if [[ -v COVERAGE_RUN ]]; then | |
| rm -rf .coverage* | |
| cmd_preamble="coverage run --parallel-mode" | |
| else | |
| cmd_preamble="" | |
| fi | |
| SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | |
| for test_file in $SCRIPT_DIR/*_test.py; do | |
| $cmd_preamble $test_file | |
| done | |
| if [[ -v COVERAGE_RUN ]]; then | |
| rm -rf .coverage* | |
| cmd_preamble=(coverage run --parallel-mode) | |
| else | |
| cmd_preamble=(python3) | |
| fi | |
| SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | |
| for test_file in "$SCRIPT_DIR"/*_test.py; do | |
| "${cmd_preamble[@]}" "$test_file" | |
| done |
| """Ensures that the results directory exists""" | ||
|
|
||
| p = str(Path(output_dir).expanduser().resolve(strict=False)) | ||
| results_dir = os.sep.join([p, memory_name]) |
There was a problem hiding this comment.
Signed-off-by: Matt Liberty <mliberty@precisioninno.com>
|
I fixed the black python formatting but will leave the general code comments for future work as this is just a code move not a refactor. |
No need to keep it separate anymore.