Skip to content

Commit

Permalink
Fix toP4 for break/continue + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDodd committed Apr 7, 2024
1 parent b92c5b2 commit 325675f
Show file tree
Hide file tree
Showing 13 changed files with 414 additions and 9 deletions.
14 changes: 14 additions & 0 deletions frontends/p4/toP4/toP4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,20 @@ bool ToP4::preorder(const IR::BlockStatement *s) {
return false;
}

bool ToP4::preorder(const IR::BreakStatement *) {
dump(1);
builder.append("break");
builder.endOfStatement();
return false;
}

bool ToP4::preorder(const IR::ContinueStatement *) {
dump(1);
builder.append("continue");
builder.endOfStatement();
return false;
}

bool ToP4::preorder(const IR::ExitStatement *) {
dump(1);
builder.append("exit");
Expand Down
2 changes: 2 additions & 0 deletions frontends/p4/toP4/toP4.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ class ToP4 : public Inspector {
bool preorder(const IR::MethodCallStatement *s) override;
bool preorder(const IR::EmptyStatement *s) override;
bool preorder(const IR::ReturnStatement *s) override;
bool preorder(const IR::BreakStatement *s) override;
bool preorder(const IR::ContinueStatement *s) override;
bool preorder(const IR::ExitStatement *s) override;
bool preorder(const IR::SwitchCase *s) override;
bool preorder(const IR::SwitchStatement *s) override;
Expand Down
15 changes: 14 additions & 1 deletion testdata/p4_16_samples/forloop3.p4
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,22 @@ control c(inout headers_t hdrs) {
}
}

action a1(bit<8> x, bit<8> y) {
bit<8> idx = 255;
for (bit<8> i in 0 .. x) {
for (bit<8> j in 0 .. y) {
idx = foo(j);
if (idx == 255) {
continue;
}
idx = foo(i);
}
}
}

table test {
key = { hdrs.t1.x: exact; }
actions = { a0; }
actions = { a0; a1; }
}

apply {
Expand Down
66 changes: 66 additions & 0 deletions testdata/p4_16_samples/forloop4.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <core.p4>
control generic<M>(inout M m);
package top<M>(generic<M> c);

header t1 {
bit<32> x;
}

struct headers_t {
t1 t1;
}

bit<8> foo(in bit<8> aa, in bit<8> bb) {
// Do some magic to ensure aa / bb will not be simplified

return aa - bb;
}

control c(inout headers_t hdrs) {
action a0(bit<8> x, bit<8> y) {
// Simple loops: with and w/o use of index variable.
for (bit<8> i in 1 .. (x+y)) {
foo(x, y);
}

for (bit<8> i in 1 .. (x+y)) {
foo(x, y + i);
}

// And with constant range as well
for (bit<8> i in 8w1 .. 42) {
foo(x, y + i);
}
}

action a1(bit<8> x, bit<8> y) {
// Nested loops
for (bit<8> i in 1 .. x) {
for (bit<8> j in i .. y)
foo(x, y + i);
}
}

// Scope test
action a2(bit<8> x, bit<8> y) {
bit<8> i = 10;
for (bit<8> i in 1 .. x) {
foo(x, y + i);
}

for (bit<8> k in i .. (x+y)) {
foo(i, 2*i+x);
}
}

table test {
key = { hdrs.t1.x: exact; }
actions = { a0; a1; a2; }
}

apply {
test.apply();
}
}

top(c()) main;
15 changes: 14 additions & 1 deletion testdata/p4_16_samples_outputs/forloop3-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,30 @@ control c(inout headers_t hdrs) {
for (bit<8> j in i1 .. y) {
idx = foo<bit<8>>(j);
if (idx == 8w255) {

break;
}
}
}
}
action a1(bit<8> x, bit<8> y) {
bit<8> idx = 8w255;
for (bit<8> i in 8w0 .. x) {
for (bit<8> j in 8w0 .. y) {
idx = foo<bit<8>>(j);
if (idx == 8w255) {
continue;
}
idx = foo<bit<8>>(i);
}
}
}
table test {
key = {
hdrs.t1.x: exact @name("hdrs.t1.x");
}
actions = {
a0();
a1();
@defaultonly NoAction();
}
default_action = NoAction();
Expand Down
21 changes: 18 additions & 3 deletions testdata/p4_16_samples_outputs/forloop3-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,39 @@ control c(inout headers_t hdrs) {
@name("c.idx") bit<8> idx_0;
@name("c.i1") bit<8> i1_0;
@name("c.j") bit<8> j_0;
@name("c.idx") bit<8> idx_1;
@name("c.i") bit<8> i_0;
@name("c.j") bit<8> j_1;
@noWarn("unused") @name(".NoAction") action NoAction_1() {
}
@name("c.a0") action a0(@name("x") bit<8> x_1, @name("y") bit<8> y) {
for (i1_0 in 8w0 .. x_1) {
@name("c.a0") action a0(@name("x") bit<8> x_2, @name("y") bit<8> y) {
for (i1_0 in 8w0 .. x_2) {
for (j_0 in i1_0 .. y) {
idx_0 = foo<bit<8>>(j_0);
if (idx_0 == 8w255) {

break;
}
}
}
}
@name("c.a1") action a1(@name("x") bit<8> x_3, @name("y") bit<8> y_2) {
for (i_0 in 8w0 .. x_3) {
for (j_1 in 8w0 .. y_2) {
idx_1 = foo<bit<8>>(j_1);
if (idx_1 == 8w255) {
continue;
}
foo<bit<8>>(i_0);
}
}
}
@name("c.test") table test_0 {
key = {
hdrs.t1.x: exact @name("hdrs.t1.x");
}
actions = {
a0();
a1();
@defaultonly NoAction_1();
}
default_action = NoAction_1();
Expand Down
21 changes: 18 additions & 3 deletions testdata/p4_16_samples_outputs/forloop3-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,39 @@ control c(inout headers_t hdrs) {
@name("c.idx") bit<8> idx_0;
@name("c.i1") bit<8> i1_0;
@name("c.j") bit<8> j_0;
@name("c.idx") bit<8> idx_1;
@name("c.i") bit<8> i_0;
@name("c.j") bit<8> j_1;
@noWarn("unused") @name(".NoAction") action NoAction_1() {
}
@name("c.a0") action a0(@name("x") bit<8> x_1, @name("y") bit<8> y) {
for (i1_0 in 8w0 .. x_1) {
@name("c.a0") action a0(@name("x") bit<8> x_2, @name("y") bit<8> y) {
for (i1_0 in 8w0 .. x_2) {
for (j_0 in i1_0 .. y) {
idx_0 = foo<bit<8>>(j_0);
if (idx_0 == 8w255) {

break;
}
}
}
}
@name("c.a1") action a1(@name("x") bit<8> x_3, @name("y") bit<8> y_2) {
for (i_0 in 8w0 .. x_3) {
for (j_1 in 8w0 .. y_2) {
idx_1 = foo<bit<8>>(j_1);
if (idx_1 == 8w255) {
continue;
}
foo<bit<8>>(i_0);
}
}
}
@name("c.test") table test_0 {
key = {
hdrs.t1.x: exact @name("hdrs.t1.x");
}
actions = {
a0();
a1();
@defaultonly NoAction_1();
}
default_action = NoAction_1();
Expand Down
15 changes: 14 additions & 1 deletion testdata/p4_16_samples_outputs/forloop3.p4
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,30 @@ control c(inout headers_t hdrs) {
for (bit<8> j in i1 .. y) {
idx = foo(j);
if (idx == 255) {

break;
}
}
}
}
action a1(bit<8> x, bit<8> y) {
bit<8> idx = 255;
for (bit<8> i in 0 .. x) {
for (bit<8> j in 0 .. y) {
idx = foo(j);
if (idx == 255) {
continue;
}
idx = foo(i);
}
}
}
table test {
key = {
hdrs.t1.x: exact;
}
actions = {
a0;
a1;
}
}
apply {
Expand Down
61 changes: 61 additions & 0 deletions testdata/p4_16_samples_outputs/forloop4-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <core.p4>

control generic<M>(inout M m);
package top<M>(generic<M> c);
header t1 {
bit<32> x;
}

struct headers_t {
t1 t1;
}

bit<8> foo(in bit<8> aa, in bit<8> bb) {
return aa - bb;
}
control c(inout headers_t hdrs) {
action a0(bit<8> x, bit<8> y) {
for (bit<8> i in 8w1 .. x + y) {
foo(x, y);
}
for (bit<8> i in 8w1 .. x + y) {
foo(x, y + i);
}
for (bit<8> i in 8w1 .. 8w42) {
foo(x, y + i);
}
}
action a1(bit<8> x, bit<8> y) {
for (bit<8> i in 8w1 .. x) {
for (bit<8> j in i .. y) {
foo(x, y + i);
}
}
}
action a2(bit<8> x, bit<8> y) {
bit<8> i = 8w10;
for (bit<8> i in 8w1 .. x) {
foo(x, y + i);
}
for (bit<8> k in i .. x + y) {
foo(i, (i << 1) + x);
}
}
table test {
key = {
hdrs.t1.x: exact @name("hdrs.t1.x");
}
actions = {
a0();
a1();
a2();
@defaultonly NoAction();
}
default_action = NoAction();
}
apply {
test.apply();
}
}

top<headers_t>(c()) main;
Loading

0 comments on commit 325675f

Please sign in to comment.