Skip to content

Commit

Permalink
added allowed pin types to get_next_sequential_gates
Browse files Browse the repository at this point in the history
  • Loading branch information
nils1603 committed Sep 12, 2023
1 parent 68ac9d2 commit fb68a73
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 7 deletions.
70 changes: 70 additions & 0 deletions include/hal_core/netlist/netlist_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ namespace hal
*/
CORE_API std::vector<Gate*> get_next_gates(const Net* net, bool get_successors, int depth = 0, const std::function<bool(const Gate*)>& filter = nullptr);

/**
* Find all sequential predecessors or successors of a gate.
* Traverses combinational logic of all input or output nets until sequential gates are found.
* The result may include the provided gate itself.
* The use of the this cached version is recommended in case of extensive usage to improve performance.
* The cache will be filled by this function and should initially be provided empty.
* Different caches for different values of get_successors shall be used.
*
* @param[in] gate - The initial gate.
* @param[in] get_successors - If true, sequential successors are returned, otherwise sequential predecessors are returned.
* @param[in] allowed_pin_types - Only returns gates which are reached through one of the pins whose types are in this vector.
* @param[inout] cache - The cache.
* @returns All sequential successors or predecessors of the gate.
*/
CORE_API std::vector<Gate*> get_next_sequential_gates(const Gate* gate, bool get_successors, const std::vector<PinType>& allowed_pin_types, std::unordered_map<u32, std::vector<Gate*>>& cache);

/**
* Find all sequential predecessors or successors of a gate.
* Traverses combinational logic of all input or output nets until sequential gates are found.
Expand All @@ -142,6 +158,37 @@ namespace hal
*/
CORE_API std::vector<Gate*> get_next_sequential_gates(const Gate* gate, bool get_successors, std::unordered_map<u32, std::vector<Gate*>>& cache);

/**
* Find all sequential predecessors or successors of a net.
* Traverses combinational logic of all input or output nets until sequential gates are found.
* The result may include the provided gate itself.
* The use of the this cached version is recommended in case of extensive usage to improve performance.
* The cache will be filled by this function and should initially be provided empty.
* Different caches for different values of get_successors shall be used.
*
* @param[in] gate - The initial gate.
* @param[in] get_successors - If true, sequential successors are returned, otherwise sequential predecessors are returned.
* @param[in] allowed_pin_types - Only returns gates which are reached through one of the pins whose types are in this vector.
* @param[inout] cache - The cache.
* @returns All sequential successors or predecessors of the gate.
*/
CORE_API std::vector<Gate*> get_next_sequential_gates(const Net* net, bool get_successors, const std::vector<PinType>& allowed_pin_types, std::unordered_map<u32, std::vector<Gate*>>& cache);

/**
* Find all sequential predecessors or successors of a net.
* Traverses combinational logic of all input or output nets until sequential gates are found.
* The result may include the provided gate itself.
* The use of the this cached version is recommended in case of extensive usage to improve performance.
* The cache will be filled by this function and should initially be provided empty.
* Different caches for different values of get_successors shall be used.
*
* @param[in] gate - The initial gate.
* @param[in] get_successors - If true, sequential successors are returned, otherwise sequential predecessors are returned.
* @param[inout] cache - The cache.
* @returns All sequential successors or predecessors of the gate.
*/
CORE_API std::vector<Gate*> get_next_sequential_gates(const Net* net, bool get_successors, std::unordered_map<u32, std::vector<Gate*>>& cache);

/**
* Find all sequential predecessors or successors of a gate.
* Traverses combinational logic of all input or output nets until sequential gates are found.
Expand Down Expand Up @@ -177,6 +224,29 @@ namespace hal
*/
CORE_API std::vector<Gate*> get_next_sequential_gates(const Net* net, bool get_successors);

