Skip to content

Commit 5be356c

Browse files
authored
Make HasTableApply a resolution context, so it could resolve declarations on its own if desired (#4781)
* Make HasTableApply a resolution context, so it could resolve declarations on its own if desired Fixes #4775 Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info> * Get rid of refMap in HasTableApply. Some cleanup while there Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info> --------- Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
1 parent 988e738 commit 5be356c

18 files changed

+293
-16
lines changed

backends/dpdk/dpdkArch.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@ const IR::Node *CopyMatchKeysToSingleStruct::doStatement(const IR::Statement *st
17271727
const IR::Expression *expression,
17281728
const Visitor::Context *ctxt) {
17291729
LOG3("Visiting " << getOriginal());
1730-
P4::HasTableApply hta(this, typeMap);
1730+
P4::HasTableApply hta(typeMap);
17311731
hta.setCalledBy(this);
17321732
(void)expression->apply(hta, ctxt);
17331733
if (hta.table == nullptr) return statement;

frontends/common/resolveReferences/resolveReferences.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ std::vector<const IR::IDeclaration *> ResolutionContext::lookupMatchKind(const I
183183
const IR::Vector<IR::Argument> *ResolutionContext::methodArguments(cstring name) const {
184184
const Context *ctxt = getChildContext();
185185
while (ctxt) {
186-
const auto *node = ctxt->node;
186+
const auto *node = ctxt->original;
187187
const IR::MethodCallExpression *mc = nullptr;
188188
if (const auto *mcs = node->to<IR::MethodCallStatement>())
189189
mc = mcs->methodCall;

frontends/p4/sideEffects.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -800,8 +800,9 @@ const IR::Node *KeySideEffect::doStatement(const IR::Statement *statement,
800800
const IR::Expression *expression,
801801
const Visitor::Context *ctxt) {
802802
LOG3("Visiting " << getOriginal());
803-
HasTableApply hta(this, typeMap);
803+
HasTableApply hta(typeMap);
804804
hta.setCalledBy(this);
805+
805806
(void)expression->apply(hta, ctxt);
806807
if (hta.table == nullptr) return statement;
807808
auto insertions = get(toInsert, hta.table);

frontends/p4/sideEffects.h

+7-10
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,19 @@ limitations under the License.
2828
namespace P4 {
2929

3030
/// Checks to see whether an IR node includes a table.apply() sub-expression
31-
class HasTableApply : public Inspector {
32-
DeclarationLookup *refMap;
31+
class HasTableApply : public Inspector, public ResolutionContext {
3332
TypeMap *typeMap;
3433

3534
public:
3635
const IR::P4Table *table;
3736
const IR::MethodCallExpression *call;
38-
HasTableApply(DeclarationLookup *refMap, TypeMap *typeMap)
39-
: refMap(refMap), typeMap(typeMap), table(nullptr), call(nullptr) {
40-
CHECK_NULL(refMap);
37+
explicit HasTableApply(TypeMap *typeMap) : typeMap(typeMap), table(nullptr), call(nullptr) {
4138
CHECK_NULL(typeMap);
4239
setName("HasTableApply");
4340
}
4441

4542
void postorder(const IR::MethodCallExpression *expression) override {
46-
auto mi = MethodInstance::resolve(expression, refMap, typeMap);
43+
auto mi = MethodInstance::resolve(expression, this, typeMap);
4744
if (!mi->isApply()) return;
4845
auto am = mi->to<P4::ApplyMethod>();
4946
if (!am->object->is<IR::P4Table>()) return;
@@ -342,7 +339,7 @@ class KeySideEffect : public Transform, public ResolutionContext {
342339
/// table Y { ... }
343340
/// table X { key = { ... Y.apply.hit() ... } }
344341
/// This inserts Y into the map invokedInKey;
345-
class TablesInKeys : public Inspector, ResolutionContext {
342+
class TablesInKeys : public Inspector {
346343
TypeMap *typeMap;
347344
std::set<const IR::P4Table *> *invokedInKey;
348345

@@ -358,7 +355,7 @@ class TablesInKeys : public Inspector, ResolutionContext {
358355
}
359356
void postorder(const IR::MethodCallExpression *mce) override {
360357
if (!findContext<IR::Key>()) return;
361-
HasTableApply hta(this, typeMap);
358+
HasTableApply hta(typeMap);
362359
hta.setCalledBy(this);
363360
(void)mce->apply(hta, getContext());
364361
if (hta.table != nullptr) {
@@ -373,14 +370,14 @@ class TablesInKeys : public Inspector, ResolutionContext {
373370
/// table s { ... }
374371
/// table t {
375372
/// actions { a(s.apply().hit ? ... ); }
376-
class TablesInActions : public Inspector, ResolutionContext {
373+
class TablesInActions : public Inspector {
377374
TypeMap *typeMap;
378375

379376
public:
380377
explicit TablesInActions(TypeMap *typeMap) : typeMap(typeMap) {}
381378
void postorder(const IR::MethodCallExpression *expression) override {
382379
if (findContext<IR::ActionList>()) {
383-
HasTableApply hta(this, typeMap);
380+
HasTableApply hta(typeMap);
384381
hta.setCalledBy(this);
385382
(void)expression->apply(hta, getContext());
386383
if (hta.table != nullptr) {

midend/hsIndexSimplify.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ class IsNonConstantArrayIndex : public KeyIsSimple, public Inspector {
160160

161161
IR::Node *HSIndexContretizer::preorder(IR::P4Control *control) {
162162
DoSimplifyKey keySimplifier(refMap, typeMap, new IsNonConstantArrayIndex());
163-
const auto *controlKeySimplified = control->apply(keySimplifier)->to<IR::P4Control>();
163+
const auto *controlKeySimplified =
164+
control->apply(keySimplifier, getContext())->to<IR::P4Control>();
164165
auto *newControl = controlKeySimplified->clone();
165166
IR::IndexedVector<IR::Declaration> newControlLocals;
166167
GeneratedVariablesMap blockGeneratedVariables;

midend/simplifyKey.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ const IR::Node *DoSimplifyKey::postorder(IR::P4Table *table) {
8080
const IR::Node *DoSimplifyKey::doStatement(const IR::Statement *statement,
8181
const IR::Expression *expression) {
8282
LOG3("Visiting " << getOriginal());
83-
HasTableApply hta(refMap, typeMap);
83+
HasTableApply hta(typeMap);
8484
hta.setCalledBy(this);
85-
(void)expression->apply(hta);
85+
(void)expression->apply(hta, getContext());
8686
if (hta.table == nullptr) return statement;
8787
auto insertions = get(toInsert, hta.table);
8888
if (insertions == nullptr) return statement;

testdata/p4_16_samples/issue4775-2.p4

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
bit<8> add_1(in bit<8> a, in bit<8> b) { return 1; }
2+
bit<8> add_1(in bit<8> c, in bit<8> d) { return 2; }
3+
4+
header hdr_t {
5+
bit<8> value_1;
6+
bit<8> value_2;
7+
bit<8> value_3;
8+
bit<8> value_4;
9+
bit<8> value_5;
10+
bit<8> value_6;
11+
bit<8> value_7;
12+
}
13+
14+
control c(inout hdr_t hdr) {
15+
apply {
16+
if (hdr.isValid()) {
17+
hdr.value_6 = add_1(a = hdr.value_1, b = hdr.value_2);
18+
hdr.value_7 = add_1(d = hdr.value_3, c = hdr.value_4);
19+
}
20+
}
21+
}
22+
23+
control ctrl(inout hdr_t hdr);
24+
package top(ctrl _c);
25+
26+
top(c()) main;

testdata/p4_16_samples/issue4775.p4

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
bit<8> add(in bit<8> a, in bit<8> b) { return 1; }
2+
bit<8> add(in bit<8> a, in bit<8> b, in bit<8> c) { return 2; }
3+
4+
header hdr_t {
5+
bit<8> value_1;
6+
bit<8> value_2;
7+
bit<8> value_3;
8+
bit<8> value_4;
9+
bit<8> value_5;
10+
bit<8> value_6;
11+
bit<8> value_7;
12+
}
13+
14+
control c(inout hdr_t hdr) {
15+
apply {
16+
if (hdr.isValid()) {
17+
hdr.value_3 = add(hdr.value_1, hdr.value_2);
18+
hdr.value_7 = add(hdr.value_4, hdr.value_5, hdr.value_6);
19+
}
20+
}
21+
}
22+
23+
control ctrl(inout hdr_t hdr);
24+
package top(ctrl _c);
25+
26+
top(c()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
bit<8> add_1(in bit<8> a, in bit<8> b) {
2+
return 8w1;
3+
}
4+
bit<8> add_1(in bit<8> c, in bit<8> d) {
5+
return 8w2;
6+
}
7+
header hdr_t {
8+
bit<8> value_1;
9+
bit<8> value_2;
10+
bit<8> value_3;
11+
bit<8> value_4;
12+
bit<8> value_5;
13+
bit<8> value_6;
14+
bit<8> value_7;
15+
}
16+
17+
control c(inout hdr_t hdr) {
18+
apply {
19+
if (hdr.isValid()) {
20+
hdr.value_6 = add_1(a = hdr.value_1, b = hdr.value_2);
21+
hdr.value_7 = add_1(d = hdr.value_3, c = hdr.value_4);
22+
}
23+
}
24+
}
25+
26+
control ctrl(inout hdr_t hdr);
27+
package top(ctrl _c);
28+
top(c()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
header hdr_t {
2+
bit<8> value_1;
3+
bit<8> value_2;
4+
bit<8> value_3;
5+
bit<8> value_4;
6+
bit<8> value_5;
7+
bit<8> value_6;
8+
bit<8> value_7;
9+
}
10+
11+
control c(inout hdr_t hdr) {
12+
@name("c.retval") bit<8> retval;
13+
@name("c.retval_0") bit<8> retval_0;
14+
apply {
15+
if (hdr.isValid()) {
16+
retval = 8w1;
17+
hdr.value_6 = retval;
18+
retval_0 = 8w2;
19+
hdr.value_7 = retval_0;
20+
}
21+
}
22+
}
23+
24+
control ctrl(inout hdr_t hdr);
25+
package top(ctrl _c);
26+
top(c()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
header hdr_t {
2+
bit<8> value_1;
3+
bit<8> value_2;
4+
bit<8> value_3;
5+
bit<8> value_4;
6+
bit<8> value_5;
7+
bit<8> value_6;
8+
bit<8> value_7;
9+
}
10+
11+
control c(inout hdr_t hdr) {
12+
@hidden action issue47752l17() {
13+
hdr.value_6 = 8w1;
14+
hdr.value_7 = 8w2;
15+
}
16+
@hidden table tbl_issue47752l17 {
17+
actions = {
18+
issue47752l17();
19+
}
20+
const default_action = issue47752l17();
21+
}
22+
apply {
23+
if (hdr.isValid()) {
24+
tbl_issue47752l17.apply();
25+
}
26+
}
27+
}
28+
29+
control ctrl(inout hdr_t hdr);
30+
package top(ctrl _c);
31+
top(c()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
bit<8> add_1(in bit<8> a, in bit<8> b) {
2+
return 1;
3+
}
4+
bit<8> add_1(in bit<8> c, in bit<8> d) {
5+
return 2;
6+
}
7+
header hdr_t {
8+
bit<8> value_1;
9+
bit<8> value_2;
10+
bit<8> value_3;
11+
bit<8> value_4;
12+
bit<8> value_5;
13+
bit<8> value_6;
14+
bit<8> value_7;
15+
}
16+
17+
control c(inout hdr_t hdr) {
18+
apply {
19+
if (hdr.isValid()) {
20+
hdr.value_6 = add_1(a = hdr.value_1, b = hdr.value_2);
21+
hdr.value_7 = add_1(d = hdr.value_3, c = hdr.value_4);
22+
}
23+
}
24+
}
25+
26+
control ctrl(inout hdr_t hdr);
27+
package top(ctrl _c);
28+
top(c()) main;

testdata/p4_16_samples_outputs/issue4775-2.p4-stderr

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
bit<8> add(in bit<8> a, in bit<8> b) {
2+
return 8w1;
3+
}
4+
bit<8> add(in bit<8> a, in bit<8> b, in bit<8> c) {
5+
return 8w2;
6+
}
7+
header hdr_t {
8+
bit<8> value_1;
9+
bit<8> value_2;
10+
bit<8> value_3;
11+
bit<8> value_4;
12+
bit<8> value_5;
13+
bit<8> value_6;
14+
bit<8> value_7;
15+
}
16+
17+
control c(inout hdr_t hdr) {
18+
apply {
19+
if (hdr.isValid()) {
20+
hdr.value_3 = add(hdr.value_1, hdr.value_2);
21+
hdr.value_7 = add(hdr.value_4, hdr.value_5, hdr.value_6);
22+
}
23+
}
24+
}
25+
26+
control ctrl(inout hdr_t hdr);
27+
package top(ctrl _c);
28+
top(c()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
header hdr_t {
2+
bit<8> value_1;
3+
bit<8> value_2;
4+
bit<8> value_3;
5+
bit<8> value_4;
6+
bit<8> value_5;
7+
bit<8> value_6;
8+
bit<8> value_7;
9+
}
10+
11+
control c(inout hdr_t hdr) {
12+
@name("c.retval") bit<8> retval;
13+
@name("c.retval_0") bit<8> retval_0;
14+
apply {
15+
if (hdr.isValid()) {
16+
retval = 8w1;
17+
hdr.value_3 = retval;
18+
retval_0 = 8w2;
19+
hdr.value_7 = retval_0;
20+
}
21+
}
22+
}
23+
24+
control ctrl(inout hdr_t hdr);
25+
package top(ctrl _c);
26+
top(c()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
header hdr_t {
2+
bit<8> value_1;
3+
bit<8> value_2;
4+
bit<8> value_3;
5+
bit<8> value_4;
6+
bit<8> value_5;
7+
bit<8> value_6;
8+
bit<8> value_7;
9+
}
10+
11+
control c(inout hdr_t hdr) {
12+
@hidden action issue4775l17() {
13+
hdr.value_3 = 8w1;
14+
hdr.value_7 = 8w2;
15+
}
16+
@hidden table tbl_issue4775l17 {
17+
actions = {
18+
issue4775l17();
19+
}
20+
const default_action = issue4775l17();
21+
}
22+
apply {
23+
if (hdr.isValid()) {
24+
tbl_issue4775l17.apply();
25+
}
26+
}
27+
}
28+
29+
control ctrl(inout hdr_t hdr);
30+
package top(ctrl _c);
31+
top(c()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
bit<8> add(in bit<8> a, in bit<8> b) {
2+
return 1;
3+
}
4+
bit<8> add(in bit<8> a, in bit<8> b, in bit<8> c) {
5+
return 2;
6+
}
7+
header hdr_t {
8+
bit<8> value_1;
9+
bit<8> value_2;
10+
bit<8> value_3;
11+
bit<8> value_4;
12+
bit<8> value_5;
13+
bit<8> value_6;
14+
bit<8> value_7;
15+
}
16+
17+
control c(inout hdr_t hdr) {
18+
apply {
19+
if (hdr.isValid()) {
20+
hdr.value_3 = add(hdr.value_1, hdr.value_2);
21+
hdr.value_7 = add(hdr.value_4, hdr.value_5, hdr.value_6);
22+
}
23+
}
24+
}
25+
26+
control ctrl(inout hdr_t hdr);
27+
package top(ctrl _c);
28+
top(c()) main;

testdata/p4_16_samples_outputs/issue4775.p4-stderr

Whitespace-only changes.

0 commit comments

Comments
 (0)