Skip to content

Commit

Permalink
Constant fold casts of serializble enums (#3181)
Browse files Browse the repository at this point in the history
* Constant fold casts of serializble enums
Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
Mihai Budiu authored Apr 7, 2022
1 parent cfa7e70 commit f4337b6
Show file tree
Hide file tree
Showing 25 changed files with 92 additions and 67 deletions.
7 changes: 7 additions & 0 deletions frontends/common/constantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,13 @@ const IR::Node *DoConstantFolding::postorder(IR::Cast *e) {
} else if (auto arg = expr->to<IR::BoolLiteral>()) {
int v = arg->value ? 1 : 0;
return new IR::Constant(e->srcInfo, type, v, 10);
} else if (expr->is<IR::Member>()) {
auto ei = EnumInstance::resolve(expr, typeMap);
if (ei == nullptr)
return e;
if (auto se = ei->to<SerEnumInstance>()) {
return se->value;
}
} else {
return e;
}
Expand Down
5 changes: 3 additions & 2 deletions frontends/p4/commonInlining.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ class InlineDriver : public Visitor {
public:
InlineDriver(
InlineList* toInline, AbstractInliner<InlineList, InlineWorkList> *inliner) :
toInline(toInline), inliner(inliner)
{ CHECK_NULL(toInline); CHECK_NULL(inliner); setName("InlineDriver"); }
toInline(toInline), inliner(inliner) {
CHECK_NULL(toInline); CHECK_NULL(inliner);
setName((cstring("InlineDriver_") + cstring(inliner->name())).c_str()); }
const IR::Node* apply_visitor(const IR::Node *program, const char * = 0) override {
LOG2("InlineDriver");
toInline->analyze();
Expand Down
1 change: 1 addition & 0 deletions frontends/p4/enumInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace P4 {

// static
EnumInstance* EnumInstance::resolve(const IR::Expression* expression, const P4::TypeMap* typeMap) {
CHECK_NULL(typeMap);
if (!expression->is<IR::Member>())
return nullptr;
auto member = expression->to<IR::Member>();
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_14_samples_outputs/copy_to_cpu-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t

control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
@name(".do_copy_to_cpu") action do_copy_to_cpu() {
clone_preserving_field_list(CloneType.I2E, 32w250, (bit<8>)FieldLists.copy_to_cpu_fields);
clone_preserving_field_list(CloneType.I2E, 32w250, 8w1);
}
@name(".copy_to_cpu") table copy_to_cpu {
actions = {
Expand Down
7 changes: 1 addition & 6 deletions testdata/p4_14_samples_outputs/copy_to_cpu-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
#define V1MODEL_VERSION 20200408
#include <v1model.p4>

enum bit<8> FieldLists {
none = 8w0,
copy_to_cpu_fields = 8w1
}

struct intrinsic_metadata_t {
bit<4> mcast_grp;
bit<4> egress_rid;
Expand Down Expand Up @@ -86,7 +81,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
@noWarn("unused") @name(".NoAction") action NoAction_3() {
}
@name(".do_copy_to_cpu") action do_copy_to_cpu() {
clone_preserving_field_list(CloneType.I2E, 32w250, (bit<8>)FieldLists.copy_to_cpu_fields);
clone_preserving_field_list(CloneType.I2E, 32w250, 8w1);
}
@name(".copy_to_cpu") table copy_to_cpu_0 {
actions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710633;
meta.mymeta.f1 = meta.mymeta.f1 + 8w23;
meta.mymeta.clone_e2e_count = meta.mymeta.clone_e2e_count + 8w1;
clone_preserving_field_list(CloneType.E2E, 32w1, (bit<8>)FieldLists.clone_e2e_FL);
clone_preserving_field_list(CloneType.E2E, 32w1, 8w1);
}
@name(".do_recirculate") action do_recirculate() {
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710637;
meta.mymeta.f1 = meta.mymeta.f1 + 8w19;
meta.mymeta.recirculate_count = meta.mymeta.recirculate_count + 8w1;
recirculate_preserving_field_list((bit<8>)FieldLists.recirculate_FL);
recirculate_preserving_field_list(8w2);
}
@name("._nop") action _nop() {
}
Expand Down Expand Up @@ -230,7 +230,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710639;
meta.mymeta.f1 = meta.mymeta.f1 + 8w17;
meta.mymeta.resubmit_count = meta.mymeta.resubmit_count + 8w1;
resubmit_preserving_field_list((bit<8>)FieldLists.resubmit_FL);
resubmit_preserving_field_list(8w3);
}
@name("._nop") action _nop() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710633;
meta.mymeta.f1 = meta.mymeta.f1 + 8w23;
meta.mymeta.clone_e2e_count = meta.mymeta.clone_e2e_count + 8w1;
clone_preserving_field_list(CloneType.E2E, 32w1, (bit<8>)FieldLists.clone_e2e_FL);
clone_preserving_field_list(CloneType.E2E, 32w1, 8w1);
}
@name(".do_recirculate") action do_recirculate() {
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710637;
meta.mymeta.f1 = meta.mymeta.f1 + 8w19;
meta.mymeta.recirculate_count = meta.mymeta.recirculate_count + 8w1;
recirculate_preserving_field_list((bit<8>)FieldLists.recirculate_FL);
recirculate_preserving_field_list(8w2);
}
@name("._nop") action _nop() {
}
Expand Down Expand Up @@ -259,7 +259,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710639;
meta.mymeta.f1 = meta.mymeta.f1 + 8w17;
meta.mymeta.resubmit_count = meta.mymeta.resubmit_count + 8w1;
resubmit_preserving_field_list((bit<8>)FieldLists.resubmit_FL);
resubmit_preserving_field_list(8w3);
}
@name("._nop") action _nop_3() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710633;
meta.mymeta.f1 = meta.mymeta.f1 + 8w23;
meta.mymeta.clone_e2e_count = meta.mymeta.clone_e2e_count + 8w1;
clone_preserving_field_list(CloneType.E2E, 32w1, (bit<8>)FieldLists.clone_e2e_FL);
clone_preserving_field_list(CloneType.E2E, 32w1, 8w1);
}
@name(".do_recirculate") action do_recirculate() {
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710637;
meta.mymeta.f1 = meta.mymeta.f1 + 8w19;
meta.mymeta.recirculate_count = meta.mymeta.recirculate_count + 8w1;
recirculate_preserving_field_list((bit<8>)FieldLists.recirculate_FL);
recirculate_preserving_field_list(8w2);
}
@name("._nop") action _nop() {
}
Expand Down Expand Up @@ -242,7 +242,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710639;
meta.mymeta.f1 = meta.mymeta.f1 + 8w17;
meta.mymeta.resubmit_count = meta.mymeta.resubmit_count + 8w1;
resubmit_preserving_field_list((bit<8>)FieldLists.resubmit_FL);
resubmit_preserving_field_list(8w3);
}
@name("._nop") action _nop() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710633;
meta.mymeta.f1 = meta.mymeta.f1 + 8w23;
meta.mymeta.clone_e2e_count = meta.mymeta.clone_e2e_count + 8w1;
clone_preserving_field_list(CloneType.E2E, 32w1, (bit<8>)FieldLists.clone_e2e_FL);
clone_preserving_field_list(CloneType.E2E, 32w1, 8w1);
}
@name(".do_recirculate") action do_recirculate() {
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710637;
meta.mymeta.f1 = meta.mymeta.f1 + 8w19;
meta.mymeta.recirculate_count = meta.mymeta.recirculate_count + 8w1;
recirculate_preserving_field_list((bit<8>)FieldLists.recirculate_FL);
recirculate_preserving_field_list(8w2);
}
@name("._nop") action _nop() {
}
Expand Down Expand Up @@ -271,7 +271,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710639;
meta.mymeta.f1 = meta.mymeta.f1 + 8w17;
meta.mymeta.resubmit_count = meta.mymeta.resubmit_count + 8w1;
resubmit_preserving_field_list((bit<8>)FieldLists.resubmit_FL);
resubmit_preserving_field_list(8w3);
}
@name("._nop") action _nop_3() {
}
Expand Down
6 changes: 3 additions & 3 deletions testdata/p4_14_samples_outputs/p414-special-ops-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710633;
meta.mymeta.f1 = meta.mymeta.f1 + 8w23;
meta.mymeta.clone_e2e_count = meta.mymeta.clone_e2e_count + 8w1;
clone_preserving_field_list(CloneType.E2E, 32w1, (bit<8>)FieldLists.clone_e2e_FL);
clone_preserving_field_list(CloneType.E2E, 32w1, 8w1);
}
@name(".do_recirculate") action do_recirculate() {
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710637;
meta.mymeta.f1 = meta.mymeta.f1 + 8w19;
meta.mymeta.recirculate_count = meta.mymeta.recirculate_count + 8w1;
recirculate_preserving_field_list((bit<8>)FieldLists.recirculate_FL);
recirculate_preserving_field_list(8w2);
}
@name("._nop") action _nop() {
}
Expand Down Expand Up @@ -229,7 +229,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710639;
meta.mymeta.f1 = meta.mymeta.f1 + 8w17;
meta.mymeta.resubmit_count = meta.mymeta.resubmit_count + 8w1;
resubmit_preserving_field_list((bit<8>)FieldLists.resubmit_FL);
resubmit_preserving_field_list(8w3);
}
@name("._nop") action _nop() {
}
Expand Down
6 changes: 3 additions & 3 deletions testdata/p4_14_samples_outputs/p414-special-ops-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710633;
meta.mymeta.f1 = meta.mymeta.f1 + 8w23;
meta.mymeta.clone_e2e_count = meta.mymeta.clone_e2e_count + 8w1;
clone_preserving_field_list(CloneType.E2E, 32w1, (bit<8>)FieldLists.clone_e2e_FL);
clone_preserving_field_list(CloneType.E2E, 32w1, 8w1);
}
@name(".do_recirculate") action do_recirculate() {
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710637;
meta.mymeta.f1 = meta.mymeta.f1 + 8w19;
meta.mymeta.recirculate_count = meta.mymeta.recirculate_count + 8w1;
recirculate_preserving_field_list((bit<8>)FieldLists.recirculate_FL);
recirculate_preserving_field_list(8w2);
}
@name("._nop") action _nop() {
}
Expand Down Expand Up @@ -258,7 +258,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
hdr.ethernet.srcAddr = hdr.ethernet.srcAddr + 48w281474976710639;
meta.mymeta.f1 = meta.mymeta.f1 + 8w17;
meta.mymeta.resubmit_count = meta.mymeta.resubmit_count + 8w1;
resubmit_preserving_field_list((bit<8>)FieldLists.resubmit_FL);
resubmit_preserving_field_list(8w3);
}
@name("._nop") action _nop_3() {
}
Expand Down
8 changes: 4 additions & 4 deletions testdata/p4_14_samples_outputs/packet_redirect-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
@name("._nop") action _nop() {
}
@name("._recirculate") action _recirculate() {
recirculate_preserving_field_list((bit<8>)FieldLists.redirect_FL);
recirculate_preserving_field_list(8w1);
}
@name("._clone_e2e") action _clone_e2e(bit<32> mirror_id) {
clone_preserving_field_list(CloneType.E2E, mirror_id, (bit<8>)FieldLists.redirect_FL);
clone_preserving_field_list(CloneType.E2E, mirror_id, 8w1);
}
@name(".t_egress") table t_egress {
actions = {
Expand Down Expand Up @@ -88,10 +88,10 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
standard_metadata.mcast_grp = mgrp;
}
@name("._resubmit") action _resubmit() {
resubmit_preserving_field_list((bit<8>)FieldLists.redirect_FL);
resubmit_preserving_field_list(8w1);
}
@name("._clone_i2e") action _clone_i2e(bit<32> mirror_id) {
clone_preserving_field_list(CloneType.I2E, mirror_id, (bit<8>)FieldLists.redirect_FL);
clone_preserving_field_list(CloneType.I2E, mirror_id, 8w1);
}
@name(".t_ingress_1") table t_ingress_1 {
actions = {
Expand Down
8 changes: 4 additions & 4 deletions testdata/p4_14_samples_outputs/packet_redirect-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
@name("._nop") action _nop() {
}
@name("._recirculate") action _recirculate() {
recirculate_preserving_field_list((bit<8>)FieldLists.redirect_FL);
recirculate_preserving_field_list(8w1);
}
@name("._clone_e2e") action _clone_e2e(@name("mirror_id") bit<32> mirror_id) {
clone_preserving_field_list(CloneType.E2E, mirror_id, (bit<8>)FieldLists.redirect_FL);
clone_preserving_field_list(CloneType.E2E, mirror_id, 8w1);
}
@name(".t_egress") table t_egress_0 {
actions = {
Expand Down Expand Up @@ -96,10 +96,10 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
standard_metadata.mcast_grp = mgrp;
}
@name("._resubmit") action _resubmit() {
resubmit_preserving_field_list((bit<8>)FieldLists.redirect_FL);
resubmit_preserving_field_list(8w1);
}
@name("._clone_i2e") action _clone_i2e(@name("mirror_id") bit<32> mirror_id_2) {
clone_preserving_field_list(CloneType.I2E, mirror_id_2, (bit<8>)FieldLists.redirect_FL);
clone_preserving_field_list(CloneType.I2E, mirror_id_2, 8w1);
}
@name(".t_ingress_1") table t_ingress {
actions = {
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_14_samples_outputs/resubmit-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
}
@name("._resubmit") action _resubmit() {
meta.mymeta.f1 = 8w1;
resubmit_preserving_field_list((bit<8>)FieldLists.resubmit_FL);
resubmit_preserving_field_list(8w1);
}
@name(".t_ingress_1") table t_ingress_1 {
actions = {
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_14_samples_outputs/resubmit-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
}
@name("._resubmit") action _resubmit() {
meta.mymeta.f1 = 8w1;
resubmit_preserving_field_list((bit<8>)FieldLists.resubmit_FL);
resubmit_preserving_field_list(8w1);
}
@name(".t_ingress_1") table t_ingress {
actions = {
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_14_samples_outputs/simple_nat-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
hdr.ipv4.ttl = hdr.ipv4.ttl + 8w255;
}
@name(".nat_miss_int_to_ext") action nat_miss_int_to_ext() {
clone_preserving_field_list(CloneType.I2E, 32w250, (bit<8>)FieldLists.copy_to_cpu_fields);
clone_preserving_field_list(CloneType.I2E, 32w250, 8w1);
}
@name(".nat_miss_ext_to_int") action nat_miss_ext_to_int() {
meta.meta.do_forward = 1w0;
Expand Down
7 changes: 1 addition & 6 deletions testdata/p4_14_samples_outputs/simple_nat-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
#define V1MODEL_VERSION 20200408
#include <v1model.p4>

enum bit<8> FieldLists {
none = 8w0,
copy_to_cpu_fields = 8w1
}

struct intrinsic_metadata_t {
bit<4> mcast_grp;
bit<4> egress_rid;
Expand Down Expand Up @@ -211,7 +206,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
hdr.ipv4.ttl = hdr.ipv4.ttl + 8w255;
}
@name(".nat_miss_int_to_ext") action nat_miss_int_to_ext() {
clone_preserving_field_list(CloneType.I2E, 32w250, (bit<8>)FieldLists.copy_to_cpu_fields);
clone_preserving_field_list(CloneType.I2E, 32w250, 8w1);
}
@name(".nat_miss_ext_to_int") action nat_miss_ext_to_int() {
meta.meta.do_forward = 1w0;
Expand Down
Loading

0 comments on commit f4337b6

Please sign in to comment.