Skip to content

Commit 4510dbc

Browse files
authored
docs: add developer chapter on the repacker (#2916)
* docs: add developer chapter on the repacker Describe what repack does and why: the physical lb_rr_graph, how nets and sinks are derived from the VPR pb_route, the router, bus-based mux handling, wire LUT identification and the design decisions behind them. Also list suspected issues found while reading the code and debugging tips. * docs: run Sphinx through a configurable Python interpreter Call sphinx and sphinx_autobuild as modules so the build does not depend on the shebang of the sphinx-build scripts, which breaks when a virtual environment is moved. PYTHON can be overridden on the command line.
1 parent f456edb commit 4510dbc

3 files changed

Lines changed: 239 additions & 2 deletions

File tree

‎docs/Makefile‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
# You can set these variables from the command line.
55
SPHINXOPTS =
6-
SPHINXBUILD = sphinx-build
6+
7+
# Python interpreter used to run Sphinx. Running Sphinx as a module avoids the
8+
# shebang of the sphinx-build/sphinx-autobuild scripts, which breaks when a
9+
# virtual environment is moved. Override to select a specific environment, e.g.,
10+
# make PYTHON=/path/to/venv/bin/python html
11+
PYTHON ?= python3
12+
SPHINXBUILD = $(PYTHON) -m sphinx
13+
SPHINXAUTOBUILD = $(PYTHON) -m sphinx_autobuild
714
SOURCEDIR = source
815
BUILDDIR = build
916

@@ -17,7 +24,7 @@ help:
1724
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
1825

1926
livehtml:
20-
sphinx-autobuild -b html $(ALL_SPHINXOPTS) $(BUILDDIR)/html
27+
$(SPHINXAUTOBUILD) -b html $(ALL_SPHINXOPTS) $(BUILDDIR)/html
2128

2229
clean:
2330
rm -rf $(BUILDDIR)/*

‎docs/source/dev_manual/index.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
tcl_api
1616

17+
repacker
18+
1719

1820
.. toctree::
1921
:maxdepth: 2
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
.. _dev_manual_repacker:
2+
3+
Repacker
4+
========
5+
6+
This chapter explains how the ``repack`` command works internally and why it is designed the way it is.
7+
For the user-facing command options, see :ref:`openfpga_bitstream_commands`.
8+
For the constraint file format, see :ref:`file_formats_repack_design_constraints`.
9+
10+
The source code lives in ``openfpga/src/repack`` (the algorithm) and ``openfpga/src/utils/physical_pb_utils.*`` (the physical pb data base).
11+
The command wrapper is ``openfpga/src/base/openfpga_repack_template.h``.
12+
13+
.. note:: This chapter describes the behavior of the code in the repository.
14+
Where a statement is an interpretation of design intent rather than something enforced by the code, it is marked as such.
15+
16+
Why repack exists
17+
-----------------
18+
19+
VPR packs a netlist into *operating modes* of a complex block.
20+
Operating modes are what the architecture author wrote for the packer: they may expose several alternative implementations of the same hardware (e.g., a fracturable LUT written as ``n1_lut4`` and ``n2_lut3``) and they include modes that exist only to help the packer (e.g., the *wire* mode of a LUT).
21+
22+
The fabric that OpenFPGA builds has only one implementation of each block: the *physical mode*.
23+
The physical mode is marked in the OpenFPGA architecture file (``physical_mode`` annotations in :ref:`annotate_vpr_arch`).
24+
The bitstream must describe how the physical hardware is configured, so the packing result has to be translated from operating modes to the physical mode.
25+
26+
Repack does this translation.
27+
It takes the clustered netlist produced by VPR and produces, for every clustered block, a ``PhysicalPb``: the same clustering expressed on the physical pb_graph, with every net assigned to physical pins and every atom block assigned to a physical primitive.
28+
29+
Inputs and outputs
30+
------------------
31+
32+
Inputs:
33+
34+
- The VPR atom netlist, clustering context and the per-cluster ``pb_route`` (the intra-cluster routing result from VPR).
35+
- ``VprDeviceAnnotation``: operating-to-physical mappings (``physical_mode``, ``physical_pb_graph_node``, ``physical_pb_graph_pin``, circuit model of each pb_type).
36+
- ``VprClusteringAnnotation``: nets renamed by VPR during routing (see ``pb_pin_fixup``) and the physical equivalent site of each block.
37+
- ``VprBitstreamAnnotation``: per-pb_type bitstream and MIF sources.
38+
- Optional: repack design constraints (pin constraints, ignored global nets).
39+
40+
Outputs:
41+
42+
- One ``PhysicalPb`` per clustered block, stored in ``VprClusteringAnnotation`` (``add_physical_pb``).
43+
- Physical LUT truth tables, built by ``build_physical_lut_truth_tables`` after repack.
44+
- One physical lb_rr_graph per logical block type, stored in ``VprDeviceAnnotation``.
45+
46+
Pipeline
47+
--------
48+
49+
``repack_template`` (``openfpga_repack_template.h``) runs:
50+
51+
1. Parse options and the optional design-constraint XML.
52+
2. ``pack_physical_pbs`` (``repack.cpp``):
53+
54+
a. ``find_clusters_bus_mux_selections``: validate physical bus-mux names and record the input set VPR used on each bus mux of each cluster.
55+
b. ``build_physical_lb_rr_graphs``: build one routing graph per logical block type, physical modes only.
56+
c. ``repack_clusters``: for each clustered block, route it on the physical graph and save the result.
57+
d. ``identify_physical_pb_wire_lut_created_by_repack``: flag LUT outputs that carry a pass-through net but are not known as wire LUTs yet.
58+
59+
3. ``build_physical_lut_truth_tables``: compute the truth table of every physical LUT output, including wire LUTs.
60+
61+
``repack`` requires ``build_fabric`` to have run first (the command dependency is enforced by the shell).
62+
63+
Physical routing resource graph
64+
-------------------------------
65+
66+
``build_physical_lb_rr_graph.cpp`` builds an ``LbRRGraph`` per logical block type.
67+
It is derived from the VPR packer's lb_rr_graph, with one key difference: **only the physical mode of each pb_type is walked.**
68+
69+
Node types:
70+
71+
- Every input and clock pin of every pb_graph_node is an ``LB_INTERMEDIATE`` node with capacity 1.
72+
- Output pins of non-primitive nodes are ``LB_INTERMEDIATE``; output pins of primitive nodes are ``LB_SOURCE``.
73+
- Every input port of a primitive gets an ``LB_SINK``. If the port declares pin equivalence, the pins of that port share one sink whose capacity is the number of pins; otherwise each pin has its own sink.
74+
- Three special nodes exist per graph: an external source, an external sink, and an external-interconnect node. The external-interconnect node connects all root outputs back to root inputs at cost 1000, so the router avoids leaving the block unless necessary.
75+
76+
Edges:
77+
78+
- An edge exists only if the pb_graph edge's interconnect belongs to the physical mode of its parent pb_type.
79+
- Normal edges cost 1.
80+
81+
Consequences that developers should keep in mind:
82+
83+
- A primitive is a terminal: nets end at its inputs (sink) or start at its outputs (source). **The graph has no path through a primitive.** There is a TODO in the source about modelling the LUT wire mode, but it is not done.
84+
- Consequently, the router cannot create a pass-through net by itself. A net appears on a LUT output pin only if the VPR ``pb_route`` already placed it there (see :ref:`dev_manual_repacker_wire_luts`).
85+
- "Primitive" is not ``blif_model``-based for LUTs. ``is_primitive_pb_type`` (``utils/pb_type_utils.cpp``) treats a ``LUT_CLASS`` pb_type with the wire mode plus the regular LUT mode as a primitive. The inner VPR-created LUT children are *not* primitives for OpenFPGA.
86+
87+
Building the nets to route
88+
--------------------------
89+
90+
This is the most intricate part of repack (``add_lb_router_nets`` in ``repack.cpp``).
91+
The router needs, for every net, a list of source nodes and a list of sink nodes in the physical graph.
92+
The only source of truth for *which* sinks a net has is the VPR ``pb_route``, which is expressed on operating pins.
93+
Repack therefore walks the ``pb_route`` and translates each operating pin to its physical pin through ``VprDeviceAnnotation::physical_pb_graph_pin``.
94+
95+
There are two families of nets.
96+
97+
Nets entering through a root input pin
98+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99+
100+
The source is the root input pin node.
101+
Complications that the code handles:
102+
103+
- **Net renaming.** VPR may move a net to another pin of an equivalent port during routing. ``VprClusteringAnnotation`` records the actual net per pin, and ``pb_pin_mapped_nets`` is built from it. The ``pb_route`` can therefore be out of date for root pins. For ``PortEquivalence::FULL`` ports, traces are matched by port; for ``NONE`` ports, by exact pin. ``INSTANCE`` equivalence is not supported (asserted).
104+
- **Pin constraints.** A ``pin_constraint`` can force a net onto a pin or declare it ``OPEN``. The behavior is a four-case table (packing mapped/unmapped x constraint mapped/unmapped) documented in the source at the "Bypass unmapped pins" comment. In short: a constrained net is routed from the constrained pin but its *sinks* are found from the packing result of the net.
105+
- **Ignored global nets.** Global nets may be excluded from given pins (``ignore_net`` or ``--ignore_global_nets_on_pins``). Their sinks are cached per net and appended to the net routed from the permitted pin, so that one routed net covers all sinks. A trace that is the only one for a net is never ignored.
106+
- **Direct paths.** If the whole path from a root pin to the sinks is made of direct interconnect, ``rec_direct_search_sink_pb_graph_pins`` finds the sinks without walking the ``pb_route``.
107+
108+
Nets driven by a primitive output
109+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
110+
111+
The source is the physical equivalent (``LB_SOURCE``) of the operating primitive's output pin.
112+
Sinks are found by walking the ``pb_route`` until a primitive input or a root output is reached.
113+
Primitive outputs that drive nothing inside the cluster are skipped with a warning, because an empty sink list indicates an inconsistent ``pb_route``.
114+
115+
Sink-to-atom-pin matching
116+
^^^^^^^^^^^^^^^^^^^^^^^^^
117+
118+
``add_lb_router_net_to_route`` (``lb_router_utils.cpp``) pairs every sink node with an atom pin so that the router can report failures in netlist terms. A sink that has no atom pin inside the cluster (for example a net leaving through a root output) uses the first available atom sink pin of the net as a stand-in.
119+
120+
The router
121+
----------
122+
123+
``LbRouter`` (``lb_router.cpp``) is derived from the VPR packer's intra-cluster router: a negotiated-congestion (PathFinder-style) router.
124+
125+
- The outer loop runs up to ``max_iterations``; ``pres_con_fac_`` grows by ``pres_fac_mult`` each iteration.
126+
- For each net, each source and each sink: seed the priority queue from the existing route tree, expand with Dijkstra until the sink is reached, then commit the tree.
127+
- Node cost is ``(intrinsic + edge intrinsic + hist_fac * history)``, multiplied by a congestion factor when over capacity and by a fanout factor that favours high-fanout nodes for high-fanout nets.
128+
- ``set_physical_pb_modes`` pins every node to its physical mode, so ``expand_node`` never explores a non-physical mode. The fallback that retries with all modes (``expand_all_modes``) is commented out for repack on purpose: the graph has no non-physical edges. ``expand_node_all_modes`` is therefore not exercised by repack.
129+
- A mode conflict at commit time (two edges selecting different modes of the same pb_graph_node) fails the route.
130+
- If any net is impossible, the router prints the source pin and sink pins and repack returns ``CMD_EXEC_FATAL_ERROR`` for the whole command. There is no partial result.
131+
132+
Bus-based muxes
133+
^^^^^^^^^^^^^^^
134+
135+
A bus-based mux (``<mux bus="true">``) selects one input *set* for the whole bus.
136+
In the physical mode the bits of such a mux share one select, so the router must not use different sets for different bits.
137+
Repack handles this in three steps:
138+
139+
1. Before routing any cluster, check that every bus-mux name is unique within the physical modes of a logical block (``rec_check_physical_bus_mux_names``). Names are the key that links operating and physical muxes.
140+
2. From the ``pb_route`` driver of each pin, find which ``driver_set`` VPR used on each mux name (``find_vpr_bus_mux_selections``). If VPR used two sets under one name, report an error (the suggested remedy is VPR ``--flat_routing on``).
141+
3. Give the selection to the router (``set_bus_mux_selections``). ``expand_edges`` skips edges of a bus mux whose ``driver_set`` is not the selected one. The selection is stored in the ``PhysicalPb`` so that bitstream generation can use it.
142+
143+
Design decision: bus muxes are *pinned to VPR's choice* rather than left free to the router. Letting the router choose could place bits of one bus on different sets, which the single physical select cannot express.
144+
145+
Saving results
146+
--------------
147+
148+
``repack_cluster`` builds the ``PhysicalPb`` in two stages:
149+
150+
1. ``rec_update_physical_pb_from_operating_pb`` walks the *operating* pb tree (mapped modes and used children only). For every primitive it copies: the atom block, mode bits, MIF data, fixed bitstream and mode-select bitstream from EBLIF params/attributes, and the nets on the primitive's pins (``pb_route``). Unused LUT children that only carry a routed net (VPR wire mode) are marked as wire LUT outputs.
151+
2. ``save_lb_router_results_to_physical_pb`` copies the router's result: every node of every net tree gets its net written onto the corresponding physical pin.
152+
153+
If the physical equivalent site differs from the logical site, ``Logical2PhysicalPbMap`` provides a 1:1 map of pb_types, pb_graph_nodes and pins. It fails if the two sites are not structurally identical apart from the top-level name.
154+
155+
.. _dev_manual_repacker_wire_luts:
156+
157+
Wire LUTs
158+
---------
159+
160+
A wire LUT passes a net from a LUT input to a LUT output without logic. Two kinds are tracked:
161+
162+
- **Created by VPR's packer.** The LUT is in its wire mode in the operating pb. It is flagged while walking the operating pb (``mark_physical_pb_wired_lut_outputs``).
163+
- **Detected after repack.** ``identify_one_physical_pb_wire_lut_created_by_repack`` flags a LUT output that (1) is driven by LUT mux logic, (2) carries a net, (3) is not already flagged, (4) has a net driver that is not an atom block of this LUT, and (5) whose net also appears on a LUT input.
164+
165+
The truth-table stage (``build_physical_truth_table.cpp``) requires every output that carries a net to be either a real logic LUT (use the driver atom block's truth table) or a flagged wire LUT (build a pass-through table). That is why the detection pass is mandatory: an unflagged pass-through would hit an assertion in that stage.
166+
167+
.. note:: Experiment, not a guarantee. Running ``repack`` on eight random 4-LUT/FF designs with the ``k4_frac_N4_tileable_40nm`` architecture created no wire LUTs in the detection pass; all wire LUTs came from VPR. Because the lb_rr_graph has no path through a LUT, the router cannot create a pass-through by itself, so the detection pass currently looks like a safety net. A pass-through that VPR created but the first pass did not reach (for example under an unnamed intermediate pb, where ``rec_update_physical_pb_from_operating_pb`` skips non-primitive children without a name) would be reported here as "created by repacker". We have not found a design that triggers this.
168+
169+
Design decisions
170+
----------------
171+
172+
Operating modes need not be a subset of the physical mode
173+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
174+
175+
Repack only requires that pb_types can be matched between the operating and physical modes; it does *not* require that every operating interconnect exists in the physical mode. Interconnect is rerouted on the physical graph.
176+
177+
The one exception is bus-based multiplexers. The user manual (:ref:`openfpga_bitstream_commands`) requires the operating modes to keep the same bus-based muxes as the physical mode, with the same names and input-set order, and forbids simplifying one into a ``<direct>``. Names are the link between the two modes, and repack follows VPR's input-set choice on each (see "Bus-based muxes" above).
178+
179+
Rationale:
180+
181+
- The operating and physical hierarchies often differ (for example ``lut3inter/ble3/lut3`` versus ``fabric/frac_logic/frac_lut4``), so "subset of interconnect" is not even well defined without a pin map between different trees.
182+
- Equivalent pins (LUT inputs, cluster input ports) can be swapped by VPR after routing; something has to apply such swaps consistently.
183+
- Architects can write operating modes for packing quality and still have a simple physical mode.
184+
185+
Costs of this decision, which developers should be aware of:
186+
187+
- Repack can fail on a clustering that VPR found legal (``Reroute failed``).
188+
- VPR's timing and power analysis assumed VPR's intra-cluster paths; rerouted paths may differ. This is not currently measured or reported.
189+
- Much of ``repack.cpp`` exists to reconstruct VPR's intent for the router: net remapping, constraints, ignored global nets, and the bus-mux selection (which pins part of the routing back to VPR's choice).
190+
191+
Possible directions (not implemented; for discussion): report how many nets were rerouted differently from VPR; try a direct translation of the ``pb_route`` first and use the router only for clusters where an operating edge has no physical image; or add an architecture-time check that lists operating interconnects without a physical image.
192+
193+
Physical-only graph, no mode search
194+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
195+
196+
Edges outside the physical mode are never added, and the router is told every node's mode up front. This keeps the search small and guarantees that a successful route is physically realizable without a post-check. The cost is that anything that needs a non-physical edge (such as a path through a LUT) cannot be represented.
197+
198+
Net identity from ``pb_route``, not from the netlist
199+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
200+
201+
Sinks come from the VPR ``pb_route`` instead of the atom netlist so that repack reproduces the same *sink set* VPR packed, including sinks of nets that leave and re-enter a cluster. The price is the reconciliation code described above, since ``pb_route`` can be stale for root pins after VPR's pin swaps.
202+
203+
Fail the whole command on the first unroutable cluster
204+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
205+
206+
``repack_clusters`` returns at the first failing block. Because a partial ``PhysicalPb`` set would produce an incorrect bitstream, no partial result is kept. Validation that does not depend on routing (bus-mux names and selections) runs for all blocks first so that user errors are reported together.
207+
208+
Known issues
209+
------------
210+
211+
The following were found by reading the code; none was reproduced with a test. Treat them as leads and confirm before fixing.
212+
213+
- ``repack.cpp``, ``add_lb_router_nets``: when ``pb_route_indices`` is empty but ignored global-net sinks exist, ``pb_route_index`` is never assigned and is then used to look up ``packing_source_pb_pin``.
214+
- ``repack.cpp``: the ``VTR_ASSERT_SAFE`` in the valid-constrained-net branch asserts that the net is *invalid*.
215+
- ``repack.cpp``: when a constrained net name does not exist, the warning says the constraint is dropped, but the pin's packed net is also not routed.
216+
- ``repack.cpp``: the verbose "Source node" message for root-input nets prints the operating pin on both sides.
217+
- ``lb_router.cpp``: ``expand_node_all_modes`` skips edges when the node has no illegal modes, which looks inverted. It is not used by repack.
218+
- ``physical_pb_utils.cpp``, ``rec_update_physical_pb_from_operating_pb``: in the ``.attr`` branches of the fixed-bitstream and mode-select-bitstream copy, the comparison is ``==`` with ``continue``, the opposite of the ``.param`` branches. Attributes would be applied from every name except the requested one.
219+
- ``build_physical_lb_rr_graph.cpp`` and ``rec_update_physical_pb_from_operating_pb`` call ``exit()`` on errors. This is a problem for callers that embed OpenFPGA (for example the Python binding), since a bad input terminates the host process.
220+
- ``alloc_and_load_pb_graph_pin_lookup_from_index`` is rebuilt for every cluster; it could be built once per logical block type.
221+
222+
Debugging repack
223+
----------------
224+
225+
- Use ``repack --verbose`` to print every net's source and sink pins, the constraint decisions, the bus-mux selections, and the wire LUT outputs that are flagged.
226+
- A routing failure prints the net name, source pin and sink pins. Check that the sink pins exist in the physical mode and that the physical mode contains a path for them.
227+
- Wire-LUT assertions in the truth-table stage (``is_wired_lut``) mean a net reached a LUT output without being flagged. Compare the LUT's input nets and output net in the verbose log.
228+
- When a bus-mux error mentions two input sets, run VPR with ``--flat_routing on`` first.

0 commit comments

Comments
 (0)