Skip to content

Commit

Permalink
Make HasTableApply a resolution context, so it could resolve declarat…
Browse files Browse the repository at this point in the history
…ions on its own if desired (p4lang#4781)

* Make HasTableApply a resolution context, so it could resolve declarations on its own if desired

Fixes p4lang#4775

Signed-off-by: Anton Korobeynikov <[email protected]>

* Get rid of refMap in HasTableApply. Some cleanup while there

Signed-off-by: Anton Korobeynikov <[email protected]>

---------

Signed-off-by: Anton Korobeynikov <[email protected]>
  • Loading branch information
asl authored Jul 8, 2024
1 parent 988e738 commit 5be356c
Show file tree
Hide file tree
Showing 18 changed files with 293 additions and 16 deletions.
2 changes: 1 addition & 1 deletion backends/dpdk/dpdkArch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ const IR::Node *CopyMatchKeysToSingleStruct::doStatement(const IR::Statement *st
const IR::Expression *expression,
const Visitor::Context *ctxt) {
LOG3("Visiting " << getOriginal());
P4::HasTableApply hta(this, typeMap);
P4::HasTableApply hta(typeMap);
hta.setCalledBy(this);
(void)expression->apply(hta, ctxt);
if (hta.table == nullptr) return statement;
Expand Down
2 changes: 1 addition & 1 deletion frontends/common/resolveReferences/resolveReferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ std::vector<const IR::IDeclaration *> ResolutionContext::lookupMatchKind(const I
const IR::Vector<IR::Argument> *ResolutionContext::methodArguments(cstring name) const {
const Context *ctxt = getChildContext();
while (ctxt) {
const auto *node = ctxt->node;
const auto *node = ctxt->original;
const IR::MethodCallExpression *mc = nullptr;
if (const auto *mcs = node->to<IR::MethodCallStatement>())
mc = mcs->methodCall;
Expand Down
3 changes: 2 additions & 1 deletion frontends/p4/sideEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,9 @@ const IR::Node *KeySideEffect::doStatement(const IR::Statement *statement,
const IR::Expression *expression,
const Visitor::Context *ctxt) {
LOG3("Visiting " << getOriginal());
HasTableApply hta(this, typeMap);
HasTableApply hta(typeMap);
hta.setCalledBy(this);

(void)expression->apply(hta, ctxt);
if (hta.table == nullptr) return statement;
auto insertions = get(toInsert, hta.table);
Expand Down
17 changes: 7 additions & 10 deletions frontends/p4/sideEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@ limitations under the License.
namespace P4 {

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

public:
const IR::P4Table *table;
const IR::MethodCallExpression *call;
HasTableApply(DeclarationLookup *refMap, TypeMap *typeMap)
: refMap(refMap), typeMap(typeMap), table(nullptr), call(nullptr) {
CHECK_NULL(refMap);
explicit HasTableApply(TypeMap *typeMap) : typeMap(typeMap), table(nullptr), call(nullptr) {
CHECK_NULL(typeMap);
setName("HasTableApply");
}

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

Expand All @@ -358,7 +355,7 @@ class TablesInKeys : public Inspector, ResolutionContext {
}
void postorder(const IR::MethodCallExpression *mce) override {
if (!findContext<IR::Key>()) return;
HasTableApply hta(this, typeMap);
HasTableApply hta(typeMap);
hta.setCalledBy(this);
(void)mce->apply(hta, getContext());
if (hta.table != nullptr) {
Expand All @@ -373,14 +370,14 @@ class TablesInKeys : public Inspector, ResolutionContext {
/// table s { ... }
/// table t {
/// actions { a(s.apply().hit ? ... ); }
class TablesInActions : public Inspector, ResolutionContext {
class TablesInActions : public Inspector {
TypeMap *typeMap;

public:
explicit TablesInActions(TypeMap *typeMap) : typeMap(typeMap) {}
void postorder(const IR::MethodCallExpression *expression) override {
if (findContext<IR::ActionList>()) {
HasTableApply hta(this, typeMap);
HasTableApply hta(typeMap);
hta.setCalledBy(this);
(void)expression->apply(hta, getContext());
if (hta.table != nullptr) {
Expand Down
3 changes: 2 additions & 1 deletion midend/hsIndexSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ class IsNonConstantArrayIndex : public KeyIsSimple, public Inspector {

IR::Node *HSIndexContretizer::preorder(IR::P4Control *control) {
DoSimplifyKey keySimplifier(refMap, typeMap, new IsNonConstantArrayIndex());
const auto *controlKeySimplified = control->apply(keySimplifier)->to<IR::P4Control>();
const auto *controlKeySimplified =
control->apply(keySimplifier, getContext())->to<IR::P4Control>();
auto *newControl = controlKeySimplified->clone();
IR::IndexedVector<IR::Declaration> newControlLocals;
GeneratedVariablesMap blockGeneratedVariables;
Expand Down
4 changes: 2 additions & 2 deletions midend/simplifyKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ const IR::Node *DoSimplifyKey::postorder(IR::P4Table *table) {
const IR::Node *DoSimplifyKey::doStatement(const IR::Statement *statement,
const IR::Expression *expression) {
LOG3("Visiting " << getOriginal());
HasTableApply hta(refMap, typeMap);
HasTableApply hta(typeMap);
hta.setCalledBy(this);
(void)expression->apply(hta);
(void)expression->apply(hta, getContext());
if (hta.table == nullptr) return statement;
auto insertions = get(toInsert, hta.table);
if (insertions == nullptr) return statement;
Expand Down
26 changes: 26 additions & 0 deletions testdata/p4_16_samples/issue4775-2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
bit<8> add_1(in bit<8> a, in bit<8> b) { return 1; }
bit<8> add_1(in bit<8> c, in bit<8> d) { return 2; }

header hdr_t {
bit<8> value_1;
bit<8> value_2;
bit<8> value_3;
bit<8> value_4;
bit<8> value_5;
bit<8> value_6;
bit<8> value_7;
}

control c(inout hdr_t hdr) {
apply {
if (hdr.isValid()) {
hdr.value_6 = add_1(a = hdr.value_1, b = hdr.value_2);
hdr.value_7 = add_1(d = hdr.value_3, c = hdr.value_4);
}
}
}

control ctrl(inout hdr_t hdr);
package top(ctrl _c);

top(c()) main;
26 changes: 26 additions & 0 deletions testdata/p4_16_samples/issue4775.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
bit<8> add(in bit<8> a, in bit<8> b) { return 1; }
bit<8> add(in bit<8> a, in bit<8> b, in bit<8> c) { return 2; }

header hdr_t {
bit<8> value_1;
bit<8> value_2;
bit<8> value_3;
bit<8> value_4;
bit<8> value_5;
bit<8> value_6;
bit<8> value_7;
}

control c(inout hdr_t hdr) {
apply {
if (hdr.isValid()) {
hdr.value_3 = add(hdr.value_1, hdr.value_2);
hdr.value_7 = add(hdr.value_4, hdr.value_5, hdr.value_6);
}
}
}

control ctrl(inout hdr_t hdr);
package top(ctrl _c);

top(c()) main;
28 changes: 28 additions & 0 deletions testdata/p4_16_samples_outputs/issue4775-2-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
bit<8> add_1(in bit<8> a, in bit<8> b) {
return 8w1;
}
bit<8> add_1(in bit<8> c, in bit<8> d) {
return 8w2;
}
header hdr_t {
bit<8> value_1;
bit<8> value_2;
bit<8> value_3;
bit<8> value_4;
bit<8> value_5;
bit<8> value_6;
bit<8> value_7;
}

control c(inout hdr_t hdr) {
apply {
if (hdr.isValid()) {
hdr.value_6 = add_1(a = hdr.value_1, b = hdr.value_2);
hdr.value_7 = add_1(d = hdr.value_3, c = hdr.value_4);
}
}
}

control ctrl(inout hdr_t hdr);
package top(ctrl _c);
top(c()) main;
26 changes: 26 additions & 0 deletions testdata/p4_16_samples_outputs/issue4775-2-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
header hdr_t {
bit<8> value_1;
bit<8> value_2;
bit<8> value_3;
bit<8> value_4;
bit<8> value_5;
bit<8> value_6;
bit<8> value_7;
}

control c(inout hdr_t hdr) {
@name("c.retval") bit<8> retval;
@name("c.retval_0") bit<8> retval_0;
apply {
if (hdr.isValid()) {
retval = 8w1;
hdr.value_6 = retval;
retval_0 = 8w2;
hdr.value_7 = retval_0;
}
}
}

control ctrl(inout hdr_t hdr);
package top(ctrl _c);
top(c()) main;
31 changes: 31 additions & 0 deletions testdata/p4_16_samples_outputs/issue4775-2-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
header hdr_t {
bit<8> value_1;
bit<8> value_2;
bit<8> value_3;
bit<8> value_4;
bit<8> value_5;
bit<8> value_6;
bit<8> value_7;
}

control c(inout hdr_t hdr) {
@hidden action issue47752l17() {
hdr.value_6 = 8w1;
hdr.value_7 = 8w2;
}
@hidden table tbl_issue47752l17 {
actions = {
issue47752l17();
}
const default_action = issue47752l17();
}
apply {
if (hdr.isValid()) {
tbl_issue47752l17.apply();
}
}
}

control ctrl(inout hdr_t hdr);
package top(ctrl _c);
top(c()) main;
28 changes: 28 additions & 0 deletions testdata/p4_16_samples_outputs/issue4775-2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
bit<8> add_1(in bit<8> a, in bit<8> b) {
return 1;
}
bit<8> add_1(in bit<8> c, in bit<8> d) {
return 2;
}
header hdr_t {
bit<8> value_1;
bit<8> value_2;
bit<8> value_3;
bit<8> value_4;
bit<8> value_5;
bit<8> value_6;
bit<8> value_7;
}

control c(inout hdr_t hdr) {
apply {
if (hdr.isValid()) {
hdr.value_6 = add_1(a = hdr.value_1, b = hdr.value_2);
hdr.value_7 = add_1(d = hdr.value_3, c = hdr.value_4);
}
}
}

control ctrl(inout hdr_t hdr);
package top(ctrl _c);
top(c()) main;
Empty file.
28 changes: 28 additions & 0 deletions testdata/p4_16_samples_outputs/issue4775-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
bit<8> add(in bit<8> a, in bit<8> b) {
return 8w1;
}
bit<8> add(in bit<8> a, in bit<8> b, in bit<8> c) {
return 8w2;
}
header hdr_t {
bit<8> value_1;
bit<8> value_2;
bit<8> value_3;
bit<8> value_4;
bit<8> value_5;
bit<8> value_6;
bit<8> value_7;
}

control c(inout hdr_t hdr) {
apply {
if (hdr.isValid()) {
hdr.value_3 = add(hdr.value_1, hdr.value_2);
hdr.value_7 = add(hdr.value_4, hdr.value_5, hdr.value_6);
}
}
}

control ctrl(inout hdr_t hdr);
package top(ctrl _c);
top(c()) main;
26 changes: 26 additions & 0 deletions testdata/p4_16_samples_outputs/issue4775-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
header hdr_t {
bit<8> value_1;
bit<8> value_2;
bit<8> value_3;
bit<8> value_4;
bit<8> value_5;
bit<8> value_6;
bit<8> value_7;
}

control c(inout hdr_t hdr) {
@name("c.retval") bit<8> retval;
@name("c.retval_0") bit<8> retval_0;
apply {
if (hdr.isValid()) {
retval = 8w1;
hdr.value_3 = retval;
retval_0 = 8w2;
hdr.value_7 = retval_0;
}
}
}

control ctrl(inout hdr_t hdr);
package top(ctrl _c);
top(c()) main;
31 changes: 31 additions & 0 deletions testdata/p4_16_samples_outputs/issue4775-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
header hdr_t {
bit<8> value_1;
bit<8> value_2;
bit<8> value_3;
bit<8> value_4;
bit<8> value_5;
bit<8> value_6;
bit<8> value_7;
}

control c(inout hdr_t hdr) {
@hidden action issue4775l17() {
hdr.value_3 = 8w1;
hdr.value_7 = 8w2;
}
@hidden table tbl_issue4775l17 {
actions = {
issue4775l17();
}
const default_action = issue4775l17();
}
apply {
if (hdr.isValid()) {
tbl_issue4775l17.apply();
}
}
}

control ctrl(inout hdr_t hdr);
package top(ctrl _c);
top(c()) main;
28 changes: 28 additions & 0 deletions testdata/p4_16_samples_outputs/issue4775.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
bit<8> add(in bit<8> a, in bit<8> b) {
return 1;
}
bit<8> add(in bit<8> a, in bit<8> b, in bit<8> c) {
return 2;
}
header hdr_t {
bit<8> value_1;
bit<8> value_2;
bit<8> value_3;
bit<8> value_4;
bit<8> value_5;
bit<8> value_6;
bit<8> value_7;
}

control c(inout hdr_t hdr) {
apply {
if (hdr.isValid()) {
hdr.value_3 = add(hdr.value_1, hdr.value_2);
hdr.value_7 = add(hdr.value_4, hdr.value_5, hdr.value_6);
}
}
}

control ctrl(inout hdr_t hdr);
package top(ctrl _c);
top(c()) main;
Empty file.

0 comments on commit 5be356c

Please sign in to comment.