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

[P4Testgen] Set other headers also invalid when calling setInvalid on a union header. #4853

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

Conversation

fruffy
Copy link
Collaborator

@fruffy fruffy commented Aug 5, 2024

Fixes #4448.

@fruffy fruffy added the p4tools Topics related to the P4Tools back end label Aug 5, 2024
@fruffy
Copy link
Collaborator Author

fruffy commented Aug 5, 2024

@p-sawicki This is a potential fix but it currently causes BMv2 test failures.

@fruffy fruffy marked this pull request as ready for review August 5, 2024 19:28
@fruffy fruffy added the bmv2 Topics related to BMv2 or v1model label Aug 5, 2024
backends/bmv2/common/action.cpp Outdated Show resolved Hide resolved
Comment on lines 177 to 176
// 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);
}
Copy link
Member

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.

backends/bmv2/common/action.cpp Outdated Show resolved Hide resolved
Copy link
Member

@antoninbas antoninbas left a 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

backends/bmv2/common/action.cpp Outdated Show resolved Hide resolved
backends/bmv2/common/action.cpp Outdated Show resolved Hide resolved
@fruffy fruffy marked this pull request as draft August 6, 2024 20:31
@fruffy
Copy link
Collaborator Author

fruffy commented Aug 7, 2024

The failure in issue1897-bmv2.p4 is because of #3842.
P4Testgen uses the CopyStructures pass, which incorrectly expands the header union assignment
from

hdr.addr_dst = addr_0;

to

hdr.addr_dst.ipv4.setValid();
hdr.addr_dst.ipv4.addr = addr_0.ipv4.addr;
hdr.addr_dst.ipv6.setValid();
hdr.addr_dst.ipv6.addr = addr_0.ipv6.addr;

This assignment always invalidates ipv4, even though it may be valid.

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.

@fruffy fruffy force-pushed the fruffy/hu_fix branch 4 times, most recently from 140f4d2 to c464233 Compare August 8, 2024 07:25
@fruffy fruffy marked this pull request as ready for review August 8, 2024 08:34
@fruffy fruffy force-pushed the fruffy/hu_fix branch 2 times, most recently from 7bc38b0 to fd9ccd4 Compare August 8, 2024 09:45
@fruffy
Copy link
Collaborator Author

fruffy commented Aug 8, 2024

@antoninbas I had to make some changes to the parser.cpp code, too. Unfortunately, that piece of code is written differently so I had to make some awkward changes. Ideally, the code there should be refactor but that is not in scope for this PR. Any suggestions for a better implementation?

@fruffy fruffy requested a review from antoninbas August 9, 2024 13:51
@antoninbas
Copy link
Member

@antoninbas I had to make some changes to the parser.cpp code, too. Unfortunately, that piece of code is written differently so I had to make some awkward changes. Ideally, the code there should be refactor but that is not in scope for this PR. Any suggestions for a better implementation?

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 remove_header_conformant primitive to bmv2 targets, which would strictly conform to the spec. In the longer term it could also become the default remove_header implementation.
I bring it up because it wouldn't be too hard to implement in bmv2. However, it is quite likely that we will see a few compatibility issues if we go this way (old simple_switch build with new p4c build).

