Skip to content

Commit

Permalink
Properly handle side-effects when removing switch statements
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
  • Loading branch information
Mihai Budiu committed Oct 28, 2022
1 parent b5980b6 commit dcd7656
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 5 deletions.
13 changes: 8 additions & 5 deletions frontends/p4/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ const IR::Node* DoSimplifyControlFlow::postorder(IR::SwitchStatement* statement)
if (statement->cases.empty()) {
// If this is a table application remove the switch altogether but keep
// the table application. Otherwise remove the switch altogether.
if (auto mem = statement->expression->to<IR::Member>()) {
if (auto mce = mem->expr->to<IR::MethodCallExpression>()) {
LOG2("Removing switch statement " << statement << " keeping " << mce);
return new IR::MethodCallStatement(mce->srcInfo, mce);
}
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;
}
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;
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.

0 comments on commit dcd7656

Please sign in to comment.