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

Merged
merged 5 commits into from
Mar 12, 2025
Merged
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
36 changes: 34 additions & 2 deletions frontends/common/constantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1108,15 +1108,47 @@ 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) {
// FIXME -- disable this warning due to worries it will trigger too often
warning(ErrorType::WARN_BRANCH_HINT, "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;
if (auto blk = ifstmt->ifFalse ? ifstmt->ifFalse->to<IR::BlockStatement>() : nullptr) {
if (auto annot = blk->getAnnotation(IR::Annotation::likelyAnnotation))
warning(ErrorType::WARN_BRANCH_HINT, "ignoring %1% on never taken statement",
annot);
}
return ifstmt->ifTrue->apply(FilterLikelyAnnot());
} else {
if (auto blk = ifstmt->ifTrue->to<IR::BlockStatement>()) {
if (auto annot = blk->getAnnotation(IR::Annotation::likelyAnnotation))
warning(ErrorType::WARN_BRANCH_HINT, "ignoring %1% on never taken statement",
annot);
}
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
12 changes: 10 additions & 2 deletions frontends/p4/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ namespace P4 {
const IR::Node *DoSimplifyControlFlow::postorder(IR::BlockStatement *statement) {
LOG3("Visiting " << dbp(getOriginal()));
if (statement->hasAnnotations() &&
!(foldInlinedFrom && statement->hasOnlyAnnotation(IR::Annotation::inlinedFromAnnotation)))
return statement;
!(foldInlinedFrom && statement->hasOnlyAnnotation(IR::Annotation::inlinedFromAnnotation))) {
if (auto *pblk = getParent<IR::BlockStatement>()) {
for (auto *annot : statement->annotations) {
if (auto *p = pblk->getAnnotation(annot->name); !p || !p->equiv(*annot))
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 @@ -39,6 +39,8 @@ 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::inlinedFromAnnotation = "inlinedFrom"_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 @@ -291,7 +291,8 @@ class Annotation {
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 inlinedFromAnnotation; /// annotation to mark block of inlined function

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
9 changes: 9 additions & 0 deletions lib/error_catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.

#include <map>

#include "error_reporter.h"
#include "lib/cstring.h"

namespace P4 {
Expand Down Expand Up @@ -71,6 +72,7 @@ const int ErrorType::WARN_ENTRIES_OUT_OF_ORDER = 1022;
const int ErrorType::WARN_MULTI_HDR_EXTRACT = 1023;
const int ErrorType::WARN_EXPRESSION = 1024;
const int ErrorType::WARN_DUPLICATE = 1025;
const int ErrorType::WARN_BRANCH_HINT = 1026;

// ------ Info messages -----------
const int ErrorType::INFO_INFERRED = WARN_MAX + 1;
Expand Down Expand Up @@ -124,9 +126,16 @@ std::map<int, cstring> ErrorCatalog::errorCatalog = {
{ErrorType::WARN_MULTI_HDR_EXTRACT, "multi_header_extract"_cs},
{ErrorType::WARN_EXPRESSION, "expr"_cs},
{ErrorType::WARN_DUPLICATE, "duplicate"_cs},
{ErrorType::WARN_BRANCH_HINT, "branch"_cs},

// Info messages
{ErrorType::INFO_INFERRED, "inferred"_cs},
{ErrorType::INFO_PROGRESS, "progress"_cs}};

void ErrorCatalog::initReporter(ErrorReporter &reporter) {
// by default, ignore warnings about branch hints -- user can turn them
// on with --Wwarn=branch
reporter.setDiagnosticAction("branch"_cs, DiagnosticAction::Ignore);
}

} // namespace P4
5 changes: 5 additions & 0 deletions lib/error_catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace P4 {

using MessageType = ErrorMessage::MessageType;

class ErrorReporter;

/// enumerate supported errors
/// It is a class and not an enum class because in C++11 you can't extend an enum class
class ErrorType {
Expand Down Expand Up @@ -85,6 +87,7 @@ class ErrorType {
static const int WARN_MULTI_HDR_EXTRACT; // same header may be extracted more than once
static const int WARN_EXPRESSION; // expression related warnings
static const int WARN_DUPLICATE; // duplicate objects
static const int WARN_BRANCH_HINT; // branch frequency/likely hints
// Backends should extend this class with additional warnings in the range 1500-2141.
static const int WARN_MIN_BACKEND = 1500; // first allowed backend warning code
static const int WARN_MAX = 2141; // last allowed warning code
Expand Down Expand Up @@ -155,6 +158,8 @@ class ErrorCatalog {
return error;
}

void initReporter(ErrorReporter &reporter);

private:
ErrorCatalog() {}

Expand Down
1 change: 1 addition & 0 deletions lib/error_reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class ErrorReporter {
defaultInfoDiagnosticAction(DiagnosticAction::Info),
defaultWarningDiagnosticAction(DiagnosticAction::Warn) {
outputstream = &std::cerr;
ErrorCatalog::getCatalog().initReporter(*this);
}
virtual ~ErrorReporter() = default;

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
27 changes: 27 additions & 0 deletions testdata/p4_16_samples/annotation-likely.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <core.p4>

@command_line("--Wwarn=branch")

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;
}
if (h.a != h.a) @likely {
h.b = 1;
}
}
}

control c<T>(inout T d);
package top<T>(c<T> _c);

top(ingress()) main;
20 changes: 20 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,20 @@
#include <core.p4>

@command_line("--Wwarn=branch") struct Headers {
bit<8> a;
bit<8> b;
}

control ingress(inout Headers h) {
apply {
h.a = 8w0;
h.b = 8w0;
if (h.a != h.a) @likely {
h.b = 8w1;
}
}
}

control c<T>(inout T d);
package top<T>(c<T> _c);
top<Headers>(ingress()) main;
20 changes: 20 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,20 @@
#include <core.p4>

@command_line("--Wwarn=branch") struct Headers {
bit<8> a;
bit<8> b;
}

control ingress(inout Headers h) {
apply {
h.a = 8w0;
h.b = 8w0;
if (h.a != h.a) @likely {
h.b = 8w1;
}
}
}

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>

@command_line("--Wwarn=branch") struct Headers {
bit<8> a;
bit<8> b;
}

control ingress(inout Headers h) {
@hidden action annotationlikely13() {
h.a = 8w0;
h.b = 8w0;
}
@hidden table tbl_annotationlikely13 {
actions = {
annotationlikely13();
}
const default_action = annotationlikely13();
}
apply {
tbl_annotationlikely13.apply();
}
}

control c<T>(inout T d);
package top<T>(c<T> _c);
top<Headers>(ingress()) main;
24 changes: 24 additions & 0 deletions testdata/p4_16_samples_outputs/annotation-likely.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <core.p4>

@command_line("--Wwarn=branch") 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;
}
if (h.a != h.a) @likely {
h.b = 1;
}
}
}

control c<T>(inout T d);
package top<T>(c<T> _c);
top(ingress()) main;
6 changes: 6 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,6 @@
annotation-likely.p4(15): [--Wwarn=branch] warning: ignoring @unlikely on always taken statement
if (true) @unlikely {
^
annotation-likely.p4(18): [--Wwarn=branch] warning: ignoring @likely on never taken statement
if (h.a != h.a) @likely {
^
Loading
Loading