|
| 1 | +// RUN: %target-sil-opt -enable-sil-verify-all %s -initialize-static-globals | %FileCheck %s |
| 2 | + |
| 3 | +// REQUIRES: swift_in_compiler |
| 4 | + |
| 5 | +sil_stage canonical |
| 6 | + |
| 7 | +import Builtin |
| 8 | +import Swift |
| 9 | +import SwiftShims |
| 10 | + |
| 11 | +public struct TStruct { |
| 12 | + let x: Int32 |
| 13 | + init(x: Int32) |
| 14 | +} |
| 15 | + |
| 16 | +struct Outer { |
| 17 | + let a: Int32 |
| 18 | + let b: TStruct |
| 19 | + let c: Int32 |
| 20 | +} |
| 21 | + |
| 22 | +let trivialglobal: TStruct |
| 23 | + |
| 24 | +public class TClass { |
| 25 | + final let x: Int32 |
| 26 | + init(x: Int32) |
| 27 | + deinit |
| 28 | +} |
| 29 | + |
| 30 | +struct GenericStruct<T> { |
| 31 | + var x: T |
| 32 | +} |
| 33 | + |
| 34 | +let nontrivialglobal: TClass |
| 35 | + |
| 36 | +// CHECK-LABEL: sil_global hidden [let] @$trivialglobal : $TStruct = { |
| 37 | +// CHECK: [[CONST:%.*]] = integer_literal $Builtin.Int32, 10 |
| 38 | +// CHECK: [[INT:%.*]] = struct $Int32 ([[CONST]] : $Builtin.Int32) |
| 39 | +// CHECK: %initval = struct $TStruct ([[INT]] : $Int32) |
| 40 | +sil_global hidden [let] @$trivialglobal : $TStruct |
| 41 | +sil_global private @globalinit_trivialglobal_token : $Builtin.Word |
| 42 | + |
| 43 | + |
| 44 | +// CHECK-LABEL: sil_global hidden [let] @$nontrivialglobal : $TClass{{$}} |
| 45 | +sil_global hidden [let] @$nontrivialglobal : $TClass |
| 46 | +sil_global private @globalinit_nontrivialglobal_token : $Builtin.Word |
| 47 | + |
| 48 | +// CHECK-LABEL: sil_global hidden [let] @empty_global : $GenericStruct<()>{{$}} |
| 49 | +sil_global hidden [let] @empty_global : $GenericStruct<()> |
| 50 | +sil_global private @empty_global_token : $Builtin.Word |
| 51 | + |
| 52 | +// CHECK: sil_global @go : $Outer = { |
| 53 | +// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 2 |
| 54 | +// CHECK-NEXT: %1 = struct $Int32 (%0 : $Builtin.Int32) |
| 55 | +// CHECK-NEXT: %2 = struct $TStruct (%1 : $Int32) |
| 56 | +// CHECK-NEXT: %initval = struct $Outer (%1 : $Int32, %2 : $TStruct, %1 : $Int32) |
| 57 | +// CHECK-NEXT: } |
| 58 | +sil_global @go : $Outer |
| 59 | +sil_global private @globalinit_token0 : $Builtin.Word |
| 60 | + |
| 61 | +// CHECK-LABEL: sil_global [let] @g1 : $Int32{{$}} |
| 62 | +sil_global [let] @g1 : $Int32 |
| 63 | +sil_global private @g1_token : $Builtin.Word |
| 64 | + |
| 65 | +// CHECK-LABEL: sil_global [let] @g2 : $Int32{{$}} |
| 66 | +sil_global [let] @g2 : $Int32 |
| 67 | +sil_global private @g2_token : $Builtin.Word |
| 68 | + |
| 69 | + |
| 70 | +// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_trivialglobal_func : |
| 71 | +// CHECK-NOT: alloc_global |
| 72 | +// CHECK-NOT: store |
| 73 | +// CHECK: } // end sil function 'globalinit_trivialglobal_func' |
| 74 | +sil [global_init_once_fn] [ossa] @globalinit_trivialglobal_func : $@convention(c) () -> () { |
| 75 | +bb0: |
| 76 | + alloc_global @$trivialglobal |
| 77 | + %1 = global_addr @$trivialglobal : $*TStruct |
| 78 | + %2 = integer_literal $Builtin.Int32, 10 |
| 79 | + %3 = struct $Int32 (%2 : $Builtin.Int32) |
| 80 | + %4 = struct $TStruct (%3 : $Int32) |
| 81 | + store %4 to [trivial] %1 : $*TStruct |
| 82 | + %6 = tuple () |
| 83 | + return %6 : $() |
| 84 | +} |
| 85 | + |
| 86 | +// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_nontrivialglobal_func : |
| 87 | +// CHECK: alloc_global |
| 88 | +// CHECK: store |
| 89 | +// CHECK: } // end sil function 'globalinit_nontrivialglobal_func' |
| 90 | +sil [global_init_once_fn] [ossa] @globalinit_nontrivialglobal_func : $@convention(c) () -> () { |
| 91 | +bb0: |
| 92 | + alloc_global @$nontrivialglobal |
| 93 | + %1 = global_addr @$nontrivialglobal : $*TClass |
| 94 | + %2 = integer_literal $Builtin.Int32, 10 |
| 95 | + %3 = struct $Int32 (%2 : $Builtin.Int32) |
| 96 | + %4 = alloc_ref $TClass |
| 97 | + %5 = begin_borrow %4 : $TClass |
| 98 | + %6 = ref_element_addr %5 : $TClass, #TClass.x |
| 99 | + store %3 to [trivial] %6 : $*Int32 |
| 100 | + end_borrow %5 : $TClass |
| 101 | + store %4 to [init] %1 : $*TClass |
| 102 | + %10 = tuple () |
| 103 | + return %10 : $() |
| 104 | +} |
| 105 | + |
| 106 | +// Check that we don't crash on an initializer struct with an "undef" operand. |
| 107 | + |
| 108 | +// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_with_undef : |
| 109 | +// CHECK: alloc_global |
| 110 | +// CHECK: store |
| 111 | +// CHECK: } // end sil function 'globalinit_with_undef' |
| 112 | +sil [global_init_once_fn] [ossa] @globalinit_with_undef : $@convention(c) () -> () { |
| 113 | +bb0: |
| 114 | + alloc_global @empty_global |
| 115 | + %1 = global_addr @empty_global : $*GenericStruct<()> |
| 116 | + %2 = struct $GenericStruct<()> (undef : $()) |
| 117 | + store %2 to [trivial] %1 : $*GenericStruct<()> |
| 118 | + %4 = tuple () |
| 119 | + return %4 : $() |
| 120 | +} |
| 121 | + |
| 122 | +// CHECK-LABEL: sil [global_init_once_fn] @globalinit_nested_struct : |
| 123 | +// CHECK-NOT: alloc_global |
| 124 | +// CHECK-NOT: store |
| 125 | +// CHECK: } // end sil function 'globalinit_nested_struct' |
| 126 | +sil [global_init_once_fn] @globalinit_nested_struct : $@convention(c) () -> () { |
| 127 | +bb0: |
| 128 | + alloc_global @go |
| 129 | + %0 = global_addr @go : $*Outer |
| 130 | + %1 = integer_literal $Builtin.Int32, 2 |
| 131 | + %2 = struct $Int32 (%1 : $Builtin.Int32) |
| 132 | + %3 = struct $TStruct (%2 : $Int32) |
| 133 | + %4 = struct $Outer (%2 : $Int32, %3 : $TStruct, %2 : $Int32) |
| 134 | + store %4 to %0 : $*Outer |
| 135 | + %r = tuple () |
| 136 | + return %r : $() |
| 137 | +} |
| 138 | + |
| 139 | +// CHECK-LABEL: sil [global_init_once_fn] @globalinit_mismatching_global : |
| 140 | +// CHECK: alloc_global |
| 141 | +// CHECK: store |
| 142 | +// CHECK: } // end sil function 'globalinit_mismatching_global' |
| 143 | +sil [global_init_once_fn] @globalinit_mismatching_global : $@convention(c) () -> () { |
| 144 | +bb0: |
| 145 | + alloc_global @g1 |
| 146 | + %1 = global_addr @g2 : $*Int32 |
| 147 | + %2 = integer_literal $Builtin.Int32, 10 |
| 148 | + %3 = struct $Int32 (%2 : $Builtin.Int32) |
| 149 | + store %3 to %1 : $*Int32 |
| 150 | + %6 = tuple () |
| 151 | + return %6 : $() |
| 152 | +} |
| 153 | + |
0 commit comments