Skip to content

Commit

Permalink
Add @likely/@unlikely annotations for blocks
Browse files Browse the repository at this point in the history
- fix a few places annotations on blocks are accidentally lost
- allow merging blocks with same annotation

Signed-off-by: Chris Dodd <cdodd@nvidia.com>
  • Loading branch information
ChrisDodd committed Oct 31, 2024
1 parent e795b84 commit 4d45a79
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 33 deletions.
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

0 comments on commit 4d45a79

Please sign in to comment.