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

Fix #4969 by modifying pass LocalizeActions #4970

Closed
Closed
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
21 changes: 18 additions & 3 deletions frontends/p4/localizeActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,24 @@ const IR::Node *TagGlobalActions::preorder(IR::P4Action *action) {
if (findContext<IR::P4Control>() == nullptr) {
auto annos = action->annotations;
if (annos == nullptr) annos = IR::Annotations::empty;
cstring name = "."_cs + action->name;
annos = annos->addAnnotationIfNew(IR::Annotation::nameAnnotation,
new IR::StringLiteral(name), false);
auto nameAnno = annos->getSingle(IR::Annotation::nameAnnotation);
if (nameAnno) {
// If the value of the existing name annotation does not
// begin with ".", prepend "." so that the name remains
// global if control plane APIs are generated later.
const auto *e0 = nameAnno->expr.at(0);
auto nameString = e0->to<IR::StringLiteral>()->value;
if (!nameString.startsWith(".")) {
nameString = "."_cs + nameString;
auto newLit = new IR::StringLiteral(e0->srcInfo, nameString);
annos = annos->addOrReplace(IR::Annotation::nameAnnotation, newLit);
}
} else {
// Add new name annotation beginning with "."
cstring name = "."_cs + action->name;
annos = annos->addAnnotationIfNew(IR::Annotation::nameAnnotation,
new IR::StringLiteral(name), false);
}
action->annotations = annos;
}
prune();
Expand Down
106 changes: 106 additions & 0 deletions testdata/p4_16_samples/issue-4969-bmv2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright 2022 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <core.p4>
#include <v1model.p4>

typedef bit<48> EthernetAddress;

header ethernet_t {
EthernetAddress dstAddr;
EthernetAddress srcAddr;
bit<16> etherType;
}

struct headers_t {
ethernet_t ethernet;
}

struct metadata_t {
}

parser parserImpl(
packet_in pkt,
out headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t stdmeta)
{
state start {
pkt.extract(hdr.ethernet);
transition accept;
}
}

control verifyChecksum(
inout headers_t hdr,
inout metadata_t meta)
{
apply { }
}

action foo1() {
}

@name("bar") action foo2() {
}

control ingressImpl(
inout headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t stdmeta)

{
table t1 {
actions = { NoAction; foo1; foo2; }
key = { hdr.ethernet.etherType: exact; }
default_action = NoAction();
size = 512;
}
apply {
t1.apply();
}
}

control egressImpl(
inout headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t stdmeta)
{
apply { }
}

control updateChecksum(
inout headers_t hdr,
inout metadata_t meta)
{
apply { }
}

control deparserImpl(
packet_out pkt,
in headers_t hdr)
{
apply {
pkt.emit(hdr.ethernet);
}
}

V1Switch(parserImpl(),
verifyChecksum(),
ingressImpl(),
egressImpl(),
updateChecksum(),
deparserImpl()) main;
69 changes: 69 additions & 0 deletions testdata/p4_16_samples_outputs/issue-4969-bmv2-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

typedef bit<48> EthernetAddress;
header ethernet_t {
EthernetAddress dstAddr;
EthernetAddress srcAddr;
bit<16> etherType;
}

struct headers_t {
ethernet_t ethernet;
}

struct metadata_t {
}

parser parserImpl(packet_in pkt, out headers_t hdr, inout metadata_t meta, inout standard_metadata_t stdmeta) {
state start {
pkt.extract<ethernet_t>(hdr.ethernet);
transition accept;
}
}

control verifyChecksum(inout headers_t hdr, inout metadata_t meta) {
apply {
}
}

action foo1() {
}
@name("bar") action foo2() {
}
control ingressImpl(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t stdmeta) {
table t1 {
actions = {
NoAction();
foo1();
foo2();
}
key = {
hdr.ethernet.etherType: exact @name("hdr.ethernet.etherType");
}
default_action = NoAction();
size = 512;
}
apply {
t1.apply();
}
}

control egressImpl(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t stdmeta) {
apply {
}
}

control updateChecksum(inout headers_t hdr, inout metadata_t meta) {
apply {
}
}

control deparserImpl(packet_out pkt, in headers_t hdr) {
apply {
pkt.emit<ethernet_t>(hdr.ethernet);
}
}

V1Switch<headers_t, metadata_t>(parserImpl(), verifyChecksum(), ingressImpl(), egressImpl(), updateChecksum(), deparserImpl()) main;
71 changes: 71 additions & 0 deletions testdata/p4_16_samples_outputs/issue-4969-bmv2-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

typedef bit<48> EthernetAddress;
header ethernet_t {
EthernetAddress dstAddr;
EthernetAddress srcAddr;
bit<16> etherType;
}

struct headers_t {
ethernet_t ethernet;
}

struct metadata_t {
}

parser parserImpl(packet_in pkt, out headers_t hdr, inout metadata_t meta, inout standard_metadata_t stdmeta) {
state start {
pkt.extract<ethernet_t>(hdr.ethernet);
transition accept;
}
}

control verifyChecksum(inout headers_t hdr, inout metadata_t meta) {
apply {
}
}

control ingressImpl(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t stdmeta) {
@noWarn("unused") @name(".NoAction") action NoAction_1() {
}
@name(".foo1") action foo1_0() {
}
@name(".bar") action foo2_0() {
}
@name("ingressImpl.t1") table t1_0 {
actions = {
NoAction_1();
foo1_0();
foo2_0();
}
key = {
hdr.ethernet.etherType: exact @name("hdr.ethernet.etherType");
}
default_action = NoAction_1();
size = 512;
}
apply {
t1_0.apply();
}
}

control egressImpl(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t stdmeta) {
apply {
}
}

control updateChecksum(inout headers_t hdr, inout metadata_t meta) {
apply {
}
}

control deparserImpl(packet_out pkt, in headers_t hdr) {
apply {
pkt.emit<ethernet_t>(hdr.ethernet);
}
}

V1Switch<headers_t, metadata_t>(parserImpl(), verifyChecksum(), ingressImpl(), egressImpl(), updateChecksum(), deparserImpl()) main;
70 changes: 70 additions & 0 deletions testdata/p4_16_samples_outputs/issue-4969-bmv2-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

header ethernet_t {
bit<48> dstAddr;
bit<48> srcAddr;
bit<16> etherType;
}

struct headers_t {
ethernet_t ethernet;
}

struct metadata_t {
}

parser parserImpl(packet_in pkt, out headers_t hdr, inout metadata_t meta, inout standard_metadata_t stdmeta) {
state start {
pkt.extract<ethernet_t>(hdr.ethernet);
transition accept;
}
}

control verifyChecksum(inout headers_t hdr, inout metadata_t meta) {
apply {
}
}

control ingressImpl(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t stdmeta) {
@noWarn("unused") @name(".NoAction") action NoAction_1() {
}
@name(".foo1") action foo1_0() {
}
@name(".bar") action foo2_0() {
}
@name("ingressImpl.t1") table t1_0 {
actions = {
NoAction_1();
foo1_0();
foo2_0();
}
key = {
hdr.ethernet.etherType: exact @name("hdr.ethernet.etherType");
}
default_action = NoAction_1();
size = 512;
}
apply {
t1_0.apply();
}
}

control egressImpl(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t stdmeta) {
apply {
}
}

control updateChecksum(inout headers_t hdr, inout metadata_t meta) {
apply {
}
}

control deparserImpl(packet_out pkt, in headers_t hdr) {
apply {
pkt.emit<ethernet_t>(hdr.ethernet);
}
}

V1Switch<headers_t, metadata_t>(parserImpl(), verifyChecksum(), ingressImpl(), egressImpl(), updateChecksum(), deparserImpl()) main;
Loading
Loading