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

Enable handling of arbitrary if statements within actions for BMv2 back end #4999

Merged
merged 7 commits into from
Nov 8, 2024

Conversation

jafingerhut
Copy link
Contributor

No description provided.

@jafingerhut
Copy link
Contributor Author

jafingerhut commented Nov 4, 2024

This eliminates the buggy Predication pass: predication-pass Issues related to p4c's Predication pass but only for the BMv2 back end. I believe the Predication pass is still used by at least the Tofino back end, and so should not be deleted.

In its place, this PR handles arbitrarily nested if statements with arbitrary statements that can appear in action bodies: not only assignment statements, but extern function/method calls, return, and exit statemnts.

It does this by taking advantage of support for jump and conditional jump primitive actions that were implemented in BMv2 in 2017! (Thanks, Antonin -- sorry the p4c back end support was not implemented for 7 years). See https://github.com/jafingerhut/p4-guide/blob/master/docs/if-statements.md#bmv2 for some details.

@fruffy fruffy added the bmv2 Topics related to BMv2 or v1model label Nov 4, 2024
@fruffy
Copy link
Collaborator

fruffy commented Nov 4, 2024

@fruffy
Copy link
Collaborator

fruffy commented Nov 4, 2024

Would this fix #644? One of the oldest open issues for the compiler.

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, thanks for this @jafingerhut

yes, this will fix #644, which was the reason why I added the _jump and _jump_if_zero primitives to bmv2

Comment on lines 130 to 141
if (!inConditional) {
// Similar to return statement optimization above.
break;
}
// Similar to return statement jump above.
unsigned int curOffset = result->size();
auto parameters = new Util::JsonArray();
auto primitive2 = mkPrimitive("_jump"_cs, result);
(*offsetToTargetLabelId)[curOffset] = labelIdEndOfAction;
(*offsetToJumpParams)[curOffset] = parameters;
primitive2->emplace_non_null("source_info"_cs, s->sourceInfoJsonObj());
continue;
Copy link
Member

Choose a reason for hiding this comment

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

nice catch. One could argue that the bmv2 implementation is not ideal here: with the existence of a dedicated exit primitive, bmv2 could have interrupted the execution of the action. But at least this way things are symmetrical with return (I could have added a return primitive too...).

@fruffy fruffy linked an issue Nov 4, 2024 that may be closed by this pull request
@jafingerhut
Copy link
Contributor Author

@fruffy Would you like any additional reviews on this, or is it OK to merge?

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.

Tagging @smolkaj and @jonathan-dilorenzo as stakeholders. From my side this seems good-to-go.

@r12f this might be useful for your DASH modelling efforts?

@@ -25,7 +25,12 @@ namespace P4::BMV2 {
class ActionConverter : public Inspector {
ConversionContext *ctxt;

void convertActionBody(const IR::Vector<IR::StatOrDecl> *body, Util::JsonArray *result);
void convertActionBodyTop(const IR::Vector<IR::StatOrDecl> *body, Util::JsonArray *result);
void convertActionBody(const IR::Vector<IR::StatOrDecl> *body, Util::JsonArray *result,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I'd consider making this a struct that has all this information. Makes it easier to change and read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Done in commit 7.

@jafingerhut
Copy link
Contributor Author

Tagging @smolkaj and @jonathan-dilorenzo as stakeholders. From my side this seems good-to-go.

Jonathan and Steffen -- I believe these changes are completely backwards compatible, meaning that if a P4 program for BMv2 v1model compiles and runs correctly today, it should continue to do so with these changes.

Call p4c-bm2-ss without these changes "A", and with these changes "B".

There exist P4 programs with if statements in actions that give no compile-time error with A, but generate BMv2 JSON files with bugs such that when they process packets, they behave differently than the P4 program specifies. These are fairly rare -- I believe you must have nested if statements within an action body to trigger these bugs in version A, but they do exist. Version B should fix all such bugs.

There also exist P4 programs with if statements in actions that give compile-time errors with A, and generate no BMv2 JSON file. Such programs should compile without error with version B, and produce BMv2 JSON files that behave as specified by the P4 program.

@r12f
Copy link

r12f commented Nov 6, 2024

Tagging @smolkaj and @jonathan-dilorenzo as stakeholders. From my side this seems good-to-go.

@r12f this might be useful for your DASH modelling efforts?

Woot!! Absolutely! Thanks a lot Fabian and Andy! Finally can remove the tricky ternary operators now!

@jafingerhut
Copy link
Contributor Author

The only failing test is for compiling Tofono back end, which appears to be related to issues between not being able to recognize string listerals as type cstring, or something similar to that, unrelated to these changes.

@smolkaj
Copy link
Member

smolkaj commented Nov 7, 2024

Awesome! I have definitely wanted this in the past. cc @kheradmandG for visibility.

@fruffy fruffy added this pull request to the merge queue Nov 8, 2024
Merged via the queue into p4lang:main with commit c11bb8b Nov 8, 2024
18 of 19 checks passed
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Conditional execution in actions is not supported
5 participants