33 #include "pybind11/pybind11.h"
34 #include "pybind11/stl.h"
36 #include <igraph/igraph.h>
38 #include <pybind11/pytypes.h>
45 class BasePluginInterface;
61 #ifdef PYBIND11_MODULE
62 PYBIND11_MODULE( clock_tree_extractor, m )
71 py::class_<ClockTreeExtractorPlugin, RawPtrWrapper<ClockTreeExtractorPlugin>,
BasePluginInterface>
72 py_clock_tree_extractor_plugin( m,
"ClockTreeExtractorPlugin",
"" );
75 The name of the plugin.
81 Get the name of the plugin.
83 :returns: The name of the plugin.
88 The version of the plugin.
94 Get the version of the plugin.
96 :returns: The version of the plugin.
100 py_clock_tree_extractor_plugin.def_property_readonly(
102 The description of the plugin.
108 Get the description of the plugin.
110 :returns: The description of the plugin.
114 py_clock_tree_extractor_plugin.def_property_readonly(
116 A set of plugin names that this plugin depends on.
122 Get a set of plugin names that this plugin depends on.
124 :returns: A set of plugin names that this plugin depends on.
128 py::enum_<cte::PtrType>( m, "PtrType", R
"(
129 The kind of object a clock tree vertex or diagnostic refers to.
136 py::enum_<cte::Severity>( m,
"Severity", R
"(
137 The severity of a diagnostic emitted during clock tree extraction.
142 py::enum_<cte::DiagnosticCode>( m,
"DiagnosticCode", R
"(
143 The machine-readable kind of a diagnostic emitted during clock tree extraction.
151 py::class_<cte::Diagnostic>( m,
"Diagnostic", R
"(
152 A problem encountered during clock tree extraction, e.g., a gate or net that had to be skipped.
157 :type: clock_tree_extractor.Severity
160 The machine-readable kind of the diagnostic.
162 :type: clock_tree_extractor.DiagnosticCode
165 Whether the subject is a gate or a net.
167 :type: clock_tree_extractor.PtrType
169 .def_property_readonly(
174 return py::cast( (
const Gate *)
self.subject_ptr );
178 return py::cast( (
const Net *)
self.subject_ptr );
184 The gate or net the diagnostic is about.
186 :type: hal_py.Gate or hal_py.Net or None
189 The ID of the gate or net the diagnostic is about.
194 The human-readable message.
199 py::class_<cte::ClockTree>( m, "ClockTree", R
"(
200 The clock distribution network of a netlist as a directed graph whose vertices are gates and nets.
204 [](
const Netlist *netlist ) -> std::unique_ptr<cte::ClockTree> {
211 log_error(
"clock_tree_extractor",
"{}", result.get_error().get() );
214 py::arg(
"netlist" ),
215 py::return_value_policy::move,
216 py::keep_alive<0, 1>(),
218 Extract the clock tree of a netlist.
220 Starting at the clock pin of every flip-flop, the extraction walks against the signal direction through buffers, inverters, delay gates, clock gates and toggle flip-flops up to the global input nets that drive them.
222 :param hal_py.Netlist netlist: The netlist.
223 :returns: The clock tree on success, ``None`` otherwise.
224 :rtype: clock_tree_extractor.ClockTree or None
229 const std::string &pathname,
230 const std::optional<std::string> &x_key,
231 const std::optional<std::string> &y_key ) ->
bool {
238 log_error(
"clock_tree_extractor",
"{}", result.get_error().get() );
241 py::arg(
"pathname" ),
242 py::arg(
"x_key" ) = py::none(),
243 py::arg(
"y_key" ) = py::none(),
245 Write the clock tree to a DOT file. Gates get the coordinates of their location in the layout, unless the
246 keys of both coordinates are given: then they are read from the gate parameters of these names, e.g.,
247 from Verilog instance parameters, which unlike a location can be negative.
249 :param str pathname: The path of the file to write.
250 :param str x_key: The name of the parameter that holds the x-coordinate, or ``None`` to use the location.
251 :param str y_key: The name of the parameter that holds the y-coordinate, or ``None`` to use the location.
252 :returns: ``True`` on success, ``False`` otherwise.
259 const bool parent ) -> std::unique_ptr<cte::ClockTree> {
260 auto result =
self.get_subtree( ptr, parent );
266 log_error(
"clock_tree_extractor",
"{}", result.get_error().get() );
270 py::arg(
"parent" ) =
false,
271 py::return_value_policy::move,
272 py::keep_alive<0, 1>(),
274 Get the clock tree below a gate or net as a clock tree of its own.
276 :param ptr: The gate or net.
277 :type ptr: hal_py.Gate or hal_py.Net
278 :param bool parent: Set ``True`` to start one level up, at the parent of the given object, if it has exactly one. Defaults to ``False``.
279 :returns: The subtree on success, ``None`` otherwise.
280 :rtype: clock_tree_extractor.ClockTree or None
286 const auto &map =
self.get_all();
287 for(
auto &[ptr,
type] : map )
291 result.append( py::cast( (
const Gate *) ptr ) );
295 result.append( py::cast( (
const Net *) ptr ) );
302 Get all gates and nets of the clock tree.
304 :returns: A list of gates and nets.
305 :rtype: list[hal_py.Gate or hal_py.Net]
308 "get_vertex_from_ptr",
310 auto result =
self.get_vertex_from_ptr( ptr );
313 return py::int_( result.get() );
315 log_error(
"clock_tree_extractor",
"{}", result.get_error().get() );
320 Get the igraph vertex ID of a gate or net of the clock tree.
322 :param ptr: The gate or net.
323 :type ptr: hal_py.Gate or hal_py.Net
324 :returns: The vertex ID on success, ``None`` otherwise.
328 "get_ptr_from_vertex",
329 [](
const cte::ClockTree &
self,
const igraph_int_t vertex ) -> py::object {
330 auto result =
self.get_ptr_from_vertex( vertex );
333 auto [ptr,
type] = result.get();
336 return py::cast( (
const Gate *) ptr );
340 return py::cast( (
const Net *) ptr );
344 log_error(
"clock_tree_extractor",
"{}", result.get_error().get() );
350 Get the gate or net behind an igraph vertex ID of the clock tree.
352 :param int vertex: The vertex ID.
353 :returns: The gate or net on success, ``None`` otherwise.
354 :rtype: hal_py.Gate or hal_py.Net or None
357 "get_vertices_from_ptrs",
358 [](
const cte::ClockTree &
self,
const std::vector<const void *> &ptrs ) -> py::list {
359 auto result =
self.get_vertices_from_ptrs( ptrs );
362 return py::cast( result.get() );
364 log_error(
"clock_tree_extractor",
"{}", result.get_error().get() );
369 Get the igraph vertex IDs of gates and nets of the clock tree.
371 :param list[hal_py.Gate or hal_py.Net] ptrs: The gates and nets.
372 :returns: The vertex IDs on success, ``None`` otherwise.
373 :rtype: list[int] or None
376 "get_ptrs_from_vertices",
377 [](
const cte::ClockTree &
self,
const std::vector<igraph_int_t> &vertices ) -> py::list {
378 auto res =
self.get_ptrs_from_vertices( vertices );
382 for(
const auto &[ptr,
type] : res.get() )
386 result.append( py::cast( (
const Gate *) ptr ) );
390 result.append( py::cast( (
const Net *) ptr ) );
394 log_error(
"clock_tree_extractor",
"unknown ptr type" );
400 log_error(
"clock_tree_extractor",
"{}", res.get_error().get() );
403 py::arg(
"vertices" ),
406 Get the gates and nets behind igraph vertex IDs of the clock tree.
408 :param list[int] vertices: The vertex IDs.
409 :returns: The gates and nets on success, ``None`` otherwise.
410 :rtype: list[hal_py.Gate or hal_py.Net] or None
415 auto res =
self.get_neighbors( ptr, IGRAPH_IN );
419 for(
const auto &[ptr,
type] : res.get() )
423 result.append( py::cast( (
const Gate *) ptr ) );
427 result.append( py::cast( (
const Net *) ptr ) );
431 log_error(
"clock_tree_extractor",
"unknown ptr type" );
437 log_error(
"clock_tree_extractor",
"{}", res.get_error().get() );
443 Get the gates and nets directly upstream of a gate or net in the clock tree.
445 :param ptr: The gate or net.
446 :type ptr: hal_py.Gate or hal_py.Net
447 :returns: The neighbors on success, ``None`` otherwise.
448 :rtype: list[hal_py.Gate or hal_py.Net] or None
453 auto res =
self.get_neighbors( ptr, IGRAPH_OUT );
457 for(
const auto &[ptr,
type] : res.get() )
461 result.append( py::cast( (
const Gate *) ptr ) );
465 result.append( py::cast( (
const Net *) ptr ) );
469 log_error(
"clock_tree_extractor",
"unknown ptr type" );
475 log_error(
"clock_tree_extractor",
"{}", res.get_error().get() );
481 Get the gates and nets directly downstream of a gate or net in the clock tree.
483 :param ptr: The gate or net.
484 :type ptr: hal_py.Gate or hal_py.Net
485 :returns: The neighbors on success, ``None`` otherwise.
486 :rtype: list[hal_py.Gate or hal_py.Net] or None
489 Get all gates of the clock tree.
492 :rtype: list[hal_py.Gate]
495 Get all nets of the clock tree.
498 :rtype: list[hal_py.Net]
501 Get the netlist the clock tree was extracted from.
503 :returns: The netlist.
504 :rtype: hal_py.Netlist
507 Get the diagnostics collected while the clock tree was recovered. Empty for subtrees.
509 :returns: The diagnostics.
510 :rtype: list[clock_tree_extractor.Diagnostic]
518 log_error(
"clock_tree_extractor",
"{}", res.get_error().get() );
523 for(
const auto &branch : res.get() )
526 for(
const auto &[ptr,
type] : branch )
530 py_branch.append( py::cast( (
const Gate *) ptr ) );
534 py_branch.append( py::cast( (
const Net *) ptr ) );
538 log_error(
"clock_tree_extractor",
"unknown ptr type" );
542 branches.append( std::move( py_branch ) );
544 return std::move( branches );
548 Get all branches of the clock tree, i.e., all paths from a root to a sink. The branches are recomputed on every call.
550 :returns: The branches on success, ``None`` otherwise. Each branch is ordered from root to sink.
551 :rtype: list[list[hal_py.Gate or hal_py.Net]] or None
554 #ifndef PYBIND11_MODULE
std::vector< const Gate * > get_gates() const
const Netlist * get_netlist() const
std::vector< const Net * > get_nets() const
const std::vector< Diagnostic > & get_diagnostics() const
static Result< std::unique_ptr< ClockTree > > from_netlist(const Netlist *netlist)
#define log_error(channel,...)
const Module * module(const Gate *g, const NodeBoxes &boxes)
Result< std::vector< Branch > > recover_branches(const ClockTree &clock_tree)
@ invalid_clock_net_sources
@ invalid_clock_pin_count
Result< std::monostate > export_dot(const ClockTree &clock_tree, const std::string &pathname, const std::optional< std::string > &x_key=std::nullopt, const std::optional< std::string > &y_key=std::nullopt)