Skip to content

Commit e0a5660

Browse files
committed
fixed pybinds for get_next_sequential_gates
1 parent b826e4e commit e0a5660

File tree

3 files changed

+115
-25
lines changed

3 files changed

+115
-25
lines changed

include/hal_core/netlist/decorators/netlist_traversal_decorator.h

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace hal
8686

8787
/**
8888
* Starting from the given net, traverse the netlist and return only the successor/predecessor gates for which the `target_gate_filter` evaluates to `true`.
89-
* Continues traversal independent of whatever `target_gate_filter` evaluates to.
89+
* Continue traversal independent of whatever `target_gate_filter` evaluates to.
9090
* Stop traversal if (1) the `exit_endpoint_filter` evaluates to `false` on a fan-in/out endpoint (i.e., when exiting the current gate during traversal) or (2) the `entry_endpoint_filter` evaluates to `false` on a successor/predecessor endpoint (i.e., when entering the next gate during traversal).
9191
* The target_gate_filter may be omitted in which case all traversed gates will be returned.
9292
* Both `entry_endpoint_filter` and the `exit_endpoint_filter` may be omitted as well.
@@ -106,6 +106,7 @@ namespace hal
106106

107107
/**
108108
* Starting from the given gate, traverse the netlist and return only the successor/predecessor gates for which the `target_gate_filter` evaluates to `true`.
109+
* Continue traversal independent of whatever `target_gate_filter` evaluates to.
109110
* Stop traversal if (1) the `exit_endpoint_filter` evaluates to `false` on a fan-in/out endpoint (i.e., when exiting the current gate during traversal) or (2) the `entry_endpoint_filter` evaluates to `false` on a successor/predecessor endpoint (i.e., when entering the next gate during traversal).
110111
* The target_gate_filter may be omitted in which case all traversed gates will be returned.
111112
* Both `entry_endpoint_filter` and the `exit_endpoint_filter` may be omitted as well.
@@ -123,13 +124,35 @@ namespace hal
123124
const std::function<bool(const Endpoint*, const u32 current_depth)>& exit_endpoint_filter = nullptr,
124125
const std::function<bool(const Endpoint*, const u32 current_depth)>& entry_endpoint_filter = nullptr) const;
125126

126-
// test & document
127+
/**
128+
* Starting from the given net, traverse the netlist and return only the next layer of sequential successor/predecessor gates.
129+
* Traverse over gates that are not sequential until a sequential gate is found.
130+
* Stops at all sequential gates, but only adds those to the result that have not been reached through a pin of one of the forbidden types.
131+
* Provide a cache to speed up traversal when calling this function multiple times on the same netlist using the same forbidden pins.
132+
*
133+
* @param[in] net - Start net.
134+
* @param[in] successors - Set `true` to get successors, set `false` to get predecessors.
135+
* @param[in] forbidden_pins - Sequential gates reached through these pins will not be part of the result. Defaults to an empty set.
136+
* @param[in] cache - An optional cache that can be used for better performance on repeated calls. Defaults to a `nullptr`.
137+
* @returns The next sequential gates.
138+
*/
127139
Result<std::set<Gate*>>
128-
get_next_sequential_gates(const Net* net, bool successors, const std::set<PinType>& forbidden_input_pins = {}, std::unordered_map<const Net*, std::set<Gate*>>* cache = nullptr) const;
140+
get_next_sequential_gates(const Net* net, bool successors, const std::set<PinType>& forbidden_pins = {}, std::unordered_map<const Net*, std::set<Gate*>>* cache = nullptr) const;
129141