The easiest alternative all around would be to change the implementation of remove_header. Usually I prefer to avoid a behavioral change like this in bmv2. This is why I first suggested handling this in the compiler. In the past, when I had to do something like this, I introduced a compile-time flag to toggle the behavior (p4lang/behavioral-model@9131ed9). If there is some sort of consensus that changing the bmv2 behavior (note that it will also bring assign_header to conformance) is the right thing to do (and that the current implementation should be considered a bug), it will hopefully only take a few lines of code. I actually think that there is a strong case to be made in favor of this, as going back to the first P4_16 version (https://p4.org/p4-spec/docs/P4-16-v1.0.0-spec.html#sec-header-unions), this was always the specified behavior. I am now leaning towards this option. I can open a PR this weekend. Let me know what you think.

@fruffy
Copy link
Collaborator Author

fruffy commented Aug 10, 2024

@antoninbas I had to make some changes to the parser.cpp code, too. Unfortunately, that piece of code is written differently so I had to make some awkward changes. Ideally, the code there should be refactor but that is not in scope for this PR. Any suggestions for a better implementation?

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 remove_header_conformant primitive to bmv2 targets, which would strictly conform to the spec. In the longer term it could also become the default remove_header implementation. I bring it up because it wouldn't be too hard to implement in bmv2. However, it is quite likely that we will see a few compatibility issues if we go this way (old simple_switch build with new p4c build).

The easiest alternative all around would be to change the implementation of remove_header. Usually I prefer to avoid a behavioral change like this in bmv2. This is why I first suggested handling this in the compiler. In the past, when I had to do something like this, I introduced a compile-time flag to toggle the behavior (p4lang/behavioral-model@9131ed9). If there is some sort of consensus that changing the bmv2 behavior (note that it will also bring assign_header to conformance) is the right thing to do (and that the current implementation should be considered a bug), it will hopefully only take a few lines of code. I actually think that there is a strong case to be made in favor of this, as going back to the first P4_16 version (https://p4.org/p4-spec/docs/P4-16-v1.0.0-spec.html#sec-header-unions), this was always the specified behavior. I am now leaning towards this option. I can open a PR this weekend. Let me know what you think.

The current changes in the compiler work, but they are inefficient/hacky. We could end up generating many redundant remove_header calls. So I also prefer fixing this in BMv2.

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.

@jafingerhut
Copy link
Contributor

@antoninbas I had to make some changes to the parser.cpp code, too. Unfortunately, that piece of code is written differently so I had to make some awkward changes. Ideally, the code there should be refactor but that is not in scope for this PR. Any suggestions for a better implementation?

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 remove_header_conformant primitive to bmv2 targets, which would strictly conform to the spec. In the longer term it could also become the default remove_header implementation. I bring it up because it wouldn't be too hard to implement in bmv2. However, it is quite likely that we will see a few compatibility issues if we go this way (old simple_switch build with new p4c build).
The easiest alternative all around would be to change the implementation of remove_header. Usually I prefer to avoid a behavioral change like this in bmv2. This is why I first suggested handling this in the compiler. In the past, when I had to do something like this, I introduced a compile-time flag to toggle the behavior (p4lang/behavioral-model@9131ed9). If there is some sort of consensus that changing the bmv2 behavior (note that it will also bring assign_header to conformance) is the right thing to do (and that the current implementation should be considered a bug), it will hopefully only take a few lines of code. I actually think that there is a strong case to be made in favor of this, as going back to the first P4_16 version (https://p4.org/p4-spec/docs/P4-16-v1.0.0-spec.html#sec-header-unions), this was always the specified behavior. I am now leaning towards this option. I can open a PR this weekend. Let me know what you think.

The current changes in the compiler work, but they are inefficient/hacky. We could end up generating many redundant remove_header calls. So I also prefer fixing this in BMv2.

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.

@fruffy fruffy force-pushed the fruffy/hu_fix branch 2 times, most recently from 8344c44 to 5fa67ef Compare August 15, 2024 09:33
@jafingerhut
Copy link
Contributor

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.

@fruffy
Copy link
Collaborator Author

fruffy commented Sep 27, 2024

This PR is currently stuck. I can move out the testgen-specific fixes and mark the failures Xfail.

@fruffy
Copy link
Collaborator Author

fruffy commented Oct 25, 2024

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.

@fruffy fruffy requested review from vlstill and removed request for antoninbas and vlstill January 6, 2025 10:17
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.

The testgen change makes sense to me. A few small comments for the rest.

Comment on lines +28 to +29
/// errorOnMethodCall flag will produce an error message if such a
/// method is encountered.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// 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.

Comment on lines +34 to +36
/// 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.
Copy link
Contributor

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?

Copy link
Collaborator Author

@fruffy fruffy Jan 6, 2025

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.

Copy link
Contributor

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 ifsforisValid()` 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.

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

@fruffy fruffy Jan 6, 2025

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.

Copy link
Contributor

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;
Copy link
Contributor

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?

Copy link
Collaborator Author

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().

Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bmv2 Topics related to BMv2 or v1model p4tools Topics related to the P4Tools back end
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[P4Testgen] incorrect handling of setInvalid() called on headers in unions
4 participants