Skip to content

Commit

Permalink
Try to unbreak Tofino backend due to recent annotations changes (#5015)
Browse files Browse the repository at this point in the history
* Fix annotations usage in Tofino frontend bits

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Try to unbreak Tofino backend due to recent annotations changes

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Simplify

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Update error message

Co-authored-by: Fabian Ruffy <5960321+fruffy@users.noreply.github.com>
Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

---------

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
Co-authored-by: Fabian Ruffy <5960321+fruffy@users.noreply.github.com>
  • Loading branch information
asl and fruffy authored Nov 13, 2024
1 parent dbe0f7e commit ddd368c
Show file tree
Hide file tree
Showing 78 changed files with 438 additions and 481 deletions.
5 changes: 2 additions & 3 deletions backends/tofino/bf-p4c/arch/add_t2na_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ void AddT2naMeta::postorder(IR::Type_StructLike *typeStructLike) {
for (const auto *structField : typeStructLike->fields) {
lastStructField = structField;
};
if (lastStructField && lastStructField->annotations->getSingle("padding"_cs)) {
if (lastStructField && lastStructField->getAnnotation("padding"_cs)) {
LOG3("AddT2naMeta : " << typeStructLikeName << " already complete");
return;
}
LOG3("AddT2naMeta : Adding missing fields to " << typeStructLikeName);
typeStructLike->fields.push_back(new IR::StructField(
"_pad_eg_int_md", new IR::Annotations({new IR::Annotation("padding", {})}),
IR::Type::Bits::get(8)));
"_pad_eg_int_md", {new IR::Annotation("padding", {})}, IR::Type::Bits::get(8)));
}
}

Expand Down
38 changes: 19 additions & 19 deletions backends/tofino/bf-p4c/arch/arch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,23 +213,25 @@ void ParseTna::parseSingleParserPipeline(const IR::PackageBlock *block, unsigned
}

