Skip to content

Add @likely/@unlikely annotations for blocks#4979

Merged
ChrisDodd merged 5 commits into
p4lang:mainfrom
ChrisDodd:cdodd-likely
Mar 12, 2025
Merged

Add @likely/@unlikely annotations for blocks#4979
ChrisDodd merged 5 commits into
p4lang:mainfrom
ChrisDodd:cdodd-likely

Conversation

@ChrisDodd

Copy link
Copy Markdown
Contributor

These will generally be used by backends and simply passed through the frontend/midend, so not much is needed here.

Perhaps some frontend passes (in particular, DoSimplifyControlFlow in frontends/p4/simplify.cpp) could be extended to understand these -- currently, any annotations on a block will inhibit combining the blocks, whereas @likely/@unlikely on a block should not inhibit combining it with a block with no annotation (the annotation should be on the resulting combined block).

See discussion in p4lang/p4-spec#1308

@fruffy fruffy added the p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). label Oct 24, 2024
@kfcripps kfcripps requested a review from asl October 24, 2024 13:28
@ChrisDodd ChrisDodd marked this pull request as draft October 24, 2024 21:48
@ChrisDodd ChrisDodd force-pushed the cdodd-likely branch 3 times, most recently from d86089f to d482036 Compare October 28, 2024 03:35
@ChrisDodd ChrisDodd marked this pull request as ready for review October 28, 2024 03:36
@ChrisDodd ChrisDodd force-pushed the cdodd-likely branch 2 times, most recently from 46257c9 to 1847985 Compare October 31, 2024 20:28
@ChrisDodd ChrisDodd requested review from fruffy and vlstill October 31, 2024 21:38

@vlstill vlstill left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps some frontend passes (in particular, DoSimplifyControlFlow in frontends/p4/simplify.cpp) could be extended to understand these -- currently, any annotations on a block will inhibit combining the blocks, whereas @likely/@unlikely on a block should not inhibit combining it with a block with no annotation (the annotation should be on the resulting combined block).

Sounds reasonable. Is there a risk that the annotation will currently inhibit some optimization? That would be very unfortunate.

However, if we start merging blocks where the annotations don't match exactly, could we end up with both @likely and @unlikely on a single blocks? What should be done then? Imagine code like this:

if (some_runtime_condition) @likely {
    if (some_condition_compiler_proves_true) @unlikely {
        code;
    }
}

obviously, this example is a bit pathological as the code claims the branch is unlikely, but the compiler proves it can be taken always.

@ChrisDodd ChrisDodd force-pushed the cdodd-likely branch 6 times, most recently from b116b77 to ecb291c Compare December 9, 2024 20:48
@ChrisDodd ChrisDodd requested a review from vlstill December 11, 2024 22:03
@ChrisDodd

Copy link
Copy Markdown
Contributor Author

So it seems that something like

if (condition_that_is_always_true) @likely {  ...some code... }

is quite plausible, and if the compiler can prove it is always true and remove the if, it should probably also remove the @likely. For the more pathological case of having an @unlikely here, it should probably also remove it, as it just seems wrong. Maybe a warning about that?

@ChrisDodd ChrisDodd force-pushed the cdodd-likely branch 2 times, most recently from 05e10e9 to da01db2 Compare December 16, 2024 00:23
@ChrisDodd

Copy link
Copy Markdown
Contributor Author

So I've added warnings for always taken/@unlikely and never taken/@likely branches and a testcase. Overall, I think this is ready to go -- do we need more discussion in the LDWG next week?

@ChrisDodd ChrisDodd force-pushed the cdodd-likely branch 3 times, most recently from f1652aa to 35b4a33 Compare January 7, 2025 07:02
Comment thread testdata/p4_16_samples_outputs/fabric_20190420/fabric-midend.p4 Outdated
@ChrisDodd ChrisDodd requested a review from asl March 1, 2025 08:12
@ChrisDodd

Copy link
Copy Markdown
Contributor Author

I think this PR is ready to go, except for discussion on what should and should not be a warning. @asl, @vlstill what do you think?

@ChrisDodd ChrisDodd force-pushed the cdodd-likely branch 2 times, most recently from 962d58e to da05bef Compare March 5, 2025 20:51
@ChrisDodd

Copy link
Copy Markdown
Contributor Author

@fruffy @jafingerhut -- can I get approval for this PR?

@jafingerhut

Copy link
Copy Markdown
Contributor

@ChrisDodd Given my familiarity with only a tiny subset of the C++ implementation code for p4c, unfortunately I don't think I'm qualified to review that, and that is pretty critical here.

@asl

asl commented Mar 5, 2025

Copy link
Copy Markdown
Contributor

I think this PR is ready to go, except for discussion on what should and should not be a warning. @asl, @vlstill what do you think?

Yeah. @vlstill what do you think for the warning? I doubt that it should be there and e.g. existing practice with C/C++ compilers with __builtin_expect shows that we might easily inline through unexpected branch (e.g. inside error path handling) and therefore things will be too verbose.

@ChrisDodd

Copy link
Copy Markdown
Contributor Author

I commented out the warning for @unlikely on always taken blocks. Unfortunately looks like all the CI is broken currently

@ChrisDodd ChrisDodd force-pushed the cdodd-likely branch 2 times, most recently from 6fd64dd to 91b031d Compare March 11, 2025 04:59
- fix a few places annotations on blocks are accidentally lost
- allow merging blocks with same annotation

Signed-off-by: Chris Dodd <cdodd@nvidia.com>
Signed-off-by: Chris Dodd <cdodd@nvidia.com>
- warn if @unlikely is always taken.

Signed-off-by: Chris Dodd <cdodd@nvidia.com>
Signed-off-by: Chris Dodd <cdodd@nvidia.com>
@asl

asl commented Mar 11, 2025

Copy link
Copy Markdown
Contributor

@ChrisDodd seems annotation-likely.p4-stderr requires an update. And it seems some warnings are still produced?

@ChrisDodd

Copy link
Copy Markdown
Contributor Author

@ChrisDodd seems annotation-likely.p4-stderr requires an update. And it seems some warnings are still produced?

Yes, I only disabled the warning for an always taken @unlikely branch. It still warns for a never taken @likely branch.

Perhaps both these warnings should be in their own warning class that defaults to off, but can be turned on with an appropriate -Wwarn option

@asl

asl commented Mar 12, 2025

Copy link
Copy Markdown
Contributor

Perhaps both these warnings should be in their own warning class that defaults to off, but can be turned on with an appropriate -Wwarn option

Looks reasonable, yes.

@ChrisDodd ChrisDodd added this pull request to the merge queue Mar 12, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 12, 2025
@ChrisDodd

Copy link
Copy Markdown
Contributor Author

Perhaps both these warnings should be in their own warning class that defaults to off, but can be turned on with an appropriate -Wwarn option

Looks reasonable, yes.

So I actually did this -- all these warnings now default to off, but can be turned on with --Wwarn=branch. Its a little bit messy -- I had to add an initReporter method to ErrorCatalog to set the default initial state of error reporting.

Signed-off-by: Chris Dodd <cdodd@nvidia.com>
@ChrisDodd ChrisDodd enabled auto-merge March 12, 2025 11:30
@ChrisDodd ChrisDodd added this pull request to the merge queue Mar 12, 2025
Merged via the queue into p4lang:main with commit 9ca48e2 Mar 12, 2025
@ChrisDodd ChrisDodd deleted the cdodd-likely branch March 12, 2025 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants