Skip to content

Commit 6f7353f

Browse files
authored
Remove IR::Annotations and make IAnnotated to carry annotations inline (#4992)
* Simplify annotations query Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info> * Rearrange the fields to reduce the size of object. Fix broken initializer_list while there Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info> * Reduce the overheads Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info> * Remove IR::Annotations and make IAnnotated to carry annotations inline. Lots of cleanups and fixes here and there Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info> * Address review comments Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info> --------- Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
1 parent 8a7e01c commit 6f7353f

File tree

99 files changed

+899
-1081
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+899
-1081
lines changed

backends/bmv2/common/backend.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ limitations under the License.
2323
#include "frontends/common/model.h"
2424
#include "frontends/p4/coreLibrary.h"
2525
#include "helpers.h"
26+
#include "ir/annotations.h"
2627
#include "ir/ir.h"
2728
#include "lib/cstring.h"
2829
#include "lib/error.h"
@@ -167,10 +168,10 @@ class RenameUserMetadata : public Transform {
167168
else
168169
suffix = "."_cs + f->name;
169170
cstring newName = namePrefix + suffix;
170-
auto stringLit = new IR::StringLiteral(newName);
171171
LOG2("Renaming " << f << " to " << newName);
172-
auto annos = f->annotations->addOrReplace(IR::Annotation::nameAnnotation, stringLit);
173-
auto field = new IR::StructField(f->srcInfo, f->name, annos, f->type);
172+
auto field = new IR::StructField(
173+
f->srcInfo, f->name, IR::Annotations::setNameAnnotation(f->annotations, newName),
174+
f->type);
174175
fields.push_back(field);
175176
}
176177

backends/bmv2/simple_switch/simpleSwitch.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ void SimpleSwitchBackend::createRecirculateFieldsList(ConversionContext *ctxt,
10381038
LOG2("Scanning user metadata fields for annotations");
10391039
for (auto f : userMetaType->fields) {
10401040
LOG3("Scanning field " << f);
1041-
auto anno = f->getAnnotations()->getSingle("field_list"_cs);
1041+
auto anno = f->getAnnotation("field_list"_cs);
10421042
if (anno == nullptr) continue;
10431043

10441044
for (auto e : anno->expr) {

backends/dpdk/dpdk.def

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ abstract DpdkAsmStatement : IDPDKNode {
3434
std::ostream& toSpec(std::ostream& out) const override;
3535
}
3636

37-
class DpdkAction {
38-
optional Annotations annotations = Annotations::empty;
37+
class DpdkAction : IAnnotated {
38+
optional inline Vector<Annotation> annotations;
3939
inline IndexedVector<DpdkAsmStatement> statements;
4040
inline ID name;
4141
inline ParameterList para;
4242
std::ostream& toSpec(std::ostream& out) const;
43+
const Vector<Annotation> &getAnnotations() const override { return annotations; }
44+
Vector<Annotation> &getAnnotations() override { return annotations; }
4345
#nodbprint
4446
}
4547

backends/dpdk/dpdkArch.cpp

+17-25
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,11 @@ bool CollectTableInfo::preorder(const IR::Key *keys) {
673673

674674
const IR::Node *InjectJumboStruct::preorder(IR::Type_Struct *s) {
675675
if (s->name == structure->local_metadata_type) {
676-
auto *annotations = new IR::Annotations({new IR::Annotation(IR::ID("__metadata__"), {})});
677-
return new IR::Type_Struct(s->name, annotations, structure->compiler_added_fields);
676+
return new IR::Type_Struct(s->name, {new IR::Annotation(IR::ID("__metadata__"), {})},
677+
structure->compiler_added_fields);
678678
} else if (s->name == structure->header_type) {
679-
auto *annotations =
680-
new IR::Annotations({new IR::Annotation(IR::ID("__packet_data__"), {})});
681-
return new IR::Type_Struct(s->name, annotations, s->fields);
679+
return new IR::Type_Struct(s->name, {new IR::Annotation(IR::ID("__packet_data__"), {})},
680+
s->fields);
682681
}
683682
return s;
684683
}
@@ -1835,13 +1834,12 @@ std::tuple<const IR::P4Table *, cstring, cstring> SplitP4TableCommon::create_mat
18351834

18361835
const IR::P4Action *SplitP4TableCommon::create_action(cstring actionName, cstring group_id,
18371836
cstring param) {
1838-
auto hidden = new IR::Annotations();
1839-
hidden->add(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
18401837
auto set_id = new IR::AssignmentStatement(new IR::PathExpression(group_id),
18411838
new IR::PathExpression(param));
18421839
auto parameter = new IR::Parameter(param, IR::Direction::None, IR::Type_Bits::get(32));
1843-
auto action = new IR::P4Action(actionName, hidden, new IR::ParameterList({parameter}),
1844-
new IR::BlockStatement({set_id}));
1840+
auto action =
1841+
new IR::P4Action(actionName, {new IR::Annotation(IR::Annotation::hiddenAnnotation, {})},
1842+
new IR::ParameterList({parameter}), new IR::BlockStatement({set_id}));
18451843
return action;
18461844
}
18471845

@@ -1856,12 +1854,11 @@ const IR::P4Table *SplitP4TableCommon::create_member_table(const IR::P4Table *tb
18561854
IR::IndexedVector<IR::Property> member_properties;
18571855
member_properties.push_back(new IR::Property("key", new IR::Key(member_keys), false));
18581856

1859-
auto hidden = new IR::Annotations();
1860-
hidden->add(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
1857+
IR::Vector<IR::Annotation> hidden(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
18611858
auto nameAnnon = tbl->getAnnotation(IR::Annotation::nameAnnotation);
18621859
cstring nameA = nameAnnon->getSingleString();
18631860
cstring memName = nameA.replace(nameA.findlast('.'), "." + memberTableName);
1864-
hidden->addAnnotation(IR::Annotation::nameAnnotation, new IR::StringLiteral(memName), false);
1861+
hidden.emplace_back(IR::Annotation::nameAnnotation, memName);
18651862

18661863
IR::IndexedVector<IR::ActionListElement> memberActionList;
18671864
for (auto action : tbl->getActionList()->actionList) memberActionList.push_back(action);
@@ -1874,10 +1871,8 @@ const IR::P4Table *SplitP4TableCommon::create_member_table(const IR::P4Table *tb
18741871
member_properties.push_back(new IR::Property(
18751872
"default_action", new IR::ExpressionValue(tbl->getDefaultAction()), false));
18761873

1877-
auto member_table =
1878-
new IR::P4Table(memberTableName, hidden, new IR::TableProperties(member_properties));
1879-
1880-
return member_table;
1874+
return new IR::P4Table(memberTableName, std::move(hidden),
1875+
new IR::TableProperties(member_properties));
18811876
}
18821877

18831878
const IR::P4Table *SplitP4TableCommon::create_group_table(const IR::P4Table *tbl,
@@ -1891,12 +1886,11 @@ const IR::P4Table *SplitP4TableCommon::create_group_table(const IR::P4Table *tbl
18911886
selector_keys.push_back(key);
18921887
}
18931888
}
1894-
auto hidden = new IR::Annotations();
1895-
hidden->add(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
1889+
IR::Vector<IR::Annotation> hidden(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
18961890
auto nameAnnon = tbl->getAnnotation(IR::Annotation::nameAnnotation);
18971891
cstring nameA = nameAnnon->getSingleString();
18981892
cstring selName = nameA.replace(nameA.findlast('.'), "." + selectorTableName);
1899-
hidden->addAnnotation(IR::Annotation::nameAnnotation, new IR::StringLiteral(selName), false);
1893+
hidden.emplace_back(IR::Annotation::nameAnnotation, selName);
19001894
IR::IndexedVector<IR::Property> selector_properties;
19011895
selector_properties.push_back(new IR::Property("selector", new IR::Key(selector_keys), false));
19021896
selector_properties.push_back(new IR::Property(
@@ -2908,8 +2902,8 @@ const IR::Node *ElimHeaderCopy::postorder(IR::Member *m) {
29082902

29092903
const IR::Node *DpdkAddPseudoHeaderDecl::preorder(IR::P4Program *program) {
29102904
if (is_all_args_header) return program;
2911-
auto *annotations = new IR::Annotations({new IR::Annotation(IR::ID("__pseudo_header__"), {})});
2912-
const IR::Type_Header *pseudo_hdr = new IR::Type_Header(pseudoHeaderTypeName, annotations);
2905+
const IR::Type_Header *pseudo_hdr = new IR::Type_Header(
2906+
pseudoHeaderTypeName, {new IR::Annotation(IR::ID("__pseudo_header__"), {})});
29132907
allTypeDecls.push_back(pseudo_hdr);
29142908
allTypeDecls.append(program->objects);
29152909
program->objects = allTypeDecls;
@@ -2922,11 +2916,9 @@ const IR::Node *DpdkAddPseudoHeaderDecl::preorder(IR::Type_Struct *st) {
29222916
bool header_found = isHeadersStruct(st);
29232917
if (header_found) {
29242918
IR::IndexedVector<IR::StructField> fields;
2925-
auto *annotations =
2926-
new IR::Annotations({new IR::Annotation(IR::ID("__pseudo_header__"), {})});
29272919
auto *type = new IR::Type_Name(new IR::Path(pseudoHeaderTypeName));
2928-
const IR::StructField *new_field1 =
2929-
new IR::StructField(pseudoHeaderInstanceName, annotations, type);
2920+
const IR::StructField *new_field1 = new IR::StructField(
2921+
pseudoHeaderInstanceName, {new IR::Annotation(IR::ID("__pseudo_header__"), {})}, type);
29302922
fields = st->fields;
29312923
fields.push_back(new_field1);
29322924
auto *st1 =

backends/dpdk/dpdkAsmOpt.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class ShortenTokenLength : public Transform {
210210
}
211211

212212
const IR::Node *preorder(IR::DpdkStructType *s) override {
213-
if (s->getAnnotations()->getSingle("__packet_data__"_cs)) {
213+
if (s->getAnnotation("__packet_data__"_cs)) {
214214
s->name = shortenString(s->name);
215215
IR::IndexedVector<IR::StructField> changedFields;
216216
for (auto field : s->fields) {

backends/dpdk/dpdkContext.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void DpdkContextGenerator::CollectTablesAndSetAttributes() {
5252
auto size = tbl->getSizeProperty();
5353
tblAttr.size = dpdk_default_table_size;
5454
if (size) tblAttr.size = size->asUnsigned();
55-
auto hidden = tbl->annotations->getSingle(IR::Annotation::hiddenAnnotation);
55+
auto hidden = tbl->getAnnotation(IR::Annotation::hiddenAnnotation);
5656
auto selector = tbl->properties->getProperty("selector");
5757
tblAttr.is_add_on_miss = false;
5858
tblAttr.idle_timeout_with_auto_delete = false;
@@ -247,10 +247,9 @@ void DpdkContextGenerator::setActionAttributes(const IR::P4Table *tbl) {
247247
bool can_be_default_action = !has_constant_default_action;
248248

249249
// First, check for action annotations
250-
auto actAnnot = action_decl->annotations;
251-
auto table_only_annot = actAnnot->getSingle(IR::Annotation::tableOnlyAnnotation);
252-
auto default_only_annot = actAnnot->getSingle(IR::Annotation::defaultOnlyAnnotation);
253-
auto hidden = actAnnot->getSingle(IR::Annotation::hiddenAnnotation);
250+
auto table_only_annot = action_decl->getAnnotation(IR::Annotation::tableOnlyAnnotation);
251+
auto default_only_annot = action_decl->getAnnotation(IR::Annotation::defaultOnlyAnnotation);
252+
auto hidden = action_decl->getAnnotation(IR::Annotation::hiddenAnnotation);
254253

255254
if (table_only_annot) {
256255
can_be_default_action = false;

backends/dpdk/dpdkMetadata.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ IR::DpdkExternDeclaration *DirectionToRegRead::addRegDeclInstance(cstring instan
6060
auto spectype = new IR::Type_Specialized(type, typeargs);
6161
auto args = new IR::Vector<IR::Argument>();
6262
args->push_back(new IR::Argument(new IR::Constant(IR::Type::Bits::get(32), 256)));
63-
auto annot = IR::Annotations::empty;
64-
annot->addAnnotationIfNew(IR::Annotation::nameAnnotation, new IR::StringLiteral(instanceName));
65-
auto decl = new IR::DpdkExternDeclaration(instanceName, annot, spectype, args, nullptr);
63+
auto decl = new IR::DpdkExternDeclaration(
64+
instanceName,
65+
{new IR::Annotation(IR::Annotation::nameAnnotation, new IR::StringLiteral(instanceName))},
66+
spectype, args, nullptr);
6667
return decl;
6768
}
6869

backends/dpdk/dpdkUtils.cpp

+5-11
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,7 @@ bool isHeadersStruct(const IR::Type_Struct *st) {
5757
if (annon) return true;
5858
return false;
5959
}
60-
bool isMetadataStruct(const IR::Type_Struct *st) {
61-
for (auto anno : st->annotations->annotations) {
62-
if (anno->name == "__metadata__"_cs) {
63-
return true;
64-
}
65-
}
66-
return false;
67-
}
60+
bool isMetadataStruct(const IR::Type_Struct *st) { return st->hasAnnotation("__metadata__"_cs); }
6861

6962
bool isMetadataField(const IR::Expression *e) {
7063
if (!e->is<IR::Member>()) return false;
@@ -139,9 +132,10 @@ IR::Declaration_Instance *createRegDeclarationInstance(cstring instanceName, int
139132
auto spectype = new IR::Type_Specialized(type, typeargs);
140133
auto args = new IR::Vector<IR::Argument>();
141134
args->push_back(new IR::Argument(new IR::Constant(IR::Type::Bits::get(32), regSize)));
142-
auto annot = IR::Annotations::empty;
143-
annot->addAnnotationIfNew(IR::Annotation::nameAnnotation, new IR::StringLiteral(instanceName));
144-
auto decl = new IR::Declaration_Instance(instanceName, annot, spectype, args, nullptr);
135+
auto decl = new IR::Declaration_Instance(
136+
instanceName,
137+
{new IR::Annotation(IR::Annotation::nameAnnotation, new IR::StringLiteral(instanceName))},
138+
spectype, args, nullptr);
145139
return decl;
146140
}
147141

backends/dpdk/spec.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "dpdkArch.h"
33
#include "dpdkAsmOpt.h"
44
#include "dpdkHelpers.h"
5+
#include "ir/annotations.h"
56
#include "ir/dbprint.h"
67
#include "printUtils.h"
78

@@ -223,7 +224,7 @@ std::ostream &IR::DpdkHeaderInstance::toSpec(std::ostream &out) const {
223224
}
224225

225226
std::ostream &IR::DpdkStructType::toSpec(std::ostream &out) const {
226-
if (getAnnotations()->getSingle("__packet_data__"_cs)) {
227+
if (getAnnotation("__packet_data__"_cs)) {
227228
for (auto it = fields.begin(); it != fields.end(); ++it) {
228229
add_comment(out, (*it)->name.toString());
229230
if (auto t = (*it)->type->to<IR::Type_Name>()) {
@@ -270,7 +271,7 @@ std::ostream &IR::DpdkStructType::toSpec(std::ostream &out) const {
270271
out << std::endl;
271272
}
272273
out << "}" << std::endl;
273-
if (getAnnotations()->getSingle("__metadata__"_cs)) {
274+
if (getAnnotation("__metadata__"_cs)) {
274275
out << "metadata instanceof " << name << std::endl;
275276
}
276277
}
@@ -414,8 +415,8 @@ std::ostream &IR::DpdkTable::toSpec(std::ostream &out) const {
414415
} else {
415416
out << "\t\t" << DPDK::toStr(action->expression);
416417
}
417-
if (action->annotations->getAnnotation("tableonly"_cs)) out << " @tableonly";
418-
if (action->annotations->getAnnotation("defaultonly"_cs)) out << " @defaultonly";
418+
if (action->hasAnnotation(IR::Annotation::tableOnlyAnnotation)) out << " @tableonly";
419+
if (action->hasAnnotation(IR::Annotation::defaultOnlyAnnotation)) out << " @defaultonly";
419420
out << std::endl;
420421
}
421422
out << "\t}" << std::endl;

backends/ebpf/target.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ class KernelSamplesTarget : public Target {
203203
class P4TCTarget : public KernelSamplesTarget {
204204
public:
205205
explicit P4TCTarget(bool emitTrace) : KernelSamplesTarget(emitTrace, "P4TC"_cs) {}
206-
cstring getByteOrderFromAnnotation(const IR::Vector<IR::Annotation> annotations) const {
207-
for (auto anno : annotations) {
206+
cstring getByteOrderFromAnnotation(const IR::Vector<IR::Annotation> &annotations) const {
207+
for (const auto *anno : annotations) {
208208
if (anno->name != "tc_type") continue;
209-
for (auto annoVal : anno->body) {
209+
for (const auto *annoVal : anno->body) {
210210
if (annoVal->text == "macaddr" || annoVal->text == "ipv4" ||
211211
annoVal->text == "ipv6" || annoVal->text == "be16" || annoVal->text == "be32" ||
212212
annoVal->text == "be64") {
@@ -223,14 +223,14 @@ class P4TCTarget : public KernelSamplesTarget {
223223
auto type = typeMap->getType(mem->expr, true);
224224
if (type->is<IR::Type_StructLike>()) {
225225
auto field = type->to<IR::Type_StructLike>()->getField(mem->member);
226-
return getByteOrderFromAnnotation(field->getAnnotations()->annotations);
226+
return getByteOrderFromAnnotation(field->getAnnotations());
227227
}
228228
} else if (action) {
229229
auto paramList = action->getParameters();
230230
if (paramList != nullptr && !paramList->empty()) {
231231
for (auto param : paramList->parameters) {
232232
if (param->name.originalName == exp->toString()) {
233-
return getByteOrderFromAnnotation(param->getAnnotations()->annotations);
233+
return getByteOrderFromAnnotation(param->getAnnotations());
234234
}
235235
}
236236
}

backends/graphs/controls.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ bool ControlGraphs::preorder(const IR::Key *key) {
254254
for (auto elVec : key->keyElements) {
255255
sstream << elVec->matchType->path->name.name << ": ";
256256
bool has_name = false;
257-
for (auto ann : elVec->annotations->annotations) {
257+
for (auto ann : elVec->annotations) {
258258
if (ann->toString() == "@name") {
259259
sstream << ann->getName();
260260
has_name = true;

0 commit comments

Comments
 (0)