130-
// test & document
142+
/**
143+
* Starting from the given gate, traverse the netlist and return only the next layer of sequential successor/predecessor gates.
144+
* Traverse over gates that are not sequential until a sequential gate is found.
145+
* Stops at all sequential gates, but only adds those to the result that have not been reached through a pin of one of the forbidden types.
146+
* Provide a cache to speed up traversal when calling this function multiple times on the same netlist using the same forbidden pins.
147+
*
148+
* @param[in] gate - Start gate.
149+
* @param[in] successors - Set `true` to get successors, set `false` to get predecessors.
150+
* @param[in] forbidden_pins - Sequential gates reached through these pins will not be part of the result. Defaults to an empty set.
151+
* @param[in] cache - An optional cache that can be used for better performance on repeated calls. Defaults to a `nullptr`.
152+
* @returns The next sequential gates.
153+
*/
131154
Result<std::set<Gate*>>
132-
get_next_sequential_gates(const Gate* gate, bool successors, const std::set<PinType>& forbidden_input_pins = {}, std::unordered_map<const Net*, std::set<Gate*>>* cache = nullptr) const;
155+
get_next_sequential_gates(const Gate* gate, bool successors, const std::set<PinType>& forbidden_pins = {}, std::unordered_map<const Net*, std::set<Gate*>>* cache = nullptr) const;
133156

134157
// TODO implement get_next_combinational_gates (get all combinational successor gates until sequential (non-combinational) gates are hit)
135158

src/netlist/decorators/netlist_traversal_decorator.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,8 @@ namespace hal
235235
return OK(res);
236236
}
237237

