Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abseil formatters #4971

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Add abseil formatters #4971

wants to merge 5 commits into from

Conversation

asl
Copy link
Contributor

@asl asl commented Oct 21, 2024

Add abseil formatters for

  • cstring
  • IR::Node
  • IR::ID

This improves the code itself as we no longer need to do explicit .toString() / .string_view() calls. Also, this prints directly to sink, providing some performance improvements.

@asl asl added core Topics concerning the core segments of the compiler (frontend, midend, parser) breaking-change This change may break assumptions of compiler back ends. labels Oct 21, 2024
@asl asl requested review from vlstill and fruffy October 21, 2024 19:52
@asl
Copy link
Contributor Author

asl commented Oct 21, 2024

This required few breaking changes though:

  • As there is custom formatter, things cannot be formatted via %s depending on implicit conversion to char*
  • There is an implicit conversion to std::string_view now producing some breakage with Protobuf code as now we are having ambiguous implicit conversions. Unfortunately, we cannot (yet) make conversion to char* explicit, as lots of code relies on this, and this will be a topic for another PR

@fruffy
Copy link
Collaborator

fruffy commented Oct 22, 2024

Nice, maybe we should wait for #4964 to be merged to avoid compatibility issues?

@asl
Copy link
Contributor Author

asl commented Oct 22, 2024

Nice, maybe we should wait for #4964 to be merged to avoid compatibility issues?

I not quite sure. Even more, given the size of the backend, doing project-wide refactoring and changes could be prohibitely expensive as soon as it will be merged in.

@asl asl force-pushed the absl-stringify branch 2 times, most recently from ba1e77b to 40d0a66 Compare October 28, 2024 01:14
@asl
Copy link
Contributor Author

asl commented Oct 28, 2024

Tofino failures are due to mentioned protobuf ambiguities. They will likely will be easier to fix after the subsequent PR that removes implicit conversion to char*.

@fruffy
Copy link
Collaborator

fruffy commented Oct 28, 2024

Tofino failures are due to mentioned protobuf ambiguities. They will likely will be easier to fix after the subsequent PR that removes implicit conversion to char*.

What are the planned changes for the next PR? Maybe you can stack it on this one so we can see?

@@ -147,7 +147,7 @@ class BFRuntimeArchHandler : public P4RuntimeArchHandlerCommon<arch> {
}
auto *externType = p4info->add_externs();
externType->set_extern_type_id(static_cast<p4rt_id_t>(typeId));
externType->set_extern_type_name(typeName);
externType->set_extern_type_name(typeName.string_view());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned, Protobuf strings have two constructors: from const char* and string_view. In order to use cstring directly we need to make this conversion explicit.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I assume the reason is that we also have an implicit conversion to const char*? Is the follow-up PR working on that? Maybe it should be first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. And this is misused in many places, e.g. passing cstring's to C functions like strlen, improper checks for nulls, etc. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added commits from subsequent PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes paired with the subsequent Pr look much better to me. Definitely an improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might missed few cases here and there. However, I believe that we do not need lambdas in the example like in #4951 (comment) anymore.

@@ -191,7 +191,7 @@ class cstring {
std::string_view string_view() const {
return str ? std::string_view(str) : std::string_view("");
}
explicit operator std::string_view() const { return string_view(); }
operator std::string_view() const { return string_view(); }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would add a comment here why this shouldn't be explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already discussed in #4688. But essentially: we are going to make interface similar to std::string essentially allowing string_view into corresponding cstring-backed storage. This would fix lots of cstring misuse.

  - cstring
  - IR::Node
  - IR::ID

This improves the code itself as we no longer need to do explicit
.toString() / .string_view() calls. Also, this prints directly to
sink, providing some performance improvements.

Signed-off-by: Anton Korobeynikov <[email protected]>
Signed-off-by: Anton Korobeynikov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This change may break assumptions of compiler back ends. core Topics concerning the core segments of the compiler (frontend, midend, parser)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants