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 2 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
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
2 changes: 1 addition & 1 deletion frontends/p4/removeParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const IR::Node *DoRemoveActionParameters::postorder(IR::P4Action *action) {
body->append(*postamble);

action->parameters = new IR::ParameterList(action->parameters->srcInfo, *leftParams);
action->body = new IR::BlockStatement(action->body->srcInfo, *body);
action->body = new IR::BlockStatement(action->body->srcInfo, action->body->annotations, *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 @@ -217,7 +217,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->annotations->size() > 0) return statement;
if (statement->annotations->size() > 0) {
if (auto *pblk = getParent<IR::BlockStatement>()) {
for (auto *annot : statement->annotations->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
23 changes: 13 additions & 10 deletions ir/base.def
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,25 @@ class Annotation {
: name(n), needsParsing(false), structured(structured) {
expr.push_back(new StringLiteral(v)); }

static const cstring nameAnnotation; /// Indicates the control-plane name.
static const cstring tableOnlyAnnotation; /// Action cannot be a default_action.
static const cstring defaultOnlyAnnotation; /// action can only be a default_action.
static const cstring atomicAnnotation; /// Code should be executed atomically.
static const cstring debugLoggingAnnotation; /// Used by compiler implementer to limit debug log to the annotated IR context.
static const cstring defaultOnlyAnnotation; /// action can only be a default_action.
static const cstring deprecatedAnnotation; /// Deprecation annotation.
static const cstring fieldListAnnotation; /// Used for recirculate, etc.
static const cstring hiddenAnnotation; /// Object should not be exposed to the control-plane.
static const cstring lengthAnnotation; /// P4-14 annotation for varbit fields.
static const cstring likelyAnnotation; /// annotation for likely taken blocks/branchs
static const cstring matchAnnotation; /// Match annotation (for value sets).
static const cstring nameAnnotation; /// Indicates the control-plane name.
static const cstring noSideEffectsAnnotation; /// extern function/method annotation.
static const cstring noWarnAnnotation; /// noWarn annotation.
static const cstring optionalAnnotation; /// Optional parameter annotation
static const cstring pkginfoAnnotation; /// Package documentation annotation.
static const cstring deprecatedAnnotation; /// Deprecation annotation.
static const cstring synchronousAnnotation; /// Synchronous annotation.
static const cstring pureAnnotation; /// extern function/method annotation.
static const cstring noSideEffectsAnnotation; /// extern function/method annotation.
static const cstring noWarnAnnotation; /// noWarn annotation.
static const cstring matchAnnotation; /// Match annotation (for value sets).
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 synchronousAnnotation; /// Synchronous annotation.
static const cstring tableOnlyAnnotation; /// Action cannot be a default_action.
static const cstring unlikelyAnnotation; /// annotation for likely not taken blocks/branchs

toString{ return absl::StrCat("@", name.string_view()); }
validate{
BUG_CHECK(!name.name.isNullOrEmpty(), "empty annotation name");
Expand Down
22 changes: 12 additions & 10 deletions ir/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,24 @@ const IR::ID IR::Type_Table::hit = ID("hit");
const IR::ID IR::Type_Table::miss = ID("miss");
const IR::ID IR::Type_Table::action_run = ID("action_run");

const cstring IR::Annotation::nameAnnotation = "name"_cs;
const cstring IR::Annotation::tableOnlyAnnotation = "tableonly"_cs;
const cstring IR::Annotation::defaultOnlyAnnotation = "defaultonly"_cs;
const cstring IR::Annotation::atomicAnnotation = "atomic"_cs;
const cstring IR::Annotation::debugLoggingAnnotation = "__debug"_cs;
const cstring IR::Annotation::defaultOnlyAnnotation = "defaultonly"_cs;
const cstring IR::Annotation::deprecatedAnnotation = "deprecated"_cs;
const cstring IR::Annotation::fieldListAnnotation = "field_list"_cs;
const cstring IR::Annotation::hiddenAnnotation = "hidden"_cs;
const cstring IR::Annotation::lengthAnnotation = "length"_cs;
const cstring IR::Annotation::likelyAnnotation = "likely"_cs;
const cstring IR::Annotation::matchAnnotation = "match"_cs;
const cstring IR::Annotation::nameAnnotation = "name"_cs;
const cstring IR::Annotation::noSideEffectsAnnotation = "noSideEffects"_cs;
const cstring IR::Annotation::noWarnAnnotation = "noWarn"_cs;
const cstring IR::Annotation::optionalAnnotation = "optional"_cs;
const cstring IR::Annotation::pkginfoAnnotation = "pkginfo"_cs;
const cstring IR::Annotation::deprecatedAnnotation = "deprecated"_cs;
const cstring IR::Annotation::synchronousAnnotation = "synchronous"_cs;
const cstring IR::Annotation::pureAnnotation = "pure"_cs;
const cstring IR::Annotation::noSideEffectsAnnotation = "noSideEffects"_cs;
const cstring IR::Annotation::noWarnAnnotation = "noWarn"_cs;
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::synchronousAnnotation = "synchronous"_cs;
const cstring IR::Annotation::tableOnlyAnnotation = "tableonly"_cs;
const cstring IR::Annotation::unlikelyAnnotation = "unlikely"_cs;

long Type_Declaration::nextId = 0;
long Type_InfInt::nextId = 0;
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
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
38 changes: 26 additions & 12 deletions testdata/p4_16_samples_outputs/fabric_20190420/fabric-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ control FabricIngress(inout parsed_headers_t hdr, inout fabric_metadata_t fabric
}
@name("FabricIngress.forwarding.bridging_counter") direct_counter(CounterType.packets_and_bytes) forwarding_bridging_counter;
@name("FabricIngress.forwarding.set_next_id_bridging") action forwarding_set_next_id_bridging_0(@name("next_id") bit<32> next_id_0) {
fabric_metadata._next_id10 = next_id_0;
@hidden {
fabric_metadata._next_id10 = next_id_0;
}
forwarding_bridging_counter.count();
}
@name("FabricIngress.forwarding.bridging") table forwarding_bridging {
Expand All @@ -430,7 +432,9 @@ control FabricIngress(inout parsed_headers_t hdr, inout fabric_metadata_t fabric
@name("FabricIngress.forwarding.mpls_counter") direct_counter(CounterType.packets_and_bytes) forwarding_mpls_counter;
@name("FabricIngress.forwarding.pop_mpls_and_next") action forwarding_pop_mpls_and_next_0(@name("next_id") bit<32> next_id_6) {
fabric_metadata._mpls_label5 = 20w0;
fabric_metadata._next_id10 = next_id_6;
@hidden {
fabric_metadata._next_id10 = next_id_6;
}
forwarding_mpls_counter.count();
}
@name("FabricIngress.forwarding.mpls") table forwarding_mpls {
Expand All @@ -447,7 +451,9 @@ control FabricIngress(inout parsed_headers_t hdr, inout fabric_metadata_t fabric
}
@name("FabricIngress.forwarding.routing_v4_counter") direct_counter(CounterType.packets_and_bytes) forwarding_routing_v4_counter;
@name("FabricIngress.forwarding.set_next_id_routing_v4") action forwarding_set_next_id_routing_v4_0(@name("next_id") bit<32> next_id_7) {
fabric_metadata._next_id10 = next_id_7;
@hidden {
fabric_metadata._next_id10 = next_id_7;
}
forwarding_routing_v4_counter.count();
}
@name("FabricIngress.forwarding.nop_routing_v4") action forwarding_nop_routing_v4_0() {
Expand Down Expand Up @@ -533,7 +539,9 @@ control FabricIngress(inout parsed_headers_t hdr, inout fabric_metadata_t fabric
}
@name("FabricIngress.next.xconnect_counter") direct_counter(CounterType.packets_and_bytes) next_xconnect_counter;
@name("FabricIngress.next.output_xconnect") action next_output_xconnect_0(@name("port_num") bit<9> port_num) {
standard_metadata.egress_spec = port_num;
@hidden {
standard_metadata.egress_spec = port_num;
}
next_xconnect_counter.count();
}
@name("FabricIngress.next.set_next_id_xconnect") action next_set_next_id_xconnect_0(@name("next_id") bit<32> next_id_9) {
Expand All @@ -557,20 +565,26 @@ control FabricIngress(inout parsed_headers_t hdr, inout fabric_metadata_t fabric
@max_group_size(16) @name("FabricIngress.next.hashed_selector") action_selector(HashAlgorithm.crc16, 32w1024, 32w16) next_hashed_selector;
@name("FabricIngress.next.hashed_counter") direct_counter(CounterType.packets_and_bytes) next_hashed_counter;
@name("FabricIngress.next.output_hashed") action next_output_hashed_0(@name("port_num") bit<9> port_num_0) {
standard_metadata.egress_spec = port_num_0;
@hidden {
standard_metadata.egress_spec = port_num_0;
}
next_hashed_counter.count();
}
@name("FabricIngress.next.routing_hashed") action next_routing_hashed_0(@name("port_num") bit<9> port_num_1, @name("smac") bit<48> smac, @name("dmac") bit<48> dmac) {
hdr.ethernet.src_addr = smac;
hdr.ethernet.dst_addr = dmac;
standard_metadata.egress_spec = port_num_1;
@hidden {
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") bit<9> port_num_2, @name("smac") bit<48> smac_0, @name("dmac") bit<48> dmac_0, @name("label") bit<20> label_0) {
fabric_metadata._mpls_label5 = label_0;
hdr.ethernet.src_addr = smac_0;
hdr.ethernet.dst_addr = dmac_0;
standard_metadata.egress_spec = port_num_2;
@hidden {
fabric_metadata._mpls_label5 = 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();
}
@name("FabricIngress.next.hashed") table next_hashed {
Expand Down
Loading
Loading