diff --git a/midend/local_copyprop.cpp b/midend/local_copyprop.cpp index 1ad76baabf..ced0b5a228 100644 --- a/midend/local_copyprop.cpp +++ b/midend/local_copyprop.cpp @@ -585,8 +585,10 @@ void DoLocalCopyPropagation::apply_function(DoLocalCopyPropagation::FuncInfo *ac void DoLocalCopyPropagation::apply_table(DoLocalCopyPropagation::TableInfo *tbl) { ++tbl->apply_count; + std::unordered_set remaps_seen; for (auto key : tbl->keyreads) { - forOverlapAvail(key, [key, tbl, this](cstring vname, VarInfo *var) { + forOverlapAvail(key, [&remaps_seen, key, tbl, this](cstring vname, VarInfo *var) { + remaps_seen.insert(vname); if (var->val && lvalue_out(var->val)->is()) { if (tbl->apply_count > 1 && (!tbl->key_remap.count(vname) || !tbl->key_remap.at(vname)->equiv(*var->val))) { @@ -606,6 +608,12 @@ void DoLocalCopyPropagation::apply_table(DoLocalCopyPropagation::TableInfo *tbl) LOG4(" table using " << key << " with " << (var->val ? "value to complex for key" : "no propagated value")); var->live = true; } }); } + for (auto it = tbl->key_remap.begin(); it != tbl->key_remap.end(); ) { + if (remaps_seen.count(it->first) == 0) { + LOG3(" no value used for some applies for key " << it->first); + it = tbl->key_remap.erase(it); + } else { + ++it; } } for (auto action : tbl->actions) apply_function(&actions[action]); } diff --git a/testdata/p4_16_samples/copyprop1.p4 b/testdata/p4_16_samples/copyprop1.p4 new file mode 100644 index 0000000000..3c41015cfb --- /dev/null +++ b/testdata/p4_16_samples/copyprop1.p4 @@ -0,0 +1,65 @@ +#include +#include + +header payload_t { + bit<8> x; + bit<8> y; +} +struct header_t { + payload_t payload; +} +struct metadata {} + +parser MyParser(packet_in packet, + out header_t hdr, + inout metadata meta, + inout standard_metadata_t standard_metadata) { + state start { + packet.extract(hdr.payload); + transition accept; + } +} + +control MyIngress(inout header_t hdr, + inout metadata meta, + inout standard_metadata_t standard_metadata) { + + action a1() { + hdr.payload.x = 0xaa; + } + + table t1 { + key = { hdr.payload.x : exact; } + actions = { a1; } + size = 1024; + } + + apply { + if (hdr.payload.y == 0) { + hdr.payload.x = hdr.payload.y; + t1.apply(); + } else { + t1.apply(); + } + standard_metadata.egress_spec = 2; + } +} + +control MyVerifyChecksum(inout header_t hdr, inout metadata meta) { apply { } } +control MyEgress(inout header_t hdr, inout metadata meta, + inout standard_metadata_t standard_metadata) { apply { } } +control MyDeparser(packet_out packet, in header_t hdr) { + apply { + packet.emit(hdr); + } +} +control MyComputeChecksum(inout header_t hdr, inout metadata meta) { apply { } } + +V1Switch( +MyParser(), +MyVerifyChecksum(), +MyIngress(), +MyEgress(), +MyComputeChecksum(), +MyDeparser() +) main; diff --git a/testdata/p4_16_samples_outputs/copyprop1-first.p4 b/testdata/p4_16_samples_outputs/copyprop1-first.p4 new file mode 100644 index 0000000000..2d37494f48 --- /dev/null +++ b/testdata/p4_16_samples_outputs/copyprop1-first.p4 @@ -0,0 +1,72 @@ +#include +#define V1MODEL_VERSION 20180101 +#include + +header payload_t { + bit<8> x; + bit<8> y; +} + +struct header_t { + payload_t payload; +} + +struct metadata { +} + +parser MyParser(packet_in packet, out header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + state start { + packet.extract(hdr.payload); + transition accept; + } +} + +control MyIngress(inout header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + action a1() { + hdr.payload.x = 8w0xaa; + } + table t1 { + key = { + hdr.payload.x: exact @name("hdr.payload.x") ; + } + actions = { + a1(); + @defaultonly NoAction(); + } + size = 1024; + default_action = NoAction(); + } + apply { + if (hdr.payload.y == 8w0) { + hdr.payload.x = hdr.payload.y; + t1.apply(); + } else { + t1.apply(); + } + standard_metadata.egress_spec = 9w2; + } +} + +control MyVerifyChecksum(inout header_t hdr, inout metadata meta) { + apply { + } +} + +control MyEgress(inout header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + apply { + } +} + +control MyDeparser(packet_out packet, in header_t hdr) { + apply { + packet.emit(hdr); + } +} + +control MyComputeChecksum(inout header_t hdr, inout metadata meta) { + apply { + } +} + +V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main; + diff --git a/testdata/p4_16_samples_outputs/copyprop1-frontend.p4 b/testdata/p4_16_samples_outputs/copyprop1-frontend.p4 new file mode 100644 index 0000000000..1321c9dacd --- /dev/null +++ b/testdata/p4_16_samples_outputs/copyprop1-frontend.p4 @@ -0,0 +1,74 @@ +#include +#define V1MODEL_VERSION 20180101 +#include + +header payload_t { + bit<8> x; + bit<8> y; +} + +struct header_t { + payload_t payload; +} + +struct metadata { +} + +parser MyParser(packet_in packet, out header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + state start { + packet.extract(hdr.payload); + transition accept; + } +} + +control MyIngress(inout header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + @noWarn("unused") @name(".NoAction") action NoAction_1() { + } + @name("MyIngress.a1") action a1() { + hdr.payload.x = 8w0xaa; + } + @name("MyIngress.t1") table t1_0 { + key = { + hdr.payload.x: exact @name("hdr.payload.x") ; + } + actions = { + a1(); + @defaultonly NoAction_1(); + } + size = 1024; + default_action = NoAction_1(); + } + apply { + if (hdr.payload.y == 8w0) { + hdr.payload.x = hdr.payload.y; + t1_0.apply(); + } else { + t1_0.apply(); + } + standard_metadata.egress_spec = 9w2; + } +} + +control MyVerifyChecksum(inout header_t hdr, inout metadata meta) { + apply { + } +} + +control MyEgress(inout header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + apply { + } +} + +control MyDeparser(packet_out packet, in header_t hdr) { + apply { + packet.emit(hdr); + } +} + +control MyComputeChecksum(inout header_t hdr, inout metadata meta) { + apply { + } +} + +V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main; + diff --git a/testdata/p4_16_samples_outputs/copyprop1-midend.p4 b/testdata/p4_16_samples_outputs/copyprop1-midend.p4 new file mode 100644 index 0000000000..49eb7e1e71 --- /dev/null +++ b/testdata/p4_16_samples_outputs/copyprop1-midend.p4 @@ -0,0 +1,92 @@ +#include +#define V1MODEL_VERSION 20180101 +#include + +header payload_t { + bit<8> x; + bit<8> y; +} + +struct header_t { + payload_t payload; +} + +struct metadata { +} + +parser MyParser(packet_in packet, out header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + state start { + packet.extract(hdr.payload); + transition accept; + } +} + +control MyIngress(inout header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + @noWarn("unused") @name(".NoAction") action NoAction_1() { + } + @name("MyIngress.a1") action a1() { + hdr.payload.x = 8w0xaa; + } + @name("MyIngress.t1") table t1_0 { + key = { + hdr.payload.x: exact @name("hdr.payload.x") ; + } + actions = { + a1(); + @defaultonly NoAction_1(); + } + size = 1024; + default_action = NoAction_1(); + } + @hidden action copyprop1l39() { + hdr.payload.x = hdr.payload.y; + } + @hidden action copyprop1l44() { + standard_metadata.egress_spec = 9w2; + } + @hidden table tbl_copyprop1l39 { + actions = { + copyprop1l39(); + } + const default_action = copyprop1l39(); + } + @hidden table tbl_copyprop1l44 { + actions = { + copyprop1l44(); + } + const default_action = copyprop1l44(); + } + apply { + if (hdr.payload.y == 8w0) { + tbl_copyprop1l39.apply(); + t1_0.apply(); + } else { + t1_0.apply(); + } + tbl_copyprop1l44.apply(); + } +} + +control MyVerifyChecksum(inout header_t hdr, inout metadata meta) { + apply { + } +} + +control MyEgress(inout header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + apply { + } +} + +control MyDeparser(packet_out packet, in header_t hdr) { + apply { + packet.emit(hdr.payload); + } +} + +control MyComputeChecksum(inout header_t hdr, inout metadata meta) { + apply { + } +} + +V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main; + diff --git a/testdata/p4_16_samples_outputs/copyprop1.p4 b/testdata/p4_16_samples_outputs/copyprop1.p4 new file mode 100644 index 0000000000..3ae79d1ec5 --- /dev/null +++ b/testdata/p4_16_samples_outputs/copyprop1.p4 @@ -0,0 +1,70 @@ +#include +#define V1MODEL_VERSION 20180101 +#include + +header payload_t { + bit<8> x; + bit<8> y; +} + +struct header_t { + payload_t payload; +} + +struct metadata { +} + +parser MyParser(packet_in packet, out header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + state start { + packet.extract(hdr.payload); + transition accept; + } +} + +control MyIngress(inout header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + action a1() { + hdr.payload.x = 0xaa; + } + table t1 { + key = { + hdr.payload.x: exact; + } + actions = { + a1; + } + size = 1024; + } + apply { + if (hdr.payload.y == 0) { + hdr.payload.x = hdr.payload.y; + t1.apply(); + } else { + t1.apply(); + } + standard_metadata.egress_spec = 2; + } +} + +control MyVerifyChecksum(inout header_t hdr, inout metadata meta) { + apply { + } +} + +control MyEgress(inout header_t hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { + apply { + } +} + +control MyDeparser(packet_out packet, in header_t hdr) { + apply { + packet.emit(hdr); + } +} + +control MyComputeChecksum(inout header_t hdr, inout metadata meta) { + apply { + } +} + +V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main; + diff --git a/testdata/p4_16_samples_outputs/copyprop1.p4-stderr b/testdata/p4_16_samples_outputs/copyprop1.p4-stderr new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testdata/p4_16_samples_outputs/copyprop1.p4.entries.txt b/testdata/p4_16_samples_outputs/copyprop1.p4.entries.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testdata/p4_16_samples_outputs/copyprop1.p4.p4info.txt b/testdata/p4_16_samples_outputs/copyprop1.p4.p4info.txt new file mode 100644 index 0000000000..0069013e17 --- /dev/null +++ b/testdata/p4_16_samples_outputs/copyprop1.p4.p4info.txt @@ -0,0 +1,42 @@ +pkg_info { + arch: "v1model" +} +tables { + preamble { + id: 44659798 + name: "MyIngress.t1" + alias: "t1" + } + match_fields { + id: 1 + name: "hdr.payload.x" + bitwidth: 8 + match_type: EXACT + } + action_refs { + id: 23754841 + } + action_refs { + id: 21257015 + annotations: "@defaultonly" + scope: DEFAULT_ONLY + } + size: 1024 +} +actions { + preamble { + id: 21257015 + name: "NoAction" + alias: "NoAction" + annotations: "@noWarn(\"unused\")" + } +} +actions { + preamble { + id: 23754841 + name: "MyIngress.a1" + alias: "a1" + } +} +type_info { +}