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

Workaround for gcc-11.4/draft 2x spec flaw #4679

Merged
merged 1 commit into from
May 30, 2024

Conversation

ChrisDodd
Copy link
Contributor

@ChrisDodd ChrisDodd commented May 27, 2024

TLDR: workaround for problems compiling p4c with gcc 11.4 (and probably others) with -std=c++2x

A late draft of the C++2x spec introduced a non-backwards compatible change in the way overloading for operator ==/!= is handled; this was considered a defect and was fixed in the final C++20 spec. However, the default version of gcc on Ubuntu 22.04 (gcc 11.4) happens to implement that spec draft, so gives errors with our code.

More recent versions of gcc (and older versions too!) don't have this problem. So this change isn't really necessary, but is pretty small and may help others who run into this specific problem.

Copy link
Contributor

@vlstill vlstill left a comment

Choose a reason for hiding this comment

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

Thanks for this! I did not realize this is GCC 11 specific problem. Is this enough to compile P4C in C++20 mode?

@@ -130,7 +130,7 @@ class NameMap : public Node {
}

IRNODE_SUBCLASS(NameMap)
bool operator==(const Node &a) const override { return a == *this; }
bool operator==(const Node &a) const override { return a.operator==(*this); }
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a comment for this one why it is needed. This is not obvious.

Copy link
Contributor Author

@ChrisDodd ChrisDodd May 27, 2024

Choose a reason for hiding this comment

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

Its also unfortunately not easy to explain, but is the core of the issue.

C++20 adds the ability to resolve overload of relational operators with the operands swapped -- so a == b can call either a.operator==(b) or b.operator==(a), and the flaw in the early c++2x draft meant that this specific function would end up calling itself recursively. Using an explicit operator== forces it to use that version and not the reversed version.

@vlstill vlstill requested a review from fruffy May 27, 2024 06:03
@fruffy fruffy added the core Topics concerning the core segments of the compiler (frontend, midend, parser) label May 27, 2024
Copy link
Collaborator

@fruffy fruffy left a comment

Choose a reason for hiding this comment

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

Great. Let's see whether we can make progress on the C++20 port with this.

@vlstill
Copy link
Contributor

vlstill commented May 27, 2024

Great. Let's see whether we can make progress on the C++20 port with this.

One conclusion from this PR is that it could be a bit tricky to test if we move to C++20 as various compilers have various incomplete and not entirely correct implementations. I'd suggest testing at least with GCC 9 (as that is our minimum), GCC 11 (Ubuntu 22 and it also has this problem) and GCC 13 (Ubuntu 24). But we probably should have CI for all of these anyway...

@fruffy
Copy link
Collaborator

fruffy commented May 27, 2024

I'd suggest testing at least with GCC 9 (as that is our minimum)

We might upgrade our minimum to 10 considering it is now the default gcc for Ubuntu 20.04?

Great. Let's see whether we can make progress on the C++20 port with this.

One conclusion from this PR is that it could be a bit tricky to test if we move to C++20 as various compilers have various incomplete and not entirely correct implementations. I'd suggest testing at least with GCC 9 (as that is our minimum), GCC 11 (Ubuntu 22 and it also has this problem) and GCC 13 (Ubuntu 24). But we probably should have CI for all of these anyway...

It might be hard to make guarantees beyond what we have running in our CI. We could also consider raising the minimum required GCC version to 10. It is installable in Ubuntu 20.04 In any case, Ubuntu 20.04 will hit EOL next year. We could shift to Ubuntu 22.04/24 split and explicitly support the compilers provided by those distributions.

- A late draft of the C++2x spec introduced a non-backwards compatible
  change in the way overloading for operator ==/!= is handled; this was
  considered a defect and was fix in the final C++20 spec.
@ChrisDodd ChrisDodd added this pull request to the merge queue May 30, 2024
Merged via the queue into p4lang:main with commit 90e90e6 May 30, 2024
17 checks passed
@ChrisDodd ChrisDodd deleted the cdodd-spec2Xdefect branch May 30, 2024 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

3 participants