-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Optimize enum comparisons #81780
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
Optimize enum comparisons #81780
Conversation
@swift-ci test |
@swift-ci apple silicon benchmark |
1 similar comment
@swift-ci apple silicon benchmark |
@swift-ci clean apple silicon benchmark |
@swift-ci benchmark |
Optimize (the very inefficient) RawRepresentable comparison function call to a simple compare of enum tags. For example, ``` enum E: String { case a, b, c } ``` is compared by getting the raw values of both operands and doing a string compare. This peephole optimizations replaces the call to such a comparison function with a direct compare of the enum tags, which boils down to a single integer comparison instruction. rdar://151788987
…nctions So that optimizations can identify and deal with them.
… of the operands is a constant enum If there is a "constant" enum argument to a synthesized enum comparison, we can always inline it, because most of it will be constant folded anyway. This ensures the compiler is not creating terrible code for very simple enum comparisons, like ``` if someEnum == .someCase { ... } ``` rdar://85677499
ed521c7
to
ed8922b
Compare
@swift-ci test |
Func ==(_:_:) has generic signature change from to <T where T : Swift.RawRepresentable, T.RawValue : Swift.Equatable> | ||
Func ==(_:_:) has mangled name changing from 'Swift.== infix(Swift.Optional<Any.Type>, Swift.Optional<Any.Type>) -> Swift.Bool' to 'Swift.== infix<A where A: Swift.RawRepresentable, A.RawValue: Swift.Equatable>(A, A) -> Swift.Bool' | ||
Func ==(_:_:) has parameter 0 type change from (any Any.Type)? to τ_0_0 | ||
Func ==(_:_:) has parameter 1 type change from (any Any.Type)? to τ_0_0 |
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.
Are these ABI changes caused just by the addition of @_semantics("rawrepresentable.is_equal")
?
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.
Yes, I think what's happening here is that the api-digester is mixing up different overloads of the func ==
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.
Ah, that's unfortunate.
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.
(probably worth a bug report against the api-digester, if you can file one)
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.
@nkcsgexi do you know, is my assumption correct?
@swift-ci smoke test windows |
@swift-ci smoke test macos |
This PR contains two optimizations which dramatically improve performance of certain enum comparisons:
Peephole optimization for raw-value enum comparisons
Optimize (the very inefficient) RawRepresentable comparison function call to a simple compare of enum tags.
For example,
is compared by getting the raw values of both operands and doing a string compare.
This peephole optimizations replaces the call to such a comparison function with a direct compare of the enum tags, which boils down to a single integer comparison instruction.
rdar://151788987
Always inline synthesized enum comparisons if one of the operands is a constant enum
If there is a "constant" enum argument to a synthesized enum comparison, we can always inline it, because most of it will be constant folded anyway.
This ensures the compiler is not creating terrible code for very simple enum comparisons, like
rdar://85677499