8#ifndef CONTROL_PLANE_P4RUNTIMEARCHHANDLER_H_
9#define CONTROL_PLANE_P4RUNTIMEARCHHANDLER_H_
11#pragma GCC diagnostic push
12#pragma GCC diagnostic ignored "-Wunused-parameter"
13#pragma GCC diagnostic ignored "-Wpedantic"
14#include <google/protobuf/util/json_util.h>
15#pragma GCC diagnostic pop
20#pragma GCC diagnostic push
21#pragma GCC diagnostic ignored "-Wunused-parameter"
22#pragma GCC diagnostic ignored "-Wpedantic"
23#include "p4/config/v1/p4info.pb.h"
24#include "p4/v1/p4runtime.pb.h"
25#pragma GCC diagnostic pop
27#include "frontends/common/resolveReferences/referenceMap.h"
28#include "frontends/p4/externInstance.h"
29#include "frontends/p4/methodInstance.h"
30#include "frontends/p4/typeMap.h"
32#include "lib/cstring.h"
33#include "lib/ordered_set.h"
34#include "typeSpecConverter.h"
45using p4rt_id_t = uint32_t;
54class P4RuntimeSymbolType {
56 virtual ~P4RuntimeSymbolType() {}
61 explicit operator p4rt_id_t()
const {
return id; }
66 static P4RuntimeSymbolType P4RT_TABLE() {
67 return P4RuntimeSymbolType(::p4::config::v1::P4Ids::TABLE);
69 static P4RuntimeSymbolType P4RT_VALUE_SET() {
70 return P4RuntimeSymbolType(::p4::config::v1::P4Ids::VALUE_SET);
72 static P4RuntimeSymbolType P4RT_CONTROLLER_HEADER() {
73 return P4RuntimeSymbolType(::p4::config::v1::P4Ids::CONTROLLER_HEADER);
76 static P4RuntimeSymbolType P4RT_OTHER_EXTERNS_START() {
77 return P4RuntimeSymbolType(::p4::config::v1::P4Ids::OTHER_EXTERNS_START);
80 bool operator==(
const P4RuntimeSymbolType &other)
const {
return id == other.id; }
82 bool operator!=(
const P4RuntimeSymbolType &other)
const {
return !(*
this == other); }
84 bool operator<(
const P4RuntimeSymbolType &other)
const {
return id < other.id; }
87 static P4RuntimeSymbolType make(p4rt_id_t
id) {
return P4RuntimeSymbolType(
id); }
92 explicit constexpr P4RuntimeSymbolType(p4rt_id_t
id) noexcept : id(
id) {}
107 std::optional<p4rt_id_t>
id = std::nullopt) = 0;
135 auto decl = block->getContainer();
136 return decl ? decl->controlPlaneName() : cstring::empty;
141 const IR::TableBlock *tableBlock) = 0;
144 const IR::ExternBlock *externBlock) = 0;
147 const IR::BaseAssignmentStatement *assign) = 0;
164 ::p4::config::v1::P4Info *p4info,
165 ::p4::config::v1::Table *table,
166 const IR::TableBlock *tableBlock) = 0;
170 ::p4::config::v1::P4Info *p4info,
171 const IR::ExternBlock *externBlock) = 0;
175 ::p4::config::v1::P4Info *p4info,
180 ::p4::config::v1::P4Info *p4info) = 0;
184 const IR::ExternBlock *externBlock) = 0;
204 const IR::ToplevelBlock *evaluatedProgram)
const = 0;
216 bool *isConstructedInPlace =
nullptr);
224template <
typename Func>
226 std::set<const IR::Block *> visited;
229 while (!frontier.empty()) {
231 auto evaluatedBlock = *frontier.begin();
232 frontier.erase(frontier.begin());
233 visited.insert(evaluatedBlock);
235 function(evaluatedBlock);
238 for (
auto evaluatedChild : evaluatedBlock->constantValue) {
240 if (!evaluatedChild.second)
continue;
241 if (!evaluatedChild.second->is<IR::Block>())
continue;
242 auto evaluatedChildBlock = evaluatedChild.second->to<IR::Block>();
243 if (visited.find(evaluatedChildBlock) != visited.end())
continue;
244 frontier.insert(evaluatedChildBlock);
253void serializeOneStructuredAnnotation(
const IR::Annotation *annotation,
254 ::p4::config::v1::StructuredAnnotation *structuredAnnotation);
261template <
typename Message,
typename UnaryPredicate>
262void addAnnotations(Message *message,
const IR::IAnnotated *annotated, UnaryPredicate p) {
266 if (annotated ==
nullptr)
return;
268 for (
const IR::Annotation *annotation : annotated->getAnnotations()) {
270 if (annotation->annotationKind() != IR::Annotation::Kind::Unstructured &&
271 annotation->annotationKind() != IR::Annotation::Kind::Unparsed) {
272 serializeOneStructuredAnnotation(annotation, message->add_structured_annotations());
277 if (annotation->name == IR::Annotation::nameAnnotation)
continue;
278 if (annotation->name ==
"id")
continue;
282 if (annotation->name ==
"brief" || annotation->name ==
"description")
continue;
284 if (p(annotation->name))
continue;
291template <
typename Message>
292void addAnnotations(Message *message,
const IR::IAnnotated *annotated) {
293 addAnnotations(message, annotated, [](
cstring) {
return false; });
298template <
typename Message>
303 if (annotated ==
nullptr)
return;
305 ::p4::config::v1::Documentation doc;
311 for (
const IR::Annotation *annotation : annotated->getAnnotations()) {
312 if (annotation->name ==
"brief") {
313 auto brief = annotation->getExpr(0)->to<IR::StringLiteral>();
316 doc.set_brief(brief->value);
320 if (annotation->name ==
"description") {
321 auto description = annotation->getExpr(0)->to<IR::StringLiteral>();
323 CHECK_NULL(description);
324 doc.set_description(description->value);
330 if (hasDoc) message->mutable_doc()->CopyFrom(doc);
336template <
typename UnaryPredicate>
337void setPreamble(::p4::config::v1::Preamble *preamble, p4rt_id_t
id,
cstring name,
cstring alias,
338 const IR::IAnnotated *annotated, UnaryPredicate p) {
339 CHECK_NULL(preamble);
340 preamble->set_id(
id);
341 preamble->set_name(name);
342 preamble->set_alias(alias);
343 addAnnotations(preamble, annotated, p);
349inline void setPreamble(::p4::config::v1::Preamble *preamble, p4rt_id_t
id,
cstring name,
350 cstring alias,
const IR::IAnnotated *annotated) {
351 setPreamble(preamble,
id, name, alias, annotated, [](
cstring) {
return false; });
359template <
typename Kind>
367template <
typename Kind>
385 static std::optional<Counterlike<Kind>>
from(
const IR::ExternBlock *instance,
387 ::p4::config::v1::P4TypeInfo *p4RtTypeInfo) {
388 CHECK_NULL(instance);
389 auto declaration = instance->node->to<IR::Declaration_Instance>();
394 auto unit = instance->getParameterValue(
"type"_cs);
395 if (!
unit->is<IR::Declaration_ID>()) {
397 "%1% '%2%' has a unit type which is not an enum constant: %3%",
404 if (
size->template is<IR::Constant>()) {
405 val =
size->template to<IR::Constant>()->value;
406 }
else if (
size->template is<IR::SerEnumMember>()) {
407 auto sem =
size->template to<IR::SerEnumMember>();
408 val = sem->value->template to<IR::Constant>()->value;
410 ::P4::error(ErrorType::ERR_INVALID,
"%1% '%2%' has a non-constant size: %3%",
418 if (indexTypeParamIdx != std::nullopt) {
420 BUG_CHECK(declaration->type->is<IR::Type_Specialized>(),
421 "%1%: expected Type_Specialized", declaration->type);
422 auto type = declaration->type->to<IR::Type_Specialized>();
423 BUG_CHECK(type->arguments->size() > *indexTypeParamIdx,
424 "%1%: expected at least %2% type arguments", instance,
425 *indexTypeParamIdx + 1);
426 auto typeArg = type->arguments->at(*indexTypeParamIdx);
434 declaration->to<IR::IAnnotated>(),
435 unit->to<IR::Declaration_ID>()->name,
445 const IR::P4Table *
table) {
447 BUG_CHECK(instance.name != std::nullopt,
"Caller should've ensured we have a name");
450 ::P4::error(ErrorType::ERR_EXPECTED,
"Expected a direct %1%: %2%",
455 auto unitArgument = instance.substitution.lookupByName(
"type"_cs)->expression;
456 if (unitArgument ==
nullptr) {
458 "Direct %1% instance %2% should take a constructor argument",
462 if (!unitArgument->is<IR::Member>()) {
464 "Direct %1% instance %2% has an unexpected constructor argument",
469 auto unit = unitArgument->to<IR::Member>()->member.name;
471 instance.annotations,
474 table->controlPlaneName(),
481template <
typename Kind>
486 if (!instance)
return std::nullopt;
Definition p4RuntimeArchHandler.h:130
virtual void collectTableProperties(P4RuntimeSymbolTableIface *symbols, const IR::TableBlock *tableBlock)=0
virtual void addExternInstance(const P4RuntimeSymbolTableIface &symbols, ::p4::config::v1::P4Info *p4info, const IR::ExternBlock *externBlock)=0
virtual void collectAssignmentStatement(P4RuntimeSymbolTableIface *symbols, const IR::BaseAssignmentStatement *assign)=0
Collects architecture-specific used in assignment statements.
virtual void postAdd(const P4RuntimeSymbolTableIface &symbols, ::p4::config::v1::P4Info *p4info)=0
virtual void addTableProperties(const P4RuntimeSymbolTableIface &symbols, ::p4::config::v1::P4Info *p4info, ::p4::config::v1::Table *table, const IR::TableBlock *tableBlock)=0
virtual cstring getControlPlaneName(const IR::Block *block)
Get control plane name for @block.
Definition p4RuntimeArchHandler.h:134
virtual void collectExternFunction(P4RuntimeSymbolTableIface *symbols, const P4::ExternFunction *externFunction)=0
virtual void addExternFunction(const P4RuntimeSymbolTableIface &symbols, ::p4::config::v1::P4Info *p4info, const P4::ExternFunction *externFunction)=0
virtual void postCollect(const P4RuntimeSymbolTableIface &symbols)=0
virtual google::protobuf::util::JsonPrintOptions getJsonPrintOptions()=0
Control how JSON is output.
virtual void collectExternInstance(P4RuntimeSymbolTableIface *symbols, const IR::ExternBlock *externBlock)=0
Collects architecture-specific @externBlock instance in @symbols table.
virtual bool filterAnnotations(cstring anno)=0
called when processing annotations via setPreamble
virtual void collectExternMethod(P4RuntimeSymbolTableIface *symbols, const P4::ExternMethod *externMethod)=0
Collects architecture-specific @externMethod instance in @symbols table.
virtual void addExternEntries(const p4::v1::WriteRequest *entries, const P4RuntimeSymbolTableIface &symbols, const IR::ExternBlock *externBlock)=0
This method is called to add target specific extern entries.
virtual void collectExtra(P4RuntimeSymbolTableIface *symbols)=0
Definition p4RuntimeArchHandler.h:100
virtual p4rt_id_t getId(P4RuntimeSymbolType type, const IR::IDeclaration *declaration) const =0
virtual cstring getAlias(cstring name) const =0
virtual void add(P4RuntimeSymbolType type, const IR::IDeclaration *declaration)=0
Add a @type symbol, extracting the name and id from @declaration.
Definition p4RuntimeArchHandler.h:54
static const ::p4::config::v1::P4DataTypeSpec * convert(const P4::ReferenceMap *refMap, P4::TypeMap *typeMap, const IR::Type *type, ::p4::config::v1::P4TypeInfo *typeInfo)
Definition typeSpecConverter.cpp:377
Definition methodInstance.h:194
Definition methodInstance.h:168
The Declaration interface, representing objects with names.
Definition declaration.h:17
Class used to encode maps from paths to declarations.
Definition referenceMap.h:67
Definition ordered_set.h:32
Definition tofino/bf-p4c/control-plane/bfruntime_arch_handler.h:77
void forAllEvaluatedBlocks(const IR::Block *block, Func function)
Definition p4RuntimeArchHandler.h:225
std::string serializeOneAnnotation(const IR::Annotation *annotation)
Serialize an unstructured @annotation to a string.
Definition p4RuntimeArchHandler.cpp:108
std::optional< ExternInstance > getExternInstanceFromProperty(const IR::P4Table *table, const cstring &propertyName, ReferenceMap *refMap, TypeMap *typeMap, bool *isConstructedInPlace)
Definition p4RuntimeArchHandler.cpp:30
bool isExternPropertyConstructedInPlace(const IR::P4Table *table, const cstring &propertyName)
Definition p4RuntimeArchHandler.cpp:66
int64_t getTableSize(const IR::P4Table *table)
Definition p4RuntimeArchHandler.cpp:80
void addDocumentation(Message *message, const IR::IAnnotated *annotated)
' and '@description' annotations if present.
Definition p4RuntimeArchHandler.h:299
std::optional< Counterlike< Kind > > getDirectCounterlike(const IR::P4Table *table, ReferenceMap *refMap, TypeMap *typeMap)
Definition p4RuntimeArchHandler.h:482
A traits class describing the properties of "counterlike" things.
Definition p4RuntimeArchHandler.h:360
TODO(antonin): High level goals of the generator go here!!
Definition dpdk/control-plane/bfruntime_arch_handler.h:44
cstring getTypeName(const IR::Type *type, TypeMap *typeMap)
Definition typeSpecConverter.cpp:68
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58
Definition p4RuntimeArchHandler.h:368
const std::optional< cstring > table
If not none, the instance is a direct resource associated with @table.
Definition p4RuntimeArchHandler.h:378
const cstring name
The name of the instance.
Definition p4RuntimeArchHandler.h:370
const IR::IAnnotated * annotations
If non-null, the instance's annotations.
Definition p4RuntimeArchHandler.h:372
static std::optional< Counterlike< Kind > > from(const IR::ExternBlock *instance, const ReferenceMap *refMap, P4::TypeMap *typeMap, ::p4::config::v1::P4TypeInfo *p4RtTypeInfo)
Definition p4RuntimeArchHandler.h:385
const cstring unit
The units parameter to the instance; valid values vary depending on @Kind.
Definition p4RuntimeArchHandler.h:374
const cstring index_type_name
Definition p4RuntimeArchHandler.h:381
static std::optional< Counterlike< Kind > > fromDirect(const ExternInstance &instance, const IR::P4Table *table)
Definition p4RuntimeArchHandler.h:444
const int64_t size
The size parameter to the instance.
Definition p4RuntimeArchHandler.h:376
Definition p4RuntimeArchHandler.h:195
virtual P4RuntimeArchHandlerIface * operator()(ReferenceMap *refMap, TypeMap *typeMap, const IR::ToplevelBlock *evaluatedProgram) const =0
Definition externInstance.h:42