Skip to content
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

Add @likely/@unlikely annotations for blocks #4979

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove @likely/@unlikely when constfolding if statements
- warn if @unlikely is always taken.

Signed-off-by: Chris Dodd <cdodd@nvidia.com>
  • Loading branch information
ChrisDodd committed Dec 16, 2024
commit da01db2aed16e0f545818d59656cf8cf5fc53498
25 changes: 23 additions & 2 deletions frontends/common/constantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1108,15 +1108,36 @@ const IR::Node *DoConstantFolding::postorder(IR::SelectExpression *expression) {
return result;
}

class FilterLikelyAnnot : public Transform {
IR::Statement *preorder(IR::Statement *stmt) {
prune();
return stmt;
}
IR::BlockStatement *preorder(IR::BlockStatement *stmt) {
prune();
visit(stmt->annotations, "annotations");
return stmt;
}
IR::Annotation *preorder(IR::Annotation *annot) {
prune();
if (annot->name == IR::Annotation::likelyAnnotation) return nullptr;
if (annot->name == IR::Annotation::unlikelyAnnotation) {
warning(ErrorType::WARN_IGNORE, "ignoring %1% on always taken statement", annot);
return nullptr;
}
return annot;
}
};

const IR::Node *DoConstantFolding::postorder(IR::IfStatement *ifstmt) {
if (auto cond = ifstmt->condition->to<IR::BoolLiteral>()) {
if (cond->value) {
return ifstmt->ifTrue;
return ifstmt->ifTrue->apply(FilterLikelyAnnot());
} else {
if (ifstmt->ifFalse == nullptr) {
return new IR::EmptyStatement(ifstmt->srcInfo);
} else {
return ifstmt->ifFalse;
return ifstmt->ifFalse->apply(FilterLikelyAnnot());
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions testdata/p4_16_samples/annotation-likely.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <core.p4>

struct Headers {
bit<8> a;
bit<8> b;
}

control ingress(inout Headers h) {
apply {
if (true) @likely {
h.a = 0;
}
if (true) @unlikely {
h.b = 0;
}
}
}

control c<T>(inout T d);
package top<T>(c<T> _c);

top(ingress()) main;
17 changes: 17 additions & 0 deletions testdata/p4_16_samples_outputs/annotation-likely-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <core.p4>

struct Headers {
bit<8> a;
bit<8> b;
}

control ingress(inout Headers h) {
apply {
h.a = 8w0;
h.b = 8w0;
}
}

control c<T>(inout T d);
package top<T>(c<T> _c);
top<Headers>(ingress()) main;
17 changes: 17 additions & 0 deletions testdata/p4_16_samples_outputs/annotation-likely-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <core.p4>

struct Headers {
bit<8> a;
bit<8> b;
}

control ingress(inout Headers h) {
apply {
h.a = 8w0;
h.b = 8w0;
}
}

control c<T>(inout T d);
package top<T>(c<T> _c);
top<Headers>(ingress()) main;
26 changes: 26 additions & 0 deletions testdata/p4_16_samples_outputs/annotation-likely-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <core.p4>

struct Headers {
bit<8> a;
bit<8> b;
}

control ingress(inout Headers h) {
@hidden action annotationlikely11() {
h.a = 8w0;
h.b = 8w0;
}
@hidden table tbl_annotationlikely11 {
actions = {
annotationlikely11();
}
const default_action = annotationlikely11();
}
apply {
tbl_annotationlikely11.apply();
}
}

control c<T>(inout T d);
package top<T>(c<T> _c);
top<Headers>(ingress()) main;
21 changes: 21 additions & 0 deletions testdata/p4_16_samples_outputs/annotation-likely.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <core.p4>

struct Headers {
bit<8> a;
bit<8> b;
}

control ingress(inout Headers h) {
apply {
if (true) @likely {
h.a = 0;
}
if (true) @unlikely {
h.b = 0;
}
}
}

control c<T>(inout T d);
package top<T>(c<T> _c);
top(ingress()) main;
3 changes: 3 additions & 0 deletions testdata/p4_16_samples_outputs/annotation-likely.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
annotation-likely.p4(13): [--Wwarn=ignore] warning: ignoring @unlikely on always taken statement
if (true) @unlikely {
^
Loading