void ParseTna::parsePortMapAnnotation(const IR::PackageBlock *block, DefaultPortMap &map) {
if (auto anno = block->node->getAnnotation("default_portmap"_cs)) {
int index = 0;
for (auto expr : anno->expr) {
std::vector<int> ports;
if (auto cst = expr->to<IR::Constant>()) {
ports.push_back(cst->asInt());
} else if (auto list = expr->to<IR::ListExpression>()) {
for (auto expr : list->components) {
ports.push_back(expr->to<IR::Constant>()->asInt());
}
} else if (auto list = expr->to<IR::StructExpression>()) {
for (auto expr : list->components) {
ports.push_back(expr->expression->to<IR::Constant>()->asInt());
if (auto annot = block->node->to<IR::IAnnotated>()) {
if (auto anno = annot->getAnnotation("default_portmap"_cs)) {
int index = 0;
for (auto expr : anno->expr) {
std::vector<int> ports;
if (auto cst = expr->to<IR::Constant>()) {
ports.push_back(cst->asInt());
} else if (auto list = expr->to<IR::ListExpression>()) {
for (auto expr : list->components) {
ports.push_back(expr->to<IR::Constant>()->asInt());
}
} else if (auto list = expr->to<IR::StructExpression>()) {
for (auto expr : list->components) {
ports.push_back(expr->expression->to<IR::Constant>()->asInt());
}
}
map[index] = ports;
index++;
}
map[index] = ports;
index++;
}
}
}
Expand Down Expand Up @@ -397,10 +399,8 @@ void add_param(ordered_map<cstring, cstring> &tnaParams, const IR::ParameterList
tnaParams.emplace(hdr, param->name);
} else {
// add optional parameter to parser and control type
auto *annotations = new IR::Annotations();
annotations->annotations.push_back(new IR::Annotation("optional"_cs, {}));
newParams->push_back(
new IR::Parameter(IR::ID(hdr), annotations, dir, new IR::Type_Name(IR::ID(hdr_type))));
newParams->push_back(new IR::Parameter(IR::ID(hdr), {new IR::Annotation("optional"_cs, {})},
dir, new IR::Type_Name(IR::ID(hdr_type))));
tnaParams.emplace(hdr, hdr);
}
}
Expand Down
10 changes: 5 additions & 5 deletions backends/tofino/bf-p4c/arch/bridge_metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ struct BridgeIngressToEgress : public Transform {
LOG3("\tNumber of fields to bridge: " << fieldsToBridge.size());
for (auto *f : structFields) LOG3("\t Bridged field: " << f->name);
}
auto annot = new IR::Annotations({new IR::Annotation(IR::ID("flexible"), {})});
auto bridgedMetaType = new IR::Type_Struct("fields", annot, structFields);
auto bridgedMetaType = new IR::Type_Struct(
"fields", {new IR::Annotation(IR::ID("flexible"), {})}, structFields);

fields.push_back(new IR::StructField(BRIDGED_MD_FIELD, annot, bridgedMetaType));
fields.push_back(new IR::StructField(
BRIDGED_MD_FIELD, {new IR::Annotation(IR::ID("flexible"), {})}, bridgedMetaType));
}

auto *layoutKind = new IR::StringLiteral(IR::ID("BRIDGED"));
bridgedHeaderType = new IR::Type_Header(
BRIDGED_MD_HEADER,
new IR::Annotations({new IR::Annotation(IR::ID("layout"), {layoutKind})}), fields);
BRIDGED_MD_HEADER, {new IR::Annotation(IR::ID("layout"), {layoutKind})}, fields);
LOG1("Bridged header type: " << bridgedHeaderType);

// We'll inject a field containing the new header into the user metadata
Expand Down
8 changes: 3 additions & 5 deletions backends/tofino/bf-p4c/arch/fromv1.0/checksum.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ static std::vector<gress_t> getChecksumUpdateLocations(const IR::MethodCallExpre
else
BUG("Invalid use of function getChecksumUpdateLocation");

for (auto annot : block->annotations->annotations) {
for (auto annot : block->annotations) {
if (annot->name.name == pragma) {
auto &exprs = annot->expr;
auto gress = exprs[0]->to<IR::StringLiteral>();
Expand Down Expand Up @@ -452,13 +452,11 @@ class InsertParserChecksums : public Inspector {

if (!translate->residualChecksums.count(destfield)) {
auto *compilerMetadataPath = new IR::PathExpression(COMPILER_META);
auto *cgAnnotation =
new IR::Annotations({new IR::Annotation(IR::ID("__compiler_generated"), {})});

auto *compilerMetadataDecl = const_cast<IR::Type_Struct *>(
structure->type_declarations.at("compiler_generated_metadata_t"_cs)
->to<IR::Type_Struct>());
compilerMetadataDecl->annotations = cgAnnotation;
compilerMetadataDecl->annotations = {
new IR::Annotation(IR::ID("__compiler_generated"), {})};

std::stringstream residualFieldName;
residualFieldName << "residual_checksum_";
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/arch/fromv1.0/field_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const IR::Node *P4V1::FieldListConverter::convertFieldList(const IR::Node *node)

std::set<cstring> sliced_fields;
std::map<cstring, std::pair<int, int>> field_slices;
for (auto anno : fl->annotations->annotations) {
for (auto anno : fl->annotations) {
if (anno->name == pragma_string) {
if (anno->expr.size() != 3) error("Invalid pragma specification -- ", pragma_string);

Expand Down
7 changes: 4 additions & 3 deletions backends/tofino/bf-p4c/arch/fromv1.0/lpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,19 @@ const IR::Declaration_Instance *P4V1::LpfConverter::convertExternInstance(
if (direct && instance_count) error("lpf %s specifies both 'direct' and 'instance_count'", ext);

auto *externalName = new IR::StringLiteral(IR::ID("." + name));
auto *annotations = new IR::Annotations({new IR::Annotation(IR::ID("name"), {externalName})});
auto args = new IR::Vector<IR::Argument>();
if (instance_count) {
args->push_back(new IR::Argument(instance_count));
auto type = new IR::Type_Specialized(
new IR::Type_Name("Lpf"),
new IR::Vector<IR::Type>({filt_type, IR::Type::Bits::get(32)}));
return new IR::Declaration_Instance(ext->srcInfo, name, annotations, type, args);
return new IR::Declaration_Instance(
ext->srcInfo, name, {new IR::Annotation(IR::ID("name"), {externalName})}, type, args);
} else {
auto type = new IR::Type_Specialized(new IR::Type_Name("DirectLpf"),
new IR::Vector<IR::Type>({filt_type}));
return new IR::Declaration_Instance(ext->srcInfo, name, annotations, type, args);
return new IR::Declaration_Instance(
ext->srcInfo, name, {new IR::Annotation(IR::ID("name"), {externalName})}, type, args);
}
}

Expand Down
17 changes: 9 additions & 8 deletions backends/tofino/bf-p4c/arch/fromv1.0/meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "meter.h"

#include "ir/annotations.h"
#include "programStructure.h"

P4V1::MeterConverter::MeterConverter() { addConverter("meter"_cs, this); }
Expand All @@ -38,7 +39,7 @@ const IR::Declaration_Instance *P4V1::MeterConverter::convertExternInstance(
const IR::Expression *instance_count = nullptr;
const IR::Expression *table = nullptr;
auto *externalName = new IR::StringLiteral(IR::ID("." + name));
auto *annotations = new IR::Annotations({new IR::Annotation(IR::ID("name"), {externalName})});
IR::Vector<IR::Annotation> annotations{new IR::Annotation(IR::ID("name"), {externalName})};
bool direct = false;
LOG1("ext : " << ext);
for (auto prop : Values(ext->properties)) {
Expand All @@ -65,22 +66,22 @@ const IR::Declaration_Instance *P4V1::MeterConverter::convertExternInstance(
} else if (prop->name == "instance_count") {
instance_count = val;
} else if (prop->name == "green_value") {
annotations->addAnnotation("green"_cs, val);
IR::Annotations::addOrReplace(annotations, "green"_cs, val);
} else if (prop->name == "yellow_value") {
annotations->addAnnotation("yellow"_cs, val);
IR::Annotations::addOrReplace(annotations, "yellow"_cs, val);
} else if (prop->name == "red_value") {
annotations->addAnnotation("red"_cs, val);
IR::Annotations::addOrReplace(annotations, "red"_cs, val);
} else if (prop->name == "meter_sweep_interval") {
auto ival = val->to<IR::Constant>()->asInt();
BUG_CHECK(ival >= 0 || ival <= 4,
"Meter sweep interval value is %d must be in the range [0:4] - %s", ival,
ext);
annotations->addAnnotation("meter_sweep_interval"_cs, val);
IR::Annotations::addOrReplace(annotations, "meter_sweep_interval"_cs, val);
} else if (prop->name == "meter_profile") {
auto pval = val->to<IR::Constant>()->asInt();
BUG_CHECK(pval >= 0 || pval <= 15,
"Meter profile value is %d must be in the range [0:15] - %s", pval, ext);
annotations->addAnnotation("meter_profile"_cs, val);
IR::Annotations::addOrReplace(annotations, "meter_profile"_cs, val);
} else {
error("Unknown property %s on meter", prop);
continue;
Expand All @@ -96,11 +97,11 @@ const IR::Declaration_Instance *P4V1::MeterConverter::convertExternInstance(
args->push_back(new IR::Argument(meter_type));
auto type = new IR::Type_Specialized(new IR::Type_Name("Meter"),
new IR::Vector<IR::Type>({IR::Type::Bits::get(32)}));
return new IR::Declaration_Instance(ext->srcInfo, name, annotations, type, args);
return new IR::Declaration_Instance(ext->srcInfo, name, std::move(annotations), type, args);
} else {
args->push_back(new IR::Argument(meter_type));
auto type = new IR::Type_Name("DirectMeter");
return new IR::Declaration_Instance(ext->srcInfo, name, annotations, type, args);
return new IR::Declaration_Instance(ext->srcInfo, name, std::move(annotations), type, args);
}
}

Expand Down
5 changes: 3 additions & 2 deletions backends/tofino/bf-p4c/arch/fromv1.0/mirror.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "frontends/p4/cloner.h"
#include "frontends/p4/coreLibrary.h"
#include "frontends/p4/methodInstance.h"
#include "ir/annotations.h"

namespace BFN {
namespace {
Expand Down Expand Up @@ -250,8 +251,8 @@ class AddMirroredFieldListParser : public Transform {
auto select = new IR::PathExpression(IR::ID(nextState));
auto newStateName = IR::ID(cstring("__") + name);
auto *newState = new IR::ParserState(newStateName, *statements, select);
newState->annotations = newState->annotations->addAnnotationIfNew(
IR::Annotation::nameAnnotation, new IR::StringLiteral(cstring(cstring("$") + name)));
newState->addAnnotationIfNew(IR::Annotation::nameAnnotation,
new IR::StringLiteral(cstring("$" + name)));
return newState;
}

Expand Down
25 changes: 11 additions & 14 deletions backends/tofino/bf-p4c/arch/fromv1.0/phase0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "frontends/p4/coreLibrary.h"
#include "frontends/p4/typeChecking/typeChecker.h"
#include "frontends/p4/typeMap.h"
#include "ir/annotations.h"
#include "ir/ir.h"
#include "lib/cstring.h"
#include "lib/indent.h"
Expand Down Expand Up @@ -162,8 +163,7 @@ struct FindPhase0Table : public Inspector {
// Pragma value = 1 => Table must be implemented only in parser
// Pragma value = 0 => Table must be implemented only in MAU
bool phase0PragmaSet = false;
auto annot = table->getAnnotations();
if (auto s = annot->getSingle("phase0"_cs)) {
if (auto s = table->getAnnotation("phase0"_cs)) {
auto pragma_val = s->expr.at(0)->to<IR::Constant>();
ERROR_CHECK(
(pragma_val != nullptr),
Expand Down Expand Up @@ -520,8 +520,7 @@ struct FindPhase0Table : public Inspector {
padFieldName += cstring::to_cstring(padFieldId++);
auto *padFieldType = IR::Type::Bits::get(packedField.width);
fields.push_back(new IR::StructField(
padFieldName, new IR::Annotations({new IR::Annotation(IR::ID("padding"), {})}),
padFieldType));
padFieldName, {new IR::Annotation(IR::ID("padding"), {})}, padFieldType));
continue;
}

Expand Down Expand Up @@ -678,11 +677,11 @@ struct RewritePhase0IfPresent : public Transform {
}

LOG4("Add phase0 annotation: " << phase0->table->name);
state->annotations = state->annotations->addAnnotation(
"override_phase0_table_name"_cs, new IR::StringLiteral(phase0->table->name));
state->addOrReplaceAnnotation("override_phase0_table_name"_cs,
new IR::StringLiteral(phase0->table->name));

state->annotations = state->annotations->addAnnotation(
"override_phase0_action_name"_cs, new IR::StringLiteral(phase0->actionName));
state->addOrReplaceAnnotation("override_phase0_action_name"_cs,
new IR::StringLiteral(phase0->actionName));

return state;
}
Expand Down Expand Up @@ -732,16 +731,15 @@ bool CheckPhaseZeroExtern::preorder(const IR::MethodCallExpression *expr) {
}

bool CollectPhase0Annotation::preorder(const IR::ParserState *state) {
auto annot = state->getAnnotations();
if (auto ann = annot->getSingle("override_phase0_table_name"_cs)) {
if (auto ann = state->getAnnotation("override_phase0_table_name"_cs)) {
if (auto phase0 = ann->expr.at(0)->to<IR::StringLiteral>()) {
auto parser = findOrigCtxt<IR::P4Parser>();
if (!parser) return false;
auto name = parser->externalName();
phase0_name_annot->emplace(name, phase0->value);
}
}
if (auto ann = annot->getSingle("override_phase0_action_name"_cs)) {
if (auto ann = state->getAnnotation("override_phase0_action_name"_cs)) {
if (auto phase0 = ann->expr.at(0)->to<IR::StringLiteral>()) {
auto parser = findOrigCtxt<IR::P4Parser>();
if (!parser) return false;
Expand All @@ -767,7 +765,7 @@ IR::IndexedVector<IR::StructField> *UpdatePhase0NodeInParser::canPackDataIntoPha
// TODO: Once phase0 node is properly supported in the
// backend, we won't need this (or any padding), so we should remove
// it at that point.
if (param->annotations->getSingle("padding"_cs)) continue;
if (param->getAnnotation("padding"_cs)) continue;
const int fieldSize = param->type->width_bits();
const int alignment = getAlignment(fieldSize);
bool is_pad_field = param->getAnnotation("padding"_cs);
Expand Down Expand Up @@ -807,8 +805,7 @@ IR::IndexedVector<IR::StructField> *UpdatePhase0NodeInParser::canPackDataIntoPha
padFieldName += cstring::to_cstring(padFieldId++);
auto *padFieldType = IR::Type::Bits::get(packedField.width);
packFields->push_back(new IR::StructField(
padFieldName, new IR::Annotations({new IR::Annotation(IR::ID("padding"), {})}),
padFieldType));
padFieldName, {new IR::Annotation(IR::ID("padding"), {})}, padFieldType));
continue;
}

Expand Down
4 changes: 1 addition & 3 deletions backends/tofino/bf-p4c/arch/fromv1.0/primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,7 @@ CONVERT_PRIMITIVE(modify_field_with_hash_based_offset, 1) {
auto mc = new IR::MethodCallExpression(primitive->srcInfo, hash, args);
auto result = new IR::MethodCallStatement(primitive->srcInfo, mc);

auto annotations = new IR::Annotations();
for (auto annot : fl->annotations->annotations) annotations->annotations.push_back(annot);
auto block = new IR::BlockStatement(annotations);
auto block = new IR::BlockStatement(fl->annotations);
block->components.push_back(result);

return block;
Expand Down
Loading

0 comments on commit ddd368c

Please sign in to comment.