3 #include "pybind11/operators.h"
4 #include "pybind11/pybind11.h"
5 #include "pybind11/stl.h"
6 #include "pybind11/stl_bind.h"
13 namespace py = pybind11;
21 #ifdef PYBIND11_MODULE
22 PYBIND11_MODULE(z3_utils, m)
24 m.doc() =
"hal Z3UtilsPlugin python bindings";
28 py::module m(
"z3_utils",
"hal Z3UtilsPlugin python bindings");
31 py::class_<Z3UtilsPlugin, RawPtrWrapper<Z3UtilsPlugin>,
BasePluginInterface> py_z3_utils(m,
"Z3UtilsPlugin", R
"(
32 The plugin that provides utilities to translate between HAL Boolean functions and z3 expressions.
36 The name of the plugin.
42 Get the name of the plugin.
44 :returns: The name of the plugin.
49 The version of the plugin.
55 Get the version of the plugin.
57 :returns: The version of the plugin.
62 "get_subgraph_function",
63 [](
const std::vector<Gate*>& subgraph_gates,
const Net* subgraph_output) -> std::optional<hal::BooleanFunction> {
69 log_error(
"z3_utils",
"{}", res.get_error().get());
77 py::arg(
"subgraph_gates"),
78 py::arg(
"subgraph_output"),
80 Get the combined Boolean function of a subgraph of combinational gates starting at the source of the provided subgraph output net.
82 :param list[hal_py.Gate] subgraph_gates: The gates making up the subgraph to consider.
83 :param hal_py.Net subgraph_output: The output net of the subgraph whose function shall be generated.
84 :returns: The Boolean function implemented by the subgraph on success, ``None`` otherwise.
85 :rtype: hal_py.BooleanFunction or None
90 [](
const Netlist* netlist_a,
const Netlist* netlist_b,
const Net* net_a,
const Net* net_b,
const bool fail_on_unknown =
true,
const u32 solver_timeout = 10) -> std::optional<bool> {
98 log_error(
"python_context",
"{}", res.get_error().get());
102 py::arg(
"netlist_a"),
103 py::arg(
"netlist_b"),
106 py::arg(
"fail_on_unknown") =
true,
107 py::arg(
"solver_timeout") = 10,
109 Compare two nets from two different netlists.
110 This is done on a functional level by building the subgraph function of each net considering all combinational gates of the netlist.
111 In order for this to work the sequential gates of both netlists must have identical names and only the combinational gates may differ.
113 :param hal_py.Netlist netlist_a: First netlist.
114 :param hal_py.Netlist netlist_b: Second netlist.
115 :param hal_py.Net net_a: First net.
116 :param hal_py.Net net_b: Second net.
117 :param bool fail_on_unknown: Determines whether the function returns ``False`` or ``True`` in case the SAT solver returns unknown.
118 :param int solver_timeout: The timeout for each SAT solver query in seconds.
119 :returns: A Boolean indicating whether the two nets are functionally equivalent on success, ``None`` otherwise.
125 [](
const Netlist* netlist_a,
const Netlist* netlist_b,
const std::vector<std::pair<Net*, Net*>>& nets,
const bool fail_on_unknown =
true,
const u32 solver_timeout = 10)
126 -> std::optional<bool> {
134 log_error(
"python_context",
"{}", res.get_error().get());
138 py::arg(
"netlist_a"),
139 py::arg(
"netlist_b"),
141 py::arg(
"fail_on_unknown") =
true,
142 py::arg(
"solver_timeout") = 10,
144 Compare two nets from two different netlists.
145 This is done on a functional level by building the subgraph function of each net considering all combinational gates of the netlist.
146 In order for this to work the sequential gates of both netlists must have identical names and only the combinational gates may differ.
148 :param hal_py.Netlist netlist_a: First netlist.
149 :param hal_py.Netlist netlist_b: Second netlist.
150 :param list[tuple(hal_py.Net,hal_py.Net)] nets: The pairs of nets to compare against each other.
151 :param bool fail_on_unknown: Determines whether the function returns ``False`` or ``True`` in case the SAT solver returns unknown.
152 :param int solver_timeout: The timeout for each SAT solver query in seconds.
153 :returns: A Boolean indicating whether the two nets are functionally equivalent on success, ``None`` otherwise.
159 [](
const Netlist* netlist_a,
const Netlist* netlist_b,
const bool fail_on_unknown =
true,
const u32 solver_timeout = 10) -> std::optional<bool> {
167 log_error(
"python_context",
"{}", res.get_error().get());
171 py::arg(
"netlist_a"),
172 py::arg(
"netlist_b"),
173 py::arg(
"fail_on_unknown") =
true,
174 py::arg(
"solver_timeout") = 10,
176 Compares two netlists by finding a corresponding partner for each sequential gate in the netlist and checking whether they are identical.
177 This is done on a functional level by building the subgraph function of all their input nets considering all combinational gates of the netlist.
178 In order for this to work the sequential gates of both netlists must have identical names and only the combinational gates may differ.
180 :param hal_py.Netlist netlist_a: First netlist to compare.
181 :param hal_py.Netlist netlist_b: Second netlist to compare.
182 :param bool fail_on_unknown: Determines whether the function returns ``False`` or ``True`` in case the SAT solver returns unknown.
183 :param int solver_timeout: The timeout for each SAT solver query in seconds.
184 :returns: A Boolean indicating whether the two netlists are functionally equivalent on success, ``None`` otherwise.
191 auto ctx = z3::context();
197 log_error(
"python_context",
"{}", res.get_error().get());
208 log_error(
"python_context",
"{}", res.get_error().get());
214 Simplifies a Boolean function using the Z3 solver.
215 This is done by using the Z3 solver to simplify the function and then converting it back to a Boolean function.
216 :param hal_py.BooleanFunction bf: The Boolean function to simplify.
217 :returns: The simplified Boolean function on success, ``None`` otherwise.
218 :rtype: hal_py.BooleanFunction or None
221 #ifndef PYBIND11_MODULE
std::string get_version() const override
std::string get_name() const override
#define log_error(channel,...)
const Module * module(const Gate *g, const NodeBoxes &boxes)
Result< bool > compare_netlists(const Netlist *netlist_a, const Netlist *netlist_b, const bool fail_on_unknown=true, const u32 solver_timeout=10)
Compares two netlists on a functional level.
Result< z3::expr > simplify_local(const z3::expr &e, std::unordered_map< u32, z3::expr > &cache, const bool check_correctness=false)
Applies hand-crafted simplification rules iteratively until no further simplifications can be made.
z3::expr from_bf(const BooleanFunction &bf, z3::context &ctx, const std::map< std::string, z3::expr > &var2expr={})
Result< z3::expr > get_subgraph_z3_function(const std::vector< Gate * > &subgraph_gates, const Net *subgraph_output, z3::context &ctx)
Get the z3 expression representation of a combined Boolean function of a subgraph of combinational ga...
Result< bool > compare_nets(const Netlist *netlist_a, const Netlist *netlist_b, const Net *net_a, const Net *net_b, const bool fail_on_unknown=true, const u32 solver_timeout=10)
Compare two nets from two different netlists.
Result< BooleanFunction > to_bf(const z3::expr &e)