Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nanocoh committed Jan 7, 2025
1 parent 4d0dd2e commit 6f173a6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
43 changes: 19 additions & 24 deletions najaeda_examples/load_design_python_primitives/najaeda_dle.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# SPDX-FileCopyrightText: 2024 The Naja authors
# <https://github.com/najaeda/naja/blob/main/AUTHORS>
#
# SPDX-License-Identifier: Apache-2.0

# snippet-start: load_design

from os import path
import sys
from collections import deque

from najaeda import netlist
from najaeda import snl
Expand All @@ -17,30 +11,31 @@
# Load design
netlist.load_primitives('xilinx')
instances = set()
benchmarks = path.join('..','benchmarks')
benchmarks = path.join('..', 'benchmarks')
top = netlist.load_verilog([path.join(benchmarks, 'verilog', 'arm_core_netlist.v')])

# Trace back from design outputs
visited = set()
for termToTrace in top.get_flat_output_terms():
queue = []
queue.append(termToTrace)
output_terms = top.get_flat_output_terms()
for termToTrace in output_terms:
queue = deque([termToTrace])
while queue:
term = queue.pop(0)
term = queue.popleft()
if term in visited:
continue
visited.add(term)
for driver in term.get_equipotential().get_leaf_drivers():
instances.add(driver.get_instance())
for reader in driver.get_instance().get_flat_input_terms():
queue.append(reader)
count = 0
toDelete = []
for leaf in top.get_leaf_children():
if not leaf in instances:
toDelete.append(leaf)
count += 1
for leaf in toDelete:
equipotential = term.get_equipotential()
leaf_drivers = equipotential.get_leaf_drivers()
for driver in leaf_drivers:
instance = driver.get_instance()
instances.add(instance)
input_terms = instance.get_flat_input_terms()
queue.extend(input_terms)

leaf_children = top.get_leaf_children()
to_delete = [leaf for leaf in leaf_children if leaf not in instances]
for leaf in to_delete:
leaf.delete()

top.dump_verilog("./", "result.v")
print("deleted ",count)
print("deleted", len(to_delete))
36 changes: 22 additions & 14 deletions src/najaeda/najaeda/netlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
def consistent_hash(obj):
def default_serializer(o):
if isinstance(o, (str, int, float, bool, type(None))):
return o
return o
elif isinstance(o, (list, tuple)):
return [default_serializer(i) for i in o]
elif isinstance(o, dict):
return {default_serializer(k): default_serializer(v) for k, v in o.items()}
return {default_serializer(k): default_serializer(v) for k, v in o.items()}
else:
return str(o)

Expand Down Expand Up @@ -335,10 +335,11 @@ def __ge__(self, other) -> bool:

def __hash__(self):
termIDs = []
if isinstance(get_snl_term_for_ids(self.pathIDs, self.termIDs), snl.SNLBusTerm):
termIDs = [get_snl_term_for_ids(self.pathIDs, self.termIDs).getID(), -1]
snlterm = get_snl_term_for_ids(self.pathIDs, self.termIDs)
if isinstance(snlterm, snl.SNLBusTerm):
termIDs = [snlterm.getID(), -1]
else:
termIDs = [get_snl_term_for_ids(self.pathIDs, self.termIDs).getID(), get_snl_term_for_ids(self.pathIDs, self.termIDs).getBit()]
termIDs = [snlterm.getID(), snlterm.getBit()]
return consistent_hash((self.pathIDs, termIDs))

def __str__(self):
Expand All @@ -355,7 +356,7 @@ def __repr__(self) -> str:
def __make_unique(self):
path = get_snl_path_from_id_list(self.pathIDs)
if path.size() > 1:
path = path.getHeadPath()
path = path.getHeadPath()
snl.SNLUniquifier(path)

def is_bus(self) -> bool:
Expand Down Expand Up @@ -396,11 +397,12 @@ def get_name(self) -> str:

def get_direction(self) -> snl.SNLTerm.Direction:
"""Return the direction of the term."""
if get_snl_term_for_ids(self.pathIDs, self.termIDs).getDirection() == snl.SNLTerm.Direction.Input:
snlterm = get_snl_term_for_ids(self.pathIDs, self.termIDs)
if snlterm.getDirection() == snl.SNLTerm.Direction.Input:
return Term.INPUT
elif get_snl_term_for_ids(self.pathIDs, self.termIDs).getDirection() == snl.SNLTerm.Direction.Output:
elif snlterm.getDirection() == snl.SNLTerm.Direction.Output:
return Term.OUTPUT
elif get_snl_term_for_ids(self.pathIDs, self.termIDs).getDirection() == snl.SNLTerm.Direction.InOut:
elif snlterm.getDirection() == snl.SNLTerm.Direction.InOut:
return Term.INOUT

def __get_snl_bitnet(self, bit) -> Net:
Expand Down Expand Up @@ -479,11 +481,13 @@ def get_equipotential(self) -> Equipotential:

def is_input(self) -> bool:
"""Return True if the term is an input."""
return get_snl_term_for_ids(self.pathIDs, self.termIDs).getDirection() == snl.SNLTerm.Direction.Input
snlterm = get_snl_term_for_ids(self.pathIDs, self.termIDs)
return snlterm.getDirection() == snl.SNLTerm.Direction.Input

def is_output(self) -> bool:
"""Return True if the term is an output."""
return get_snl_term_for_ids(self.pathIDs, self.termIDs).getDirection() == snl.SNLTerm.Direction.Output
snlterm = get_snl_term_for_ids(self.pathIDs, self.termIDs)
return snlterm.getDirection() == snl.SNLTerm.Direction.Output

def get_bits(self):
path = get_snl_path_from_id_list(self.pathIDs)
Expand Down Expand Up @@ -511,14 +515,18 @@ def connect(self, net: Net):
if self.get_width() != net.get_width():
raise ValueError("Width mismatch")
if self.get_instance().is_top():
for bterm, bnet in zip(get_snl_term_for_ids(self.pathIDs, self.termIDs).getBits(), net.net.getBits()):
for bterm, bnet in zip(get_snl_term_for_ids(self.pathIDs,
self.termIDs).getBits(),
net.net.getBits()):
logging.debug(f"Connecting {bterm} to {bnet}")
bterm.setNet(bnet)
else:
self.__make_unique()
path = get_snl_path_from_id_list(self.pathIDs)
inst = path.getTailInstance()
for bterm, bnet in zip(get_snl_term_for_ids(self.pathIDs, self.termIDs).getBits(), net.net.getBits()):
for bterm, bnet in zip(get_snl_term_for_ids(self.pathIDs,
self.termIDs).getBits(),
net.net.getBits()):
iterm = inst.getInstTerm(bterm)
iterm.setNet(bnet)

Expand Down Expand Up @@ -767,7 +775,7 @@ def delete_instance(self, name: str):
path = snl.SNLPath(init_path, self.__get_snl_model().getInstance(name))
snl.SNLUniquifier(path)
if init_path.size() > 0:
# Delete the last instance in uniq_path
# Delete the last instance in uniq_path
self.__get_snl_model().getInstance(name).destroy()

def delete_instance_by_id(self, id: str):
Expand Down

0 comments on commit 6f173a6

Please sign in to comment.