Skip to content

Commit

Permalink
[#4625] Prune MethodCallStatements if child MethodCallExpression
Browse files Browse the repository at this point in the history
…resolves to a compile-time constant. (#4627)

* Prune MethodCallStatements if child MethodCallExpression resolves to a compile-time constant.

* Apply clang formatting

* code cleanup
  • Loading branch information
kfcripps committed Apr 19, 2024
1 parent 81af965 commit 8d21a8b
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 0 deletions.
7 changes: 7 additions & 0 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3615,6 +3615,11 @@ bool TypeInference::onlyBitsOrBitStructs(const IR::Type *type) const {
return false;
}

const IR::Node *TypeInference::postorder(IR::MethodCallStatement *mcs) {
// Remove mcs if child methodCall resolves to a compile-time constant.
return !mcs->methodCall ? nullptr : mcs;
}

const IR::Node *TypeInference::postorder(IR::MethodCallExpression *expression) {
if (done()) return expression;
LOG2("Solving method call " << dbp(expression));
Expand Down Expand Up @@ -3653,6 +3658,7 @@ const IR::Node *TypeInference::postorder(IR::MethodCallExpression *expression) {
LOG3("Folding " << mem << " to " << w);
if (w < 0) return expression;
if (mem->member.name.endsWith("Bytes")) w = ROUNDUP(w, 8);
if (getParent<IR::MethodCallStatement>()) return nullptr;
auto result = new IR::Constant(expression->srcInfo, w);
auto tt = new IR::Type_Type(result->type);
setType(result->type, tt);
Expand All @@ -3670,6 +3676,7 @@ const IR::Node *TypeInference::postorder(IR::MethodCallExpression *expression) {
lit = new IR::BoolLiteral(expression->srcInfo, true);
if (lit) {
LOG3("Folding " << mem << " to " << lit);
if (getParent<IR::MethodCallStatement>()) return nullptr;
setType(lit, IR::Type_Boolean::get());
setCompileTimeConstant(lit);
return lit;
Expand Down
1 change: 1 addition & 0 deletions frontends/p4/typeChecking/typeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ class TypeInference : public Transform {
const IR::Node *postorder(IR::P4ListExpression *expression) override;
const IR::Node *postorder(IR::StructExpression *expression) override;
const IR::Node *postorder(IR::HeaderStackExpression *expression) override;
const IR::Node *postorder(IR::MethodCallStatement *mcs) override;
const IR::Node *postorder(IR::MethodCallExpression *expression) override;
const IR::Node *postorder(IR::ConstructorCallExpression *expression) override;
const IR::Node *postorder(IR::SelectExpression *expression) override;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
header h_t {
bit<8> f;
}

control C() {
action bar() {
h_t h = (h_t){#};
h.isValid();
}

table t {
actions = { bar; }
default_action = bar;
}

apply {
t.apply();
}
}

control proto();
package top(proto p);

top(C()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
header h_t {
bit<8> f;
}

control C() {
action bar() {
h_t h;
h.minSizeInBits();
}

table t {
actions = { bar; }
default_action = bar;
}

apply {
t.apply();
}
}

control proto();
package top(proto p);

top(C()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
header h_t {
bit<8> f;
}

control C() {
action bar() {
h_t h = (h_t){#};
h.isValid();
}
table t {
actions = {
bar();
}
default_action = bar();
}
apply {
t.apply();
}
}

control proto();
package top(proto p);
top(C()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
header h_t {
bit<8> f;
}

control C() {
@name("C.bar") action bar() {
}
@name("C.t") table t_0 {
actions = {
bar();
}
default_action = bar();
}
apply {
t_0.apply();
}
}

control proto();
package top(proto p);
top(C()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
header h_t {
bit<8> f;
}

control C() {
@name("C.bar") action bar() {
}
@name("C.t") table t_0 {
actions = {
bar();
}
default_action = bar();
}
apply {
t_0.apply();
}
}

control proto();
package top(proto p);
top(C()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
header h_t {
bit<8> f;
}

control C() {
action bar() {
h_t h = (h_t){#};
h.isValid();
}
table t {
actions = {
bar;
}
default_action = bar;
}
apply {
t.apply();
}
}

control proto();
package top(proto p);
top(C()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
header h_t {
bit<8> f;
}

control C() {
action bar() {
h_t h;
}
table t {
actions = {
bar();
}
default_action = bar();
}
apply {
t.apply();
}
}

control proto();
package top(proto p);
top(C()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
header h_t {
bit<8> f;
}

control C() {
@name("C.bar") action bar() {
}
@name("C.t") table t_0 {
actions = {
bar();
}
default_action = bar();
}
apply {
t_0.apply();
}
}

control proto();
package top(proto p);
top(C()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
header h_t {
bit<8> f;
}

control C() {
@name("C.bar") action bar() {
}
@name("C.t") table t_0 {
actions = {
bar();
}
default_action = bar();
}
apply {
t_0.apply();
}
}

control proto();
package top(proto p);
top(C()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
header h_t {
bit<8> f;
}

control C() {
action bar() {
h_t h;
h.minSizeInBits();
}
table t {
actions = {
bar;
}
default_action = bar;
}
apply {
t.apply();
}
}

control proto();
package top(proto p);
top(C()) main;

0 comments on commit 8d21a8b

Please sign in to comment.