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

Overview Of Swiftc

elmetal
December 06, 2019

Overview Of Swiftc

elmetal

December 06, 2019
Tweet

More Decks by elmetal

Other Decks in Programming

Transcript

  1. 0WFSWJFXPG4XJGUD
    !FM@NFUBM@

    View Slide

  2. 8IZ$PNQJMFS
    w MFBSOIJHIUFDIOJDBMTLJMMT
    w NPSFVOEFSTUBOE4XJGUUZQFTZTUFN
    w GVO

    View Slide

  3. View Slide

  4. SIL
    SILGen
    Parse Sema
    AST IRGen IR LLVM *.o
    Swift
    4XJGU$PNQJMFS
    https://llvm.org/devmtg/2015-10/slides/GroffLattner-SILHighLevelIR.pdf

    View Slide

  5. 1BSTF
    SIL
    SILGen
    Parse Sema
    AST IRGen IR LLVM *.o
    Swift

    View Slide

  6. 1BSTF
    (source_file "1.swift"
    (top_level_code_decl range=[1.swift:1:1 - line:1:9]
    (brace_stmt implicit range=[1.swift:1:1 - line:1:9]
    (pattern_binding_decl range=[1.swift:1:1 - line:1:9]
    (pattern_named 'a')
    (integer_literal_expr type='' value=1 builtin_initializer=**NULL** initializer=**NULL**))
    ))
    (var_decl range=[1.swift:1:5 - line:1:5] "a" type='' let readImpl=stored immutable))
    swiftc -dump-parse 1.swift
    1.swift
    let a = 1

    View Slide

  7. 4FNB
    SIL
    SILGen
    Parse Sema
    AST IRGen IR LLVM *.o
    Swift

    View Slide

  8. 4FNB
    (source_file "1.swift"
    (top_level_code_decl range=[1.swift:1:1 - line:1:9]
    (brace_stmt implicit range=[1.swift:1:1 - line:1:9]
    (pattern_binding_decl range=[1.swift:1:1 - line:1:9]
    (pattern_named type='Int' 'a')
    (integer_literal_expr type='Int' location=1.swift:1:9 range=[1.swift:1:9 - line:1:9] value=1
    builtin_initializer=Swift.(file).Int.init(_builtinIntegerLiteral:) initializer=**NULL**))
    ))
    (var_decl range=[1.swift:1:5 - line:1:5] "a" type='Int' interface type='Int' access=internal let
    readImpl=stored immutable))
    swiftc -dump-ast 1.swift
    1.swift
    let a = 1

    View Slide

  9. 4*-(FO
    SIL
    SILGen
    Parse Sema
    AST IRGen IR LLVM *.o
    Swift

    View Slide

  10. SIL
    SILGen
    Parse Sema
    AST IRGen IR LLVM *.o
    Swift
    4*-(FO
    4*-(FO 4*-0QUJNJ[FS
    SBX
    4*-
    DBOPOJDBM
    4*-

    View Slide

  11. SIL
    SILGen
    Parse Sema
    AST IRGen IR LLVM *.o
    Swift
    4*-(FO
    4*-(FO 4*-0QUJNJ[FS
    SBX
    4*-
    DBOPOJDBM
    4*-

    View Slide

  12. sil_stage raw
    import Builtin
    import Swift
    import SwiftShims
    @_hasStorage @_hasInitialValue let a: Int { get }
    // a
    sil_global hidden [let] @$s4main1aSivp : $Int
    // main
    sil [ossa] @main : $@convention(c) (Int32, UnsafeMutablePointer>>) -> Int32 {
    bb0(%0 : $Int32, %1 : $UnsafeMutablePointer>>):
    alloc_global @$s4main1aSivp // id: %2
    %3 = global_addr @$s4main1aSivp : $*Int // user: %8
    %4 = integer_literal $Builtin.IntLiteral, 1 // user: %7
    %5 = metatype $@thin Int.Type // user: %7
    // function_ref Int.init(_builtinIntegerLiteral:)
    %6 = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int // user: %7
    %7 = apply %6(%4, %5) : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int // user: %8
    store %7 to [trivial] %3 : $*Int // id: %8
    %9 = integer_literal $Builtin.Int32, 0 // user: %10
    %10 = struct $Int32 (%9 : $Builtin.Int32) // user: %11
    return %10 : $Int32 // id: %11
    } // end sil function 'main'
    // Int.init(_builtinIntegerLiteral:)
    sil [transparent] [serialized] @$sSi22_builtinIntegerLiteralSiBI_tcfC : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int
    4*-(FO
    swiftc -emit-silgen 1.swift
    1.swift
    let a = 1

    View Slide

  13. sil_stage raw
    import Builtin
    import Swift
    import SwiftShims
    @_hasStorage @_hasInitialValue let a: Int { get }
    // a
    sil_global hidden [let] @$s4main1aSivp : $Int
    // main
    sil [ossa] @main : $@convention(c) (Int32, UnsafeMutablePointer>>) -> Int32 {
    bb0(%0 : $Int32, %1 : $UnsafeMutablePointer>>):
    alloc_global @$s4main1aSivp // id: %2
    %3 = global_addr @$s4main1aSivp : $*Int // user: %8
    %4 = integer_literal $Builtin.IntLiteral, 1 // user: %7
    %5 = metatype $@thin Int.Type // user: %7
    // function_ref Int.init(_builtinIntegerLiteral:)
    %6 = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC : $@convention(method) (Builtin.IntLiteral, @thin
    Int.Type) -> Int // user: %7
    %7 = apply %6(%4, %5) : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int // user: %8
    store %7 to [trivial] %3 : $*Int // id: %8
    %9 = integer_literal $Builtin.Int32, 0 // user: %10
    %10 = struct $Int32 (%9 : $Builtin.Int32) // user: %11
    return %10 : $Int32 // id: %11
    } // end sil function 'main'
    // Int.init(_builtinIntegerLiteral:)
    sil [transparent] [serialized] @$sSi22_builtinIntegerLiteralSiBI_tcfC : $@convention(method) (Builtin.IntLiteral,
    @thin Int.Type) -> Int
    4*-(FO

    View Slide

  14. SIL
    SILGen
    Parse Sema
    AST IRGen IR LLVM *.o
    Swift
    4*-0QUJNJ[FS
    4*-(FO 4*-0QUJNJ[FS
    SBX
    4*-
    DBOPOJDBM
    4*-

    View Slide

  15. 4*-0QUJNJ[FS
    sil_stage canonical
    import Builtin
    import Swift
    import SwiftShims
    @_hasStorage @_hasInitialValue let a: Int { get }
    // a
    sil_global hidden [let] @$s4main1aSivp : $Int
    // main
    sil @main : $@convention(c) (Int32, UnsafeMutablePointer>>) -> Int32 {
    bb0(%0 : $Int32, %1 : $UnsafeMutablePointer>>):
    alloc_global @$s4main1aSivp // id: %2
    %3 = global_addr @$s4main1aSivp : $*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'
    // Int.init(_builtinIntegerLiteral:)
    sil public_external [transparent] [serialized] @$sSi22_builtinIntegerLiteralSiBI_tcfC : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int {
    // %0 // user: %2
    bb0(%0 : $Builtin.IntLiteral, %1 : $@thin Int.Type):
    %2 = builtin "s_to_s_checked_trunc_IntLiteral_Int64"(%0 : $Builtin.IntLiteral) : $(Builtin.Int64, Builtin.Int1) // user: %3
    %3 = tuple_extract %2 : $(Builtin.Int64, Builtin.Int1), 0 // user: %4
    %4 = struct $Int (%3 : $Builtin.Int64) // user: %5
    return %4 : $Int // id: %5
    } // end sil function '$sSi22_builtinIntegerLiteralSiBI_tcfC'
    swiftc -emit-sil 1.swift
    1.swift
    let a = 1

    View Slide

  16. 4*-0QUJNJ[FS
    sil_stage canonical
    import Builtin
    import Swift
    import SwiftShims
    @_hasStorage @_hasInitialValue let a: Int { get }
    // a
    sil_global hidden [let] @$s4main1aSivp : $Int
    // main
    sil @main : $@convention(c) (Int32, UnsafeMutablePointer>>) -> Int32 {
    bb0(%0 : $Int32, %1 : $UnsafeMutablePointer>>):
    alloc_global @$s4main1aSivp // id: %2
    %3 = global_addr @$s4main1aSivp : $*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'
    // Int.init(_builtinIntegerLiteral:)
    sil public_external [transparent] [serialized] @$sSi22_builtinIntegerLiteralSiBI_tcfC : $@convention(method)
    (Builtin.IntLiteral, @thin Int.Type) -> Int {
    // %0 // user: %2
    bb0(%0 : $Builtin.IntLiteral, %1 : $@thin Int.Type):
    %2 = builtin "s_to_s_checked_trunc_IntLiteral_Int64"(%0 : $Builtin.IntLiteral) : $(Builtin.Int64, Builtin.Int1) //
    user: %3
    %3 = tuple_extract %2 : $(Builtin.Int64, Builtin.Int1), 0 // user: %4
    %4 = struct $Int (%3 : $Builtin.Int64) // user: %5
    return %4 : $Int // id: %5
    } // end sil function '$sSi22_builtinIntegerLiteralSiBI_tcfC'

    View Slide

  17. 4*-0QUJNJ[FS
    swiftc -O —emit-sil 1.swift
    1.swift
    let a = 1
    sil_stage canonical
    import Builtin
    import Swift
    import SwiftShims
    @_hasStorage @_hasInitialValue let a: Int { get }
    // a
    sil_global hidden [let] @$s4main1aSivp : $Int
    // main
    sil @main : $@convention(c) (Int32, UnsafeMutablePointer>>) -> Int32 {
    bb0(%0 : $Int32, %1 : $UnsafeMutablePointer>>):
    alloc_global @$s4main1aSivp // id: %2
    %3 = global_addr @$s4main1aSivp : $*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'

    View Slide

  18. 4*-0QUJNJ[FS
    sil_stage canonical
    import Builtin
    import Swift
    import SwiftShims
    @_hasStorage @_hasInitialValue let a: Int { get }
    // a
    sil_global hidden [let] @$s4main1aSivp : $Int
    // main
    sil @main : $@convention(c) (Int32,
    UnsafeMutablePointer>>) -> Int32 {
    bb0(%0 : $Int32, %1 : $UnsafeMutablePointer>>):
    alloc_global @$s4main1aSivp // id: %2
    %3 = global_addr @$s4main1aSivp : $*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'

    View Slide

  19. *3(FO
    SIL
    SILGen
    Parse Sema
    AST IRGen IR LLVM *.o
    Swift

    View Slide

  20. *3(FO
    swiftc -emit-ir 1.swift
    1.swift
    let a = 1
    ; ModuleID = '-'
    source_filename = "-"
    target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
    target triple = "x86_64-apple-macosx10.14.0"
    %TSi = type <{ i64 }>
    @"$s4main1aSivp" = hidden global %TSi zeroinitializer, align 8
    @"_swift_FORCE_LOAD_$_swiftCompatibility50_$_main" = weak_odr hidden constant void ()* @"_swift_FORCE_LOAD_$_swiftCompatibility50"
    @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements_$_main" = weak_odr hidden constant void ()* @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements"
    @__swift_reflection_version = linkonce_odr hidden constant i16 3
    @llvm.used = appending global [3 x i8*] [i8* bitcast (void ()** @"_swift_FORCE_LOAD_$_swiftCompatibility50_$_main" to i8*), i8* bitcast (void ()** @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements_$_main" to i8*), i8* bitcast
    (i16* @__swift_reflection_version to i8*)], section "llvm.metadata", align 8
    define i32 @main(i32, i8**) #0 {
    entry:
    %2 = bitcast i8** %1 to i8*
    store i64 1, i64* getelementptr inbounds (%TSi, %TSi* @"$s4main1aSivp", i32 0, i32 0), align 8
    ret i32 0
    }
    declare extern_weak void @"_swift_FORCE_LOAD_$_swiftCompatibility50"()
    declare extern_weak void @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements"()
    attributes #0 = { "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sahf,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" }
    !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
    !swift.module.flags = !{!9}
    !llvm.linker.options = !{!10, !11, !12, !13, !14}
    !llvm.asan.globals = !{!15}
    !0 = !{i32 2, !"SDK Version", [2 x i32] [i32 10, i32 15]}
    !1 = !{i32 1, !"Objective-C Version", i32 2}
    !2 = !{i32 1, !"Objective-C Image Info Version", i32 0}
    !3 = !{i32 1, !"Objective-C Image Info Section", !"__DATA,__objc_imageinfo,regular,no_dead_strip"}
    !4 = !{i32 4, !"Objective-C Garbage Collection", i32 83953408}
    !5 = !{i32 1, !"Objective-C Class Properties", i32 64}
    !6 = !{i32 1, !"wchar_size", i32 4}
    !7 = !{i32 7, !"PIC Level", i32 2}
    !8 = !{i32 1, !"Swift Version", i32 7}
    !9 = !{!"standard-library", i1 false}
    !10 = !{!"-lswiftSwiftOnoneSupport"}
    !11 = !{!"-lswiftCore"}
    !12 = !{!"-lobjc"}
    !13 = !{!"-lswiftCompatibility50"}
    !14 = !{!"-lswiftCompatibilityDynamicReplacements"}
    !15 = !{[3 x i8*]* @llvm.used, null, null, i1 false, i1 true}

    View Slide

  21. *3(FO
    ; ModuleID = '-'
    source_filename = "-"
    target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
    target triple = "x86_64-apple-macosx10.14.0"
    %TSi = type <{ i64 }>
    @"$s4main1aSivp" = hidden global %TSi zeroinitializer, align 8
    @"_swift_FORCE_LOAD_$_swiftCompatibility50_$_main" = weak_odr hidden constant void ()* @"_swift_FORCE_LOAD_$_swiftCompatibility50"
    @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements_$_main" = weak_odr hidden constant void ()* @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements"
    @__swift_reflection_version = linkonce_odr hidden constant i16 3
    @llvm.used = appending global [3 x i8*] [i8* bitcast (void ()** @"_swift_FORCE_LOAD_$_swiftCompatibility50_$_main" to i8*), i8* bitcast (void ()**
    @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements_$_main" to i8*), i8* bitcast (i16* @__swift_reflection_version to i8*)], section "llvm.metadata", align 8
    define i32 @main(i32, i8**) #0 {
    entry:
    %2 = bitcast i8** %1 to i8*
    store i64 1, i64* getelementptr inbounds (%TSi, %TSi* @"$s4main1aSivp", i32 0, i32 0), align 8
    ret i32 0
    }
    declare extern_weak void @"_swift_FORCE_LOAD_$_swiftCompatibility50"()
    declare extern_weak void @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements"()
    attributes #0 = { "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sahf,+sse,
    +sse2,+sse3,+sse4.1,+ssse3,+x87" }
    !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
    !swift.module.flags = !{!9}
    !llvm.linker.options = !{!10, !11, !12, !13, !14}
    !llvm.asan.globals = !{!15}
    !0 = !{i32 2, !"SDK Version", [2 x i32] [i32 10, i32 15]}
    !1 = !{i32 1, !"Objective-C Version", i32 2}
    !2 = !{i32 1, !"Objective-C Image Info Version", i32 0}
    !3 = !{i32 1, !"Objective-C Image Info Section", !"__DATA,__objc_imageinfo,regular,no_dead_strip"}
    !4 = !{i32 4, !"Objective-C Garbage Collection", i32 83953408}
    !5 = !{i32 1, !"Objective-C Class Properties", i32 64}
    !6 = !{i32 1, !"wchar_size", i32 4}
    !7 = !{i32 7, !"PIC Level", i32 2}
    !8 = !{i32 1, !"Swift Version", i32 7}
    !9 = !{!"standard-library", i1 false}
    !10 = !{!"-lswiftSwiftOnoneSupport"}
    !11 = !{!"-lswiftCore"}
    !12 = !{!"-lobjc"}
    !13 = !{!"-lswiftCompatibility50"}
    !14 = !{!"-lswiftCompatibilityDynamicReplacements"}
    !15 = !{[3 x i8*]* @llvm.used, null, null, i1 false, i1 true}

    View Slide

  22. --7.
    SIL
    SILGen
    Parse Sema
    AST IRGen IR LLVM *.o
    Swift

    View Slide