5 #include "pybind11/operators.h"
6 #include "pybind11/pybind11.h"
7 #include "pybind11/stl.h"
8 #include "pybind11/stl_bind.h"
10 namespace py = pybind11;
18 #ifdef PYBIND11_MODULE
19 PYBIND11_MODULE(netlist_preprocessing, m)
21 m.doc() =
"hal NetlistPreprocessingPlugin python bindings";
25 py::module m(
"netlist_preprocessing",
"hal NetlistPreprocessingPlugin python bindings");
28 py::class_<NetlistPreprocessingPlugin, RawPtrWrapper<NetlistPreprocessingPlugin>,
BasePluginInterface> py_netlist_preprocessing(m,
"NetlistPreprocessingPlugin");
30 The name of the plugin.
36 Get the name of the plugin.
38 :returns: Plugin name.
43 The version of the plugin.
49 Get the version of the plugin.
51 :returns: Plugin version.
56 The description of the plugin.
62 Get the description of the plugin.
64 :returns: The description of the plugin.
69 A set of plugin names that this plugin depends on.
75 Get a set of plugin names that this plugin depends on.
77 :returns: A set of plugin names that this plugin depends on.
82 "remove_unused_lut_inputs",
83 [](
Netlist* nl) -> std::optional<u32> {
91 log_error(
"python_context",
"{}", res.get_error().get());
97 Removes all LUT fan-in endpoints that do not correspond to a variable within the Boolean function that determines the output of a gate.
99 :param hal_py.Netlist nl: The netlist to operate on.
100 :returns: The number of removed LUT endpoints on success, ``None`` otherwise.
101 :rtype: int or ``None``
106 [](
Netlist* nl) -> std::optional<u32> {
114 log_error(
"python_context",
"{}", res.get_error().get());
120 Removes buffer gates from the netlist and connect their fan-in to their fan-out nets.
121 Considers all combinational gates and takes their inputs into account.
122 For example, a 2-input AND gate with one input being connected to constant ``1`` will also be removed.
124 :param hal_py.Netlist nl: The netlist to operate on.
125 :returns: The number of removed buffers on success, ``None`` otherwise.
126 :rtype: int or ``None``
130 "remove_redundant_gates",
131 [](
Netlist* nl,
const std::function<
bool(
const Gate*)>& filter =
nullptr) -> std::optional<u32> {
139 log_error(
"python_context",
"{}", res.get_error().get());
144 py::arg(
"filter") =
nullptr,
146 Removes redundant gates from the netlist, i.e., gates that are functionally equivalent and are connected to the same input nets.
148 :param hal_py.Netlist nl: The netlist to operate on.
149 :param lambda filter: Optional filter to fine-tune which gates are being replaced. Default to a ``None``.
150 :returns: The number of removed gates on success, ``None`` otherwise.
151 :rtype: int or ``None``
155 "remove_redundant_loops",
156 [](
Netlist* nl) -> std::optional<u32> {
164 log_error(
"python_context",
"{}", res.get_error().get());
170 Removes redundant sequential feedback loops.
171 Sometimes flip-flops and some of their combinational fan-in form a feedback loop where the flip-flop input depends on its own output.
172 For optimization, some synthesizers create multiple equivalent instances of these feedback loops.
173 To simplify structural analysis, this function removes the redundant flip-flop gate of the loop from the netlist.
174 Other preprocessing functions can then take care of the remaining combination gates of the loop.
176 :param hal_py.Netlist nl: The netlist to operate on.
177 :returns: The number of removed gates on success, ``None`` otherwise.
178 :rtype: int or ``None``
182 "remove_redundant_logic_trees",
183 [](
Netlist* nl) -> std::optional<u32> {
191 log_error(
"python_context",
"{}", res.get_error().get());
197 Removes redundant logic trees made up of combinational gates.
198 If two trees compute the exact same function even if implemented with different gates we will disconnect one of the trees and afterwards clean up all dangling gates and nets.
200 :param hal_py.Netlist nl: The netlist to operate on.
201 :returns: The number of removed gates on success, ``None`` otherwise.
202 :rtype: int or ``None``
206 "remove_unconnected_gates",
207 [](
Netlist* nl) -> std::optional<u32> {
215 log_error(
"python_context",
"{}", res.get_error().get());
221 Removes gates for which all fan-out nets do not have a destination and are not global output nets.
223 :param hal_py.Netlist nl: The netlist to operate on.
224 :returns: The number of removed gates on success, ``None`` otherwise.
225 :rtype: int or ``None``
229 "remove_unconnected_nets",
230 [](
Netlist* nl) -> std::optional<u32> {
238 log_error(
"python_context",
"{}", res.get_error().get());
244 Removes nets who have neither a source, nor a destination.
246 :param hal_py.Netlist nl: The netlist to operate on.
247 :returns: The number of removed nets on success, ``None`` otherwise.
248 :rtype: int or ``None``
252 "remove_unconnected_looped",
253 [](
Netlist* nl) -> std::optional<u32> {
261 log_error(
"python_context",
"{}", res.get_error().get());
267 Calls remove_unconnected_gates / remove_unconnected_nets until there are no further changes.
269 :param hal_py.Netlist nl: The netlist to operate on.
270 :returns: The number of removed nets and gates on success, ``None`` otherwise.
271 :rtype: int or ``None``
275 "manual_mux_optimizations",
284 log_error(
"python_context",
"{}", res.get_error().get());
289 py::arg(
"mux_inv_gl"),
291 Apply manually implemented optimizations to the netlist centered around muxes.
292 Currently implemented optimizations include:
294 - removing inverters in case there are inverter gates in front of and behind every data input and output of the mux
295 - optimizing and therefore unifying possible inverters preceding the select signals by resynthesizing
297 :param hal_py.Netlist nl: The netlist to operate on.
298 :param hal_py.GateLibrary mux_inv_gl: A gate library only containing mux and inverter gates used for resynthesis.
299 :returns: The difference in the total number of gates caused by these optimizations.
300 :rtype: int or ``None``
304 "propagate_constants",
305 [](
Netlist* nl) -> std::optional<u32> {
313 log_error(
"python_context",
"{}", res.get_error().get());
319 Builds for all gate output nets the Boolean function and substitutes all variables connected to vcc/gnd nets with the respective boolean value.
320 If the function simplifies to a static boolean constant cut the connection to the nets destinations and directly connect it to vcc/gnd.
322 :param hal_py.Netlist nl: The netlist to operate on.
323 :returns: The number of rerouted nets on success, ``None`` otherwise.
324 :rtype: int or ``None``
328 "remove_consecutive_inverters",
329 [](
Netlist* nl) -> std::optional<u32> {
337 log_error(
"python_context",
"{}", res.get_error().get());
343 Removes two consecutive inverters and reconnects the input of the first inverter to the output of the second one.
344 If the first inverter has additional successors, only the second inverter is deleted.
346 :param hal_py.Netlist nl: The netlist to operate on.
347 :returns: The number of removed inverter gates on success, ``None`` otherwise.
348 :rtype: int or ``None``
352 "simplify_lut_inits",
353 [](
Netlist* nl) -> std::optional<u32> {
361 log_error(
"python_context",
"{}", res.get_error().get());
367 Replaces pins connected to GND/VCC with constants and simplifies the boolean function of a LUT by recomputing the INIT string.
369 :param hal_py.Netlist nl: The netlist to operate on.
370 :returns: The number of simplified INIT strings on success, ``None`` otherwise.
371 :rtype: int or ``None``
375 "reconstruct_indexed_ff_identifiers",
376 [](
Netlist* nl) -> std::optional<u32> {
384 log_error(
"python_context",
"{}", res.get_error().get());
390 Tries to reconstruct a name and index for each flip flop that was part of a multi-bit wire in the verilog code.
391 This is NOT a general netlist reverse engineering algorithm and ONLY works on synthesized netlists with names annotated by the synthesizer.
392 This function mainly focuses netlists synthesized with yosys since yosys names the output wires of the flip flops but not the gate it self.
393 We try to reconstruct name and index for each flip flop based on the name of its output nets.
395 :param hal_py.Netlist nl: The netlist to operate on.
396 :returns: The number of reconstructed names on success, ``None`` otherwise.
397 :rtype: int or ``None``
401 "reconstruct_top_module_pin_groups",
402 [](
Netlist* nl) -> std::optional<u32> {
410 log_error(
"python_context",
"{}", res.get_error().get());
416 Tries to reconstruct top module pin groups via indexed pin names.
418 :param hal_py.Netlist nl: The netlist to operate on.
419 :returns: The number of reconstructed pin groups on success, ``None`` otherwise.
420 :rtype: int or ``None``
425 [](
Netlist* nl,
const std::filesystem::path& def_file) ->
bool {
433 log_error(
"python_context",
"{}", res.get_error().get());
440 Parses a design exchange format file and extracts the coordinates of a placed design for each component/gate.
441 The extracted coordinates get annotated to the gates.
443 :param hal_py.Netlist nl: The netlist to operate on.
444 :param pathlib.Path def_file: The path to the def file
445 :returns: ``True`` on success, ``False`` otherwise.
450 "create_multi_bit_gate_modules",
451 [](
Netlist* nl,
const std::map<std::string, std::map<std::string, std::vector<std::string>>>& concatenated_pin_groups) -> std::vector<Module*> {
459 log_error(
"python_context",
"{}", res.get_error().get());
464 py::arg(
"concatenated_pin_groups"),
466 Create modules from large gates like RAMs and DSPs with the option to concatenate multiple gate pin groups into larger consecutive pin groups.
468 :param hal_py.Netlist nl: The netlist to operate on.
469 :param dict[str,dict[str,list[str]]] concatenated_pin_groups: A dict from gate type name to a dict from the name of the resulting pin group to the names of the pin groups it is concatenated from.
470 :returns: The created modules on success, an empty list otherwise.
471 :rtype: list[hal_py.Module]
475 "create_nets_at_unconnected_pins",
476 [](
Netlist* nl) -> std::vector<Net*> {
484 log_error(
"python_context",
"{}", res.get_error().get());
490 Create a new net for every unconnected output pin of every gate of the netlist.
491 The new nets are named ``HAL_UNCONNECTED_<net_id>``.
493 :param hal_py.Netlist nl: The netlist to operate on.
494 :returns: The created nets on success, an empty list otherwise.
495 :rtype: list[hal_py.Net]
500 [](
Netlist* nl,
const std::vector<Gate*>& ffs = {},
GateType* inverter_type =
nullptr) -> std::optional<u32> {
508 log_error(
"python_context",
"{}", res.get_error().get());
513 py::arg(
"ffs") = std::vector<Gate*>(),
514 py::arg(
"inverter_type") =
nullptr,
516 Iterates all flip-flops of the netlist or specified by the user.
517 If a flip-flop has a ``state`` and a ``neg_state`` output, a new inverter gate is created and connected to the ``state`` output net as an additional destination.
518 Finally, the ``neg_state`` output net is disconnected from the ``neg_state`` pin and re-connected to the new inverter gate's output.
520 :param hal_py.Netlist nl: The netlist to operate on.
521 :param list[hal_py.Gate] ffs: The flip-flops to operate on. Defaults to an empty vector, in which case all flip-flops of the netlist are considered.
522 :param hal_py.GateType inverter_type: The inverter gate type to use. Defaults to a ``None``, in which case the first inverter type found in the gate library is used.
523 :returns: The number of rerouted ``neg_state`` outputs on success, ``None`` otherwise.
524 :rtype: int or ``None``
527 #ifndef PYBIND11_MODULE
std::string get_name() const override
Get the name of the plugin.
std::string get_description() const override
Get a short description of the plugin.
std::string get_version() const override
Get the version of the plugin.
std::set< std::string > get_dependencies() const override
Get the plugin dependencies.
#define log_error(channel,...)
const Module * module(const Gate *g, const NodeBoxes &boxes)
Result< u32 > remove_unconnected_gates(Netlist *nl)
Result< std::monostate > parse_def_file(Netlist *nl, const std::filesystem::path &def_file)
Result< u32 > manual_mux_optimizations(Netlist *nl, GateLibrary *mux_inv_gl)
Result< u32 > simplify_lut_inits(Netlist *nl)
Result< std::vector< Net * > > create_nets_at_unconnected_pins(Netlist *nl)
Result< u32 > remove_redundant_loops(Netlist *nl)
Result< u32 > remove_redundant_logic_trees(Netlist *nl)
Result< u32 > reconstruct_top_module_pin_groups(Netlist *nl)
Result< u32 > remove_redundant_gates(Netlist *nl, const std::function< bool(const Gate *)> &filter=nullptr)
Result< u32 > remove_buffers(Netlist *nl)
Result< u32 > propagate_constants(Netlist *nl)
Result< u32 > remove_unused_lut_inputs(Netlist *nl)
Result< u32 > remove_unconnected_looped(Netlist *nl)
Result< u32 > remove_unconnected_nets(Netlist *nl)
Result< u32 > reconstruct_indexed_ff_identifiers(Netlist *nl)
Result< u32 > remove_consecutive_inverters(Netlist *nl)
Result< std::vector< Module * > > create_multi_bit_gate_modules(Netlist *nl, const std::map< std::string, std::map< std::string, std::vector< std::string >>> &concatenated_pin_groups)
Result< u32 > unify_ff_outputs(Netlist *nl, const std::vector< Gate * > &ffs={}, GateType *inverter_type=nullptr)
This file contains all functions related to the HAL plugin API.