-
Notifications
You must be signed in to change notification settings - Fork 446
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
Do not allow function calls in select labels #2259
Conversation
struct Meta { | ||
} | ||
|
||
bit<16> simple_action() { |
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 realize the terminology used in the compiler sources might be different than in the spec, but isn't this a function, not an action, according to spec language?
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.
Yes that is a mistake that happened during the test-case reduction. Feel free to rename it.
Instead of disallowing all functions, could we just disallow functions with a side-effect? I found myself many times wanting to call a side-effect free function in a key, e.g. to extract a certain range of bits. |
@konne88 My proposed change to the P4_16 language spec allows implementations to use pure functions. You can see that proposed change here, which sounds to me like it is perhaps the same as what you are suggesting: p4lang/p4-spec#831 I do not know how difficult it is to recognize pure functions written in P4_16, but I would guess that at least a subset of pure functions should be straightforward to recognize at compile time. |
I'm catching up on this thread late: what's bad about saying that each expression in the |
In fact, assuming See the pseudocode for match-action unit execution given in Section 13.2.3. |
One could allow such things, and define their behavior precisely, as you suggest. An additional difficulty of implementation for having such side effects in table key expressions is that it could require introducing new temporaries in the compiled code, for some targets. I would expect it is common in some high speed targets to need to create the entire search key, i.e. the value of all search key fields, at one time, and then ship that off to some table implementation. For example, this source code:
For the third key, Also, we need to define whether the modifications made for Then there is the question of abstract/virtual functions, and whether it interacts with this behavior in some way, if and when those behaviors are agreed upon. |
(Sorry for inadvertently closing the issue...) I think the spec does imply this behavior. In particular, the line Of course using arbitrary expressions as keys will sometimes have a cost. But we are not increasing the expressiveness of the language by supporting this feature. If
is equivalent to
where Why force the programmer to jump through hoops when the compiler can easily do this transformation for them? There are plenty of other places in P4 where we provide an abstraction that is convenient for programmers even though it may imply some extra costs. Here, the compiler will be able to deduce that extra state is not needed in the common case (e.g., variables, and |
I have no personal problem with a P4 compiler allowing and supporting this. The implied support seem non-obvious to me from a reading of the current spec, given that If it is decided that it ought to be defined as you suggest, adding an example that makes it obvious that this is what is implied, as opposed to a judgment call, would be a good idea, including a mention that it would be correct for an implementation to perform all of the side effects of |
Agree this is vague. Shall we discuss at the next LDWG? Seems like reaching community consensus is probably best... |
Certainly seems reasonable to discuss it at a LDWG meeting. There is already an entry in the LDWG Wiki page table for it, with links to corresponding p4-spec issue and proposed PR. |
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.
Should definitely add a test case that uses hdr.isValid() in a table key, as that should be allowed (but in a quick scan of testdata I could not see any testcases)
It looks like we will have to rework this, since the LDWG decided to allow expressions in table keys. |
I have narrowed down the scope of this PR; we will deal with issue #2258 separately. |
I think this is ready for review; I think that this part is somewhat less controversial in the spec. |
@@ -3179,6 +3179,10 @@ const IR::Node* TypeInference::postorder(IR::MethodCallExpression* expression) { | |||
auto prop = findContext<IR::Property>(); | |||
if (prop != nullptr && prop->name == IR::TableProperties::actionsPropertyName) | |||
inActionsList = true; | |||
if (findContext<IR::Key>()) { |
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.
Since the scope got narrowed, is this check still correct?
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.
We should probably bring this up again in the language design group. (or figure out what the conclusion was when we discussed this last time.)
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 will remove this code, it is not necessary, since actions cannot be called in a key computation because they return void, so they cannot be part of an expression anyway.
* Reject function calls in select labels
I think that this is the right approach; otherwise it's very complicated to explain when the possible side-effects of the function call may take place. But the working group has to validate this restriction.
Fixes #122