Share
Embed
Contributing to the Swift Compiler
Slides on my talk on contributing to the Swift compiler at NextDoor Conf 2018.
David Hart
June 05, 2018
Transcript
86 slides
-
David Hart Contributing to the Swift Compiler Welcome to the
world of dragons and wizards -
Compilers 101 Optimization (Middle-end) IR IR Analysis (Front-end) Generation (Back-end)
Source Code Target Code -
Front-end x * 40 / 2 Lexing (Lexical Analysis) /
* 2 x 40 Tokens Source Code -
Front-end AST (Abstract Syntax Tree) / * 2 x 40
/ * 2 x 40 Tokens Parsing (Syntax Analysis) -
Front-end AST (Abstract Syntax Tree) / * 2 x 40
Decorated AST / * 2 x 40 int int int int int Semantic Analysis -
Front-end Decorated AST / * 2 x 40 int int
int int int IR (Internal Representation) push x push 40 multiply push 2 divide IR Generation -
Middle-end Optimized IR push x push 20 multiply IR (Internal
Representation) push x push 40 multiply push 2 divide Optimization -
LLVM Front-end Source Code x86-64 AMD64 Back-end LLVM Middle-end IR
IR arm64 ARM64 Back-end -
Swift Front-end Source Code Swift Middle-end SIL AMD64 Back-end LLVM
Middle-end IR IR ARM64 Back-end -
Swift Front-end print("Hello World") Source Code AST (source_file (top_level_code_decl (brace_stmt
(call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>' name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) Parse (Lexer + Parser) % swiftc -dump-parse -O file.swift -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>'
name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) print("Hello World") -
(source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>' name=print function_ref=unapplied)
(paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) print("Hello World") Swift Front-end (/ (* x 40) 2) / * 2 x 40 -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>'
name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) ( ) "Hello World" print -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>'
name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) ( ) "Hello World" print -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>'
name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) ( ) "Hello World" print -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>'
name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) ( ) "Hello World" print -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>'
name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) ( ) "Hello World" print -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>'
name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) ( ) "Hello World" print -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>'
name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) ( ) "Hello World" print -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>'
name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) ( ) "Hello World" print -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>'
name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) print("Hello World") -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='()' nothrow arg_labels=_: (declref_expr
type='(Any..., String, String) -> ()' decl=Swift.(file).print(_:separator:terminator:) function_ref=single) (tuple_shuffle_expr implicit type='(Any..., separator: String, terminator: String)' (paren_expr type='Any' (erasure_expr implicit type='Any' (string_literal_expr type='String' encoding=utf8 value="Hello World")))))))) Decorated AST (source_file (top_level_code_decl (brace_stmt (call_expr type='<null>' arg_labels=_: (unresolved_decl_ref_expr type='<null>' name=print function_ref=unapplied) (paren_expr type='<null>' (string_literal_expr type='<null>' encoding=utf8 value="Hello World")))))) AST Sema % swiftc -dump-ast -O file.swift -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='()' nothrow arg_labels=_: (declref_expr
type='(Any..., String, String) -> ()' decl=Swift.(file).print(_:separator:terminator:) function_ref=single) (tuple_shuffle_expr implicit type='(Any..., separator: String, terminator: String)' (paren_expr type='Any' (erasure_expr implicit type='Any' (string_literal_expr type='String' encoding=utf8 value="Hello World")))))))) -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='()' nothrow arg_labels=_: (declref_expr
type='(Any..., String, String) -> ()' decl=Swift.(file).print(_:separator:terminator:) function_ref=single) (tuple_shuffle_expr implicit type='(Any..., separator: String, terminator: String)' (paren_expr type='Any' (erasure_expr implicit type='Any' (string_literal_expr type='String' encoding=utf8 value="Hello World")))))))) -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='()' nothrow arg_labels=_: (declref_expr
type='(Any..., String, String) -> ()' decl=Swift.(file).print(_:separator:terminator:) function_ref=single) (tuple_shuffle_expr implicit type='(Any..., separator: String, terminator: String)' (paren_expr type='Any' (erasure_expr implicit type='Any' (string_literal_expr type='String' encoding=utf8 value="Hello World")))))))) -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='()' nothrow arg_labels=_: (declref_expr
type='(Any..., String, String) -> ()' decl=Swift.(file).print(_:separator:terminator:) function_ref=single) (tuple_shuffle_expr implicit type='(Any..., separator: String, terminator: String)' (paren_expr type='Any' (erasure_expr implicit type='Any' (string_literal_expr type='String' encoding=utf8 value="Hello World")))))))) -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='()' nothrow arg_labels=_: (declref_expr
type='(Any..., String, String) -> ()' decl=Swift.(file).print(_:separator:terminator:) function_ref=single) (tuple_shuffle_expr implicit type='(Any..., separator: String, terminator: String)' (paren_expr type='Any' (erasure_expr implicit type='Any' (string_literal_expr type='String' encoding=utf8 value="Hello World")))))))) -
Swift Front-end (source_file (top_level_code_decl (brace_stmt (call_expr type='()' nothrow arg_labels=_: (declref_expr
type='(Any..., String, String) -> ()' decl=Swift.(file).print(_:separator:terminator:) function_ref=single) (tuple_shuffle_expr implicit type='(Any..., separator: String, terminator: String)' (paren_expr type='Any' (erasure_expr implicit type='Any' (string_literal_expr type='String' encoding=utf8 value="Hello World")))))))) Decorated AST sil_stage raw import Builtin import Swift import SwiftShims // main sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 { bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>): %2 = integer_literal $Builtin.Word, 1 // user: %4 // function_ref _allocateUninitializedArray<A>(_:) %3 = function_ref @_T0s27_allocateUninitializedArraySayxG_BptBwlF : $@convention(thin) < _0_0> (Builtin.Word) -> (@owned Array< _0_0>, Builtin.RawPointer) // user: %4 ... SIL (Swift Intermediate Language) SILGen % swiftc -emit-silgen -O file.swift -
Swift Middle-end sil_stage raw import Builtin import Swift import SwiftShims
// main sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 { bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>): %2 = integer_literal $Builtin.Word, 1 // user: %4 // function_ref _allocateUninitializedArray<A>(_:) %3 = function_ref @_T0s27_allocateUninitializedArraySayxG_BptBwlF : $@convention(thin) < _0_0> (Builtin.Word) -> (@owned Array< _0_0>, Builtin.RawPointer) // user: %4 ... SIL (Swift Intermediate Language) sil_stage canonical import Builtin import Swift import SwiftShims // _swiftEmptyArrayStorage sil_global @_swiftEmptyArrayStorage : $_SwiftEmptyArrayStorage // main sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 { bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>): %2 = integer_literal $Builtin.Word, 1 // user: %5 %3 = integer_literal $Builtin.Int64, 1 // user: %4 ... Optimized SIL SILOptimizer % swiftc -emit-sil -O file.swift -
Swift Middle-end sil_stage canonical import Builtin import Swift import SwiftShims
// _swiftEmptyArrayStorage sil_global @_swiftEmptyArrayStorage : $_SwiftEmptyArrayStorage // main sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 { bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>): %2 = integer_literal $Builtin.Word, 1 // user: %5 %3 = integer_literal $Builtin.Int64, 1 // user: %4 ... Optimized SIL ; ModuleID = '-' source_filename = "-" target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.13" define i32 @main(i32, i8**) #0 { entry: %2 = bitcast i8** %1 to i8* %3 = call %swift.type* @_T0s23_ContiguousArrayStorageCyypGMa() #6 %4 = bitcast %swift.type* %3 to i8* %5 = getelementptr inbounds i8, i8* %4, i32 48 %6 = bitcast i8* %5 to i32* %7 = load i32, i32* %6, align 8 %8 = zext i32 %7 to i64 %9 = getelementptr inbounds i8, i8* %4, i32 52 %10 = bitcast i8* %9 to i16* %11 = load i16, i16* %10, align 4 ... LLVM-IR IRGen % swiftc -emit-ir -Xfrontend -disable-llvm-optzns -O file.swift -
Prerequisites 20Gb of free disk space A recent version of
Xcode https://ci.swift.org As fast a CPU as you can get -
Building (Generator) Xcode -x, --xcode ! Fast " No IDE
Ninja default ! IDE " Test integration -
Building (Variant) Release ! Debug symbols " Slow, Large Debug
! Fast, Small " No debug symbols -R, --release -d, --debug -
Building $ swift/utils/build-script \ --skip-build-benchmarks \ --xcode —-debug $ swift/utils/build-script
\ --skip-build-benchmarks \ --release -
Compiler Directive SE-0212 struct GridPoint: Hashable { let x: Int
let y: Int static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { return lhs.x == rhs.x && lhs.y == lhs.y } var hashValue: Int { return x ^ y } } -
Compiler Directive SE-0212 struct GridPoint: Hashable { let x: Int
let y: Int static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { return lhs.x == rhs.x && lhs.y == lhs.y } var hashValue: Int { return x ^ y } } -
Compiler Directive SE-0212 struct GridPoint: Hashable { let x: Int
let y: Int static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { return lhs.x == rhs.x && lhs.y == lhs.y } var hashValue: Int { return x ^ y } } -
Compiler Directive SE-0212 struct GridPoint: Hashable { let x: Int
let y: Int #if !swift(>=4.1) static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { return lhs.x == rhs.x && lhs.y == rhs.y } var hashValue: Int { return x ^ y } #endif } -
Compiler Directive SE-0212 struct GridPoint: Hashable { let x: Int
let y: Int #if !swift(>=3.3) static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { return lhs.x == rhs.x && lhs.y == rhs.y } var hashValue: Int { return x ^ y } #endif } -
Compiler Directive SE-0212 struct GridPoint: Hashable { let x: Int
let y: Int #if !swift(>=3.3) || (swift(>=4) && !swift(>=4.1)) static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { return lhs.x == rhs.x && lhs.y == rhs.y } var hashValue: Int { return x ^ y } #endif } -
Compiler Directive SE-0212 struct GridPoint: Hashable { let x: Int
let y: Int #if !compiler(>=4.1) static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { return lhs.x == rhs.x && lhs.y == rhs.y } var hashValue: Int { return x ^ y } #endif } -
Testing $ llvm/utils/lit/lit.py --succinct --verbose build/[config]/swift-[host]/test-[target] $ llvm/utils/lit/lit.py --succinct --verbose
build/[config]/swift-[host]/validation-test-[target] -
LIT // RUN: %target-typecheck-verify-swift // RUN: %target-swift-frontend -c -verify %s
-o /dev/null #warning("this should be a warning”) // expected-warning {{this should be a warning}} // expected-error@+1 {{this should be an error}} #error("this should be an error”) #if false #error("this shouldn't cause any errors") #endif // expected-error@+1 {{expected string literal in #error directive}}{{8-8="}}{{16-16="}} #error(test 123) test/Sema/pound_diagnostics.swift -
LIT // RUN: %target-typecheck-verify-swift // RUN: %target-swift-frontend -c -verify %s
-o /dev/null #warning("this should be a warning”) // expected-warning {{this should be a warning}} // expected-error@+1 {{this should be an error}} #error("this should be an error”) #if false #error("this shouldn't cause any errors") #endif // expected-error@+1 {{expected string literal in #error directive}}{{8-8="}}{{16-16="}} #error(test 123) test/Sema/pound_diagnostics.swift -
LIT // RUN: %target-typecheck-verify-swift // RUN: %target-swift-frontend -c -verify %s
-o /dev/null #warning("this should be a warning”) // expected-warning {{this should be a warning}} // expected-error@+1 {{this should be an error}} #error("this should be an error”) #if false #error("this shouldn't cause any errors") #endif // expected-error@+1 {{expected string literal in #error directive}}{{8-8="}}{{16-16="}} #error(test 123) test/Sema/pound_diagnostics.swift -
LIT // RUN: %target-typecheck-verify-swift // RUN: %target-swift-frontend -c -verify %s
-o /dev/null #warning("this should be a warning”) // expected-warning {{this should be a warning}} // expected-error@+1 {{this should be an error}} #error("this should be an error”) #if false #error("this shouldn't cause any errors") #endif // expected-error@+1 {{expected string literal in #error directive}}{{8-8="}}{{16-16="}} #error(test 123) test/Sema/pound_diagnostics.swift -
LIT // RUN: %target-typecheck-verify-swift // RUN: %target-swift-frontend -c -verify %s
-o /dev/null #warning("this should be a warning”) // expected-warning {{this should be a warning}} // expected-error@+1 {{this should be an error}} #error("this should be an error”) #if false #error("this shouldn't cause any errors") #endif // expected-error@+1 {{expected string literal in #error directive}}{{8-8="}}{{16-16="}} #error(test 123) test/Sema/pound_diagnostics.swift -
LIT // RUN: %target-typecheck-verify-swift // RUN: %target-swift-frontend -c -verify %s
-o /dev/null #warning("this should be a warning”) // expected-warning {{this should be a warning}} // expected-error@+1 {{this should be an error}} #error("this should be an error”) #if false #error("this shouldn't cause any errors") #endif // expected-error@+1 {{expected string literal in #error directive}}{{8-8="}}{{16-16="}} #error(test 123) test/Sema/pound_diagnostics.swift -
Apparent Complexity A type of complex situation that appears complicated,
but simple patterns lie underneath the surface.