Skip to content

Commit

Permalink
Handle correctly general switch statements when simplifying (#3624)
Browse files Browse the repository at this point in the history
* Handle correctly general switch statements when simplifying
* Properly handle side-effects when removing switch statements
Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
  • Loading branch information
Mihai Budiu authored Nov 2, 2022
1 parent 97cffff commit 31e2d5f
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 10 deletions.
24 changes: 14 additions & 10 deletions frontends/p4/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

#include "simplify.h"
#include "sideEffects.h"
#include "tableApply.h"

namespace P4 {

Expand Down Expand Up @@ -92,16 +93,19 @@ const IR::Node* DoSimplifyControlFlow::postorder(IR::EmptyStatement* statement)
const IR::Node* DoSimplifyControlFlow::postorder(IR::SwitchStatement* statement) {
LOG3("Visiting " << dbp(getOriginal()));
if (statement->cases.empty()) {
// The P4_16 spec prohibits expressions other than table application as
// switch conditions. The parser should have rejected programs for
// which this is not the case.
BUG_CHECK(statement->expression->is<IR::Member>(),
"%1%: expected a Member", statement->expression);
auto expr = statement->expression->to<IR::Member>();
BUG_CHECK(expr->expr->is<IR::MethodCallExpression>(),
"%1%: expected a table invocation", expr->expr);
auto mce = expr->expr->to<IR::MethodCallExpression>();
return new IR::MethodCallStatement(mce->srcInfo, mce);
// If this is a table application remove the switch altogether but keep
// the table application. Otherwise remove the switch altogether.
if (TableApplySolver::isActionRun(statement->expression, refMap, typeMap)) {
auto mce = statement->expression->checkedTo<IR::Member>()->
expr->checkedTo<IR::MethodCallExpression>();
LOG2("Removing switch statement " << statement << " keeping " << mce);
return new IR::MethodCallStatement(statement->srcInfo, mce);
}
if (SideEffects::check(statement->expression, this, refMap, typeMap))
// This can happen if this pass is run before SideEffectOrdering.
return statement;
LOG2("Removing switch statement " << statement);
return nullptr;
}
auto last = statement->cases.back();
if (last->statement == nullptr) {
Expand Down
15 changes: 15 additions & 0 deletions testdata/p4_16_samples/issue3619-1.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
bit<4> f(in bit<4> d) {
return d;
}

control c(in bit<4> v) {
apply {
switch(f(v)) {
}
}
}

control C(in bit<4> b);
package top(C c);

top(c()) main;
7 changes: 7 additions & 0 deletions testdata/p4_16_samples/issue3619.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
control c(in bit<4> v) {
apply {
switch(v) {
default:
}
}
}
13 changes: 13 additions & 0 deletions testdata/p4_16_samples_outputs/issue3619-1-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
bit<4> f(in bit<4> d) {
return d;
}
control c(in bit<4> v) {
apply {
switch (f(v)) {
}
}
}

control C(in bit<4> b);
package top(C c);
top(c()) main;
8 changes: 8 additions & 0 deletions testdata/p4_16_samples_outputs/issue3619-1-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
control c(in bit<4> v) {
apply {
}
}

control C(in bit<4> b);
package top(C c);
top(c()) main;
8 changes: 8 additions & 0 deletions testdata/p4_16_samples_outputs/issue3619-1-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
control c(in bit<4> v) {
apply {
}
}

control C(in bit<4> b);
package top(C c);
top(c()) main;
13 changes: 13 additions & 0 deletions testdata/p4_16_samples_outputs/issue3619-1.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
bit<4> f(in bit<4> d) {
return d;
}
control c(in bit<4> v) {
apply {
switch (f(v)) {
}
}
}

control C(in bit<4> b);
package top(C c);
top(c()) main;
Empty file.
5 changes: 5 additions & 0 deletions testdata/p4_16_samples_outputs/issue3619-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
control c(in bit<4> v) {
apply {
}
}

Empty file.
8 changes: 8 additions & 0 deletions testdata/p4_16_samples_outputs/issue3619.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
control c(in bit<4> v) {
apply {
switch (v) {
default:
}
}
}

4 changes: 4 additions & 0 deletions testdata/p4_16_samples_outputs/issue3619.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
issue3619.p4(4): [--Wwarn=missing] warning: SwitchCase: fallthrough with no statement
default:
^^^^^^^
[--Wwarn=missing] warning: Program does not contain a `main' module

0 comments on commit 31e2d5f

Please sign in to comment.