/**
* Find all sequential predecessors or successors of a gate.
* Traverses combinational logic of all input or output nets until sequential gates are found.
* The result may include the provided gate itself.
*
* @param[in] gate - The initial gate.
* @param[in] get_successors - If true, sequential successors are returned, otherwise sequential predecessors are returned.
* @param[in] allowed_pin_types - Only returns gates which are reached through one of the pins whose types are in this vector.
* @returns All sequential successors or predecessors of the gate.
*/
CORE_API std::vector<Gate*> get_next_sequential_gates(const Gate* gate, bool get_successors, const std::vector<PinType>& allowed_pin_types);

/**
* Find all sequential predecessors or successors of a net.
* Traverses combinational logic of all input or output nets until sequential gates are found.
*
* @param[in] net - The initial net.
* @param[in] get_successors - If true, sequential successors are returned, otherwise sequential predecessors are returned.
* @param[in] allowed_pin_types - Only returns gates which are reached through one of the pins whose types are in this vector.
* @returns All sequential successors or predecessors of the net.
*/
CORE_API std::vector<Gate*> get_next_sequential_gates(const Net* net, bool get_successors, const std::vector<PinType>& allowed_pin_types);

/**
* Find all gates on the predecessor or successor path of a gate.
* Traverses all input or output nets until gates of the specified base types are found.
Expand Down
63 changes: 56 additions & 7 deletions src/netlist/netlist_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ namespace hal

namespace
{
std::vector<Gate*> get_next_sequential_gates_internal(const Net* start_net, bool forward, std::unordered_set<u32>& seen, std::unordered_map<u32, std::vector<Gate*>>& cache)
std::vector<Gate*> get_next_sequential_gates_internal(const Net* start_net,
bool forward,
std::unordered_set<u32>& seen,
const std::vector<PinType>& allowed_pin_types,
std::unordered_map<u32, std::vector<Gate*>>& cache)
{
if (auto it = cache.find(start_net->get_id()); it != cache.end())
{
Expand All @@ -301,13 +305,16 @@ namespace hal

if (next_gate->get_type()->has_property(GateTypeProperty::ff))
{
found_ffs.push_back(next_gate);
if (std::find(allowed_pin_types.begin(), allowed_pin_types.end(), endpoint->get_pin()->get_type()) != allowed_pin_types.end() || allowed_pin_types.empty())
{
found_ffs.push_back(next_gate);
}
}
else
{
for (auto n : forward ? next_gate->get_fan_out_nets() : next_gate->get_fan_in_nets())
{
auto next_gates = get_next_sequential_gates_internal(n, forward, seen, cache);
auto next_gates = get_next_sequential_gates_internal(n, forward, seen, allowed_pin_types, cache);
found_ffs.insert(found_ffs.end(), next_gates.begin(), next_gates.end());
}
}
Expand All @@ -321,9 +328,31 @@ namespace hal
}
} // namespace

std::vector<Gate*> get_next_sequential_gates(const Gate* gate, bool get_successors, const std::vector<PinType>& allowed_pin_types, std::unordered_map<u32, std::vector<Gate*>>& cache)
{
std::vector<Gate*> found_ffs;

for (const auto& endpoint : get_successors ? gate->get_fan_out_endpoints() : gate->get_fan_in_endpoints())
{
if (std::find(allowed_pin_types.begin(), allowed_pin_types.end(), endpoint->get_pin()->get_type()) != allowed_pin_types.end() || allowed_pin_types.empty())
{
auto n = endpoint->get_net();
auto suc = get_next_sequential_gates(n, get_successors, allowed_pin_types, cache);
found_ffs.insert(found_ffs.end(), suc.begin(), suc.end());
}
}

std::sort(found_ffs.begin(), found_ffs.end());
found_ffs.erase(std::unique(found_ffs.begin(), found_ffs.end()), found_ffs.end());

return found_ffs;
}

std::vector<Gate*> get_next_sequential_gates(const Gate* gate, bool get_successors, std::unordered_map<u32, std::vector<Gate*>>& cache)
{
std::vector<Gate*> found_ffs;
std::vector<PinType> allowed_pin_types;

for (const auto& n : get_successors ? gate->get_fan_out_nets() : gate->get_fan_in_nets())
{
auto suc = get_next_sequential_gates(n, get_successors, cache);
Expand All @@ -336,22 +365,42 @@ namespace hal
return found_ffs;
}

std::vector<Gate*> get_next_sequential_gates(const Net* net, bool get_successors, std::unordered_map<u32, std::vector<Gate*>>& cache)
std::vector<Gate*> get_next_sequential_gates(const Net* net, bool get_successors, const std::vector<PinType>& allowed_pin_types, std::unordered_map<u32, std::vector<Gate*>>& cache)
{
std::unordered_set<u32> seen;
return get_next_sequential_gates_internal(net, get_successors, seen, cache);
return get_next_sequential_gates_internal(net, get_successors, seen, allowed_pin_types, cache);
}

std::vector<Gate*> get_next_sequential_gates(const Gate* gate, bool get_successors)
{
std::unordered_map<u32, std::vector<Gate*>> cache;
return get_next_sequential_gates(gate, get_successors, cache);
std::vector<PinType> allowed_pin_types;
return get_next_sequential_gates(gate, get_successors, allowed_pin_types, cache);
}

std::vector<Gate*> get_next_sequential_gates(const Net* net, bool get_successors)
{
std::unordered_map<u32, std::vector<Gate*>> cache;
return get_next_sequential_gates(net, get_successors, cache);
std::vector<PinType> allowed_pin_types;
return get_next_sequential_gates(net, get_successors, allowed_pin_types, cache);
}

std::vector<Gate*> get_next_sequential_gates(const Net* net, bool get_successors, const std::vector<PinType>& allowed_pin_types)
{
std::unordered_map<u32, std::vector<Gate*>> cache;
return get_next_sequential_gates(net, get_successors, allowed_pin_types, cache);
}

std::vector<Gate*> get_next_sequential_gates(const Gate* gate, bool get_successors, const std::vector<PinType>& allowed_pin_types)
{
std::unordered_map<u32, std::vector<Gate*>> cache;
return get_next_sequential_gates(gate, get_successors, allowed_pin_types, cache);
}

std::vector<Gate*> get_next_sequential_gates(const Net* net, bool get_successors, std::unordered_map<u32, std::vector<Gate*>>& cache)
{
std::vector<PinType> allowed_pin_types;
return get_next_sequential_gates(net, get_successors, allowed_pin_types, cache);
}

namespace
Expand Down
82 changes: 82 additions & 0 deletions src/python_bindings/bindings/netlist_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ namespace hal
:rtype: list[hal_py.Gate]
)");

py_netlist_utils.def("get_next_sequential_gates",
py::overload_cast<const Gate*, bool, const std::vector<PinType>&, std::unordered_map<u32, std::vector<Gate*>>&>(&netlist_utils::get_next_sequential_gates),
py::arg("gate"),
py::arg("get_successors"),
py::arg("allowed_pin_types"),
py::arg("cache"),
R"(
Find all sequential predecessors or successors of a gate.
Traverses combinational logic of all input or output nets until sequential gates are found.
The result may include the provided gate itself.
The use of the this cached version is recommended in case of extensive usage to improve performance.
The cache will be filled by this function and should initially be provided empty.
Different caches for different values of get_successors shall be used.
:param hal_py.Gate gate: The initial gate.
:param bool get_successors: If true, sequential successors are returned, otherwise sequential predecessors are returned.
:param list[hal_py.PinType] allowed_pin_types: Only returns gates which are reached through one of the pins whose types are in this vector.
:param dict[int, list[hal_py.Gate]] cache: The cache.
:returns: All sequential successors or predecessors of the gate.
:rtype: list[hal_py.Gate]
)");

py_netlist_utils.def("get_next_sequential_gates",
py::overload_cast<const Gate*, bool, std::unordered_map<u32, std::vector<Gate*>>&>(&netlist_utils::get_next_sequential_gates),
py::arg("gate"),
Expand All @@ -114,6 +136,26 @@ namespace hal
:rtype: list[hal_py.Gate]
)");

py_netlist_utils.def("get_next_sequential_gates",
py::overload_cast<const Gate*, bool, const std::vector<PinType>&>(&netlist_utils::get_next_sequential_gates),
py::arg("gate"),
py::arg("get_successors"),
py::arg("allowed_pin_types"),
R"(
Find all sequential predecessors or successors of a gate.
Traverses combinational logic of all input or output nets until sequential gates are found.
The result may include the provided gate itself.
The use of the this cached version is recommended in case of extensive usage to improve performance.
The cache will be filled by this function and should initially be provided empty.
Different caches for different values of get_successors shall be used.
:param hal_py.Gate gate: The initial gate.
:param bool get_successors: If true, sequential successors are returned, otherwise sequential predecessors are returned.
:param list[hal_py.PinType] allowed_pin_types: Only returns gates which are reached through one of the pins whose types are in this vector.
:returns: All sequential successors or predecessors of the gate.
:rtype: list[hal_py.Gate]
)");

py_netlist_utils.def("get_next_sequential_gates", py::overload_cast<const Gate*, bool>(&netlist_utils::get_next_sequential_gates), py::arg("gate"), py::arg("get_successors"), R"(
Find all sequential predecessors or successors of a gate.
Traverses combinational logic of all input or output nets until sequential gates are found.
Expand All @@ -125,6 +167,27 @@ namespace hal
:rtype: list[hal_py.Gate]
)");

py_netlist_utils.def("get_next_sequential_gates",
py::overload_cast<const Net*, bool, const std::vector<PinType>&, std::unordered_map<u32, std::vector<Gate*>>&>(&netlist_utils::get_next_sequential_gates),
py::arg("net"),
py::arg("get_successors"),
py::arg("allowed_pin_types"),
py::arg("cache"),
R"(
Find all sequential predecessors or successors of a net.
Traverses combinational logic of all input or output nets until sequential gates are found.
The use of the cache is recommended in case of extensive usage of this function.
The cache will be filled by this function and should initially be provided empty.
Different caches for different values of get_successors shall be used.
:param hal_py.Net net: The initial net.
:param bool get_successors: If true, sequential successors are returned, otherwise sequential predecessors are returned.
:param list[hal_py.PinType] allowed_pin_types: Only returns gates which are reached through one of the pins whose types are in this vector.
:param dict[int, list[hal_py.Gate]] cache: The cache.
:returns: All sequential successors or predecessors of the net.
:rtype: list[hal_py.Net]
)");

py_netlist_utils.def("get_next_sequential_gates",
py::overload_cast<const Net*, bool, std::unordered_map<u32, std::vector<Gate*>>&>(&netlist_utils::get_next_sequential_gates),
py::arg("net"),
Expand All @@ -144,6 +207,25 @@ namespace hal
:rtype: list[hal_py.Net]
)");

py_netlist_utils.def("get_next_sequential_gates",
py::overload_cast<const Net*, bool, const std::vector<PinType>&>(&netlist_utils::get_next_sequential_gates),
py::arg("net"),
py::arg("get_successors"),
py::arg("allowed_pin_types"),
R"(
Find all sequential predecessors or successors of a net.
Traverses combinational logic of all input or output nets until sequential gates are found.
The use of the cache is recommended in case of extensive usage of this function.
The cache will be filled by this function and should initially be provided empty.
Different caches for different values of get_successors shall be used.
:param hal_py.Net net: The initial net.
:param bool get_successors: If true, sequential successors are returned, otherwise sequential predecessors are returned.
:param list[hal_py.PinType] allowed_pin_types: Only returns gates which are reached through one of the pins whose types are in this vector.
:returns: All sequential successors or predecessors of the net.
:rtype: list[hal_py.Net]
)");

py_netlist_utils.def("get_next_sequential_gates", py::overload_cast<const Net*, bool>(&netlist_utils::get_next_sequential_gates), py::arg("net"), py::arg("get_successors"), R"(
Find all sequential predecessors or successors of a net.
Traverses combinational logic of all input or output nets until sequential gates are found.
Expand Down

0 comments on commit fb68a73

Please sign in to comment.