-
Notifications
You must be signed in to change notification settings - Fork 10.6k
SE-0492: Metatypes and existential metatypes #85139
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
DougGregor
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.
This approach looks reasonable to me. I do really like having this feature, but it's a reasonable basis for any kind of "type registry" in the language
SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift
Outdated
Show resolved
Hide resolved
test/ConstValues/SectionIR.swift
Outdated
| @section("mysection") let metatype2: Any.Type = Int.self | ||
| @section("mysection") let metatype3: Any.Type = S.self | ||
| @section("mysection") let metatype4: any (Hashable & Sendable).Type = Int.self | ||
| @section("mysection") let metatype5: any (Hashable & Sendable).Type = S.self |
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 add tests for bound generic types, e.g. Array<Int>.self?
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.
We're rejecting that case syntactically: https://github.com/swiftlang/swift/pull/85139/files#diff-ea7de05f211d676ea604c35f0e6b253fb4a454efbfcc84928a931bab113e7bdcR82
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.
...but I did add that as a SIL test into test/SILOptimizer/static_init_metatypes.swift.
a27abcc to
16ebf84
Compare
|
@swift-ci please test |
|
And to point out explicitly: The OptUtils.swift and IRGen changes now cause MetatypeInst and InitExistentialMetatypeInst to be constant folded and statically initialized even outside of |
| @@ -0,0 +1,57 @@ | |||
| // RUN: %target-swift-frontend %s -parse-as-library -module-name=test -emit-sil | %FileCheck %s | |||
| // RUN: %target-swift-frontend %s -parse-as-library -O -module-name=test -emit-sil | %FileCheck %s | |||
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.
Please also add a -emit-ir invocation to make sure that IRGen can deal with 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.
Is that reasonable
Yes, that should be fine. As long as IRGen can handle those SIL initializer instructions.
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.
Added the -emit-ir checks
|
https://ci.swift.org/job/swift-PR-macos/25548/console https://ci.swift.org/job/swift-PR-Linux/24711/console https://ci-external.swift.org/job/swift-PR-windows/47464/console I think those are all real issues caused by the new constant folding actually adding these referencing to the witness tables. @eeckstein any ideas here? |
|
Difficult to say without looking at the generated llvm ir. Maybe it's a problem if the referenced type is defined in a different module |
|
Hm so this is related to conformances being considered "resilient" separately from the type being resilient: https://github.com/swiftlang/swift/blob/main/lib/IRGen/GenProto.cpp#L990 I think this means that we should not and cannot constant fold any conformances to the common Hashable, Equatable, etc. protocols, and only allow constant folding when the protocol is defined in the same module as the type (basically mirror the rules of https://github.com/swiftlang/swift/blob/main/lib/IRGen/GenProto.cpp#L990). @eeckstein does that sound right? @DougGregor I think this also somewhat restricts the usability of |
|
Yeah, I think this is right. If metatype needs runtime instantiation, as a workaround one could add functions/closures in a section which return the metatype, e.g. |
1caa69b to
3376a9b
Compare
|
@swift-ci please test |
4 similar comments
|
@swift-ci please test |
|
@swift-ci please test |
|
@swift-ci please test |
|
@swift-ci please test |
65e9122 to
31e114b
Compare
|
@swift-ci please test |
…stead of isGeneric()
31e114b to
75f6a28
Compare
|
@swift-ci please test |
|
swiftlang/swift-foundation#1602 @swift-ci please test Linux platform |
|
@swift-ci please test Windows platform |
|
@swift-ci please smoke test Windows |
|
swiftlang/swift-foundation#1602 @swift-ci please test Linux platform |
SE-0492 declared that constant expressions should support metatypes (of non-generic, non-resilient types). This adds support for using metatypes (T.self) and existential metatypes (upcasts to Any.Type, etc.) as static initializers for global variables, including those with @section attributes.
Major parts: