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

Implict cast fix #4399

Merged
merged 1 commit into from
Mar 25, 2024
Merged

Implict cast fix #4399

merged 1 commit into from
Mar 25, 2024

Conversation

ChrisDodd
Copy link
Contributor

  • Allow implicit casts between serializable enums and their underlying type in action calls

@ChrisDodd ChrisDodd force-pushed the cdodd-serenum branch 2 times, most recently from 2463ff3 to 10d3573 Compare February 7, 2024 07:36
@vlstill
Copy link
Contributor

vlstill commented Feb 7, 2024

What part of the spec is this implementing? As far as I see 8.11.2 of the v1.2.4 spec allows only implicit casts of serializable enum → underlying type, but not the other way around. Your test seems to imply you have now allowed both.

@ChrisDodd
Copy link
Contributor Author

Hmm -- it seems weird to allow implicit casts one way and not the other. I was originally just looking at the implicit cast from serializable enum to underlying type, but the way typechecking works, it ends up being symmetric.

@vlstill
Copy link
Contributor

vlstill commented Feb 7, 2024

Hmm -- it seems weird to allow implicit casts one way and not the other. I was originally just looking at the implicit cast from serializable enum to underlying type, but the way typechecking works, it ends up being symmetric.

I would argue that for implicit casts the spec makes sense -- a seralizable-enum-typed variable is supposed to contain one of the named values, but an implicit cast from the underlying type would make it very easy to accidentally create a value that is not one of the named once. I think such a thing should be only for explicit casts.

@ChrisDodd
Copy link
Contributor Author

Ok, I've managed to figure out how to get this to allow the implicit casts one way and not the other -- there's some confusion here between type unification (which is symmetric) and type checking (which is not), so it needs an extra type check to disallow the 'wrong' conversion.

@ChrisDodd ChrisDodd force-pushed the cdodd-serenum branch 3 times, most recently from 58936fa to becb84c Compare February 8, 2024 04:42
@fruffy fruffy requested a review from jnfoster February 12, 2024 21:33
@@ -3512,6 +3512,15 @@ const IR::Expression *TypeInference::actionCall(bool inActionList,
typeError("%1%: action argument must be a compile-time constant",
arg->expression);
}
// This is like an assignment; may make additional conversions.
newExpr = assignment(arg, param->type, arg->expression);
if (readOnly) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you elaborate on that bug? Maybe we can fix the DPDK back end.

Copy link
Contributor Author

@ChrisDodd ChrisDodd Feb 13, 2024

Choose a reason for hiding this comment

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

Basically, the DPDK backend creates new code that is not type-correct, so a following TypeChecking pass tries to introduce new implicit casts, but fails as the readOnly flag is set. I'm not sure why it only fails after this change, as it seems like it should fail regardless. Specifically the incorrect added code is an action call where the actual argument type does not exactly match the formal parameter type (but can be converted implicitly). It seems that typeChecking is not correctly checking this case, and this change fixes that (as part of getting the serializable enum casts working)

I tried adding a additional TypeInferencing pass in the DPDK backend to introduce the implicit casts correctly, but that caused some other problems. This feels like a bit of a hack, leaving the code as semi-type-incorrect (not making the implicit cast explicit) when the readOnly flag is set.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Which pass is this? Maybe we can fix the generation of this action call in the first place?

I think this is something the DPDK back end needs to fix before we add more workarounds to the type checker. The class is already complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is in DPDK::PrependPDotToActionArgs (line ~1318) where it is creating a ListExpression rather than a StructExpression to initialize a struct. The typechecker wants this to be a StructExpression, so will introduce a "cast" (actually rewrite the ListExpression as a StructExpression), which then causes a problem as it is running with readOnly = true (backend.cpp line 107). If I change that call to have readOnly = false, then it doesn't assert and just makes the change. However, it then has problems later in the backend with code that doesn't know what to do with a StructExpression.

Without my change, the typeChecker does not correctly type check action args, so it doesn't try to insert the conversion.

The easiest fix is probably just to add a call to P4::TypeInference with readOnly = false somewhere, and fix the backend code to understand a StructExpression.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@usha1830 Could you take a look at this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure. I will check this out.

@ChrisDodd ChrisDodd force-pushed the cdodd-serenum branch 2 times, most recently from f72261d to 8edd2a7 Compare February 19, 2024 23:11
@fruffy fruffy added core Topics concerning the core segments of the compiler (frontend, midend, parser) p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). labels Feb 20, 2024
@ChrisDodd ChrisDodd force-pushed the cdodd-serenum branch 3 times, most recently from f409877 to 18ade2b Compare March 13, 2024 21:37
@ChrisDodd ChrisDodd force-pushed the cdodd-serenum branch 2 times, most recently from 14b4823 to 3dc2a0d Compare March 18, 2024 02:42
- Allow implicit casts from serializable enums to their underlying
  type in action calls
Copy link
Collaborator

@fruffy fruffy left a comment

Choose a reason for hiding this comment

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

Tentatively approving. @vlstill any follow-up?

@ChrisDodd ChrisDodd added this pull request to the merge queue Mar 24, 2024
Merged via the queue into p4lang:main with commit 4971e06 Mar 25, 2024
17 checks passed
@ChrisDodd ChrisDodd deleted the cdodd-serenum branch March 25, 2024 00:10
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) p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants