Skip to content

Conversation

@fruffy
Copy link
Collaborator

@fruffy fruffy commented Nov 4, 2025

Try to make sure the converter actually propagates source info. Should cover most places in some form.

Corresponding PR on P4 Open Studio: p4lang/open-p4studio#120

@fruffy fruffy added core Topics concerning the core segments of the compiler (frontend, midend, parser) tofino Topics related to the Tofino switch and back end. labels Nov 4, 2025
@fruffy fruffy force-pushed the fruffy/p4-14-tofino branch 2 times, most recently from f9dd52c to eb78c89 Compare November 6, 2025 00:21
#define YYLLOC_DEFAULT(Cur, Rhs, N) \
((Cur) = (N) ? YYRHSLOC(Rhs, 1) + YYRHSLOC(Rhs, N) \
: Util::SourceInfo(driver.sources, YYRHSLOC(Rhs, 0).getEnd()))
#define YYLLOC_DEFAULT(Cur, Rhs, N) \
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@ChrisDodd I tried to fix some crashes that happen because the error reporter is accessing an uninitialized source info. It can also happen that the parsers produce a 0:0 source info, which is invalid. Not quite clear to me why. Might be that that source info is temporary and it does not matter.

Any idea why? Is that expected?

Copy link
Contributor

Choose a reason for hiding this comment

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

The 0:0 source info is an 'invalid' source info, generated by the default ctor and corresponds to there being no source location for something. All cases that access that should deal with it -- the error reporter special-cases it to not print anything source related. It should not crash.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The error report will crash if the sources object is nullptr, which can happen when you create an invalid source info. I fixed some cases up in the program structure translator.

((Cur) = (N) ? YYRHSLOC(Rhs, 1) + YYRHSLOC(Rhs, N) \
: (YYRHSLOC(Rhs, 0).isValid() \
? Util::SourceInfo(driver.sources, YYRHSLOC(Rhs, 0).getEnd()) \
: Util::SourceInfo()))
Copy link
Contributor

Choose a reason for hiding this comment

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

...so this is a noop change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I changed the constructor here to apply checks consistently (making sure that you do not pass invalid source info). Which requires this change here. I do not know why the parser passes objects with no source location available.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure how YYRHSLOC(Rhs, 0).isValid() could ever be false. The only case I can think of is if the stack is empty (null reduction before any shifts?) in which case I think YYRHSLOC(Rhs, 0) would itself be invalid, so you can't safely call isValid on it. Not entirely clear how bison initializes the stack.

However, if this could happen, it would seem like it would happen every time yyparse was called, so it would always be asserting.

@ChrisDodd
Copy link
Contributor

Not sure what of this is needed or why? Most P4_14 programs compile fine with tofino -- #5416

@fruffy
Copy link
Collaborator Author

fruffy commented Nov 6, 2025

Not sure what of this is needed or why? Most P4_14 programs compile fine with tofino -- #5416

The program that fails is stful.p4 in the Open P4Studio. Not sure whether we have this as sample for the compiler here.

@ChrisDodd
Copy link
Contributor

The program that fails is stful.p4 in the Open P4Studio. Not sure whether we have this as sample for the compiler here.

The bug there seems to be that frontend/p4/unusedDeclarations.cpp:84 calls getSourceFile() on a SourceInfo without first checking that it is valid. As to why there is a declaration node with invalid SourceInfo, that would seem to be a bug in the tofino backend's conversion of P4_14 blackbox registers into P4_16 (the source info gets dropped). I'll see if I can see a way to fix it.

@fruffy
Copy link
Collaborator Author

fruffy commented Nov 6, 2025

The bug there seems to be that frontend/p4/unusedDeclarations.cpp:84 calls getSourceFile() on a SourceInfo without first checking that it is valid. As to why there is a declaration node with invalid SourceInfo, that would seem to be a bug in the tofino backend's conversion of P4_14 blackbox registers into P4_16 (the source info gets dropped). I'll see if I can see a way to fix it.

The PR here already has a fix for it, adding srcInfo to several nodes in the convertor. While debugging I ran into the other issue, which is the parser generating these source infos that are invalid.

@ChrisDodd
Copy link
Contributor

The PR here already has a fix for it, adding srcInfo to several nodes in the convertor. While debugging I ran into the other issue, which is the parser generating these source infos that are invalid.

That would seem to be an independent problem, and the changes to the parser code don't seem to do anything useful. Generally the parser should not generate invalid source infos (the DEFAULT_YYLOC is supposed to see to that), but if it did, they shouldn't be a problem.

@fruffy
Copy link
Collaborator Author

fruffy commented Nov 7, 2025

and the changes to the parser code don't seem to do anything useful

They prevent an assert from firing because of an invalid source info being generated. Just uses the other debug constructor instead. I'll factor them out but that's probably something we should address.

@fruffy fruffy force-pushed the fruffy/p4-14-tofino branch from eb78c89 to b27a218 Compare November 7, 2025 20:21
@fruffy fruffy marked this pull request as ready for review November 7, 2025 20:24
@fruffy fruffy added this pull request to the merge queue Nov 7, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 7, 2025
@fruffy fruffy added this pull request to the merge queue Nov 8, 2025
Merged via the queue into main with commit b652e51 Nov 8, 2025
22 checks passed
@fruffy fruffy deleted the fruffy/p4-14-tofino branch November 8, 2025 01:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Topics concerning the core segments of the compiler (frontend, midend, parser) tofino Topics related to the Tofino switch and back end.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants