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 <[email protected]>
  • Loading branch information
ChrisDodd committed Dec 9, 2024
1 parent 9d97cb5 commit 001dbd8
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 14 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 @@ -155,7 +155,7 @@ 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

0 comments on commit 001dbd8

Please sign in to comment.