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

Fix incorrect copy propagation into table match keys #3399

Merged
merged 2 commits into from
Jun 22, 2022

Conversation

vhavel
Copy link

@vhavel vhavel commented Jun 20, 2022

If table is applied at multiple places, local copy propagation must ensure expressions are propagated only if all control flow paths to all apply method calls assign the same expression to a variable. Currently, LocalCopyPropagation correctly inhibits copy propagation into match key k if two different paths to apply() call assign a different (not equivalent) expressions into k. But if one path does not assign any value to k, and all other assign k=a, e.g.:

if (cond) {
    k = a;
    t.apply();
} else {
    t.apply();
}

the current implementation incorrectly propagates a into the table's match key (rewriting k to a).

This PR fixes this problem by forbidding copy propagation of a match key in presence of a path without any remap. Before this change, the testcase testdata/p4_16_samples/copyprop1.p4 fails because copyprop1-midend.p4 incorrectly matches on hdr.payload.y instead of x.

Vojtech Havel added 2 commits June 20, 2022 08:24
The `y` field is incorrectly copy-propagated into the match key, but
this is incorrect due to the false if branch where `y` isn't assigned
into `x`.
Copy propagation is currently inhibited if two branches assign
not-equivalent expressions into a match key. This commit also inhibits
copy propagation when one branch assigns an expression into a match key,
but another doesn't assign no expression into the key.
@mihaibudiu
Copy link
Contributor

@ChrisDodd wrote this code, so he should review this PR.

@mihaibudiu mihaibudiu merged commit 49a08d6 into p4lang:main Jun 22, 2022
@vhavel vhavel deleted the vh/fix_copyprop_tablekeys branch June 24, 2022 07:56
vhavel pushed a commit to vhavel/p4c that referenced this pull request Jul 15, 2022
* Add (now failing) testcase for copyprop bug

The `y` field is incorrectly copy-propagated into the match key, but
this is incorrect due to the false if branch where `y` isn't assigned
into `x`.

* Fix local copy propagation for table match keys

Copy propagation is currently inhibited if two branches assign
not-equivalent expressions into a match key. This commit also inhibits
copy propagation when one branch assigns an expression into a match key,
but another doesn't assign no expression into the key.
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.

3 participants