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 all commits
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
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
6 changes: 4 additions & 2 deletions frontends/p4/dontcareArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ class DontcareArgs : public Transform, public ResolutionContext {
IR::IndexedVector<IR::StatOrDecl> body;
for (auto d : toAdd) body.push_back(d);
body.append(function->body->components);
function->body = new IR::BlockStatement(function->body->srcInfo, body);
function->body =
new IR::BlockStatement(function->body->srcInfo, function->body->annotations, body);
toAdd.clear();
return function;
}
const IR::Node *postorder(IR::P4Action *action) override {
IR::IndexedVector<IR::StatOrDecl> body;
for (auto d : toAdd) body.push_back(d);
body.append(action->body->components);
action->body = new IR::BlockStatement(action->body->srcInfo, body);
action->body =
new IR::BlockStatement(action->body->srcInfo, action->body->annotations, body);
toAdd.clear();
return action;
}
Expand Down
8 changes: 5 additions & 3 deletions frontends/p4/parseAnnotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ namespace P4 {
ParseAnnotations::HandlerMap ParseAnnotations::standardHandlers() {
return {
// These annotations have empty bodies.
PARSE_EMPTY(IR::Annotation::tableOnlyAnnotation),
PARSE_EMPTY(IR::Annotation::atomicAnnotation),
PARSE_EMPTY(IR::Annotation::defaultOnlyAnnotation),
PARSE_EMPTY(IR::Annotation::hiddenAnnotation),
PARSE_EMPTY(IR::Annotation::atomicAnnotation),
PARSE_EMPTY(IR::Annotation::likelyAnnotation),
PARSE_EMPTY(IR::Annotation::noSideEffectsAnnotation),
PARSE_EMPTY(IR::Annotation::optionalAnnotation),
PARSE_EMPTY(IR::Annotation::pureAnnotation),
PARSE_EMPTY(IR::Annotation::noSideEffectsAnnotation),
PARSE_EMPTY(IR::Annotation::tableOnlyAnnotation),
PARSE_EMPTY(IR::Annotation::unlikelyAnnotation),
PARSE_EMPTY("disable_optimization"_cs),
PARSE_EMPTY("unroll"_cs),
PARSE_EMPTY("nounroll"_cs),
Expand Down
3 changes: 2 additions & 1 deletion frontends/p4/removeParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ const IR::Node *DoRemoveActionParameters::postorder(IR::P4Action *action) {
body.append(postamble);

action->parameters = new IR::ParameterList(action->parameters->srcInfo, std::move(leftParams));
action->body = new IR::BlockStatement(action->body->srcInfo, std::move(body));
action->body =
new IR::BlockStatement(action->body->srcInfo, action->body->annotations, std::move(body));
LOG1("To replace " << dbp(action));
result->push_back(action);
return result;
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/removeReturns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ const IR::Node *DoRemoveReturns::preorder(IR::ExitStatement *statement) {
}

const IR::Node *DoRemoveReturns::preorder(IR::BlockStatement *statement) {
auto block = new IR::BlockStatement;
auto block = new IR::BlockStatement(statement->srcInfo, statement->annotations);
auto currentBlock = block;
TernaryBool ret = TernaryBool::No;
for (auto s : statement->components) {
Expand Down
4 changes: 2 additions & 2 deletions frontends/p4/sideEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ const IR::Node *DoSimplifyExpressions::preorder(IR::MethodCallExpression *mce) {

const IR::Node *DoSimplifyExpressions::postorder(IR::Function *function) {
if (toInsert.empty()) return function;
auto body = new IR::BlockStatement(function->body->srcInfo);
auto body = new IR::BlockStatement(function->body->srcInfo, function->body->annotations);
for (auto a : toInsert) body->push_back(a);
for (auto s : function->body->components) body->push_back(s);
function->body = body;
Expand All @@ -604,7 +604,7 @@ const IR::Node *DoSimplifyExpressions::postorder(IR::P4Control *control) {

const IR::Node *DoSimplifyExpressions::postorder(IR::P4Action *action) {
if (toInsert.empty()) return action;
auto body = new IR::BlockStatement(action->body->srcInfo);
auto body = new IR::BlockStatement(action->body->srcInfo, action->body->annotations);
for (auto a : toInsert) body->push_back(a);
for (auto s : action->body->components) body->push_back(s);
action->body = body;
Expand Down
14 changes: 13 additions & 1 deletion frontends/p4/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ namespace P4 {

const IR::Node *DoSimplifyControlFlow::postorder(IR::BlockStatement *statement) {
LOG3("Visiting " << dbp(getOriginal()));
if (statement->hasAnnotations()) return statement;
if (statement->hasAnnotations()) {
if (auto *pblk = getParent<IR::BlockStatement>()) {
for (auto *annot : statement->annotations) {
if (auto *p = pblk->getAnnotation(annot->name)) {
if (!p->equiv(*annot)) return statement;
} else {
return statement;
}
}
} else {
return statement;
}
}
auto parent = getContext()->node;
CHECK_NULL(parent);
if (parent->is<IR::SwitchCase>() || parent->is<IR::P4Control>() || parent->is<IR::Function>() ||
Expand Down
2 changes: 2 additions & 0 deletions ir/annotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const cstring IR::Annotation::matchAnnotation = "match"_cs;
const cstring IR::Annotation::fieldListAnnotation = "field_list"_cs;
const cstring IR::Annotation::debugLoggingAnnotation = "__debug"_cs;
const cstring IR::Annotation::disableOptimizationAnnotation = "disable_optimization"_cs;
const cstring IR::Annotation::likelyAnnotation = "likely"_cs;
const cstring IR::Annotation::unlikelyAnnotation = "unlikely"_cs;

namespace Annotations {
void addIfNew(Vector<Annotation> &annotations, cstring name, const Expression *expr,
Expand Down
3 changes: 2 additions & 1 deletion ir/base.def
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ class Annotation {
static const cstring fieldListAnnotation; /// Used for recirculate, etc.
static const cstring debugLoggingAnnotation; /// Used by compiler implementer to limit debug log to the annotated IR context.
static const cstring disableOptimizationAnnotation; /// annotation to disable certain optimization

static const cstring likelyAnnotation; /// annotation for likely taken blocks/branchs
static const cstring unlikelyAnnotation; /// annotation for likely not taken blocks/branchs
toString{ return absl::StrCat("@", name); }
validate{
BUG_CHECK(!name.name.isNullOrEmpty(), "empty annotation name");
Expand Down
4 changes: 2 additions & 2 deletions midend/flattenUnions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const IR::Node *HandleValidityHeaderUnion::postorder(IR::MethodCallStatement *mc

const IR::Node *HandleValidityHeaderUnion::postorder(IR::P4Action *action) {
if (toInsert.empty()) return action;
auto body = new IR::BlockStatement(action->body->srcInfo);
auto body = new IR::BlockStatement(action->body->srcInfo, action->body->annotations);
for (auto a : toInsert) body->push_back(a);
for (auto s : action->body->components) body->push_back(s);
action->body = body;
Expand Down Expand Up @@ -380,7 +380,7 @@ const IR::Node *DoFlattenHeaderUnion::postorder(IR::P4Action *action) {
}
}
}
auto body = new IR::BlockStatement(action->body->srcInfo);
auto body = new IR::BlockStatement(action->body->srcInfo, action->body->annotations);
for (auto a : actiondecls) body->push_back(a);
action->body = body;
return action;
Expand Down
2 changes: 1 addition & 1 deletion midend/removeExits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const IR::Node *DoRemoveExits::preorder(IR::P4Control *control) {
}

const IR::Node *DoRemoveExits::preorder(IR::BlockStatement *statement) {
auto block = new IR::BlockStatement;
auto block = new IR::BlockStatement(statement->srcInfo, statement->annotations);
auto currentBlock = block;
TernaryBool ret = TernaryBool::No;
for (auto s : statement->components) {
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 {
^
30 changes: 7 additions & 23 deletions testdata/p4_16_samples_outputs/fabric_20190420/fabric-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -562,34 +562,18 @@ control FabricIngress(inout parsed_headers_t hdr, inout fabric_metadata_t fabric
}
@name("FabricIngress.next.routing_hashed") action next_routing_hashed_0(@name("port_num") port_num_t port_num_1, @name("smac") mac_addr_t smac, @name("dmac") mac_addr_t dmac) {
@hidden {
@hidden {
hdr.ethernet.src_addr = smac;
}
@hidden {
hdr.ethernet.dst_addr = dmac;
}
@hidden {
standard_metadata.egress_spec = port_num_1;
}
hdr.ethernet.src_addr = smac;
hdr.ethernet.dst_addr = dmac;
standard_metadata.egress_spec = port_num_1;
}
next_hashed_counter.count();
}
@name("FabricIngress.next.mpls_routing_hashed") action next_mpls_routing_hashed_0(@name("port_num") port_num_t port_num_2, @name("smac") mac_addr_t smac_0, @name("dmac") mac_addr_t dmac_0, @name("label") mpls_label_t label_0) {
@hidden {
@hidden {
fabric_metadata.mpls_label = label_0;
}
@hidden {
@hidden {
hdr.ethernet.src_addr = smac_0;
}
@hidden {
hdr.ethernet.dst_addr = dmac_0;
}
@hidden {
standard_metadata.egress_spec = port_num_2;
}
}
fabric_metadata.mpls_label = label_0;
hdr.ethernet.src_addr = smac_0;
hdr.ethernet.dst_addr = dmac_0;
standard_metadata.egress_spec = port_num_2;
}
next_hashed_counter.count();
}
Expand Down
Loading
Loading