Skip to content

Commit

Permalink
Remove @likely/@unlikely when constfolding if statements
Browse files Browse the repository at this point in the history
- warn if @unlikely is always taken.
  • Loading branch information
ChrisDodd committed Dec 12, 2024
1 parent 34982a1 commit 05e10e9
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 2 deletions.
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 {
^

0 comments on commit 05e10e9

Please sign in to comment.