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

Merged
merged 5 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Simplify annotations query
Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
  • Loading branch information
asl committed Nov 9, 2024
commit cce4c5bc19fc1bc58b483be6c289d9b91dedb582
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
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
4 changes: 2 additions & 2 deletions backends/dpdk/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,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 +270,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
20 changes: 8 additions & 12 deletions backends/tc/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,8 @@ void ConvertToBackendIR::updateConstEntries(const IR::P4Table *t, IR::TCTable *t
for (size_t itr = 0; itr < keyset->components.size(); itr++) {
auto keyElement = keys->keyElements.at(itr);
auto keyString = keyElement->expression->toString();
auto annotations = keyElement->getAnnotations();
if (annotations) {
if (auto anno = annotations->getSingle("name"_cs)) {
keyString = anno->expr.at(0)->to<IR::StringLiteral>()->value;
}
if (auto anno = keyElement->getAnnotation("name"_cs)) {
keyString = anno->expr.at(0)->to<IR::StringLiteral>()->value;
}
auto keySetElement = keyset->components.at(itr);
auto key = keySetElement->toString();
Expand Down Expand Up @@ -385,8 +382,7 @@ void ConvertToBackendIR::updateDefaultMissAction(const IR::P4Table *t, IR::TCTab
}
bool isTCMayOverrideMiss = false;
const IR::Annotation *overrideAnno =
defaultActionProperty->getAnnotations()->getSingle(
ParseTCAnnotations::tcMayOverride);
defaultActionProperty->getAnnotation(ParseTCAnnotations::tcMayOverride);
if (overrideAnno) {
isTCMayOverrideMiss = true;
}
Expand Down Expand Up @@ -876,12 +872,12 @@ safe_vector<const IR::TCKey *> ConvertToBackendIR::processExternConstructor(
for (unsigned itr = 0; itr < params->size(); itr++) {
auto parameter = params->getParameter(itr);
auto exp = decl->arguments->at(itr)->expression;
if (parameter->getAnnotations()->getSingle(ParseTCAnnotations::tc_numel)) {
if (parameter->getAnnotation(ParseTCAnnotations::tc_numel)) {
if (exp->is<IR::Constant>()) {
instance->is_num_elements = true;
instance->num_elements = exp->to<IR::Constant>()->asInt();
}
} else if (parameter->getAnnotations()->getSingle(ParseTCAnnotations::tc_init_val)) {
} else if (parameter->getAnnotation(ParseTCAnnotations::tc_init_val)) {
// TODO: Process tc_init_val.
} else {
/* If a parameter is not annoated by tc_init or tc_numel then it is emitted as
Expand Down Expand Up @@ -1022,7 +1018,7 @@ safe_vector<const IR::TCKey *> ConvertToBackendIR::HandleTypeNameStructField(
if (auto param_struct = param_val->to<IR::Type_Struct>()) {
for (auto f : param_struct->fields) {
cstring ptype = absl::StrCat("bit", f->type->width_bits());
if (auto anno = f->getAnnotations()->getSingle(ParseTCAnnotations::tcType)) {
if (auto anno = f->getAnnotation(ParseTCAnnotations::tcType)) {
auto expr = anno->expr[0];
if (auto typeLiteral = expr->to<IR::StringLiteral>()) {
ptype = typeLiteral->value;
Expand All @@ -1034,7 +1030,7 @@ safe_vector<const IR::TCKey *> ConvertToBackendIR::HandleTypeNameStructField(
}
} else {
cstring ptype = absl::StrCat("bit", param_val->width_bits());
if (auto anno = field->getAnnotations()->getSingle(ParseTCAnnotations::tcType)) {
if (auto anno = field->getAnnotation(ParseTCAnnotations::tcType)) {
auto expr = anno->expr[0];
if (auto typeLiteral = expr->to<IR::StringLiteral>()) {
ptype = typeLiteral->value;
Expand All @@ -1057,7 +1053,7 @@ bool ConvertToBackendIR::hasExecuteMethod(const IR::Type_Extern *extn) {
}
auto method = gd->getNode()->to<IR::Method>();
const IR::Annotation *execAnnotation =
method->getAnnotations()->getSingle(ParseTCAnnotations::tc_md_exec);
method->getAnnotation(ParseTCAnnotations::tc_md_exec);
if (execAnnotation) {
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1607,8 +1607,7 @@ const IR::P4Action *ControlBodyTranslatorPNA::GetAddOnMissHitAction(cstring acti
auto adecl = control->program->refMap->getDeclaration(a->getPath(), true);
auto action = adecl->getNode()->to<IR::P4Action>();
if (action->name.originalName == actionName) {
auto annotations = a->getAnnotations();
if (annotations && annotations->getSingle("defaultonly"_cs)) {
if (a->getAnnotation("defaultonly"_cs)) {
::P4::error(ErrorType::ERR_UNEXPECTED,
"add_entry hit action %1% cannot be annotated with defaultonly.",
actionName);
Expand Down
2 changes: 1 addition & 1 deletion frontends/common/constantFolding.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class DoConstantFolding : public Transform, public ResolutionContext {
const IR::Node *preorder(IR::ArrayIndex *e) override;
const IR::Node *preorder(IR::SwitchCase *c) override;
const IR::BlockStatement *preorder(IR::BlockStatement *bs) override {
if (bs->annotations->getSingle("disable_optimization"_cs)) prune();
if (bs->getAnnotation("disable_optimization"_cs)) prune();
return bs;
}
};
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4-14/fromv1.0/programStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ const IR::ParserState *ProgramStructure::convertParser(
value_sets_implemented.emplace(first->path->name);

auto type = explodeType(fieldTypes);
auto sizeAnnotation = value_set->annotations->getSingle("parser_value_set_size"_cs);
auto sizeAnnotation = value_set->getAnnotation("parser_value_set_size"_cs);
const IR::Constant *sizeConstant;
if (sizeAnnotation) {
if (sizeAnnotation->expr.size() != 1) {
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/deprecated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace P4 {

void Deprecated::warnIfDeprecated(const IR::IAnnotated *annotated, const IR::Node *errorNode) {
if (annotated == nullptr) return;
auto anno = annotated->getAnnotations()->getSingle(IR::Annotation::deprecatedAnnotation);
auto anno = annotated->getAnnotation(IR::Annotation::deprecatedAnnotation);
if (anno == nullptr) return;

std::string message;
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/reassociation.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Reassociation final : public Transform {
const IR::Node *postorder(IR::BAnd *expr) override { return reassociate(expr); }
const IR::Node *postorder(IR::BXor *expr) override { return reassociate(expr); }
const IR::BlockStatement *preorder(IR::BlockStatement *bs) override {
if (bs->annotations->getSingle("disable_optimization"_cs)) prune();
if (bs->getAnnotation("disable_optimization"_cs)) prune();
return bs;
}
};
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/simplifyDefUse.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class RemoveHidden : public Transform {
const IR::Node *postorder(IR::BlockStatement *stat) override {
if (!stat->components.empty()) return stat;
if (stat->annotations->size() != 1) return stat;
auto anno = stat->annotations->getSingle(IR::Annotation::hiddenAnnotation);
auto anno = stat->getAnnotation(IR::Annotation::hiddenAnnotation);
if (!anno) return stat;
// Lose the annotation.
return new IR::BlockStatement(stat->srcInfo);
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/strengthReduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class DoStrengthReduction final : public Transform {
const IR::Node *postorder(IR::ArrayIndex *expr) override;

const IR::BlockStatement *preorder(IR::BlockStatement *bs) override {
if (bs->annotations->getSingle("disable_optimization"_cs)) prune();
if (bs->getAnnotation("disable_optimization"_cs)) prune();
return bs;
}
};
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/typeChecking/typeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ bool TypeInferenceBase::checkAbstractMethods(const IR::Declaration_Instance *ins
}
bool rv = true;
for (const auto &vm : virt) {
if (!vm.second->annotations->getSingle("optional"_cs)) {
if (!vm.second->getAnnotation("optional"_cs)) {
typeError("%1%: %2% abstract method not implemented", inst, vm.second);
rv = false;
}
Expand Down
4 changes: 3 additions & 1 deletion ir/base.def
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ class Annotations {
interface IAnnotated {
virtual Annotations getAnnotations() const = 0;
Annotation getAnnotation(cstring name) const override {
return getAnnotations()->getSingle(name); }
auto annotations = getAnnotations();
return annotations ? annotations->getSingle(name) : nullptr;
}
}

interface IInstance {
Expand Down