238-
Result<std::set<Gate*>> NetlistTraversalDecorator::get_next_sequential_gates(const Net* net,
239-
bool successors,
240-
const std::set<PinType>& forbidden_input_pins,
241-
std::unordered_map<const Net*, std::set<Gate*>>* cache) const
238+
Result<std::set<Gate*>>
239+
NetlistTraversalDecorator::get_next_sequential_gates(const Net* net, bool successors, const std::set<PinType>& forbidden_pins, std::unordered_map<const Net*, std::set<Gate*>>* cache) const
242240
{
243241
if (net == nullptr)
244242
{
@@ -289,7 +287,7 @@ namespace hal
289287
if (gate->get_type()->has_property(GateTypeProperty::sequential))
290288
{
291289
// stop traversal if gate is sequential
292-
if (forbidden_input_pins.find(pin->get_type()) == forbidden_input_pins.end())
290+
if (forbidden_pins.find(pin->get_type()) == forbidden_pins.end())
293291
{
294292
// only add gate to result if it has not been reached through a forbidden pin (e.g., control pin)
295293
res.insert(gate);
@@ -332,10 +330,8 @@ namespace hal
332330
return OK(res);
333331
}
334332

335-
Result<std::set<Gate*>> NetlistTraversalDecorator::get_next_sequential_gates(const Gate* gate,
336-
bool successors,
337-
const std::set<PinType>& forbidden_input_pins,
338-
std::unordered_map<const Net*, std::set<Gate*>>* cache) const
333+
Result<std::set<Gate*>>
334+
NetlistTraversalDecorator::get_next_sequential_gates(const Gate* gate, bool successors, const std::set<PinType>& forbidden_pins, std::unordered_map<const Net*, std::set<Gate*>>* cache) const
339335
{
340336
if (gate == nullptr)
341337
{
@@ -350,7 +346,7 @@ namespace hal
350346
std::set<Gate*> res;
351347
for (const auto* exit_ep : successors ? gate->get_fan_out_endpoints() : gate->get_fan_in_endpoints())
352348
{
353-
const auto next_res = this->get_next_sequential_gates(exit_ep->get_net(), successors, forbidden_input_pins, cache);
349+
const auto next_res = this->get_next_sequential_gates(exit_ep->get_net(), successors, forbidden_pins, cache);
354350
if (next_res.is_error())
355351
{
356352
return ERR(next_res.get_error());

src/python_bindings/bindings/netlist_traversal_decorator.cpp

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ namespace hal
117117
py::arg("entry_endpoint_filter") = nullptr,
118118
R"(
119119
Starting from the given net, traverse the netlist and return only the successor/predecessor gates for which the ``target_gate_filter`` evaluates to ``True``.
120-
Continues traversal independent of whatever ``target_gate_filter`` evaluates to.
120+
Continue traversal independent of whatever ``target_gate_filter`` evaluates to.
121121
Stop traversal if (1) the ``exit_endpoint_filter`` evaluates to ``False`` on a fan-in/out endpoint (i.e., when exiting the current gate during traversal) or (2) the ``entry_endpoint_filter`` evaluates to ``False`` on a successor/predecessor endpoint (i.e., when entering the next gate during traversal).
122122
The target_gate_filter may be omitted in which case all traversed gates will be returned.
123123
Both ``entry_endpoint_filter`` and the ``exit_endpoint_filter`` may be omitted as well.
@@ -157,6 +157,7 @@ namespace hal
157157
py::arg("entry_endpoint_filter") = nullptr,
158158
R"(
159159
Starting from the given gate, traverse the netlist and return only the successor/predecessor gates for which the ``target_gate_filter`` evaluates to ``True``.
160+
Continue traversal independent of whatever ``target_gate_filter`` evaluates to.
160161
Stop traversal if (1) the ``exit_endpoint_filter`` evaluates to ``False`` on a fan-in/out endpoint (i.e., when exiting the current gate during traversal) or (2) the ``entry_endpoint_filter`` evaluates to ``False`` on a successor/predecessor endpoint (i.e., when entering the next gate during traversal).
161162
The target_gate_filter may be omitted in which case all traversed gates will be returned.
162163
Both ``entry_endpoint_filter`` and the ``exit_endpoint_filter`` may be omitted as well.
@@ -172,9 +173,38 @@ namespace hal
172173

173174
py_netlist_traversal_decorator.def(
174175
"get_next_sequential_gates",
175-
[](NetlistTraversalDecorator& self, const Net* net, bool successors, const std::set<PinType>& forbidden_input_pins = {}, std::unordered_map<const Net*, std::set<Gate*>>* cache = nullptr)
176+
[](NetlistTraversalDecorator& self, const Net* net, bool successors, const std::set<PinType>& forbidden_pins = {}) -> std::optional<std::set<Gate*>> {
177+
auto res = self.get_next_sequential_gates(net, successors, forbidden_pins, nullptr);
178+
if (res.is_ok())
179+
{
180+
return res.get();
181+
}
182+
else
183+
{
184+
log_error("python_context", "error encountered while getting next sequential gates:\n{}", res.get_error().get());
185+
return std::nullopt;
186+
}
187+
},
188+
py::arg("net"),
189+
py::arg("successors"),
190+
py::arg("forbidden_pins") = std::set<PinType>(),
191+
R"(
192+
Starting from the given net, traverse the netlist and return only the next layer of sequential successor/predecessor gates.
193+
Traverse over gates that are not sequential until a sequential gate is found.
194+
Stops at all sequential gates, but only adds those to the result that have not been reached through a pin of one of the forbidden types.
195+
196+
:param hal_py.Net net: Start net.
197+
:param bool successors: Set ``True`` to get successors, set ``False`` to get predecessors.
198+
:param set[hal_py.PinType] forbidden_pins: Sequential gates reached through these pins will not be part of the result. Defaults to an empty set.
199+
:returns: The next sequential gates.
200+
:rtype: set[hal_py.Gate] or None
201+
)");
202+
203+
py_netlist_traversal_decorator.def(
204+
"get_next_sequential_gates",
205+
[](NetlistTraversalDecorator& self, const Net* net, bool successors, const std::set<PinType>& forbidden_pins, std::unordered_map<const Net*, std::set<Gate*>>* cache)
176206
-> std::optional<std::set<Gate*>> {
177-
auto res = self.get_next_sequential_gates(net, successors, forbidden_input_pins, cache);
207+
auto res = self.get_next_sequential_gates(net, successors, forbidden_pins, cache);
178208
if (res.is_ok())
179209
{
180210
return res.get();
@@ -187,21 +217,56 @@ namespace hal
187217
},
188218
py::arg("net"),
189219
py::arg("successors"),
190-
py::arg("forbidden_input_pins") = std::set<PinType>(),
191-
py::arg("cache") = nullptr,
220+
py::arg("forbidden_pins"),
221+
py::arg("cache"),
192222
R"(
223+
Starting from the given net, traverse the netlist and return only the next layer of sequential successor/predecessor gates.
224+
Traverse over gates that are not sequential until a sequential gate is found.
225+
Stops at all sequential gates, but only adds those to the result that have not been reached through a pin of one of the forbidden types.
226+
Provide a cache to speed up traversal when calling this function multiple times on the same netlist using the same forbidden pins.
193227
194228
:param hal_py.Net net: Start net.
195229
:param bool successors: Set ``True`` to get successors, set ``False`` to get predecessors.
230+
:param set[hal_py.PinType] forbidden_pins: Sequential gates reached through these pins will not be part of the result.
231+
:param dict[hal_py.Net, set[hal_py.Gate]] cache: A cache that can be used for better performance on repeated calls.
232+
:returns: The next sequential gates.
233+
:rtype: set[hal_py.Gate] or None
234+
)");
196235

236+
py_netlist_traversal_decorator.def(
237+
"get_next_sequential_gates",
238+
[](NetlistTraversalDecorator& self, const Gate* gate, bool successors, const std::set<PinType>& forbidden_pins = {}) -> std::optional<std::set<Gate*>> {
239+
auto res = self.get_next_sequential_gates(gate, successors, forbidden_pins, nullptr);
240+
if (res.is_ok())
241+
{
242+
return res.get();
243+
}
244+
else
245+
{
246+
log_error("python_context", "error encountered while getting next sequential gates:\n{}", res.get_error().get());
247+
return std::nullopt;
248+
}
249+
},
250+
py::arg("gate"),
251+
py::arg("successors"),
252+
py::arg("forbidden_pins") = std::set<PinType>(),
253+
R"(
254+
Starting from the given gate, traverse the netlist and return only the next layer of sequential successor/predecessor gates.
255+
Traverse over gates that are not sequential until a sequential gate is found.
256+
Stops at all sequential gates, but only adds those to the result that have not been reached through a pin of one of the forbidden types.
257+
258+
:param hal_py.Gate gate: Start gate.
259+
:param bool successors: Set ``True`` to get successors, set ``False`` to get predecessors.
260+
:param set[hal_py.PinType] forbidden_pins: Sequential gates reached through these pins will not be part of the result. Defaults to an empty set.
261+
:returns: The next sequential gates.
197262
:rtype: set[hal_py.Gate] or None
198263
)");
199264

200265
py_netlist_traversal_decorator.def(
201266
"get_next_sequential_gates",
202-
[](NetlistTraversalDecorator& self, const Gate* gate, bool successors, const std::set<PinType>& forbidden_input_pins = {}, std::unordered_map<const Net*, std::set<Gate*>>* cache = nullptr)
267+
[](NetlistTraversalDecorator& self, const Gate* gate, bool successors, const std::set<PinType>& forbidden_pins, std::unordered_map<const Net*, std::set<Gate*>>* cache)
203268
-> std::optional<std::set<Gate*>> {
204-
auto res = self.get_next_sequential_gates(gate, successors, forbidden_input_pins, cache);
269+
auto res = self.get_next_sequential_gates(gate, successors, forbidden_pins, cache);
205270
if (res.is_ok())
206271
{
207272
return res.get();
@@ -214,13 +279,19 @@ namespace hal
214279
},
215280
py::arg("gate"),
216281
py::arg("successors"),
217-
py::arg("forbidden_input_pins") = std::set<PinType>(),
218-
py::arg("cache") = nullptr,
282+
py::arg("forbidden_pins"),
283+
py::arg("cache"),
219284
R"(
285+
Starting from the given gate, traverse the netlist and return only the next layer of sequential successor/predecessor gates.
286+
Traverse over gates that are not sequential until a sequential gate is found.
287+
Stops at all sequential gates, but only adds those to the result that have not been reached through a pin of one of the forbidden types.
288+
Provide a cache to speed up traversal when calling this function multiple times on the same netlist using the same forbidden pins.
220289
221290
:param hal_py.Gate gate: Start gate.
222291
:param bool successors: Set ``True`` to get successors, set ``False`` to get predecessors.
223-
292+
:param set[hal_py.PinType] forbidden_pins: Sequential gates reached through these pins will not be part of the result.
293+
:param dict[hal_py.Net, set[hal_py.Gate]] cache: A cache that can be used for better performance on repeated calls.
294+
:returns: The next sequential gates.
224295
:rtype: set[hal_py.Gate] or None
225296
)");
226297
}

0 commit comments

Comments
 (0)