Skip to content

Conversation

@PietroGhg
Copy link
Contributor

This PR adds an initial implementation for BMv2 Headers and Parser. Only a few parsing constructs are supported, more will be added with follow up PRs.
In order to match the BMv2 json spec, transitions, transition keys and parsing operations are split into three separate regions (with appropriate checks in the verifier) that resemble the final json nodes that we want to emit.

Copy link
Collaborator

@asl asl left a comment

Choose a reason for hiding this comment

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

See first round of comments

LogicalResult matchAndRewrite(P4CoreLib::PacketExtractOp op, OpAdaptor operands,
ConversionPatternRewriter &rewriter) const override {
auto context = op.getContext();
auto fieldRefOp = op.getHdr().getDefiningOp<P4HIR::StructFieldRefOp>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

So we cannot extract to arbitrary variable? E.g.

bit<32> foo;
packet_in.extract(foo);

?

How does BMV2 backend deal with that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the particular example in your comment, p4c raises an error if I try to pass a bit<42> variable to extract (says only headers are allowed). If we have an header instance as a local variable, the header instance will be part of the headers JSON node, and the extract node can refer to it from there

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok, then we'd need similar error here :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

So, what's about the error here? Here is the place where we'd need to emit user-visible errors in case of match failures.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I missed the comment. I've added the check for header type, although I'm not sure here it's the right place for this type of errors, maybe it's better to raise this in the front end?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, this is a target-specific limitation. So it should be handled during the translation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok makes sense, we got the error message now 👍

LogicalResult matchAndRewrite(P4HIR::ParserOp op, OpAdaptor operands,
ConversionPatternRewriter &rewriter) const override {
auto loc = op.getLoc();
auto firstTransition = cast<P4HIR::ParserTransitionOp>(op.getBody().back().getTerminator());
Copy link
Collaborator

Choose a reason for hiding this comment

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

getStartState

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I think I still need op.getBody().back().getTerminator() to access the actual transition op since we want to remove it from the region after retrieving the starting state

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd add then something like getStartTransition to the ParserOp.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thank you

@PietroGhg PietroGhg marked this pull request as ready for review November 20, 2025 14:06
@PietroGhg PietroGhg force-pushed the pietro/headers branch 2 times, most recently from 7a5bbfb to c944090 Compare November 26, 2025 10:20
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