Pitch: Back-deployment for fixed-layout value types - #3571

Hi all,

@tshortli and I found a way to do back-deployment for a limited class of types, specifically, @frozen struct and enum types with a fixed layout. The proposal here is to extend the existing @backDeployed(before:) attribute to cover these types. Then, we can adopt it for a number of core types in the standard library:

  • Span family:

    • Span

    • RawSpan

    • MutableSpan

    • MutableRawSpan

    • OutputSpan

    • OutputRawSpan

    • ByteOrder

  • Ref family

    • Ref (only back-deployable to Swift 6.2-era runtimes)

    • MutableRef

  • Cell family (still under review)

    • Cell

    • ConstCell

    • Volatile

  • UInt128 and Int128

  • Unique (non-copyable) types

    • UniqueArray

    • UniqueBox

  • Synchronization

    • Mutex

    • Disconnected

  • Concurrency

    • Continuation

    • TaskID

There is one limitation on retroactive protocol conformances to resilient protocols, which is outlined in the proposal itself.

Doug

41 Likes

In Xcode 27.2 beta Span type is already available: @available(macOS 10.14.4, iOS 12.2, ...). Could you clarify what is then meant by extend the existing @backDeployed(before:) attribute and makes it possible for new APIs to become available in earlier OS versions?
Does this mean that APIs like Array.span and Optional.mutableRef will become available on older OS versions?

I am so excited for this overall feature. Is this a potentially solvable, temporary limitation or a fundamental one? And do we have a sense of the scale of the limitation--as in, is it largely a corner case, or likely to bite folks in practice?

My fear would be if it's a broad enough limit that we're pushing back the frontier of availability only to end up with fractal subsets of functionality, which would be difficult to reason about.

3 Likes

Back-deploying these types will allow us to use them in Swift Testing, so… yes please!

4 Likes

I’m curious why this doesn’t generalize to types with dynamic layout? Not saying we need that though.

Is there a way we can add this to Swift version N+1, so that if Swift N+2 later adds such a conformance it can backward deploy to N+1 at least? Same question applies to types with dynamic layout also.

I’ve always wanted to finish the implementation of tuple conformances, but I think to be usable that would need something like what you describe, where we backward deploy the Equatable/Hashable/Comparable conformances of the fake tuple type to those protocols.

5 Likes

IIUC, we're using the foreign type metadata uniquing mechanism to reconcile these, which expects the foreign metadata candidates to all be fully-formed, so I think we would have to add new runtime functionality to use it with runtime-resolved metadata candidates. As you noted, if we have the opportunity, it might be nice to also do that runtime work now so we have a backstop version for future more expressive back deployment features.

3 Likes

This is awesome news. It would be great for Ref and MutableRef to back deploy, so more packages can use those types.

That uses a different mechanism (a bundled back-deployment dylib) that has some downsides. I describe the tradeoffs in the alternatives considered.

I* think* it's fundamental with the Swift runtime we have. Witness tables for resilient protocols are constructed in the runtime itself based on a pattern in the library. I don't think the runtime mechanism we have here can be repurposes along with the foreign witness-table uniquing mechanism we're using.

It's well in the realm of "discouraged" behavior: this happens when you're taking a system type (e.g., one in the standard library) and making it conform to another system protocol (e.g., from the standard library or another system library). These are marked @retroactive in part because it's an undesirable thing to do---the system should provide such a conformance, and if it does eventually do so, that one will end up being preferred over yours.

We could, yes. My implementation is effectively abusing the existing foreign type and foreign witness table uniquing logic that's been in the Swift runtime for years, so we get back-deployment, but we're limited to what we can do through that mechanism. We could add a new uniquing mechanism that eliminates these limitations, but it only helps us deploy back to the runtime where we added that mechanism. I'm not sure whether it's worth adding that new mechanism.

Doug

3 Likes

Two additional questions—sorry if obvious but I can’t glean it immediately from the text:

  • If my projects have 20 dependencies that each want to use UniqueArray, does my binary have 1 copy of UniqueArray or 21 copies of it?

  • Are there any runtime penalties for this backdeployment mechanism? Going back to the UniqueArray example: if I replaced Array lookup tables with UniqueArray lookup tables for performance, could relying on this new feature cost more than it saves in performance for certain tables?

Asking for a friend…

1 Like

The static archives along the way will each have a copy of the type metadata for UniqueArray. They are weak definitions that the linker should merge.

There is a runtime cost to accessing type metadata to ensure that we have a unique type metadata symbol. However, that's already a cost we pay for generics, because you need to go through the runtime to get uniqued type metadata for, e.g., UniqueArray<Int> even if it's not back-deployed. Performance-minded Swift code often ends up specializing away the generics and eliminating all accesses to the type metadata, so I expect that there won't be a significant effect in practice.

Doug

4 Likes