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

Set headers valid when expanding initializations #2385

Merged
merged 2 commits into from
May 26, 2020
Merged
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
14 changes: 14 additions & 0 deletions midend/copyStructures.cpp
Original file line number Diff line number Diff line change
@@ -71,6 +71,13 @@ const IR::Node* DoCopyStructures::postorder(IR::AssignmentStatement* statement)
auto retval = new IR::IndexedVector<IR::StatOrDecl>();
auto strct = ltype->to<IR::Type_StructLike>();
if (auto list = statement->right->to<IR::ListExpression>()) {
if (ltype->is<IR::Type_Header>()) {
auto setValid = new IR::Member(
statement->srcInfo, statement->left, IR::Type_Header::setValid);
auto mc = new IR::MethodCallStatement(
new IR::MethodCallExpression(statement->srcInfo, setValid));
retval->push_back(mc);
}
unsigned index = 0;
for (auto f : strct->fields) {
auto right = list->components.at(index);
@@ -79,6 +86,13 @@ const IR::Node* DoCopyStructures::postorder(IR::AssignmentStatement* statement)
index++;
}
} else if (auto si = statement->right->to<IR::StructExpression>()) {
if (ltype->is<IR::Type_Header>()) {
auto setValid = new IR::Member(
statement->srcInfo, statement->left, IR::Type_Header::setValid);
auto mc = new IR::MethodCallStatement(
new IR::MethodCallExpression(statement->srcInfo, setValid));
retval->push_back(mc);
}
for (auto f : strct->fields) {
auto right = si->components.getDeclaration<IR::NamedExpression>(f->name);
auto left = new IR::Member(statement->left, f->name);
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta, inout
@name(".parse_ipv4") state parse_ipv4 {
tmp = packet.lookahead<bit<160>>();
tmp_hdr_0.setValid();
tmp_hdr_0.setValid();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it looks like we get redundant setValid calls in some cases? Harmless, but do we know why this happens?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this happens if the user was already setting the header to valid before assigning to it.
We should write an analysis which can remove the useless setValid; it could probably be folded in the def-use analysis.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2396

tmp_hdr_0.version = tmp[159:156];
tmp_hdr_0.ihl = tmp[155:152];
tmp_hdr_0.diffserv = tmp[151:144];
1 change: 1 addition & 0 deletions testdata/p4_14_samples_outputs/TLV_parsing-midend.p4
Original file line number Diff line number Diff line change
@@ -114,6 +114,7 @@ parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta, inout
@name(".parse_ipv4_option_timestamp") state parse_ipv4_option_timestamp {
tmp_0 = packet.lookahead<bit<16>>();
tmp_hdr_0.setValid();
tmp_hdr_0.setValid();
tmp_hdr_0.value = tmp_0[15:8];
tmp_hdr_0.len = tmp_0[7:0];
packet.extract<ipv4_option_timestamp_t>(hdr.ipv4_option_timestamp, ((bit<32>)tmp_0[7:0] << 3) + 32w4294967280);
2 changes: 2 additions & 0 deletions testdata/p4_14_samples_outputs/issue576-midend.p4
Original file line number Diff line number Diff line change
@@ -69,6 +69,7 @@ parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta, inout
packet.extract<simpleipv4_t>(hdr.sh.next);
tmp = packet.lookahead<bit<160>>();
tmp_hdr_1.setValid();
tmp_hdr_1.setValid();
tmp_hdr_1.version = tmp[159:156];
tmp_hdr_1.ihl = tmp[155:152];
tmp_hdr_1.diffserv = tmp[151:144];
@@ -84,6 +85,7 @@ parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta, inout
packet.extract<ipv4_t>(hdr.h.next, ((bit<32>)tmp[155:152] << 5) + 32w4294967136);
tmp_0 = packet.lookahead<bit<160>>();
tmp_hdr_2.setValid();
tmp_hdr_2.setValid();
tmp_hdr_2.version = tmp_0[159:156];
tmp_hdr_2.ihl = tmp_0[155:152];
tmp_hdr_2.diffserv = tmp_0[151:144];
1 change: 1 addition & 0 deletions testdata/p4_14_samples_outputs/issue781-midend.p4
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@ parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta, inout
@name(".start") state start {
tmp = packet.lookahead<bit<160>>();
tmp_hdr_0.setValid();
tmp_hdr_0.setValid();
tmp_hdr_0.version = tmp[159:156];
tmp_hdr_0.ihl = tmp[155:152];
tmp_hdr_0.diffserv = tmp[151:144];
62 changes: 62 additions & 0 deletions testdata/p4_16_samples/issue2383-bmv2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <core.p4>
#include <v1model.p4>

header ethernet_t {
bit<48> dst_addr;
bit<48> src_addr;
bit<16> eth_type;
}


struct Headers {
ethernet_t eth_hdr;
}

struct Meta {
}


ethernet_t do_function() {
return {1, 1, 1};
}

parser p(packet_in pkt, out Headers hdr, inout Meta m, inout standard_metadata_t sm) {
state start {
transition parse_hdrs;
}
state parse_hdrs {
pkt.extract(hdr.eth_hdr);
transition accept;
}
}

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {

apply {
h.eth_hdr = do_function();

}
}

control vrfy(inout Headers h, inout Meta m) {
apply {
}
}

control update(inout Headers h, inout Meta m) {
apply {
}
}

control egress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
apply {
}
}

control deparser(packet_out pkt, in Headers h) {
apply {
pkt.emit(h);
}
}

V1Switch(p(), vrfy(), ingress(), egress(), update(), deparser()) main;
2 changes: 2 additions & 0 deletions testdata/p4_16_samples/issue2383-bmv2.stf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packet 0 00 00 00 00 00 00 00 00 00 00 00 00 00 00
expect 0 00 00 00 00 00 01 00 00 00 00 00 01 00 01
3 changes: 3 additions & 0 deletions testdata/p4_16_samples_outputs/calc-ebpf-midend.p4
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ parser Parser(packet_in packet, out headers hdr) {
state check_p4calc {
tmp_3 = packet.lookahead<bit<128>>();
tmp.setValid();
tmp.setValid();
tmp.p = tmp_3[127:120];
tmp.four = tmp_3[119:112];
tmp.ver = tmp_3[111:104];
@@ -48,6 +49,7 @@ parser Parser(packet_in packet, out headers hdr) {
tmp.res = tmp_3[31:0];
tmp_4 = packet.lookahead<bit<128>>();
tmp_0.setValid();
tmp_0.setValid();
tmp_0.p = tmp_4[127:120];
tmp_0.four = tmp_4[119:112];
tmp_0.ver = tmp_4[111:104];
@@ -57,6 +59,7 @@ parser Parser(packet_in packet, out headers hdr) {
tmp_0.res = tmp_4[31:0];
tmp_5 = packet.lookahead<bit<128>>();
tmp_1.setValid();
tmp_1.setValid();
tmp_1.p = tmp_5[127:120];
tmp_1.four = tmp_5[119:112];
tmp_1.ver = tmp_5[111:104];
1 change: 1 addition & 0 deletions testdata/p4_16_samples_outputs/checksum1-bmv2-midend.p4
Original file line number Diff line number Diff line change
@@ -78,6 +78,7 @@ parser parserI(packet_in pkt, out headers hdr, inout metadata meta, inout standa
state parse_ipv4 {
tmp_4 = pkt.lookahead<bit<8>>();
tmp.setValid();
tmp.setValid();
tmp.version = tmp_4[7:4];
tmp.ihl = tmp_4[3:0];
pkt.extract<ipv4_t>(hdr.ipv4, (bit<32>)(((bit<9>)tmp_4[3:0] << 2) + 9w492 << 3));
3 changes: 3 additions & 0 deletions testdata/p4_16_samples_outputs/crc32-bmv2-midend.p4
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ parser MyParser(packet_in packet, out headers hdr, inout metadata meta, inout st
state check_p4calc {
tmp_3 = packet.lookahead<bit<128>>();
tmp.setValid();
tmp.setValid();
tmp.p = tmp_3[127:120];
tmp.four = tmp_3[119:112];
tmp.ver = tmp_3[111:104];
@@ -52,6 +53,7 @@ parser MyParser(packet_in packet, out headers hdr, inout metadata meta, inout st
tmp.res = tmp_3[31:0];
tmp_4 = packet.lookahead<bit<128>>();
tmp_0.setValid();
tmp_0.setValid();
tmp_0.p = tmp_4[127:120];
tmp_0.four = tmp_4[119:112];
tmp_0.ver = tmp_4[111:104];
@@ -61,6 +63,7 @@ parser MyParser(packet_in packet, out headers hdr, inout metadata meta, inout st
tmp_0.res = tmp_4[31:0];
tmp_5 = packet.lookahead<bit<128>>();
tmp_1.setValid();
tmp_1.setValid();
tmp_1.p = tmp_5[127:120];
tmp_1.four = tmp_5[119:112];
tmp_1.ver = tmp_5[111:104];
1 change: 1 addition & 0 deletions testdata/p4_16_samples_outputs/issue1025-bmv2-midend.p4
Original file line number Diff line number Diff line change
@@ -71,6 +71,7 @@ parser parserI(packet_in pkt, out headers hdr, inout metadata meta, inout standa
state parse_ipv4 {
tmp_2 = pkt.lookahead<bit<8>>();
tmp.setValid();
tmp.setValid();
tmp.version = tmp_2[7:4];
tmp.ihl = tmp_2[3:0];
pkt.extract<ipv4_t>(hdr.ipv4, (bit<32>)((bit<9>)tmp_2[3:0] << 3));
1 change: 1 addition & 0 deletions testdata/p4_16_samples_outputs/issue1560-bmv2-midend.p4
Original file line number Diff line number Diff line change
@@ -79,6 +79,7 @@ parser parserI(packet_in pkt, out headers hdr, inout metadata meta, inout standa
state parse_ipv4 {
tmp_4 = pkt.lookahead<bit<8>>();
tmp.setValid();
tmp.setValid();
tmp.version = tmp_4[7:4];
tmp.ihl = tmp_4[3:0];
pkt.extract<ipv4_t>(hdr.ipv4, (bit<32>)(((bit<9>)tmp_4[3:0] << 2) + 9w492 << 3));
1 change: 1 addition & 0 deletions testdata/p4_16_samples_outputs/issue1765-bmv2-midend.p4
Original file line number Diff line number Diff line change
@@ -79,6 +79,7 @@ parser parserI(packet_in pkt, out headers hdr, inout metadata meta, inout standa
state parse_ipv4 {
tmp_4 = pkt.lookahead<bit<8>>();
tmp.setValid();
tmp.setValid();
tmp.version = tmp_4[7:4];
tmp.ihl = tmp_4[3:0];
pkt.extract<ipv4_t>(hdr.ipv4, (bit<32>)(((bit<9>)tmp_4[3:0] << 2) + 9w492 << 3));
1 change: 1 addition & 0 deletions testdata/p4_16_samples_outputs/issue2261-midend.p4
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ parser p(packet_in pkt, out Headers hdr, inout Meta m, inout standard_metadata_t
control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
ethernet_t tmp_struct_0_eth_hdr;
@hidden action issue2261l22() {
tmp_struct_0_eth_hdr.setValid();
tmp_struct_0_eth_hdr.setValid();
tmp_struct_0_eth_hdr.dst_addr = 48w0;
tmp_struct_0_eth_hdr.src_addr = 48w0;
59 changes: 59 additions & 0 deletions testdata/p4_16_samples_outputs/issue2383-bmv2-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

header ethernet_t {
bit<48> dst_addr;
bit<48> src_addr;
bit<16> eth_type;
}

struct Headers {
ethernet_t eth_hdr;
}

struct Meta {
}

ethernet_t do_function() {
return { 48w1, 48w1, 16w1 };
}
parser p(packet_in pkt, out Headers hdr, inout Meta m, inout standard_metadata_t sm) {
state start {
transition parse_hdrs;
}
state parse_hdrs {
pkt.extract<ethernet_t>(hdr.eth_hdr);
transition accept;
}
}

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
apply {
h.eth_hdr = do_function();
}
}

control vrfy(inout Headers h, inout Meta m) {
apply {
}
}

control update(inout Headers h, inout Meta m) {
apply {
}
}

control egress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
apply {
}
}

control deparser(packet_out pkt, in Headers h) {
apply {
pkt.emit<Headers>(h);
}
}

V1Switch<Headers, Meta>(p(), vrfy(), ingress(), egress(), update(), deparser()) main;

59 changes: 59 additions & 0 deletions testdata/p4_16_samples_outputs/issue2383-bmv2-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

header ethernet_t {
bit<48> dst_addr;
bit<48> src_addr;
bit<16> eth_type;
}

struct Headers {
ethernet_t eth_hdr;
}

struct Meta {
}

parser p(packet_in pkt, out Headers hdr, inout Meta m, inout standard_metadata_t sm) {
state start {
pkt.extract<ethernet_t>(hdr.eth_hdr);
transition accept;
}
}

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
apply {
{
bool hasReturned = false;
ethernet_t retval;
hasReturned = true;
retval = { 48w1, 48w1, 16w1 };
h.eth_hdr = retval;
}
}
}

control vrfy(inout Headers h, inout Meta m) {
apply {
}
}

control update(inout Headers h, inout Meta m) {
apply {
}
}

control egress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
apply {
}
}

control deparser(packet_out pkt, in Headers h) {
apply {
pkt.emit<Headers>(h);
}
}

V1Switch<Headers, Meta>(p(), vrfy(), ingress(), egress(), update(), deparser()) main;

Loading