-
Notifications
You must be signed in to change notification settings - Fork 445
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
[P4Testgen] Set other headers also invalid when calling setInvalid on a union header. #4853
base: main
Are you sure you want to change the base?
Conversation
@p-sawicki This is a potential fix but it currently causes BMv2 test failures. |
backends/bmv2/common/action.cpp
Outdated
// If setInvalid is called on a header union, we need to invalidate all other | ||
// headers in the union. | ||
if (const auto *parentStructure = builtin->appliedTo->to<IR::Member>()) { | ||
invalidateOtherHeaderUnionHeaders(parentStructure, *ctxt, result, s); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can think about whether it makes sense to do that in bmv2 (to be fair, I find the specified behavior a bit surprising here), but this part looks good to me for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM on the bmv2 side
The failure in issue1897-bmv2.p4 is because of #3842.
to
This assignment always invalidates Unfortunately, the FlattenUnion pass, which could be used to fix this, does not work. Instead, I introduced a configuration option for CopyStructures to disable expansion of header union assignment in specific cases. |
140f4d2
to
c464233
Compare
7bc38b0
to
fd9ccd4
Compare
@antoninbas I had to make some changes to the |
I haven't worked on this code in so long that it's hard for me to provide good insights there (I also didn't author it in the first place). Ideally there wouldn't be that many differences in the backend code between parser and actions, as there aren't really differences when it comes to the format of the output JSON. A simple alternative would be to add a new The easiest alternative all around would be to change the implementation of |
The current changes in the compiler work, but they are inefficient/hacky. We could end up generating many redundant If the implementation in BMv2 is not too hard we can do it. It might require a package update too etc. I am all for considering this a bug in BMv2 and adding a config (this could even be optional). We can get the conversation on that started with the PR on the BMv2 side. |
A minor suggestion: If you make changes to BMv2 to make this more efficient, I would recommend adding a new operation that causes all members of the union to become invalid, while also keeping the operation that makes only the selected member of the header union invalid, without modifying the validity of any other members. Reason: In case the language spec changes to have both kinds of operations in the future. I have no plans to make such a change, but others have asked about it on occasion. |
8344c44
to
5fa67ef
Compare
Antonin's suggestion of adding a new method to BMv2, e.g. "A simple alternative would be to add a new remove_header_conformant primitive to bmv2 targets, which would strictly conform to the spec." and then having the p4c BMv2 back end use that, and leave the existing BMv2 methods unchanged, is certainly the easiest way to get backwards compatibility across all code that exists today. I don't see any disadvantages to that approach, especially if it is documented in comments why the new method was created, and how it differs in behavior from the existing method. |
This PR is currently stuck. I can move out the testgen-specific fixes and mark the failures Xfail. |
I have split the PR into two. This PR only contains the P4Testgen fixes. #4982 contains the BMv2 compiler fixes, which may or may not be used. |
…der. Signed-off-by: fruffy <[email protected]>
Signed-off-by: fruffy <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The testgen change makes sense to me. A few small comments for the rest.
/// errorOnMethodCall flag will produce an error message if such a | ||
/// method is encountered. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// errorOnMethodCall flag will produce an error message if such a | |
/// method is encountered. | |
/// errorOnMethodCall flag will produce an error message if such a | |
/// method is encountered. |
/// Also expand header union assignments. | ||
/// TODO: This is only necessary because the copy structure pass does not correctly expand | ||
/// header unions for some back ends. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate a bit what this expansion means?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me rethink this. The context is #4853 (comment)
The way P4Testgen interprets this expansion leads to behavior that is not correct. I do not recall whether other back ends have a similar problem but P4Testgen just interprets the spec which says:
u.hi.setValid(): sets the valid bit for header hi to true and sets the valid bit for all other headers to false, which implies that it is unspecified what value reading any member header of u will return.
It might actually make sense to generally disable this expansion until it is safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, given that it is fundamentally broken, we might as well say that CopyStructures
does not support expansion of header union assignments and error in such case. Still, I'm worried how this would impact backends that use it (if any).
It seems to me the only way to do this in midend would be to do consecutive if
sfor
isValid()` for all alternatives, which could lead to inefficient code in many backend. But backends can always opt-out of expanding headers.
BTW. how does testgen process header union assignments if it expands assignments of headers, but not of header unions?
Also, this PR should probably have a breaking-change
tag because of the changes in CopyStructures
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I will split the change to CopyStructures out again and mark it breaking, then rebase the P4Testgen fixes on top.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW. how does testgen process header union assignments if it expands assignments of headers, but not of header unions?
Header union and header assignments should now be handled by https://github.com/p4lang/p4c/blob/main/backends/p4tools/modules/testgen/core/small_step/cmd_stepper.cpp#L68 and https://github.com/p4lang/p4c/blob/main/backends/p4tools/common/core/abstract_execution_state.cpp#L186C30-L186C46
Iirc CopyStructures is technically not needed by P4Testgen anymore but there are other passes it uses (ParserUnroll?) which require it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Iirc CopyStructures is technically not needed by P4Testgen anymore but there are other passes it uses (ParserUnroll?) which require it.
It might be worth checking if we can just disable it in testgen (or leave it just for structures).
/// Do not only copy normal structures but also perform copy assignments for headers. | ||
bool copyHeaders; | ||
/// Configuration options. | ||
CopyStructuresConfig _config; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to use the underscore in name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a habit I have picked up recently. I can change it back. The reason it is useful is that you can declare private members as _member
and then the public getter as member()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see why this is useful (I used to use this convention myself), but my point was more that it is quite unusual in P4C codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, will change it back. We have this style in the testgen back end so sometimes it creeps back in.
One problem is that the style in P4C can be random and changes from file to file.
Ideally, we should push #4410 forward.
Fixes #4448.