-
Notifications
You must be signed in to change notification settings - Fork 351
Add support for conditional jump in action bodies #379
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
Conversation
@jafingerhut this means that your PR #355 will need to be updated but I think the code will look nicer. I created the |
@antoninbas If we get a little generalization in bmv2 for the P4-16 programs that can be compiled, I am more than happy to update that PR to adjust. Thanks. I did have a question on that PR regarding log levels that may address your concern about log file growth there. |
Codecov Report
@@ Coverage Diff @@
## master #379 +/- ##
==========================================
+ Coverage 76.28% 76.35% +0.06%
==========================================
Files 112 112
Lines 8802 8833 +31
==========================================
+ Hits 6715 6744 +29
- Misses 2087 2089 +2
Continue to review full report at Codecov.
|
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 haven't read through this with enough care to detect subtle bugs, but I got the basic idea enough to see that it looks reasonable. I'm probably not the reviewer you need approval from before committing, though.
Wouldn't it be simple to introduce a |
The label primitive would be a pain to support; it would require some special handling wrt other primitives for backward-jumps (which I agree are not needed for if/else support), which I don't think is a good idea. Supporting string labels would also not be ideal. One thing I could do is try to replace relative jumps with absolute jumps to a primitive operation. In a way this is the same as supporting labels, instead the label would be the index of the primitive operation in the sequence and there would be no need for a special label primitive as each primitive operation would be implicitly labelled with its index. @mbudiu-vmw would you like this better? |
That would work; so the label is just a number denoting the position within the action. |
To make sure we are on the same page, I'll give some more details.
Right now this pull request would require some json that looks like this for the primitive list in the action:
Notice how the jumps use relative offsets. When What I'm proposing is to make the jumps absolute. The new json would look like this:
The offsets are now absolute. When The change I'm offering is a very small one. But using absolute offsets make things a little more readable and maybe a little easier to generate in p4c. As you correctly mention, the offsets can be explicitly added in the JSON for the sake for readability, the bmv2 json parser will ignore them. For example:
@mbudiu-vmw let me know if we are on the same page and I'll make the change this afternoon. |
Yes, that looks good. Thank you. |
9fe4739
to
15cc42c
Compare
We tentatively add support for 2 "core" action primitives for absolute jumps: `_jump` and `_jump_if_zero`. `_jump` takes one parameter which must resolve to an integral value `offset`. When it is executed, we jump to the primitive call at index `offset` in the enclosing action. `_jump_if_zero` is a conditional jump and takes two parameters which must both resolve to integral values, which we call respectively `cond` and `offset`. If `cond` is 0, the primitive behaves exactly like `_jump` and we jump to the primitive call at index `offset`; otherwise the primitive is a no-op. This was implemented at no additional cost compared to the existing implementation. In other words, if an input does not use these 2 primitives, performance will not be affected. This can be used by a P4_16 compiler to support conditions in actions. The JSON documentation was updated to reflect this change, and the version number was bumped up to 2.13
15cc42c
to
cd1595b
Compare
I updated the PR to support absolute jumps. |
const auto &primitive = primitives[idx]; | ||
param_offset = primitive.get_param_offset(); | ||
primitive.execute(&state, &(action_fn->params[param_offset])); | ||
idx = primitive.get_jump_offset(idx); |
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.
Would you like to check here that idx is greater than the previous value?
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 does not have to be, we could imagine supporting backward jumps (for loops)
what would make sense is to check the jump indices in the bmv2 json parser but that would require some special handling for the jump primitives, which I am trying to avoid
@antoninbas I like what you did for code for this issue. However, I wonder, if one could use keywords in JSON such as if_cond, else_cond, else_if_cond to support if-statements? This way, the format is also consistent with the if_cond used in checksum. Further, whether the if-statement exists in the apply block of a control or inside an action, we could generate the same JSON, couldn't we? |
We tentatively add support for 2 "core" action primitives for absolute
jumps:
_jump
and_jump_if_zero
._jump
takes one parameter whichmust resolve to an integral value
offset
. When it is executed, we jumpto the primitive call at index
offset
in the enclosingaction.
_jump_if_zero
is a conditional jump and takes two parameterswhich must both resolve to integral values, which we call respectively
cond
andoffset
. Ifcond
is 0, the primitive behaves exactly like_jump
and we jump to the primitive call at indexoffset
; otherwisethe primitive is a no-op.
This was implemented at no additional cost compared to the existing
implementation. In other words, if an input does not use these 2
primitives, performance will not be affected.
This can be used by a P4_16 compiler to support conditions in actions.
The JSON documentation was updated to reflect this change, and the
version number was bumped up to 2.13