Skip to content

Commit

Permalink
Add a constant folding pass after inlining (#4727)
Browse files Browse the repository at this point in the history
* Add a constant folding pass after inlining

Signed-off-by: Mihai Budiu <mbudiu@feldera.com>

* Updated reference outputs

Signed-off-by: Mihai Budiu <mbudiu@feldera.com>

---------

Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
  • Loading branch information
mihaibudiu authored Jun 19, 2024
1 parent 7f98dad commit de82285
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 16 deletions.
1 change: 1 addition & 0 deletions frontends/p4/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ const IR::P4Program *FrontEnd::run(const CompilerOptions &options, const IR::P4P
new SetHeaders(&refMap, &typeMap),
// Check for constants only after inlining
new CheckConstants(&refMap, &typeMap),
new ConstantFolding(&refMap, &typeMap, constantFoldingPolicy),
new SimplifyControlFlow(&refMap, &typeMap),
// more ifs may have been added to parsers
new RemoveParserControlFlow(&refMap, &typeMap),
Expand Down
20 changes: 20 additions & 0 deletions testdata/p4_16_samples/issue4548.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <core.p4>

bit<16> fun1(bit<8> args1) {
return (bit<16>)args1;
}
bit<8> fun2(bit<4> args2) {
fun1(8w10);
return 8w0;
}

control ingress() {
apply {
fun2(4w3);
fun2(4w3);
}
}

control Ingress();
package top( Ingress ig);
top( ingress()) main;
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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) {
@name("ingress.retval") bit<16> retval;
apply {
retval = ((Headers){eth_hdr = (ethernet_t){dst_addr = 48w1,src_addr = 48w1,eth_type = 16w1}}).eth_hdr.eth_type + 16w1;
retval = 16w2;
h.eth_hdr.eth_type = retval;
}
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/issue1638-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ control MyC(inout hdr_t hdr, inout meta_t meta, in intrinsic_metadata_t intr_md)
}
@name("MyC.c2.a") table c2_a {
key = {
((meta_t){f0 = 8w0,f1 = 8w0,f2 = 8w0}).f0: exact @name("meta.f0");
8w0: exact @name("meta.f0");
}
actions = {
NoAction_1();
Expand Down
10 changes: 5 additions & 5 deletions testdata/p4_16_samples_outputs/issue1638-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ control MyC(inout hdr_t hdr, inout meta_t meta, in intrinsic_metadata_t intr_md)
}
default_action = NoAction_1();
}
@hidden action issue1638l23() {
@hidden action issue1638l20() {
key_0 = 8w0;
}
@hidden table tbl_issue1638l23 {
@hidden table tbl_issue1638l20 {
actions = {
issue1638l23();
issue1638l20();
}
const default_action = issue1638l23();
const default_action = issue1638l20();
}
apply {
tbl_issue1638l23.apply();
tbl_issue1638l20.apply();
c2_a.apply();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ parser parserImpl(packet_in packet, out headers_t hdr, inout metadata_t meta, in
transition foo_start_0;
}
state foo_start_0 {
hdr.h1.f2 = 8w5 >> 2;
hdr.h1.f2 = 8w1;
transition start_1;
}
state start_1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ control ingressImpl(inout headers_t hdr, inout metadata_t meta, inout standard_m
apply {
tmp = hdr.h1.f1;
hdr.h1.f1 = tmp >> 2;
hdr.h1.f2 = 8w5 >> 2;
hdr.h1.f2 = 8w1;
}
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/issue1937-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ parser parserImpl(out h1_t hdr) {
transition foo_start_0;
}
state foo_start_0 {
hdr.f2 = 8w5 >> 2;
hdr.f2 = 8w1;
transition start_1;
}
state start_1 {
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/issue2303-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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) {
apply {
h.h.a = ((Meta){f0 = 8w0,f1 = 8w0,f2 = 8w0}).f0;
h.h.a = 8w0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/issue2648-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ control ingress(inout Headers h) {
@name("ingress.tmp_0") bit<8> tmp_0;
@name("ingress.retval") bit<8> retval;
apply {
retval = ((H){a = 8w1}).a;
retval = 8w1;
tmp = retval;
tmp_0 = tmp;
h.h[tmp_0].a = 8w1;
Expand Down
4 changes: 1 addition & 3 deletions testdata/p4_16_samples_outputs/issue4500-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ extern void baz();
control c() {
apply {
bar(E.e0);
if (E.e0 == E.e0) {
baz();
}
baz();
}
}

Expand Down
19 changes: 19 additions & 0 deletions testdata/p4_16_samples_outputs/issue4548-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <core.p4>

bit<16> fun1(bit<8> args1) {
return (bit<16>)args1;
}
bit<8> fun2(bit<4> args2) {
fun1(8w10);
return 8w0;
}
control ingress() {
apply {
fun2(4w3);
fun2(4w3);
}
}

control Ingress();
package top(Ingress ig);
top(ingress()) main;
10 changes: 10 additions & 0 deletions testdata/p4_16_samples_outputs/issue4548-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <core.p4>

control ingress() {
apply {
}
}

control Ingress();
package top(Ingress ig);
top(ingress()) main;
10 changes: 10 additions & 0 deletions testdata/p4_16_samples_outputs/issue4548-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <core.p4>

control ingress() {
apply {
}
}

control Ingress();
package top(Ingress ig);
top(ingress()) main;
19 changes: 19 additions & 0 deletions testdata/p4_16_samples_outputs/issue4548.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <core.p4>

bit<16> fun1(bit<8> args1) {
return (bit<16>)args1;
}
bit<8> fun2(bit<4> args2) {
fun1(8w10);
return 8w0;
}
control ingress() {
apply {
fun2(4w3);
fun2(4w3);
}
}

control Ingress();
package top(Ingress ig);
top(ingress()) main;
Empty file.
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/named-arg1-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parser par(out bool b) {
transition adder_0_start;
}
state adder_0_start {
x_0 = 32w0 + 32w6;
x_0 = 32w6;
transition start_0;
}
state start_0 {
Expand Down

0 comments on commit de82285

Please sign in to comment.