Skip to content

Commit 3deeedd

Browse files
authored
Initial implementation of GateGraph, an alternative PyRTL logic representation (#470)
* Initial implementation of `GateGraph`, an alternative PyRTL logic representation: `GateGraph` attempts to address several issues with PyRTL's standard `WireVector` and `LogicNet` representation: 1. A `GateGraph` can be directly traversed, without the help of side data structures like the `wire_src_dict` and `wire_sink_dict` returned by `net_connections`. 2. `GateGraph` builds a graph where each node is a `Gate`, and the edges are all references to other `Gates`. By using only one node type, rather than two (`WireVector`, `LogicNet`), it becomes easier to work with the nodes in a `GateGraph`, because every node is the same type, with the same interface. 3. `GateGraph` decouples the user interface and the internal representation. Users directly instantiate `WireVectors` as they build circuits. A user that is only building circuits should never interact with a `GateGraph` or a `Gate`. Users should only interact with `GateGraph` when they need reflection, which typically happens when writing an analysis or optimization pass. Another advantage of this decoupling is that `Gate` does not need to support `WireVector`'s quality-of-life features for users, like inferring bitwidth from assignment.
1 parent 370aa4b commit 3deeedd

File tree

6 files changed

+1461
-3
lines changed

6 files changed

+1461
-3
lines changed

docs/blocks.rst

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
Block and Logic Nets
22
=====================
33

4-
:class:`.Block` and :class:`.LogicNet` are lower level PyRTL abstractions. Most
5-
users won't need to understand them, unless they are implementing
6-
:ref:`analysis_and_optimization` passes or modifying PyRTL itself.
4+
:class:`.Block` and :class:`.LogicNet` are lower level PyRTL abstractions for
5+
representing a hardware design. Most users won't need to understand them,
6+
unless they are implementing :ref:`analysis_and_optimization` passes or
7+
modifying PyRTL itself.
8+
9+
:ref:`gate_graphs` are an alternative representation that makes it easier to
10+
write analysis passes.
711

812
Blocks
913
------
@@ -34,3 +38,19 @@ LogicNets
3438
.. autoclass:: pyrtl.LogicNet
3539
:members:
3640
:undoc-members:
41+
42+
.. _gate_graphs:
43+
44+
GateGraphs
45+
----------
46+
47+
.. automodule:: pyrtl.gate_graph
48+
49+
.. autoclass:: pyrtl.Gate
50+
:members:
51+
:special-members: __str__
52+
53+
.. autoclass:: pyrtl.GateGraph
54+
:members:
55+
:special-members: __init__, __str__
56+

pyrtl/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# convenience classes for building hardware
2020
from .wire import WireVector, Input, Output, Const, Register
2121

22+
from .gate_graph import GateGraph, Gate
23+
2224
# helper functions
2325
from .helperfuncs import (
2426
input_list,
@@ -160,6 +162,9 @@
160162
"Output",
161163
"Const",
162164
"Register",
165+
# gate_graph
166+
"GateGraph",
167+
"Gate",
163168
# helperfuncs
164169
"input_list",
165170
"output_list",

pyrtl/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,10 @@ def net_connections(
581581
This information helps when building a graph representation for the ``Block``.
582582
See :func:`net_graph` for an example.
583583
584+
.. note::
585+
586+
Consider using :ref:`gate_graphs` instead.
587+
584588
:param include_virtual_nodes: If ``True``, external `sources` (such as an
585589
:class:`Inputs<Input>` and :class:`Consts<Const>`) will be represented as
586590
wires that set themselves, and external `sinks` (such as

0 commit comments

Comments
 (0)