-
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
Standard annotations for expected code path to optimize #1308
Comments
I like the syntax more than the GCC one. It is also very similar (taking into account the different syntax for annotations/attributes) to the C++20 way of doing the same, the Presumably, this would have very little impact on the rest of the spec as it would only say these are recommendations the compiler might take into account, right? How does this apply to |
The grammar (in p4c and the spec) currently does not allow annotations on select cases -- that should probably be fixed.
|
The most unpleasant part may be to make sure the annotations are not lost by any optimization. |
Or just any transformation, e.g. just wrapping a block in a new one will probably remove or deactivate the annotation unless it is handled somehow. I presume
I think it would make sense for these annotations to not hamper other optimizations. So things like hoisting common code out of then and else branch should absolutely be OK, even if one of the branches is |
An alternative would be to put annotations on switch and select cases and somehow attach an annotation to if which says that the IF is likely to go to then or else branch -- this way, we would avoid putting them on block statements and therefore preserving them would be presumably easier. |
most passes don't know anything about annotations, so they try to leave the code involving them untouched. |
In general, passes should preserve annotations on blocks, which inhibits some cases where blocks could be combined. In my PR I fixed a number of cases where annotations were inadvertently being deleted from blocks when those blocks were being moved from one place to another, and extended the code to collapse blocks to allow collapsing blocks when they have the same annotation (things like |
I'd just like to highlight this, as it was not addressed (#1308 (comment)):
Since select right-hand-side cannot be a block but just name, how do we add |
Still, this looks a bit fragile. But I guess there is no currently a way to enforce this (e.g. to check if an annotation is lost or preserved). Fortunately these are just optimization hints, but what if annotation would be required for correctness? Having an extern (similar to gcc's |
Having an extern means that backends that don't care will need to explicitly look for and skip/remove the extern. Annotations are generally ignored by backends by default, so no changes are needed. |
The best way to enforce this are the usual ways:
We currently have many tests that check whether the output of the frontend and midend compiler passes, when used to generate P4 code from them, are identical to expected file contents in the p4c repo, in the testdata/*_outputs/ directories. Is that a sufficient way to have a test that determines whether annotations have been lost or preserved? |
There should be a way for the programmer to indicate which path through a control or parser should be the most common and should be optimized for.
One possibility would be something like:
putting an
@unexpected
annotation on the block in theif
statement tells optimizers that the code in the block will rarely be executed and the fall-through case should be optimized for.Putting the annotation on the statement fits well with what p4c currently supports (annotations on block statements) and also works in other cases like:
This contrasts with eg. gccc's way of doing this sort of thing, which is by having a
__builtin_expected(expression, value)
function.The text was updated successfully, but these errors were encountered: