From 05e10e917a46233822c5c7c58b3ac1f29850c2d8 Mon Sep 17 00:00:00 2001 From: Chris Dodd Date: Thu, 12 Dec 2024 02:00:19 +0000 Subject: [PATCH] Remove @likely/@unlikely when constfolding if statements - warn if @unlikely is always taken. --- frontends/common/constantFolding.cpp | 25 ++++++++++++++++-- testdata/p4_16_samples/annotation-likely.p4 | 22 ++++++++++++++++ .../annotation-likely-first.p4 | 17 ++++++++++++ .../annotation-likely-frontend.p4 | 17 ++++++++++++ .../annotation-likely-midend.p4 | 26 +++++++++++++++++++ .../annotation-likely.p4 | 21 +++++++++++++++ .../annotation-likely.p4-stderr | 3 +++ 7 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 testdata/p4_16_samples/annotation-likely.p4 create mode 100644 testdata/p4_16_samples_outputs/annotation-likely-first.p4 create mode 100644 testdata/p4_16_samples_outputs/annotation-likely-frontend.p4 create mode 100644 testdata/p4_16_samples_outputs/annotation-likely-midend.p4 create mode 100644 testdata/p4_16_samples_outputs/annotation-likely.p4 create mode 100644 testdata/p4_16_samples_outputs/annotation-likely.p4-stderr diff --git a/frontends/common/constantFolding.cpp b/frontends/common/constantFolding.cpp index 2ba8c18ae94..35b14dd0166 100644 --- a/frontends/common/constantFolding.cpp +++ b/frontends/common/constantFolding.cpp @@ -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()) { 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()); } } } diff --git a/testdata/p4_16_samples/annotation-likely.p4 b/testdata/p4_16_samples/annotation-likely.p4 new file mode 100644 index 00000000000..ba9726f6c8a --- /dev/null +++ b/testdata/p4_16_samples/annotation-likely.p4 @@ -0,0 +1,22 @@ +#include + +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(inout T d); +package top(c _c); + +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/annotation-likely-first.p4 b/testdata/p4_16_samples_outputs/annotation-likely-first.p4 new file mode 100644 index 00000000000..8661c2d5bea --- /dev/null +++ b/testdata/p4_16_samples_outputs/annotation-likely-first.p4 @@ -0,0 +1,17 @@ +#include + +struct Headers { + bit<8> a; + bit<8> b; +} + +control ingress(inout Headers h) { + apply { + h.a = 8w0; + h.b = 8w0; + } +} + +control c(inout T d); +package top(c _c); +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/annotation-likely-frontend.p4 b/testdata/p4_16_samples_outputs/annotation-likely-frontend.p4 new file mode 100644 index 00000000000..8661c2d5bea --- /dev/null +++ b/testdata/p4_16_samples_outputs/annotation-likely-frontend.p4 @@ -0,0 +1,17 @@ +#include + +struct Headers { + bit<8> a; + bit<8> b; +} + +control ingress(inout Headers h) { + apply { + h.a = 8w0; + h.b = 8w0; + } +} + +control c(inout T d); +package top(c _c); +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/annotation-likely-midend.p4 b/testdata/p4_16_samples_outputs/annotation-likely-midend.p4 new file mode 100644 index 00000000000..3c46706ac0f --- /dev/null +++ b/testdata/p4_16_samples_outputs/annotation-likely-midend.p4 @@ -0,0 +1,26 @@ +#include + +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(inout T d); +package top(c _c); +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/annotation-likely.p4 b/testdata/p4_16_samples_outputs/annotation-likely.p4 new file mode 100644 index 00000000000..4ea67d25908 --- /dev/null +++ b/testdata/p4_16_samples_outputs/annotation-likely.p4 @@ -0,0 +1,21 @@ +#include + +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(inout T d); +package top(c _c); +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/annotation-likely.p4-stderr b/testdata/p4_16_samples_outputs/annotation-likely.p4-stderr new file mode 100644 index 00000000000..fe7c594fa6a --- /dev/null +++ b/testdata/p4_16_samples_outputs/annotation-likely.p4-stderr @@ -0,0 +1,3 @@ +annotation-likely.p4(13): [--Wwarn=ignore] warning: ignoring @unlikely on always taken statement + if (true) @unlikely { + ^