Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle correctly general switch statements when simplifying #3624

Merged
merged 2 commits into from
Nov 2, 2022

Conversation

mihaibudiu
Copy link
Contributor

Signed-off-by: Mihai Budiu mbudiu@vmware.com
Fixes #3619

Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
@apinski-cavium
Copy link

Does this handle side effects in the switch expression that is:

action NoAction() {}
control c(in bit<4> v)
{
  table t { actions = {} }
  apply{
    switch(t.apply().hit) {
      default:
    }
  }
}

And still have the table apply there?

@mihaibudiu
Copy link
Contributor Author

It should, if there is a method call in the switch statement it is preserved.
I was assuming this pass is run after the pass which moves side effects in separate statements, but I have to check.

@mihaibudiu
Copy link
Contributor Author

I don't think this is conservative enough, since it is also run before the side-effect-ordering pass.
I will improve it.

@mihaibudiu mihaibudiu marked this pull request as draft October 26, 2022 17:46
@mihaibudiu mihaibudiu marked this pull request as ready for review October 28, 2022 18:49
Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
"%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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test for this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also another test for a function call with side-effects could be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue3619-1.p4 is testing the side-effect.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that function call is side-effect free. Also this does not include a table.apply(), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pass does not know that the function has no side effects. All tests with table applications would qualify for the second case.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (SideEffects::check(statement->expression, this, refMap, typeMap))
    // This can happen if this pass is run before SideEffectOrdering.
    return statement;

I am trying to understand this segment. If the function call has side effects, ideally only the method call statement should remain, right? I do not think we have a test for this.

Similarly,
if (TableApplySolver::isActionRun(statement->expression, refMap, typeMap)) {
should check whether there is an action_run call and then only leave the table apply statement.

I assume we have tests already for this case? Since this was the default behavior.

Copy link
Contributor Author

@mihaibudiu mihaibudiu Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the argument of the switch may have side-effects then we leave the whole switch unchaged. This pass is invoked again later, after SideEffectOrdering has removed calls with side effects from switch statements (all except table.apply().action_run), and that case will never occur again.

Copy link
Member

@rst0git rst0git left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mihaibudiu mihaibudiu merged commit 31e2d5f into p4lang:main Nov 2, 2022
@mihaibudiu mihaibudiu deleted the issue3619 branch November 2, 2022 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler bug with switch fall through at the end (and no other statements)
4 participants