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

Remove IR::Annotations and make IAnnotated to carry annotations inline #4992

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
7 changes: 4 additions & 3 deletions backends/bmv2/common/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "frontends/common/model.h"
#include "frontends/p4/coreLibrary.h"
#include "helpers.h"
#include "ir/annotations.h"
#include "ir/ir.h"
#include "lib/cstring.h"
#include "lib/error.h"
Expand Down Expand Up @@ -167,10 +168,10 @@ class RenameUserMetadata : public Transform {
else
suffix = "."_cs + f->name;
cstring newName = namePrefix + suffix;
auto stringLit = new IR::StringLiteral(newName);
LOG2("Renaming " << f << " to " << newName);
auto annos = f->annotations->addOrReplace(IR::Annotation::nameAnnotation, stringLit);
auto field = new IR::StructField(f->srcInfo, f->name, annos, f->type);
auto field = new IR::StructField(
f->srcInfo, f->name, IR::Annotations::setNameAnnotation(newName, f->annotations),
f->type);
fields.push_back(field);
}

Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/simple_switch/simpleSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ void SimpleSwitchBackend::createRecirculateFieldsList(ConversionContext *ctxt,
LOG2("Scanning user metadata fields for annotations");
for (auto f : userMetaType->fields) {
LOG3("Scanning field " << f);
auto anno = f->getAnnotations()->getSingle("field_list"_cs);
auto anno = f->getAnnotation("field_list"_cs);
if (anno == nullptr) continue;

for (auto e : anno->expr) {
Expand Down
6 changes: 4 additions & 2 deletions backends/dpdk/dpdk.def
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ abstract DpdkAsmStatement : IDPDKNode {
std::ostream& toSpec(std::ostream& out) const override;
}

class DpdkAction {
optional Annotations annotations = Annotations::empty;
class DpdkAction : IAnnotated {
optional inline Vector<Annotation> annotations;
inline IndexedVector<DpdkAsmStatement> statements;
inline ID name;
inline ParameterList para;
std::ostream& toSpec(std::ostream& out) const;
const Vector<Annotation> &getAnnotations() const override { return annotations; }
Vector<Annotation> &getAnnotations() override { return annotations; }
#nodbprint
}

Expand Down
42 changes: 17 additions & 25 deletions backends/dpdk/dpdkArch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,12 +673,11 @@ bool CollectTableInfo::preorder(const IR::Key *keys) {

const IR::Node *InjectJumboStruct::preorder(IR::Type_Struct *s) {
if (s->name == structure->local_metadata_type) {
auto *annotations = new IR::Annotations({new IR::Annotation(IR::ID("__metadata__"), {})});
return new IR::Type_Struct(s->name, annotations, structure->compiler_added_fields);
return new IR::Type_Struct(s->name, {new IR::Annotation(IR::ID("__metadata__"), {})},
structure->compiler_added_fields);
} else if (s->name == structure->header_type) {
auto *annotations =
new IR::Annotations({new IR::Annotation(IR::ID("__packet_data__"), {})});
return new IR::Type_Struct(s->name, annotations, s->fields);
return new IR::Type_Struct(s->name, {new IR::Annotation(IR::ID("__packet_data__"), {})},
s->fields);
}
return s;
}
Expand Down Expand Up @@ -1835,13 +1834,12 @@ std::tuple<const IR::P4Table *, cstring, cstring> SplitP4TableCommon::create_mat

const IR::P4Action *SplitP4TableCommon::create_action(cstring actionName, cstring group_id,
cstring param) {
auto hidden = new IR::Annotations();
hidden->add(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
auto set_id = new IR::AssignmentStatement(new IR::PathExpression(group_id),
new IR::PathExpression(param));
auto parameter = new IR::Parameter(param, IR::Direction::None, IR::Type_Bits::get(32));
auto action = new IR::P4Action(actionName, hidden, new IR::ParameterList({parameter}),
new IR::BlockStatement({set_id}));
auto action =
new IR::P4Action(actionName, {new IR::Annotation(IR::Annotation::hiddenAnnotation, {})},
new IR::ParameterList({parameter}), new IR::BlockStatement({set_id}));
return action;
}

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

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

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

auto member_table =
new IR::P4Table(memberTableName, hidden, new IR::TableProperties(member_properties));

return member_table;
return new IR::P4Table(memberTableName, std::move(hidden),
new IR::TableProperties(member_properties));
}

const IR::P4Table *SplitP4TableCommon::create_group_table(const IR::P4Table *tbl,
Expand All @@ -1891,12 +1886,11 @@ const IR::P4Table *SplitP4TableCommon::create_group_table(const IR::P4Table *tbl
selector_keys.push_back(key);
}
}
auto hidden = new IR::Annotations();
hidden->add(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
IR::Vector<IR::Annotation> hidden(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
auto nameAnnon = tbl->getAnnotation(IR::Annotation::nameAnnotation);
cstring nameA = nameAnnon->getSingleString();
cstring selName = nameA.replace(nameA.findlast('.'), "." + selectorTableName);
hidden->addAnnotation(IR::Annotation::nameAnnotation, new IR::StringLiteral(selName), false);
hidden.emplace_back(IR::Annotation::nameAnnotation, selName);
IR::IndexedVector<IR::Property> selector_properties;
selector_properties.push_back(new IR::Property("selector", new IR::Key(selector_keys), false));
selector_properties.push_back(new IR::Property(
Expand Down Expand Up @@ -2908,8 +2902,8 @@ const IR::Node *ElimHeaderCopy::postorder(IR::Member *m) {

const IR::Node *DpdkAddPseudoHeaderDecl::preorder(IR::P4Program *program) {
if (is_all_args_header) return program;
auto *annotations = new IR::Annotations({new IR::Annotation(IR::ID("__pseudo_header__"), {})});
const IR::Type_Header *pseudo_hdr = new IR::Type_Header(pseudoHeaderTypeName, annotations);
const IR::Type_Header *pseudo_hdr = new IR::Type_Header(
pseudoHeaderTypeName, {new IR::Annotation(IR::ID("__pseudo_header__"), {})});
allTypeDecls.push_back(pseudo_hdr);
allTypeDecls.append(program->objects);
program->objects = allTypeDecls;
Expand All @@ -2922,11 +2916,9 @@ const IR::Node *DpdkAddPseudoHeaderDecl::preorder(IR::Type_Struct *st) {
bool header_found = isHeadersStruct(st);
if (header_found) {
IR::IndexedVector<IR::StructField> fields;
auto *annotations =
new IR::Annotations({new IR::Annotation(IR::ID("__pseudo_header__"), {})});
auto *type = new IR::Type_Name(new IR::Path(pseudoHeaderTypeName));
const IR::StructField *new_field1 =
new IR::StructField(pseudoHeaderInstanceName, annotations, type);
const IR::StructField *new_field1 = new IR::StructField(
pseudoHeaderInstanceName, {new IR::Annotation(IR::ID("__pseudo_header__"), {})}, type);
fields = st->fields;
fields.push_back(new_field1);
auto *st1 =
Expand Down
2 changes: 1 addition & 1 deletion backends/dpdk/dpdkAsmOpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class ShortenTokenLength : public Transform {
}

const IR::Node *preorder(IR::DpdkStructType *s) override {
if (s->getAnnotations()->getSingle("__packet_data__"_cs)) {
if (s->getAnnotation("__packet_data__"_cs)) {
s->name = shortenString(s->name);
IR::IndexedVector<IR::StructField> changedFields;
for (auto field : s->fields) {
Expand Down
9 changes: 4 additions & 5 deletions backends/dpdk/dpdkContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void DpdkContextGenerator::CollectTablesAndSetAttributes() {
auto size = tbl->getSizeProperty();
tblAttr.size = dpdk_default_table_size;
if (size) tblAttr.size = size->asUnsigned();
auto hidden = tbl->annotations->getSingle(IR::Annotation::hiddenAnnotation);
auto hidden = tbl->getAnnotation(IR::Annotation::hiddenAnnotation);
auto selector = tbl->properties->getProperty("selector");
tblAttr.is_add_on_miss = false;
tblAttr.idle_timeout_with_auto_delete = false;
Expand Down Expand Up @@ -247,10 +247,9 @@ void DpdkContextGenerator::setActionAttributes(const IR::P4Table *tbl) {
bool can_be_default_action = !has_constant_default_action;

// First, check for action annotations
auto actAnnot = action_decl->annotations;
auto table_only_annot = actAnnot->getSingle(IR::Annotation::tableOnlyAnnotation);
auto default_only_annot = actAnnot->getSingle(IR::Annotation::defaultOnlyAnnotation);
auto hidden = actAnnot->getSingle(IR::Annotation::hiddenAnnotation);
auto table_only_annot = action_decl->getAnnotation(IR::Annotation::tableOnlyAnnotation);
auto default_only_annot = action_decl->getAnnotation(IR::Annotation::defaultOnlyAnnotation);
auto hidden = action_decl->getAnnotation(IR::Annotation::hiddenAnnotation);

if (table_only_annot) {
can_be_default_action = false;
Expand Down
7 changes: 4 additions & 3 deletions backends/dpdk/dpdkMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ IR::DpdkExternDeclaration *DirectionToRegRead::addRegDeclInstance(cstring instan
auto spectype = new IR::Type_Specialized(type, typeargs);
auto args = new IR::Vector<IR::Argument>();
args->push_back(new IR::Argument(new IR::Constant(IR::Type::Bits::get(32), 256)));
auto annot = IR::Annotations::empty;
annot->addAnnotationIfNew(IR::Annotation::nameAnnotation, new IR::StringLiteral(instanceName));
auto decl = new IR::DpdkExternDeclaration(instanceName, annot, spectype, args, nullptr);
auto decl = new IR::DpdkExternDeclaration(
instanceName,
{new IR::Annotation(IR::Annotation::nameAnnotation, new IR::StringLiteral(instanceName))},
spectype, args, nullptr);
return decl;
}

Expand Down
16 changes: 5 additions & 11 deletions backends/dpdk/dpdkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@ bool isHeadersStruct(const IR::Type_Struct *st) {
if (annon) return true;
return false;
}
bool isMetadataStruct(const IR::Type_Struct *st) {
for (auto anno : st->annotations->annotations) {
if (anno->name == "__metadata__"_cs) {
return true;
}
}
return false;
}
bool isMetadataStruct(const IR::Type_Struct *st) { return st->hasAnnotation("__metadata__"_cs); }

bool isMetadataField(const IR::Expression *e) {
if (!e->is<IR::Member>()) return false;
Expand Down Expand Up @@ -139,9 +132,10 @@ IR::Declaration_Instance *createRegDeclarationInstance(cstring instanceName, int
auto spectype = new IR::Type_Specialized(type, typeargs);
auto args = new IR::Vector<IR::Argument>();
args->push_back(new IR::Argument(new IR::Constant(IR::Type::Bits::get(32), regSize)));
auto annot = IR::Annotations::empty;
annot->addAnnotationIfNew(IR::Annotation::nameAnnotation, new IR::StringLiteral(instanceName));
auto decl = new IR::Declaration_Instance(instanceName, annot, spectype, args, nullptr);
auto decl = new IR::Declaration_Instance(
instanceName,
{new IR::Annotation(IR::Annotation::nameAnnotation, new IR::StringLiteral(instanceName))},
spectype, args, nullptr);
return decl;
}

Expand Down
9 changes: 5 additions & 4 deletions backends/dpdk/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "dpdkArch.h"
#include "dpdkAsmOpt.h"
#include "dpdkHelpers.h"
#include "ir/annotations.h"
#include "ir/dbprint.h"
#include "printUtils.h"

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

std::ostream &IR::DpdkStructType::toSpec(std::ostream &out) const {
if (getAnnotations()->getSingle("__packet_data__"_cs)) {
if (getAnnotation("__packet_data__"_cs)) {
for (auto it = fields.begin(); it != fields.end(); ++it) {
add_comment(out, (*it)->name.toString());
if (auto t = (*it)->type->to<IR::Type_Name>()) {
Expand Down Expand Up @@ -270,7 +271,7 @@ std::ostream &IR::DpdkStructType::toSpec(std::ostream &out) const {
out << std::endl;
}
out << "}" << std::endl;
if (getAnnotations()->getSingle("__metadata__"_cs)) {
if (getAnnotation("__metadata__"_cs)) {
out << "metadata instanceof " << name << std::endl;
}
}
Expand Down Expand Up @@ -414,8 +415,8 @@ std::ostream &IR::DpdkTable::toSpec(std::ostream &out) const {
} else {
out << "\t\t" << DPDK::toStr(action->expression);
}
if (action->annotations->getAnnotation("tableonly"_cs)) out << " @tableonly";
if (action->annotations->getAnnotation("defaultonly"_cs)) out << " @defaultonly";
if (action->hasAnnotation(IR::Annotation::tableOnlyAnnotation)) out << " @tableonly";
if (action->hasAnnotation(IR::Annotation::defaultOnlyAnnotation)) out << " @defaultonly";
out << std::endl;
}
out << "\t}" << std::endl;
Expand Down
10 changes: 5 additions & 5 deletions backends/ebpf/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ class KernelSamplesTarget : public Target {
class P4TCTarget : public KernelSamplesTarget {
public:
explicit P4TCTarget(bool emitTrace) : KernelSamplesTarget(emitTrace, "P4TC"_cs) {}
cstring getByteOrderFromAnnotation(const IR::Vector<IR::Annotation> annotations) const {
for (auto anno : annotations) {
cstring getByteOrderFromAnnotation(const IR::Vector<IR::Annotation> &annotations) const {
for (const auto *anno : annotations) {
if (anno->name != "tc_type") continue;
for (auto annoVal : anno->body) {
for (const auto *annoVal : anno->body) {
if (annoVal->text == "macaddr" || annoVal->text == "ipv4" ||
annoVal->text == "ipv6" || annoVal->text == "be16" || annoVal->text == "be32" ||
annoVal->text == "be64") {
Expand All @@ -223,14 +223,14 @@ class P4TCTarget : public KernelSamplesTarget {
auto type = typeMap->getType(mem->expr, true);
if (type->is<IR::Type_StructLike>()) {
auto field = type->to<IR::Type_StructLike>()->getField(mem->member);
return getByteOrderFromAnnotation(field->getAnnotations()->annotations);
return getByteOrderFromAnnotation(field->getAnnotations());
}
} else if (action) {
auto paramList = action->getParameters();
if (paramList != nullptr && !paramList->empty()) {
for (auto param : paramList->parameters) {
if (param->name.originalName == exp->toString()) {
return getByteOrderFromAnnotation(param->getAnnotations()->annotations);
return getByteOrderFromAnnotation(param->getAnnotations());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion backends/graphs/controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ bool ControlGraphs::preorder(const IR::Key *key) {
for (auto elVec : key->keyElements) {
sstream << elVec->matchType->path->name.name << ": ";
bool has_name = false;
for (auto ann : elVec->annotations->annotations) {
for (auto ann : elVec->annotations) {
if (ann->toString() == "@name") {
sstream << ann->getName();
has_name = true;
Expand Down
Loading
Loading