Skip to content

Commit

Permalink
Remove IR::Annotations and make IAnnotated to carry annotations inline.
Browse files Browse the repository at this point in the history
Lots of cleanups and fixes here and there

Signed-off-by: Anton Korobeynikov <[email protected]>
  • Loading branch information
asl committed Nov 6, 2024
1 parent cb2d2a0 commit 4a82acc
Show file tree
Hide file tree
Showing 96 changed files with 839 additions and 1,025 deletions.
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
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
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
5 changes: 3 additions & 2 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 @@ -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

0 comments on commit 4a82acc

Please sign in to comment.