- Notifications
You must be signed in to change notification settings - Fork 10.6k
[Sema] Extension of favored type to unary minus and literals #74566
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
base: main
Are you sure you want to change the base?
Conversation
| @swift-ci Please smoke benchmark. |
| I will take a look but we are trying not to touch favoring and optimizations that are happening in CSGen because any changes to them tend to regress other cases… |
| That's cool, I understand and thanks for taking a look! I think this is significant for arithmetic with non-Int literals, but it's not a lot of code or time investment on my part. Feel free to aim me at things that are more likely to be accepted, like with #67791. |
| @swift-ci Please smoke test compiler performance. |
| @swift-ci Please smoke test. |
| Missed line number changes on (unrelated) fixits caused by adding a line to the test file. @swift-ci Please smoke test. |
| @swift-ci Please smoke test compiler performance. |
| @swift-ci Please smoke test. |
| Huh. Failed but no problems reported. @swift-ci Please smoke test. |
| @swift-ci Please smoke test. |
| @swift-ci Please smoke test. |
| @swift-ci Please smoke test. |
| @swift-ci Please smoke test. |
| @swift-ci Please smoke test compiler performance |
| The performance test failed (if I'm reading the output right) because it failed to The example expression, on the other hand NEW:
|
xedin left a comment
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.
Sorry but I don't think we'll take these changes because favoring is not the right mechanism for these sorts of issues and we are instead looking into ways to transfer all CSGen performance "hacks" into the solver and make them incremental.
Favoring is too big of a hammer because it effectively prunes other choices and would prevent heterogeneous operator overloads from being attempted which is a big problem for custom operator overloads. Attempting to favor based on a contextual type means that the solver is only allowed to attempt overloads that match the result type exactly which is too restrictive since contextual type is a hit and we allow conversions to them which has a potential to regress existing code that relies on the current behavior.
That is not what is happening here. There is zero change to the binary operator type variable tying together bits which were in the code before. (If you examined this PR originally it could do that, but that was accidental, and is no longer the case. Sorry about that!) This does favor the contextual type, but favoring only makes that type be tried first in overload sets. The impactful change is that if a literal is determined to be the same type as the favored type, it doesn't get the This is so that when a solution is found, we can stop. The current behavior of combinatorial explosion of literal types attempted is happening because the types which actually work aren't the default literal type. |
This is the main problem with favoring and what I referring to with heterogenous operators as well, we don’t want to always stop checking when the type matches the contextual exactly. |
Some small extensions to the favored type implementation for arithmetic expressions to make the type checker decide on happy paths sooner, if those work out. This is motivated by a particularly slow example:
First, if there are no favored types found by the linked expr analyzer, use the contextual type.
Second, allow unary minus to pass its favored type down to its argument, as binary arithmetic ops do.
Third, if the assigned type of a literal is its favored type, treat that as good and don't add
SK_NonDefaultLiteralto the score.The result is being able to stop looking for additional solutions if the favored path works. For this example expression, before:
Total number of scopes explored: 905225, after:Total number of scopes explored: 337057.There is one test diagnosis that changes because of the scoring, and it changes to look like the other diagnoses for similar uses.