diff --git a/frontends/p4/localizeActions.cpp b/frontends/p4/localizeActions.cpp index 3ae11913a6..16d907d068 100644 --- a/frontends/p4/localizeActions.cpp +++ b/frontends/p4/localizeActions.cpp @@ -43,9 +43,25 @@ const IR::Node *TagGlobalActions::preorder(IR::P4Action *action) { if (findContext() == 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()->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(); diff --git a/testdata/p4_16_samples/issue-4969-bmv2.p4 b/testdata/p4_16_samples/issue-4969-bmv2.p4 new file mode 100644 index 0000000000..b0b27d0dc0 --- /dev/null +++ b/testdata/p4_16_samples/issue-4969-bmv2.p4 @@ -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 +#include + +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; diff --git a/testdata/p4_16_samples_outputs/issue-4969-bmv2-first.p4 b/testdata/p4_16_samples_outputs/issue-4969-bmv2-first.p4 new file mode 100644 index 0000000000..e489adb60a --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue-4969-bmv2-first.p4 @@ -0,0 +1,69 @@ +#include +#define V1MODEL_VERSION 20180101 +#include + +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 @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(hdr.ethernet); + } +} + +V1Switch(parserImpl(), verifyChecksum(), ingressImpl(), egressImpl(), updateChecksum(), deparserImpl()) main; diff --git a/testdata/p4_16_samples_outputs/issue-4969-bmv2-frontend.p4 b/testdata/p4_16_samples_outputs/issue-4969-bmv2-frontend.p4 new file mode 100644 index 0000000000..55db1cdb1a --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue-4969-bmv2-frontend.p4 @@ -0,0 +1,71 @@ +#include +#define V1MODEL_VERSION 20180101 +#include + +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 { + } +} + +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(hdr.ethernet); + } +} + +V1Switch(parserImpl(), verifyChecksum(), ingressImpl(), egressImpl(), updateChecksum(), deparserImpl()) main; diff --git a/testdata/p4_16_samples_outputs/issue-4969-bmv2-midend.p4 b/testdata/p4_16_samples_outputs/issue-4969-bmv2-midend.p4 new file mode 100644 index 0000000000..cf2c940913 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue-4969-bmv2-midend.p4 @@ -0,0 +1,70 @@ +#include +#define V1MODEL_VERSION 20180101 +#include + +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(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(hdr.ethernet); + } +} + +V1Switch(parserImpl(), verifyChecksum(), ingressImpl(), egressImpl(), updateChecksum(), deparserImpl()) main; diff --git a/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4 b/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4 new file mode 100644 index 0000000000..561c5cfa59 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4 @@ -0,0 +1,69 @@ +#include +#define V1MODEL_VERSION 20180101 +#include + +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; diff --git a/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4-stderr b/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4-stderr new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4.entries.txtpb b/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4.entries.txtpb new file mode 100644 index 0000000000..5cb9652623 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4.entries.txtpb @@ -0,0 +1,3 @@ +# proto-file: p4/v1/p4runtime.proto +# proto-message: p4.v1.WriteRequest + diff --git a/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4.p4info.txtpb b/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4.p4info.txtpb new file mode 100644 index 0000000000..84addd50a8 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue-4969-bmv2.p4.p4info.txtpb @@ -0,0 +1,56 @@ +# proto-file: p4/config/v1/p4info.proto +# proto-message: p4.config.v1.P4Info + +pkg_info { + arch: "v1model" +} +tables { + preamble { + id: 49173205 + name: "ingressImpl.t1" + alias: "t1" + } + match_fields { + id: 1 + name: "hdr.ethernet.etherType" + bitwidth: 16 + match_type: EXACT + } + action_refs { + id: 21257015 + } + action_refs { + id: 25646030 + } + action_refs { + id: 21008649 + } + initial_default_action { + action_id: 21257015 + } + size: 512 +} +actions { + preamble { + id: 21257015 + name: "NoAction" + alias: "NoAction" + annotations: "@noWarn(\"unused\")" + } +} +actions { + preamble { + id: 25646030 + name: "foo1" + alias: "foo1" + } +} +actions { + preamble { + id: 21008649 + name: "bar" + alias: "bar" + } +} +type_info { +}