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

Delete keys that are empty tuples #3489

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 34 additions & 12 deletions frontends/p4/createBuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ limitations under the License.
#include "frontends/p4/coreLibrary.h"

namespace P4 {
void CreateBuiltins::postorder(IR::P4Parser* parser) {
const IR::Node* CreateBuiltins::postorder(IR::P4Parser* parser) {
parser->states.push_back(new IR::ParserState(IR::ParserState::accept, nullptr));
parser->states.push_back(new IR::ParserState(IR::ParserState::reject, nullptr));
return parser;
}

void CreateBuiltins::postorder(IR::ActionListElement* element) {
const IR::Node* CreateBuiltins::postorder(IR::ActionListElement* element) {
// convert path expressions to method calls
// actions = { a; b; }
// becomes
Expand All @@ -33,9 +34,10 @@ void CreateBuiltins::postorder(IR::ActionListElement* element) {
element->expression = new IR::MethodCallExpression(
element->expression->srcInfo, element->expression,
new IR::Vector<IR::Type>(), new IR::Vector<IR::Argument>());
return element;
}

void CreateBuiltins::postorder(IR::ExpressionValue* expression) {
const IR::Node* CreateBuiltins::postorder(IR::ExpressionValue* expression) {
// convert a default_action = a; into
// default_action = a();
auto prop = findContext<IR::Property>();
Expand All @@ -45,48 +47,67 @@ void CreateBuiltins::postorder(IR::ExpressionValue* expression) {
expression->expression = new IR::MethodCallExpression(
expression->expression->srcInfo, expression->expression,
new IR::Vector<IR::Type>(), new IR::Vector<IR::Argument>());
return expression;
}

void CreateBuiltins::postorder(IR::Entry* entry) {
const IR::Node* CreateBuiltins::postorder(IR::Entry* entry) {
// convert a const table entry with action "a;" into "a();"
if (entry->action->is<IR::PathExpression>())
entry->action = new IR::MethodCallExpression(
entry->action->srcInfo, entry->action,
new IR::Vector<IR::Type>(), new IR::Vector<IR::Argument>());
return entry;
}

void CreateBuiltins::postorder(IR::ParserState* state) {
const IR::Node* CreateBuiltins::postorder(IR::ParserState* state) {
if (state->selectExpression == nullptr) {
warn(ErrorType::WARN_PARSER_TRANSITION, "%1%: implicit transition to `reject'", state);
state->selectExpression = new IR::PathExpression(IR::ParserState::reject);
}
return state;
}

void CreateBuiltins::postorder(IR::ActionList* actions) {
const IR::Node* CreateBuiltins::postorder(IR::ActionList* actions) {
if (!addNoAction)
return;
return actions;
auto decl = actions->getDeclaration(P4::P4CoreLibrary::instance.noAction.str());
if (decl != nullptr)
return;
return actions;;
actions->push_back(
new IR::ActionListElement(
new IR::Annotations(
{new IR::Annotation(IR::Annotation::defaultOnlyAnnotation, {})}),
new IR::MethodCallExpression(
new IR::PathExpression(P4::P4CoreLibrary::instance.noAction.Id(actions->srcInfo)),
new IR::Vector<IR::Type>(), new IR::Vector<IR::Argument>())));
return actions;
}

bool CreateBuiltins::preorder(IR::P4Table* table) {
const IR::Node* CreateBuiltins::preorder(IR::P4Table* table) {
addNoAction = false;
if (table->getDefaultAction() == nullptr)
addNoAction = true;
return true;
return table;
}

void CreateBuiltins::postorder(IR::TableProperties* properties) {
const IR::Node* CreateBuiltins::postorder(IR::Property* property) {
// Spec: a table with an empty key is the same
// as a table with no key.
if (property->name != IR::TableProperties::keyPropertyName)
return property;
if (auto key = property->value->to<IR::Key>()) {
if (key->keyElements.size() == 0)
return nullptr;
} else {
::error(ErrorType::ERR_INVALID, "%1%: must be a key", key);
return nullptr;
}
return property;
}

const IR::Node* CreateBuiltins::postorder(IR::TableProperties* properties) {
if (!addNoAction)
return;
return properties;
auto act = new IR::PathExpression(P4::P4CoreLibrary::instance.noAction.Id(properties->srcInfo));
auto args = new IR::Vector<IR::Argument>();
auto methodCall = new IR::MethodCallExpression(act, args);
Expand All @@ -95,6 +116,7 @@ void CreateBuiltins::postorder(IR::TableProperties* properties) {
new IR::ExpressionValue(methodCall),
/* isConstant = */ false);
properties->properties.push_back(prop);
return properties;
}

} // namespace P4
20 changes: 10 additions & 10 deletions frontends/p4/createBuiltins.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ limitations under the License.
* Parser states without selects will transition to reject.
*/
namespace P4 {
class CreateBuiltins final : public Modifier {
class CreateBuiltins final : public Transform {
bool addNoAction = false;
public:
using Modifier::postorder;
CreateBuiltins() { setName("CreateBuiltins"); }
void postorder(IR::ParserState* state) override;
void postorder(IR::P4Parser* parser) override;
void postorder(IR::ActionListElement* element) override;
void postorder(IR::ExpressionValue* property) override;
void postorder(IR::Entry* property) override;
bool preorder(IR::P4Table* table) override;
void postorder(IR::ActionList* actions) override;
void postorder(IR::TableProperties* properties) override;
const IR::Node* postorder(IR::ParserState* state) override;
const IR::Node* postorder(IR::P4Parser* parser) override;
const IR::Node* postorder(IR::ActionListElement* element) override;
const IR::Node* postorder(IR::ExpressionValue* property) override;
const IR::Node* postorder(IR::Entry* property) override;
const IR::Node* preorder(IR::P4Table* table) override;
const IR::Node* postorder(IR::ActionList* actions) override;
const IR::Node* postorder(IR::TableProperties* properties) override;
const IR::Node* postorder(IR::Property* property) override;
};
} // namespace P4

Expand Down
3 changes: 2 additions & 1 deletion frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,8 @@ const IR::Node* TypeInference::preorder(IR::EntriesList* el) {
BUG_CHECK(table != nullptr, "%1% entries not within a table", el);
const IR::Key* key = table->getKey();
if (key == nullptr) {
typeError("Could not find key for table %1%", table);
if (el->size() != 0)
typeError("Entries cannot be specified for a table with no key %1%", table);
prune();
return el;
}
Expand Down
2 changes: 0 additions & 2 deletions testdata/p4_14_samples_outputs/overflow-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
action_1_1();
@defaultonly NoAction();
}
key = {
}
default_action = NoAction();
}
apply {
Expand Down
2 changes: 0 additions & 2 deletions testdata/p4_14_samples_outputs/overflow-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
action_0();
@defaultonly NoAction_1();
}
key = {
}
default_action = NoAction_1();
}
apply {
Expand Down
2 changes: 0 additions & 2 deletions testdata/p4_14_samples_outputs/overflow-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
action_0();
@defaultonly NoAction_1();
}
key = {
}
default_action = NoAction_1();
}
apply {
Expand Down
20 changes: 0 additions & 20 deletions testdata/p4_14_samples_outputs/p414-special-ops-2-bmv2-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,12 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
actions = {
do_clone_e2e();
}
key = {
}
default_action = do_clone_e2e();
}
@name(".t_do_recirculate") table t_do_recirculate {
actions = {
do_recirculate();
}
key = {
}
default_action = do_recirculate();
}
@name(".t_egr_debug_table1") table t_egr_debug_table1 {
Expand Down Expand Up @@ -174,32 +170,24 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
actions = {
mark_egr_resubmit_packet();
}
key = {
}
default_action = mark_egr_resubmit_packet();
}
@name(".t_mark_max_clone_e2e_packet") table t_mark_max_clone_e2e_packet {
actions = {
mark_max_clone_e2e_packet();
}
key = {
}
default_action = mark_max_clone_e2e_packet();
}
@name(".t_mark_max_recirculate_packet") table t_mark_max_recirculate_packet {
actions = {
mark_max_recirculate_packet();
}
key = {
}
default_action = mark_max_recirculate_packet();
}
@name(".t_mark_vanilla_packet") table t_mark_vanilla_packet {
actions = {
mark_vanilla_packet();
}
key = {
}
default_action = mark_vanilla_packet();
}
apply {
Expand Down Expand Up @@ -247,8 +235,6 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
actions = {
do_resubmit();
}
key = {
}
default_action = do_resubmit();
}
@name(".t_ing_debug_table1") table t_ing_debug_table1 {
Expand Down Expand Up @@ -305,24 +291,18 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
actions = {
set_port_to_mac_da_lsbs();
}
key = {
}
default_action = set_port_to_mac_da_lsbs();
}
@name(".t_mark_max_resubmit_packet") table t_mark_max_resubmit_packet {
actions = {
mark_max_resubmit_packet();
}
key = {
}
default_action = mark_max_resubmit_packet();
}
@name(".t_save_ing_instance_type") table t_save_ing_instance_type {
actions = {
save_ing_instance_type();
}
key = {
}
default_action = save_ing_instance_type();
}
apply {
Expand Down
20 changes: 0 additions & 20 deletions testdata/p4_14_samples_outputs/p414-special-ops-2-bmv2-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,12 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
actions = {
do_clone_e2e();
}
key = {
}
default_action = do_clone_e2e();
}
@name(".t_do_recirculate") table t_do_recirculate_0 {
actions = {
do_recirculate();
}
key = {
}
default_action = do_recirculate();
}
@name(".t_egr_debug_table1") table t_egr_debug_table1_0 {
Expand Down Expand Up @@ -203,32 +199,24 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
actions = {
mark_egr_resubmit_packet();
}
key = {
}
default_action = mark_egr_resubmit_packet();
}
@name(".t_mark_max_clone_e2e_packet") table t_mark_max_clone_e2e_packet_0 {
actions = {
mark_max_clone_e2e_packet();
}
key = {
}
default_action = mark_max_clone_e2e_packet();
}
@name(".t_mark_max_recirculate_packet") table t_mark_max_recirculate_packet_0 {
actions = {
mark_max_recirculate_packet();
}
key = {
}
default_action = mark_max_recirculate_packet();
}
@name(".t_mark_vanilla_packet") table t_mark_vanilla_packet_0 {
actions = {
mark_vanilla_packet();
}
key = {
}
default_action = mark_vanilla_packet();
}
apply {
Expand Down Expand Up @@ -278,8 +266,6 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
actions = {
do_resubmit();
}
key = {
}
default_action = do_resubmit();
}
@name(".t_ing_debug_table1") table t_ing_debug_table1_0 {
Expand Down Expand Up @@ -336,24 +322,18 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
actions = {
set_port_to_mac_da_lsbs();
}
key = {
}
default_action = set_port_to_mac_da_lsbs();
}
@name(".t_mark_max_resubmit_packet") table t_mark_max_resubmit_packet_0 {
actions = {
mark_max_resubmit_packet();
}
key = {
}
default_action = mark_max_resubmit_packet();
}
@name(".t_save_ing_instance_type") table t_save_ing_instance_type_0 {
actions = {
save_ing_instance_type();
}
key = {
}
default_action = save_ing_instance_type();
}
apply {
Expand Down
Loading