-
Notifications
You must be signed in to change notification settings - Fork 444
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
Implict cast fix #4399
Conversation
ChrisDodd
commented
Feb 7, 2024
- Allow implicit casts between serializable enums and their underlying type in action calls
2463ff3
to
10d3573
Compare
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. |
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. |
10d3573
to
934a6e8
Compare
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. |
58936fa
to
becb84c
Compare
@@ -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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
f72261d
to
8edd2a7
Compare
f409877
to
18ade2b
Compare
14b4823
to
3dc2a0d
Compare
- Allow implicit casts from serializable enums to their underlying type in action calls
3dc2a0d
to
ca63057
Compare
There was a problem hiding this 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?