Slide 1

Slide 1 text

What Is ABI?

Slide 2

Slide 2 text

Today I talk, • What Is ABI Stability • What Is ABI • Exercises: Let's Break ABI

Slide 3

Slide 3 text

What Is ABI Stability?

Slide 4

Slide 4 text

What Is ABI Stability? • ABI stability enables OS vendors to embed a Swift standard library and runtime that is compatible with applications built with older or newer versions of Swift. This would remove the need for apps to distribute their own copy of these libraries on those platforms. It also allows for better decoupling of tools and better integration into the OS. https://github.com/apple/swift/blob/master/docs/ABIStabilityManifesto.md Swift ABI Stability Manifesto - What Does ABI Stability Enable?

Slide 5

Slide 5 text

What Is ABI Stability? • An app built with one version of the Swift compiler will be able to talk to a library built with another version. https://swift.org/blog/abi-stability-and-more/ ABI Stability and More

Slide 6

Slide 6 text

What Is ABI Stability?

Slide 7

Slide 7 text

What Is ABI Stability?

Slide 8

Slide 8 text

What Is ABI Stability?

Slide 9

Slide 9 text

What Is ABI?

Slide 10

Slide 10 text

What Is ABI? • At runtime, Swift program binaries interact with other libraries and components through an ABI. ABI is Application Binary Interface, or the specification to which independently compiled binary entities must conform to be linked together and executed. https://github.com/apple/swift/blob/master/docs/ABIStabilityManifesto.md Swift ABI Stability Manifesto

Slide 11

Slide 11 text

What Is ABI? • API: Application Programming Interface • ABI: Application Binary Interface

Slide 12

Slide 12 text

What Is ABI? • API: Application Programming Interface • ABI: Application Binary Interface

Slide 13

Slide 13 text

What Is ABI? • API: Application Programming Interface ‣ Source code level interface for your library • ABI: Application Binary Interface ‣ Machine instruction level interface for your library

Slide 14

Slide 14 text

What Is ABI? • API: Application Programming Interface ‣ Source code level interface for your library ‣ How we interface with your library before compilation • ABI: Application Binary Interface ‣ Machine instruction level interface for your library ‣ How we interface with your library after compilation

Slide 15

Slide 15 text

Components of the Swift ABI • Data Layout: a defined in-memory layout for instances of type • Type Metadata: a set of defined APIs for querying the metadata of a type • Mangling: any name in source code might not be globally unique, a unique name is produced through a technique called name mangling • Calling Convention: functions must know how to call each other • Runtime: dynamic casting, reference counting, reflection, etc • Standard Library: defines many common types, structures, and operations on these

Slide 16

Slide 16 text

Exercises: Let's Break ABI

Slide 17

Slide 17 text

Exercises: Let's Break ABI import Foundation public struct Logger { let prefix: String public init() { prefix = "" } public func log(level: LogLevel, _ message: String) { print("\(prefix) [\(level)] \(message)") } }

Slide 18

Slide 18 text

Exercises: Let's Break ABI import Foundation let logger = Logger() logger.log(level: .info, "Hello Swift!") // => [INFO] Hello Swift!

Slide 19

Slide 19 text

Exercises: Let's Break ABI Logger.swift: swift -frontend -g -Onone -c \ -primary-file src/Logger.swift src/main.swift \ -module-name main -o bin/Logger.o \ -emit-module -emit-module-path bin/Logger~partial.swiftmodule \ -sdk $(shell xcrun --sdk macosx --show-sdk-path) \ -enable-library-evolution main.swift: swift -frontend -g -Onone -c \ -primary-file src/main.swift src/Logger.swift \ -module-name main -o bin/main.o \ -emit-module -emit-module-path bin/main~partial.swiftmodule \ -sdk $(shell xcrun --sdk macosx --show-sdk-path) \ -enable-library-evolution link: swiftc $(wildcard bin/*.o) -o bin/main clean: $(shell rm -r bin/*)

Slide 20

Slide 20 text

Exercises: Let's Break ABI $ make Logger.swift # Logger.swiftΛίϯύΠϧ $ make main.swift # main.swiftΛίϯύΠϧ $ make link # Logger.oͱmain.oΛϦϯΫ

Slide 21

Slide 21 text

Exercises: Let's Break ABI • Link object files generated from different versions of the compiler • Explore cases that source code compatible but binary incompatible ‣ Add a default parameter to a public function ‣ Add a function to a struct ‣ Add a stored property to a struct

Slide 22

Slide 22 text

@frozenͷStrcutΛ࢖͏৔߹ͷSIL ௨ৗ (non-frozen)ͷStrcutΛ࢖͏৔߹ͷSIL

Slide 23

Slide 23 text

Summary • ABI binary level interface between libraries • ABI stability is binary level compatibility • ABI incompatible is where code that has already been compiled against version 1 will no longer work with version 2

Slide 24

Slide 24 text

References • ABI Stability and More
 https://swift.org/blog/abi-stability-and-more/ • Swift ABI Stability Manifesto
 https://github.com/apple/swift/blob/master/docs/ABIStabilityManifesto.md • Plan for module stability
 https://forums.swift.org/t/plan-for-module-stability/14551 • Swift Intermediate Language (SIL)
 https://github.com/apple/swift/blob/master/docs/SIL.rst • How Swift Achieved Dynamic Linking Where Rust Couldn't
 https://gankra.github.io/blah/swift-abi/