Skip to content

option to warn/return 0 on invalid field read#1377

Open
grg wants to merge 8 commits intop4lang:mainfrom
ai-fabrics:gleng/issue_1370
Open

option to warn/return 0 on invalid field read#1377
grg wants to merge 8 commits intop4lang:mainfrom
ai-fabrics:gleng/issue_1370

Conversation

@grg
Copy link
Copy Markdown
Contributor

@grg grg commented Apr 24, 2026

Add configurable options when reading an invalid field (currently defined as reading a field in a header that is not valid):

  • Report a warning.
  • Return zero.

Each of these behaviors can be enabled independently. The default behavior is to maintain the existing behavior (no warning, return the last written value, even if the header is invalid.)

Addresses #1370.

@grg grg force-pushed the gleng/issue_1370 branch from 5c5986b to f6b1b34 Compare April 24, 2026 01:15
@grg
Copy link
Copy Markdown
Contributor Author

grg commented Apr 24, 2026

Would appreciate feedback in a couple of areas in particular:

  1. The Data::value is now a private member instead of a protected one. Most accesses are now done via a virtual accessor, so most reads pay a vtable lookup penalty. Is there a better way to do this? (Considered a PIMPL approach, but seemed uglier.)
  2. Suggestions for making the new test more compact.

@grg
Copy link
Copy Markdown
Contributor Author

grg commented Apr 24, 2026

Looks like I've got some fixes to make to the old build system 🙁

@jafingerhut
Copy link
Copy Markdown
Contributor

On the performance question, hopefully this weekend some time I will get some time to try out locally building bmv2 with these changes, and running them through the p4c test suite, and measure performance with and without these changes on the same tests. I'll bet $10 that it will be difficult to measure any performance difference.

Note to self: Ensure that bmv2 with and without these changes are built with any debug/logging options turned off.

@jafingerhut
Copy link
Copy Markdown
Contributor

jafingerhut commented Apr 24, 2026

Minor question/suggestion: If this is implementing warning/force-0-value on reading a field in a header that is currently invalid, it might be better to call the options and internal variables tracking the values of the options something like "warn_on_accessing_field_in_invalid_header" or something similar.

Reason: I can imagine someone perhaps in the future wanting to go even further than what you are doing here, and tracking whether a field has actually ever been assigned to, and if not, issuing a warning if it is read before being initialized (even when the header the field is in is currently valid).

@jafingerhut
Copy link
Copy Markdown
Contributor

I have no suggestions for modifying the unit tests you have written. They have the benefit of being clear and straightforward. No need that I can see to make them more compact.

@grg
Copy link
Copy Markdown
Contributor Author

grg commented Apr 27, 2026

On the performance question, hopefully this weekend some time I will get some time to try out locally building bmv2 with these changes, and running them through the p4c test suite, and measure performance with and without these changes on the same tests. I'll bet $10 that it will be difficult to measure any performance difference.

Note to self: Ensure that bmv2 with and without these changes are built with any debug/logging options turned off.

I also don't expect a huge impact. I'm mostly highlighting it for completeness 🙂
I don't think we've ever advertised bmv2 as a performance-optimized implementation. Since it's a reference implementation, I think it's better to lean towards features that aid in verification and debugability over performance.

@grg
Copy link
Copy Markdown
Contributor Author

grg commented Apr 27, 2026

Minor question/suggestion: If this is implementing warning/force-0-value on reading a field in a header that is currently invalid, it might be better to call the options and internal variables tracking the values of the options something like "warn_on_accessing_field_in_invalid_header" or something similar.

Reason: I can imagine someone perhaps in the future wanting to go even further than what you are doing here, and tracking whether a field has actually ever been assigned to, and if not, issuing a warning if it is read before being initialized (even when the header the field is in is currently valid).

I included a FIXME comment as a suggestion for future enhancement here: https://github.com/p4lang/behavioral-model/pull/1377/changes#diff-abec37663881efd42bbc504a119d447e9df2898b11ac6c35b944103cf3a3a34dR156-R157

But I will rename the variables to better reflect their current behavior.

@grg grg force-pushed the gleng/issue_1370 branch from 01bb912 to c19e4ce Compare April 27, 2026 18:19
grg added 7 commits April 27, 2026 18:21
Make Data::value a private attribute (was protectectd). Add accessor
methods to allow reading/writing of the attribute.

Signed-off-by: Glen Gibb <gleng@ai-fabrics.com>
Switch to using the value accessor (get_value()) in Data instead of
directly accessing where appropriate.

Signed-off-by: Glen Gibb <gleng@ai-fabrics.com>
Add mechansim to report a warning and/or return zero when reading a
field in an unitialized header.

Default behavior remains unchanged; these new behaviors can be
individually/jointly enabled via command-line parameters. Can also be
enabled from tests by setting flags in the Field class.

Downside of this change is that all data value accesses now go through a
virtualized get_value() accessor.

Signed-off-by: Glen Gibb <gleng@ai-fabrics.com>
Signed-off-by: Glen Gibb <gleng@ai-fabrics.com>
Signed-off-by: Glen Gibb <gleng@ai-fabrics.com>
Rename variable refering to uninit to invalid header (e.g.,
warn_on_uninit_read -> warn_on_invalid_hdr_read)

Signed-off-by: Glen Gibb <gleng@ai-fabrics.com>
Signed-off-by: Glen Gibb <gleng@ai-fabrics.com>
@grg grg force-pushed the gleng/issue_1370 branch from c19e4ce to 0b1a23a Compare April 27, 2026 18:21
Signed-off-by: Glen Gibb <gleng@ai-fabrics.com>
Copy link
Copy Markdown
Contributor

@jafingerhut jafingerhut left a comment

Choose a reason for hiding this comment

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

LGTM. @fruffy let me know if you want to review this before we merge it, and if so, whether you want me to run a comparative before vs. after performance test to see what the effect of this, but personally my best guess is the performance difference should be difficult to measure.

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.

2 participants