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

Testgen: fix varbit handling #3761

Merged
merged 1 commit into from
Feb 27, 2023
Merged

Conversation

vhavel
Copy link

@vhavel vhavel commented Dec 7, 2022

Instead of enriching the program IR by a custom varbit type which remembers extracted size, assign bit<extracted_size> directly to varbit members.

The original approach with custom Extracted_Varbit type is problematic: the type inherits from bit type where X is the maximum size of corresponding varbit type, however the width_bits method is also overridden to return the actually extracted varbit size.

@fruffy fruffy added the p4tools Topics related to the P4Tools back end label Dec 8, 2022
@vhavel vhavel force-pushed the vh/testgen_remove_custom_varbit_type branch from 195fa63 to 5d973f1 Compare December 19, 2022 09:31
@vhavel vhavel force-pushed the vh/testgen_remove_custom_varbit_type branch 2 times, most recently from 2da70e1 to 5b10e40 Compare February 7, 2023 09:42
@vhavel vhavel changed the title Testgen: refactoring varbit handling Testgen: fix varbit handling Feb 9, 2023
@vhavel
Copy link
Author

vhavel commented Feb 9, 2023

@fruffy I decided to simplify this PR - it still solves the problem I'm facing, but without complex refactoring of varbit handling in testgen.

The problem with Extracted_Varbits<X> is that is inherits from bit<X>, so any code which is oblivious to this type can mistakenly assume the type/value is actually bit<X>. Surprisingly this doesn't occur often, but in our case it happens because canonicalize function from P4 typechecking returns bit<X> instead of the custom type, which confuses a midend pass in our target. That leads to storing bit<X> in the symbolic map for an invalid header field, which should not break anything, however parser extracts detected Extracted_Varbits based on the value stored in the symbolic map, so the incorrectly typed value prevented correct detection. This PR doesn't address the general problem of varbit handling in testgen, but rather fixes just the problem of detecting Extracted_Varbits values by looking at the type of the member instead.

@vhavel vhavel marked this pull request as ready for review February 9, 2023 11:29
@fruffy
Copy link
Collaborator

fruffy commented Feb 9, 2023

Yes, inheriting from Type_Bits was intentional so that we can actually use all the compiler optimizations we apply without having to copy and extend them. We made some modifications to passes to preserve the type, too.

Unfortunately, I do not know a better way to solve this.

Maybe there is a way to preserve the type in canonicalize?

@vhavel
Copy link
Author

vhavel commented Feb 22, 2023

Maybe there is a way to preserve the type in canonicalize?

In theory yes, type canonicalization can be customized, however the problematic pass is a re-used pass from bf-p4c compiler, i.e. a codebase which is completely Extracted_Varbits-oblivious. I'm surprised similar problems do not arise elsewhere, I consider Extracted_Varbits does break the substitution principle a bit (is it really substitutable by Type_Bits?).

Unfortunately, I do not know a better way to solve this.

In my opinion, more clean solution would be to preserve the varbit type in the IR, and storing bit values (and changing testgen internals to handle different type of key and value in the symbolic map). But it's relatively large change, and I'm still unsure if it's worth given that the current approach seems to work well in reality.

Anyway, I think the change in this PR is still step in right direction, because the type in IR (keys in symbolic map) should determine how the member should be handled, not the type of the currently stored value.

@vhavel vhavel force-pushed the vh/testgen_remove_custom_varbit_type branch from 5b10e40 to 57e097c Compare February 27, 2023 07:34
@vhavel vhavel merged commit dd26952 into main Feb 27, 2023
@vhavel vhavel deleted the vh/testgen_remove_custom_varbit_type branch February 27, 2023 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4tools Topics related to the P4Tools back end
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants