Skip to content

Commit

Permalink
Require type decl in for..in loops (#4808)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Dodd <cdodd@nvidia.com>
  • Loading branch information
ChrisDodd authored Jul 16, 2024
1 parent d323ad4 commit 578e2df
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 30 deletions.
8 changes: 7 additions & 1 deletion frontends/p4/toP4/toP4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,13 @@ bool ToP4::preorder(const IR::ForInStatement *s) {
builder.supressStatementSemi();
visit(s->decl, "decl");
} else {
visit(s->ref, "ref");
auto *decl = resolveUnique(s->ref->path->name, ResolutionType::Any);
if (auto *di = decl->to<IR::Declaration_Variable>()) {
builder.supressStatementSemi();
visit(di, "decl");
} else {
visit(s->ref, "ref");
}
}
builder.append(" in ");
visit(s->collection);
Expand Down
3 changes: 2 additions & 1 deletion frontends/p4/toP4/toP4.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
#ifndef P4_TOP4_TOP4_H_
#define P4_TOP4_TOP4_H_

#include "frontends/common/resolveReferences/resolveReferences.h"
#include "ir/ir.h"
#include "ir/visitor.h"
#include "lib/sourceCodeBuilder.h"
Expand All @@ -27,7 +28,7 @@ namespace P4 {
This pass converts a P4-16 IR into a P4 source (text) program.
It can optionally emit as comments a representation of the program IR.
*/
class ToP4 : public Inspector {
class ToP4 : public Inspector, ResolutionContext {
int expressionPrecedence; /// precedence of current IR::Operation
bool isDeclaration; /// current type is a declaration
bool showIR; /// if true dump IR as comments
Expand Down
5 changes: 3 additions & 2 deletions frontends/parsers/p4/p4parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,8 +1317,9 @@ forStatement
| optAnnotations FOR "(" typeRef name IN forCollectionExpr ")" statement
{ auto decl = new IR::Declaration_Variable(@4+@5, *$5, $4);
$$ = new IR::ForInStatement(@2+@7, $1, decl, $7, $9); }
| optAnnotations FOR "(" prefixedNonTypeName IN forCollectionExpr ")" statement
{ $$ = new IR::ForInStatement(@2+@7, $1, new IR::PathExpression(@4, $4), $6, $8); }
| optAnnotations FOR "(" annotations typeRef name IN forCollectionExpr ")" statement
{ auto decl = new IR::Declaration_Variable(@5+@6, *$6, new IR::Annotations(@4, *$4), $5);
$$ = new IR::ForInStatement(@2+@8, $1, decl, $8, $10); }
;

forInitStatements
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/forloop1-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ control c(inout headers_t hdrs) {
@name("c.v") bit<8> v_1;
@name("c.x") bit<16> x_0;
apply {
for (v_0 in hdrs.stack) {
for (@name("c.v") t2 v_0 in hdrs.stack) {
hdrs.head.f1 = hdrs.head.f1 + v_0.x;
}
for (v_1 = 8w0, x_0 = 16w1; v_1 < hdrs.head.cnt; v_1 = v_1 + 8w1, x_0 = x_0 << 1) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/forloop1-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ control c(inout headers_t hdrs) {
const default_action = forloop1l27();
}
apply {
for (v_0 in hdrs.stack) {
for (@name("c.v") t2 v_0 in hdrs.stack) {
tbl_forloop1l24.apply();
}
for (v_1 = 8w0, x_0 = 16w1; v_1 < hdrs.head.cnt; v_1 = v_1 + 8w1, x_0 = x_0 << 1) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/forloop2-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ control c(inout headers_t hdrs) {
hasReturned = false;
n_0 = 64w0;
v_0 = val;
for (popcnti_0 in 64w1 .. 64w63) {
for (@name("c.popcnti") bit<64> popcnti_0 in 64w1 .. 64w63) {
if (v_0 == 64w0) {
hasReturned = true;
retval = n_0;
Expand Down
8 changes: 4 additions & 4 deletions testdata/p4_16_samples_outputs/forloop3-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ control c(inout headers_t hdrs) {
@noWarn("unused") @name(".NoAction") action NoAction_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) {
for (@name("c.i1") bit<8> i1_0 in 8w0 .. x_2) {
for (@name("c.j") bit<8> j_0 in i1_0 .. y) {
idx_0 = foo<bit<8>>(j_0);
if (idx_0 == 8w255) {
break;
Expand All @@ -31,8 +31,8 @@ control c(inout headers_t hdrs) {
}
}
@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) {
for (@name("c.i") bit<8> i_0 in 8w0 .. x_3) {
for (@name("c.j") bit<8> j_1 in 8w0 .. y_2) {
idx_1 = foo<bit<8>>(j_1);
if (idx_1 == 8w255) {
continue;
Expand Down
8 changes: 4 additions & 4 deletions testdata/p4_16_samples_outputs/forloop3-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ control c(inout headers_t hdrs) {
@noWarn("unused") @name(".NoAction") action NoAction_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) {
for (@name("c.i1") bit<8> i1_0 in 8w0 .. x_2) {
for (@name("c.j") bit<8> j_0 in i1_0 .. y) {
idx_0 = foo<bit<8>>(j_0);
if (idx_0 == 8w255) {
break;
Expand All @@ -31,8 +31,8 @@ control c(inout headers_t hdrs) {
}
}
@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) {
for (@name("c.i") bit<8> i_0 in 8w0 .. x_3) {
for (@name("c.j") bit<8> j_1 in 8w0 .. y_2) {
idx_1 = foo<bit<8>>(j_1);
if (idx_1 == 8w255) {
continue;
Expand Down
14 changes: 7 additions & 7 deletions testdata/p4_16_samples_outputs/forloop4-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ control c(inout headers_t hdrs) {
@noWarn("unused") @name(".NoAction") action NoAction_1() {
}
@name("c.a0") action a0(@name("x") bit<8> x_3, @name("y") bit<8> y) {
for (i_0 in 8w1 .. x_3 + y) {
for (@name("c.i") bit<8> i_0 in 8w1 .. x_3 + y) {
;
}
for (i_1 in 8w1 .. x_3 + y) {
for (@name("c.i") bit<8> i_1 in 8w1 .. x_3 + y) {
;
}
for (i_2 in 8w1 .. 8w42) {
for (@name("c.i") bit<8> i_2 in 8w1 .. 8w42) {
;
}
}
@name("c.a1") action a1(@name("x") bit<8> x_4, @name("y") bit<8> y_3) {
for (i_3 in 8w1 .. x_4) {
for (j_0 in i_3 .. y_3) {
for (@name("c.i") bit<8> i_3 in 8w1 .. x_4) {
for (@name("c.j") bit<8> j_0 in i_3 .. y_3) {
;
}
}
}
@name("c.a2") action a2(@name("x") bit<8> x_5, @name("y") bit<8> y_4) {
i_4 = 8w10;
for (i_5 in 8w1 .. x_5) {
for (@name("c.i") bit<8> i_5 in 8w1 .. x_5) {
;
}
for (k_0 in i_4 .. x_5 + y_4) {
for (@name("c.k") bit<8> k_0 in i_4 .. x_5 + y_4) {
;
}
}
Expand Down
12 changes: 6 additions & 6 deletions testdata/p4_16_samples_outputs/forloop4-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ control c(inout headers_t hdrs) {
@noWarn("unused") @name(".NoAction") action NoAction_1() {
}
@name("c.a0") action a0(@name("x") bit<8> x_3, @name("y") bit<8> y) {
for (i_0 in 8w1 .. x_3 + y) {
for (@name("c.i") bit<8> i_0 in 8w1 .. x_3 + y) {
;
}
for (i_1 in 8w1 .. x_3 + y) {
for (@name("c.i") bit<8> i_1 in 8w1 .. x_3 + y) {
;
}
}
@name("c.a1") action a1(@name("x") bit<8> x_4, @name("y") bit<8> y_3) {
for (i_3 in 8w1 .. x_4) {
for (j_0 in i_3 .. y_3) {
for (@name("c.i") bit<8> i_3 in 8w1 .. x_4) {
for (@name("c.j") bit<8> j_0 in i_3 .. y_3) {
;
}
}
}
@name("c.a2") action a2(@name("x") bit<8> x_5, @name("y") bit<8> y_4) {
for (i_5 in 8w1 .. x_5) {
for (@name("c.i") bit<8> i_5 in 8w1 .. x_5) {
;
}
for (k_0 in 8w10 .. x_5 + y_4) {
for (@name("c.k") bit<8> k_0 in 8w10 .. x_5 + y_4) {
;
}
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/forloop7-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ control c(inout headers_t hdrs) {
}
@name("c.a0") action a0(@name("m") bit<8> m_1) {
hasReturned = false;
for (a_0 in 16w1 .. 16w2) {
for (@name("c.a") bit<16> a_0 in 16w1 .. 16w2) {
fn(a_0, 16w0);
for (b_0 in 16w1 .. 16w2) {
for (@name("c.b") bit<16> b_0 in 16w1 .. 16w2) {
tmp = fn(a_0, b_0);
if (tmp) {
continue;
Expand Down

0 comments on commit 578e2df

Please sign in to comment.