3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-08-04 02:10:24 +00:00

Merge pull request #5247 from calewis/concat_changes

Improve the performance of `concat_name` in the flattening pass
This commit is contained in:
Emil J 2025-07-29 18:40:36 +02:00 committed by GitHub
commit 6375c97e55
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -24,20 +24,36 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <string_view>
USING_YOSYS_NAMESPACE USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN PRIVATE_NAMESPACE_BEGIN
IdString concat_name(RTLIL::Cell *cell, IdString object_name, const std::string &separator = ".") template <typename... Args>
{ [[nodiscard]] std::string concat_views(const Args&... views) {
if (object_name[0] == '\\') static_assert((std::is_convertible_v<Args, std::string_view> && ...),
return stringf("%s%s%s", cell->name.c_str(), separator.c_str(), object_name.c_str() + 1); "All arguments must be convertible to std::string_view.");
else { const std::size_t total_size = (std::string_view(views).size() + ... + 0);
std::string object_name_str = object_name.str();
if (object_name_str.substr(0, 8) == "$flatten") std::string result;
object_name_str.erase(0, 8); result.reserve(total_size);
return stringf("$flatten%s%s%s", cell->name.c_str(), separator.c_str(), object_name_str.c_str());
(result.append(views), ...);
return result;
} }
IdString concat_name(RTLIL::Cell *cell, IdString const &object_name, const std::string &separator = ".")
{
std::string_view object_name_view(object_name.c_str());
if (object_name_view[0] == '\\'){
return concat_views(cell->name.c_str(), separator, object_name_view.substr(1));
}
constexpr std::string_view prefix = "$flatten";
if (object_name_view.substr(0, prefix.size()) == prefix){
object_name_view.remove_prefix(prefix.size());
}
return concat_views(prefix, cell->name.c_str(), separator, object_name_view);
} }
template<class T> template<class T>