Hi all. I’ve been working on abstract type layouts for some time, and have been discussing with @Xi_Ge natural extensions that I’d like to see. I’d like to solicit some feedback on my design and general roadmap.
For me, the overarching goal here is to improve Swift’s incremental build speed characteristics for non resilient libraries by reducing the amount of unnecessary information that is exposed to dependent compiler invocations.
Concretely, consider if you have modules A, B and C, where A depends on B and B depends on C. Compiling each module produces some output files, and those files are inputs to dependent modules. If those inputs do not change, general purpose build systems can avoid recompiling dependent modules. Build systems like Buck and Bazel do this by comparing input files by hash to previous compile actions, caching repeated actions, but they don’t know anything about the Swift language otherwise.
Currently the Swift compiler includes many internal implementation details in these output files that cause dependent modules to re-compile, even when they don’t actually need to. My work here is all about how we can reduce the amount of internal implementation information that we leak.
Proposed Roadmap:
I want to reduce the two forms of implementation detail leaking. The first a module leaks information about internally imported modules, the second a module leaks internal information about itself. I’ve already been working on the first, the second I would like to propose:
-
Formalization of internal imports for non-resilient modules, such that
@_implementationOnlycan be retired -
Remove module private declarations from generated binary Swift modules by default, representing private declarations that still contribute to module ABI with abstract type layouts.
I’ll give some detail on the design of both goals.
1: Formalizing internal imports for non-resilient modules
This is what I’ve been working on for a while now, but I want to re-iterate the exact goals and latest implementation strategy.
Without library evolution, if module A imports module B, and module B internal imports C, to compile A you need both B and C. This means changes to C invalidate A despite being an “internal” import. That is not true of @_implementationOnly imports, an @_implementationOnly imported module need not be provided to client module compiles, even without library evolution. I want to give internal imports this quality, and then delete @_implementationOnly from the language.
The reason access level modifiers on import statements only allow you to not provide internally imported modules to clients with library evolution, is that although declarations from internally imported modules are not allowed to contribute to public module API, they are allowed to contribute to module ABI. @_implementationOnly currently has no solution for this issue, and miscompiles are possible. Consider the canonical example:
@_implementationOnly import HiddenModule
public struct Foo {
private let privateStoredProperty: HiddenModule.HiddenStruct
}
Without visibility into HiddenModule, clients cannot derive the layout of Foo. Instead de-serialization currently drops members including stored properties if it cannot resolve their types, leading to an incorrect layout.
Doug has laid out a proposal to fix this problem, and I have begun implementing it more or less. The core idea is that we communicate the layout information that clients are currently missing via “abstract type layout information”.
The latest iteration of my design is as follows:
-
We provide abstract type layouts only for the types of ABI contributing type members (ex. a stored property in an exposed struct) that clients may not be able to resolve, because the type references a hidden type somewhere.
This mirrors the existing recovery mechanisms where we use a MissingMemberDecl as a placeholder when class / protocol members fail to de-serialize to keep layout consistent. This way we don’t bloat modules with abstract type layouts of every transitively used type in the module, and don’t leak the layout of types that don’t actually contribute to module ABI.
-
For each type identified as being in need of an abstract type layout, we largely mechanically serialize the existing lowered representations the compiler uses to guide codegen.
We serialize the approximate TypeLowering (guides SILGen) and the exact TypeInfo (guides IRGen) as well as some other properties. When layout derived from the visible representation of a type and layout derived from abstract type layout differ somehow, it is easy to create miscompilations. I’ve found it very difficult to come up with another format that faithfully captures all the subtleties of layout (ex: extra inhabitants surprised me). Therefore, I think keeping a single representation of layout as much as possible is helpful.
- As in the proposal, we insist that the native layout of a type, and the layout as derived from an abstract type layout match exactly.
We tolerate some types having more opaque abstract type layout representations, in that non trivial copy / destroy operations are performed via value witness or similar, but the layout itself, the details that might affect the calling convention of a function manipulating the type, must match exactly. This has implications in the next couple sections.
Acceptable Performance Penalty:
With internal becoming the default access level for imports for future Swift versions, we need to be careful that we don’t silently introduce too significant performance penalties as a result of using an abstract type layout to perform codegen.
So I want to highlight the ways in which abstract type layouts currently produce “worse” codegen. There are two areas I am aware of.
Firstly, there is the extra layer of indirection for performing non trivial value operations. That layer of indirection might not need to be quite as indirect as a value witness call (fetch metadata, fetch vw table, call function), but there will always be at least an extra function call.
Lastly, currently we don’t produce aggregate TypeLowerings from abstract type layouts, but what I consider to be a compatible non aggregate TypeLowering. That is, at the SIL level, hidden types are opaque, and a copy of such a type, can’t be lowered to individual copy SIL instructions on its members. That is not true of IRGen, where we faithfully re-create the TypeInfo. This will disable certain SIL optimizations, like ARC optimizations. This isn’t necessarily a fundamental limitation, but it is how I’ve currently implemented it, so I want to call out this distinction.
If we are to succeed in completing this project, I think we need to align on what an accepted performance penalty looks like.
Remaining Challenges:
A consequence of design point number 3 (insisting that abstract type layouts not regress layout precision), requires that our system be capable of specializing generic abstract type layouts. Consider this example:
@_implementationOnly import HiddenModule
public struct GenericVisibleStruct<T> {
private let privatStoredProperty: HiddenModule.HiddenGenericStruct<T>
}
public func takeAGenericVisibleStructOfInt(\_ foo: GenericVisibleStruct<Int>) {}
The calling convention of takeAGenericVisibleStructOfInt will be decided with full visibility into HiddenGenericStruct<Int> and to call the function, clients must be able to reproduce the layout of HiddenGenericStruct<Int>, given only the abstract type layout of HiddenGenericStruct<T> and the layout of Int. In other words, the client needs to be able to perform generic specialization on abstract type layouts.
This is a challenge I haven’t yet tackled with the current iteration of abstract type layouts. I expect it to be achievable, but it’s a wrinkle not covered in prior discussion that I want to raise.
2: Remove module private declarations from generated binary Swift modules
We currently include all declarations in a module’s binary interface, including private declarations that clients compiling against the module should not care about. This means adding a private function to a module for example, causes client modules to re-compile, when they really shouldn’t.
For the binary module emitted in the typical case, I want to omit all these private declarations, using abstract type layouts when a private member contributes to ABI. This would extend experimental-serialize-external-decls-only: Frontend: Introduce `-experimental-serialize-external-decls-only` option by tshortli · Pull Request #68166 · swiftlang/swift · GitHub to support non resilient modules.
However, it has become clear to me from previous discussions (1) (2), that there are some consumers of binary Swift modules that require private information. In particular, @testable imports and LLDB. To handle the testable case I suggest we produce a full module when -enable-testing is passed. To handle the LLDB case, I suggest that when modules are emitted, we emit two versions, a “stripped version” for use when compiling dependent modules, and another full version for LLDB. The stripped version may point at the full version via an embedded path. This way LLDB can still find the complete version from the stripped version.
Over the long term we can replace the second full module, with debug information as described here, but we don’t block improvements in the interface the compiler uses. I think this is the better long term direction, just as we don’t include DWARF in C++ headers / clang modules, we shouldn’t need to include information needed only by the debugger in Swift module interfaces meant for the compiler.
Diagnostic Implications:
As I discussed in this forum post, removing private declarations from binary swift modules may have implications on diagnostics. The compiler can no longer differentiate between a declaration that does not exist, and one that is private. I think this is an acceptable cost to pay, and diagnostic parity is not a goal of mine, but as I discuss in the forum thread, we have options to preserve it if we see fit.
Related Note on Textual Swift Module Interfaces:
There is often discussion that if we want interfaces that leak fewer implementation details, we should use textual swift interfaces (.swiftinterface) files. We’ve attempted this, and while they are closer to what we want, textual interfaces really aren’t supported without library evolution.
private struct PrivateStorage {
var value: Int
}
public struct PublicContainer {
private var storage: PrivateStorage
}
If you produce a textual interface for this module without library evolution, you get a warning warning: module interfaces are only supported with -enable-library-evolution. Interface emission succeeds, but it produces a broken interface that clients will fail to load:
public struct PublicContainer {
private var storage: ModuleName.PrivateStorage // PrivateStorage can't be resolved
}
Regardless of if we use textual interfaces, or binary modules, we need abstract type layouts if we are to support minimal interfaces for non resilient libraries. At that point, we can debate the merits of the two formats, but I would say the binary format is more performant, and the textual format is really only appropriate when it must be compatible with multiple compiler versions.
Related Note on Resilient v Non-Resilient Parity:
It is considered desirable to maintain feature parity between resilient and non resilient modules. Such parity is the basis for this whole project, and it will fix existing parity gaps. In particular, as mentioned it brings the same behavior for internal imports to non resilient modules already enjoyed by resilient modules. If desired, it could also allow emitting textual interfaces for non resilient modules.