-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SR-9425][Sema] Use derived conformance as witness when possible for == operator of raw representable enums #36752
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
Conversation
@swift-ci Please smoke test |
d9e0698
to
c4df9b6
Compare
c4df9b6
to
c26942a
Compare
@swift-ci Please smoke test |
c26942a
to
57d7792
Compare
There is a benchmark for this https://github.com/apple/swift/blob/main/benchmark/single-source/OpenClose.swift @swift-ci Please smoke benchmark |
Performance: -O
Code size: -O
Performance: -Osize
Code size: -Osize
Performance: -Onone
Code size: -swiftlibsHow to read the dataThe tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.If you see any unexpected regressions, you should consider fixing the Noise: Sometimes the performance results (not code size!) contain false Hardware Overview
|
This has been such a long-standing perf issue. Thanks for fixing this! |
…equatable witness
…eric function is best witness when raw representable enum can derive equatable conformance
…lib fn and the requirement are exact
57d7792
to
5eb893f
Compare
@swift-ci test |
This fixes a regression from swiftlang#36752. Previously, a RawRepresentable enum would use the default implementation of == from a protocol extension in the standard library. After the above PR, we began synthesizing the == operator, just like we do for ordinary enums. However, when type checking an older swiftinterface file, we would still synthesize the declaration and try to use it, even though it would not have existed in the older dylib built with the older compiler. Fix this by not deriving witnesses in swiftinterface files at all. The interface should already explicitly list out all derived witnesses; if one is not listed, it should not be derived, because it does not exist in the dylib. Fixes rdar://problem/80466745.
…non-resilient modules This change was originally introduced in swiftlang#36752. The problem occurs in the following scenario: - New compiler with this change is used to build a dylib and swiftinterface file for module A, which defines a RawRepresentable enum type E. - Module B imports module A and references == on two E instances. - The module B is run against a dylib of module A built with the old compiler. At this point, module B will not link (or run) because the symbol for E.== is not present in the old dylib for A. The only real solution here is to disable this new optimization when building a module for -enable-library-evolution, unfortunately. In the future, we could make the derived E.== symbol @_alwaysEmitIntoClient. However today, synthesized declarations cannot be @_alwaysEmitIntoClient because they do not have source text that can be emitted into the swiftinterface file. One day, we might gain the ability to print Exprs as source text that parses back in, at which point we could allow synthesized declarations to be @_alwaysEmitIntoClient. Fixes rdar://problem/84912735 and rdar://problem/82919247.
Unfortunately, we can't make this optimization work internally for now. However, we can revisit it later. One strategy would be to emit the witness as a private declaration, so that clients call it through the witness table. Fixes rdar://problem/86861522.
Unfortunately, we can't make this optimization work internally for now. However, we can revisit it later. One strategy would be to emit the witness as a private declaration, so that clients call it through the witness table. Fixes rdar://problem/86861522.
Sema: Back out #36752 entirely [5.6]
Sema: Back out #36752 entirely
There are two overloads involved in this case
<Self where Self : Equatable> (Self.Type) -> (Self, Self) -> Bool
<T where T : RawRepresentable, T.RawValue : Equatable> (T, T) -> Bool
Solver is finding the first one as best because the ranking rule of non-generic is better than generic.
However in CSApply https://github.com/apple/swift/blob/22506f9bdb009c0c82410406d825c87c08af7f29/lib/Sema/CSApply.cpp#L621
currently it is finding the
<T where T : RawRepresentable, T.RawValue : Equatable> (T, T) -> Bool
as theEquatable
(WitnessChecker::findBestWitness
find only this one)witness which ends up applied. What given the implementation of it:When raw value is a
String
the codegen ends up with a string compare.This PR adjusts
ConformanceChecker::resolveWitnessViaLookup
to not consider this witness as best match for swift raw representable enums when deriveEquatable
is possible(favor derived). Therefore avoiding a str compare for those cases.Note
More details on the JIRA issue
Also, this PR although fixes the issue and makes sense to me, I'm not familiar enough with this to know if it is really the best fix.
cc @eeckstein @xedin
Resolves SR-9425, rdar://46555205