Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Open Source Swiftc Workshop

Open Source Swiftc Workshop

Open Source Swift Workshop for swiftc at try! Swift Tokyo 2024

Yusuke Kita

March 17, 2024
Tweet

More Decks by Yusuke Kita

Other Decks in Programming

Transcript

  1. Open Source Swift Swiftc Workshop 2 — Open Source Swift

    Workshop, try! Swift Tokyo 2024, @kitasuke
  2. Table of Contents → Overview of Swift Compiler → Getting

    Started → Making Contributions 3 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  3. AST $ swiftc -dump-parse a.swift // let i = 1

    (source_file "a.swift" (top_level_code_decl range=[a.swift:1:1 - line:1:9] (brace_stmt implicit range=[a.swift:1:1 - line:1:9] (pattern_binding_decl range=[a.swift:1:1 - line:1:9] (pattern_named 'i') Original init: (integer_literal_expr type='<null>' value=1 builtin_initializer=**NULL** initializer=**NULL**) Processed init: (integer_literal_expr type='<null>' value=1 builtin_initializer=**NULL** initializer=**NULL**)) )) (var_decl range=[a.swift:1:5 - line:1:5] "i" let)) 7 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  4. Type-checked AST $ swiftc -dump-ast a.swift // let i =

    1 (source_file "a.swift" (top_level_code_decl range=[a.swift:1:1 - line:1:9] (brace_stmt implicit range=[a.swift:1:1 - line:1:9] (pattern_binding_decl range=[a.swift:1:1 - line:1:9] (pattern_named type='Int' 'i') Original init: (integer_literal_expr type='Int' location=a.swift:1:9 range=[a.swift:1:9 - line:1:9] value=1 builtin_initializer=Swift.(file).Int.init(_builtinIntegerLiteral:) initializer=**NULL**) Processed init: (integer_literal_expr type='Int' location=a.swift:1:9 range=[a.swift:1:9 - line:1:9] value=1 builtin_initializer=Swift.(file).Int.init(_builtinIntegerLiteral:) initializer=**NULL**)) )) (var_decl range=[a.swift:1:5 - line:1:5] "i" interface type='Int' access=internal let readImpl=stored immutable)) 9 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  5. SIL $ swiftc -emit-sil a.swift // let i = 1

    sil_stage canonical import Builtin import Swift import SwiftShims @_hasStorage @_hasInitialValue let i: Int { get } // i sil_global hidden [let] @$s1a1iSivp : $Int // main sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 { [%1: noescape **] [global: read,write,copy,deinit_barrier] bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>): alloc_global @$s1a1iSivp // id: %2 %3 = global_addr @$s1a1iSivp : $*Int // user: %6 %4 = integer_literal $Builtin.Int64, 1 // user: %5 %5 = struct $Int (%4 : $Builtin.Int64) // user: %6 store %5 to %3 : $*Int // id: %6 %7 = integer_literal $Builtin.Int32, 0 // user: %8 %8 = struct $Int32 (%7 : $Builtin.Int32) // user: %9 return %8 : $Int32 // id: %9 } // end sil function 'main' 11 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  6. LLVM IR $ swiftc -emit-ir a.swift // let i =

    1 ; ModuleID = '<swift-imported-modules>' source_filename = "<swift-imported-modules>" target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128" target triple = "arm64-apple-macosx14.0.0" %TSi = type <{ i64 }> @"$s1a1iSivp" = hidden global %TSi zeroinitializer, align 8 @"\01l_entry_point" = private constant { i32, i32 } { i32 trunc (i64 sub (i64 ptrtoint (ptr @main to i64), i64 ptrtoint (ptr @"\01l_entry_point" to i64)) to i32), i32 0 }, section "__TEXT, __swift5_entry, regular, no_dead_strip", align 4 @__swift_reflection_version = linkonce_odr hidden constant i16 3 @llvm.used = appending global [3 x ptr] [ptr @main, ptr @"\01l_entry_point", ptr @__swift_reflection_version], section "llvm.metadata" define i32 @main(i32 %0, ptr %1) #0 { entry: store i64 1, ptr @"$s1a1iSivp", align 8 ret i32 0 } 13 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  7. Packages in Swift → swift-driver → SwiftParser 15 — Open

    Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  8. SwiftParser SwiftParser produces a SwiftSyntax syntax tree from Swift source

    code mainly used for Swift Macros 16 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  9. Syntax Tree $ swift-parser-cli print-tree a.swift // let i =

    1 SourceFileSyntax ├─statements: CodeBlockItemListSyntax │ ╰─[0]: CodeBlockItemSyntax │ ╰─item: VariableDeclSyntax │ ├─attributes: AttributeListSyntax │ ├─modifiers: DeclModifierListSyntax │ ├─bindingSpecifier: keyword(SwiftSyntax.Keyword.let) │ ╰─bindings: PatternBindingListSyntax │ ╰─[0]: PatternBindingSyntax │ ├─pattern: IdentifierPatternSyntax │ │ ╰─identifier: identifier("i") │ ╰─initializer: InitializerClauseSyntax │ ├─equal: equal │ ╰─value: IntegerLiteralExprSyntax │ ╰─literal: integerLiteral("1") ╰─endOfFileToken: endOfFile 17 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  10. Clone swift $ git clone [email protected]:apple/swift.git swift $ cd swift

    $ utils/update-checkout --clone-with-ssh 19 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  11. Build using Ninja $ utils/build-script --skip-build-benchmarks \ --skip-ios --skip-watchos --skip-tvos

    \ --swift-darwin-supported-archs "$(uname -m)" \ --sccache --release-debuginfo --swift-disable-dead-stripping \ --bootstrapping=hosttools 20 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  12. Using Ninja with Xcode $ utils/build-script --swift-darwin-supported-archs "$(uname -m)" \

    --xcode --clean 21 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  13. Debugging issues # optimized Stdlib + debug Swiftc + optimized

    Clang/LLVM $ utils/build-script --release-debuginfo --debug-swift 22 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  14. Testing Swift Primary testsuites is LLVM lit-based and write in

    Swift for a Swift language feature, write in SIL for a SIL optimization 23 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  15. Running tests # Rebuild all test dependencies and run all

    tests under test/. $ utils/run-test --lit ../llvm-project/llvm/utils/lit/lit.py \ ../build/Ninja-RelWithDebInfoAssert/swift-macosx-$(uname -m)/test-macosx-$(uname -m) 24 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  16. Reporting Bugs It's a great way for anyone to help

    improve Swift on GitHub issue or Apple's bug reporter instead if Xcode related or NDA associated. 26 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  17. Finding Issues Good first issues are bugs, ideas, and tasks

    that are intended to be accessible for contributors that are new to working on the Swift project 27 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  18. Swift Evolution Shaping the future of Swift is a community

    effort that anyone can participate in via the Evolution section of the Swift forums 28 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke
  19. Swift Mentorship Program It's designed to encourage developers to actively

    participate in the Swift open source community through direct mentorship with experienced developers 29 — Open Source Swift Workshop, try! Swift Tokyo 2024, @kitasuke