-
Notifications
You must be signed in to change notification settings - Fork 80
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
Introduce compile-time bounded for loops and if statements inside of entries
table property
#1261
Comments
Rationale for introducing this feature: There are some who would like to use long lists of Of course it is possible to write a simple program in a general purpose programming language such as C++, Java, Go, Python, etc. that prints out a list of the desired entries, and #include that into a P4 program. The proposers of this feature know this, and yet would still prefer if it were possible to do something like the following straw-man-syntax example proposes:
Complete P4 program can be found here: https://github.com/jafingerhut/p4-guide/blob/master/table-entries-with-for-loops/example1.p4 2023-Aug-02 update: From discussion later in this issue, I would like to focus on only two use cases for bounded loops in P4 at this time:
This only to make sense to me for such a loop to be implemented via compile-time evaluation of the loop and/or Inside the body of a loop in context (a), all occurrences of the loop variable identifier are treated as local compile-time known values, equal to the value that the loop variable takes during that iteration of the loop. The only things that can be within such a
Loops in context (b) could be implemented either by compile-time unrolling of the bounded loop, or some targets could implement them at run-time in a way similar to how a general purpose CPU often does so -- the resulting behavior would be the same regardless of the implementation. Inside the body of a loop in context (b), all occurrences of loop variable identifiers that are newly introduced by the loop are "read only" values, i.e. they are treated similarly to a direction Any statement that can appear where a context (b) (This is the end of text specific to I propose that only I also propose that we either:
I propose that the two possible forms that the (1) It is a compile-time error if you attempt modify the value of <loop_variable_identifier> in the loop body in any way, e.g. by assigning it a value, or using it as an (2) where <assignment_statements> can be a comma-separated list of multiple assignment statements, as in C or C++. New variables whose scope is local to the TODO: One possible restriction would be that all loop variables in the parentheses after |
@thantry Please take a look at the example above, and comment if you were hoping for something else. My intent with this issue is NOT to introduce compile-time bounded for loops into arbitrary places in a P4 program. It is ONLY to enable them within the |
@thantry: I assume you want Do you want the ability to write a Do you want the ability to have other local variables declared within for/if/switch? Assignment statements with arbitrary P4 expressions on the right-hand side? |
Thanks Andy!Yes, the only usecase of this feature would be to generate constant table entries from use of variables that are compile time evaluatable.Use of conditionals on such compile time evaluatables would be a great feature to have.All of (switch, if, if/else) on such variables would contribute to making the program more readable, since I suspect front end compiler passes would just unroll and flatten it to the final array.The intent is to be able to write compact code to generate such constant entries.ThanksHariSent from my iPhoneOn Jul 22, 2023, at 6:56 PM, Andy Fingerhut ***@***.***> wrote:
@thantry: I assume you want else, and else if in addition to a simple if as shown in the example?
Do you want the ability to write a switch statement in there, too?
Do you want the ability to have other local variables declared within for/if/switch?
Assignment statements with arbitrary P4 expressions on the right-hand side?
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Why do you suspect it would be harder? It could well be that working out the design in the context of a specific feature like table entries is more work than just adding general, bounded loops. |
My suspicion stems from the amount of discussion required in the language design work group to approve it, not from anything semantically difficult in the language. e.g. suppose someone says that they want for loops to be able to cause an array of similar parser states to be generated at compile time? An array of similar transitions in a If we limit the proposal to fewer of those situations, then there is less to think about in terms of corner cases, and less time to discuss it to get to consensus (is my personal guess). Trying to cover all of those cases, and look for more, cannot possibly take less time. I do not know if all of these places where for loops could be, you consider part of a general purpose implementation or not, but just from a few minutes of thinking about possibilities:
My belief is that most of the different bullet items above cause unique extensions to the language grammar, and unique enhancements required in the p4c implementation. |
Another situation where I suspect it would be harder to generalize: Suppose we target the ability to have a for loop around a table definition, to create an array of similar tables. It seems like to make that useful, we would either need (a) to introduce arrays of tables, or (b) introduce a mechanism like My belief is that the loops around table entries is quite useful without introducing other new language features like those above. |
How about the following: start with adding |
My meta-point is that I want to discourage us from doing "bolt-on," piecemeal, ad hoc design. It makes the language more complex, and it may actually be more work for us. It is tempting to believe that designing a limited feature may be easier pull off than the general case but I believe the total-cost-of-ownership of the piecemeal approach is actually higher. So the tempting belief is actually false. |
There is also the alternative of using a more powerful preprocesor. We discussed augmenting the existing preprocessor with a |
Note: I generated that list of possible places someone might want for loops to avoid repetitive code not because I think they are all equally useful or important to work on, but in response to the suggestion that we add "general, bounded loops". Frankly, to me the idea of introducing for loops to generate repetitive lists of type parameters seems like it would never be used by anyone, because those lists of type parameters are usually less than 10 of them. If we start with adding for loops as statements, that seems to me to cover these use cases in my list:
It does not cover any of the others, as far as I can see. In particular, it does not cover creating repetitive table entries inside of an The other "syntactic places" in my list above seem similar to me, in that adding the ability to use for loops in each of them requires yet another addition to the grammar. I don't want to make a mountain out of a molehill, but every such syntactic change seems to me likely to lead to another set of discussions and carefully looking for corner cases from whoever would implement this in p4c. If you are content with starting by only adding for loops to control apply bodies and the |
@jnfoster @jonathan-dilorenzo If there is time at the next P4 LDWG meeting, I would like to discuss this issue at least briefly, to get feedback on the proposal, especially on any restrictions of use that people think are good to include. This comment proposes some restrictions that I would personally be happy with, but others may have suggestions, too: #1261 (comment) |
Sounds good. We will add it to the agenda. |
Enhance this proposal to include the |
AI Andy: From 2023-Aug-07, there were some who approved of seeing a written up specification PR for these ideas. Perhaps one of the main concerns voiced was that it would be nice for developers if they had a way in P4_16 to visibly know when something expands into something huge, vs. when it cannot. I am not sure whether the keyword Another concern raised was that it seemed odd to one commenter to leave open the option for a target device to choose whether to unroll loops, or not. |
The driving use case that started this discussion was a need for compile-time metaprogramming where loop iterations emit syntactic elements that are included in the program. We're now talking about general purpose bounded loops. These are very different things, that I think should be considered as different language features. If we're to take on the former in full generality, then I think we're talking about doing something in the existing macro machinery, or introducing a more powerful macro system. Having the same language element be a metaprogramming thing in some cases, but be a general purpose bounded loop facility in others would be highly confusing. |
Personnel
Design
Implementation
p4-spec
:p4c
: Only the beginnings of proposed changes to the language grammar in this PR: P4 spec issue 1261 add bounded loops p4c#4120Process
p4-spec
:p4c
:=======================================
Related issues:
Discussion on this issue in the LDWG led to a preference for having two different designs for loops that are done at packet processing time (perhaps unrolled by the compiler), vs. loops that are guaranteed to be done at compile time, e.g. for creating multiple entries in a table's
entries
property, but also more generally such things as table keys, table actions, multiple control or parser definitions, multiple top-level definitions of tables, actions, local variables, or extern instance declarations within the top level of a control, etc.The issue of execution loops is tracked in issue #1325
Compile-time / macro loops and conditionals are tracked in issue #1326
The text was updated successfully, but these errors were encountered: