-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic support for
@p4runtime_translation
and `@p4runtime_translatio…
…n_mappings`.
- Loading branch information
Showing
8 changed files
with
219 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
backends/p4tools/modules/testgen/targets/bmv2/p4runtime_translation.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "backends/p4tools/modules/testgen/targets/bmv2/p4runtime_translation.h" | ||
|
||
std::vector<const IR::Annotation *> | ||
P4Tools::P4Testgen::Bmv2::PropagateP4RuntimeTranslation::lookupP4RuntimeAnnotations( | ||
const P4::TypeMap &typeMap, const IR::Type *type) { | ||
std::vector<const IR::Annotation *> p4RuntimeAnnotations; | ||
const auto *typeName = type->to<IR::Type_Name>(); | ||
if (typeName != nullptr) { | ||
type = typeMap.getType(typeName); | ||
if (type == nullptr) { | ||
::error("Type %1% not found in the type map.", typeName); | ||
return p4RuntimeAnnotations; | ||
} | ||
type = type->getP4Type(); | ||
} | ||
const auto *annotatedType = type->to<IR::IAnnotated>(); | ||
if (annotatedType == nullptr) { | ||
return p4RuntimeAnnotations; | ||
} | ||
const auto *p4runtimeAnnotation = annotatedType->getAnnotation("p4runtime_translation"); | ||
if (p4runtimeAnnotation != nullptr) { | ||
BUG_CHECK(!p4runtimeAnnotation->needsParsing, | ||
"The @p4runtime_translation annotation should have been parsed already."); | ||
p4RuntimeAnnotations.push_back(p4runtimeAnnotation); | ||
} | ||
const auto *p4runtimeTranslationMappings = | ||
annotatedType->getAnnotation("p4runtime_translation_mappings"); | ||
if (p4runtimeTranslationMappings != nullptr) { | ||
BUG_CHECK( | ||
!p4runtimeTranslationMappings->needsParsing, | ||
"The @p4runtime_translation_mappings annotation should have been parsed already."); | ||
p4RuntimeAnnotations.push_back(p4runtimeTranslationMappings); | ||
} | ||
return p4RuntimeAnnotations; | ||
} | ||
|
||
const IR::Parameter *P4Tools::P4Testgen::Bmv2::PropagateP4RuntimeTranslation::preorder( | ||
IR::Parameter *parameter) { | ||
auto p4RuntimeAnnotations = lookupP4RuntimeAnnotations(_typeMap, parameter->type); | ||
if (p4RuntimeAnnotations.empty()) { | ||
return parameter; | ||
} | ||
auto *annotationsVector = parameter->annotations->clone(); | ||
for (const auto *p4runtimeAnnotation : p4RuntimeAnnotations) { | ||
annotationsVector->annotations.push_back(p4runtimeAnnotation); | ||
} | ||
parameter->annotations = annotationsVector; | ||
return parameter; | ||
} | ||
|
||
const IR::KeyElement *P4Tools::P4Testgen::Bmv2::PropagateP4RuntimeTranslation::preorder( | ||
IR::KeyElement *keyElement) { | ||
auto p4RuntimeAnnotations = lookupP4RuntimeAnnotations(_typeMap, keyElement->expression->type); | ||
if (p4RuntimeAnnotations.empty()) { | ||
return keyElement; | ||
} | ||
auto *annotationsVector = keyElement->annotations->clone(); | ||
for (const auto *p4runtimeAnnotation : p4RuntimeAnnotations) { | ||
annotationsVector->annotations.push_back(p4runtimeAnnotation); | ||
} | ||
keyElement->annotations = annotationsVector; | ||
return keyElement; | ||
} | ||
|
||
P4Tools::P4Testgen::Bmv2::PropagateP4RuntimeTranslation::PropagateP4RuntimeTranslation( | ||
const P4::TypeMap &typeMap) | ||
: _typeMap(typeMap) { | ||
setName("PropagateP4RuntimeTranslation"); | ||
} |
31 changes: 31 additions & 0 deletions
31
backends/p4tools/modules/testgen/targets/bmv2/p4runtime_translation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_P4RUNTIME_TRANSLATION_H_ | ||
#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_P4RUNTIME_TRANSLATION_H_ | ||
#include <functional> | ||
|
||
#include "frontends/p4/typeMap.h" | ||
#include "ir/ir.h" | ||
#include "ir/visitor.h" | ||
|
||
namespace P4Tools::P4Testgen::Bmv2 { | ||
|
||
/// Propagates P4Runtime annotations attached to type definitions to the nodes which use these type | ||
/// definitions. For now, this is restricted to key elements and action parameters. | ||
class PropagateP4RuntimeTranslation : public Transform { | ||
/// We use the typemap to look up the original declaration for type reference. | ||
/// These declarations may have an annotation. | ||
std::reference_wrapper<const P4::TypeMap> _typeMap; | ||
|
||
/// Look up annotations relevant to P4Runtime. They may influence the control plane interfaces. | ||
static std::vector<const IR::Annotation *> lookupP4RuntimeAnnotations( | ||
const P4::TypeMap &typeMap, const IR::Type *type); | ||
|
||
const IR::Parameter *preorder(IR::Parameter *parameter) override; | ||
const IR::KeyElement *preorder(IR::KeyElement *keyElement) override; | ||
|
||
public: | ||
explicit PropagateP4RuntimeTranslation(const P4::TypeMap &typeMap); | ||
}; | ||
|
||
} // namespace P4Tools::P4Testgen::Bmv2 | ||
|
||
#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_P4RUNTIME_TRANSLATION_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters