|
| 1 | +// RUN: %target-sil-opt -enable-sil-verify-all %s -global-opt | %FileCheck %s |
| 2 | + |
| 3 | +// This tests GlobalOpt of a trivial global variable |
| 4 | +// Loads of trivial global variable initialized with a constant is replaced with the constant in GlobalOpt |
| 5 | +// This also tests GlobalOpt to be a no-op for a non-trivial global variable used in the same way |
| 6 | +// The difference in GlobalOpt for trivial v/s non-trivial values is shown in $bartrivial and $barnontrivial |
| 7 | + |
| 8 | +sil_stage canonical |
| 9 | + |
| 10 | +import Builtin |
| 11 | +import Swift |
| 12 | +import SwiftShims |
| 13 | + |
| 14 | +public struct TStruct { |
| 15 | + let x: Int32 |
| 16 | + init(x: Int32) |
| 17 | +} |
| 18 | + |
| 19 | +let trivialglobal: TStruct |
| 20 | + |
| 21 | +public class TClass { |
| 22 | + final let x: Int32 |
| 23 | + init(x: Int32) |
| 24 | + deinit |
| 25 | +} |
| 26 | + |
| 27 | +let nontrivialglobal: TClass |
| 28 | + |
| 29 | +// CHECK-LABEL: sil_global hidden [let] @$trivialglobal : $TStruct = { |
| 30 | +// CHECK: [[CONST:%.*]] = integer_literal $Builtin.Int32, 10 |
| 31 | +// CHECK: [[INT:%.*]] = struct $Int32 ([[CONST]] : $Builtin.Int32) |
| 32 | +// CHECK: %initval = struct $TStruct ([[INT]] : $Int32) |
| 33 | +sil_global private @globalinit_trivialglobal_token : $Builtin.Word |
| 34 | + |
| 35 | +sil_global hidden [let] @$trivialglobal : $TStruct |
| 36 | + |
| 37 | +sil_global private @globalinit_nontrivialglobal_token : $Builtin.Word |
| 38 | + |
| 39 | +sil_global hidden [let] @$nontrivialglobal : $TClass |
| 40 | + |
| 41 | +sil private @globalinit_trivialglobal_func : $@convention(c) () -> () { |
| 42 | +bb0: |
| 43 | + alloc_global @$trivialglobal |
| 44 | + %1 = global_addr @$trivialglobal : $*TStruct |
| 45 | + %2 = integer_literal $Builtin.Int32, 10 |
| 46 | + %3 = struct $Int32 (%2 : $Builtin.Int32) |
| 47 | + %4 = struct $TStruct (%3 : $Int32) |
| 48 | + store %4 to %1 : $*TStruct |
| 49 | + %6 = tuple () |
| 50 | + return %6 : $() |
| 51 | +} |
| 52 | + |
| 53 | +// CHECK-LABEL: sil hidden [global_init] @$trivialglobal_unsafemutableaddressor : |
| 54 | +// CHECK: [[GLOBL:%.*]] = global_addr @$trivialglobal : $*TStruct |
| 55 | +// CHECK: [[GLOBL_ADDR:%.*]] = address_to_pointer [[GLOBL]] : $*TStruct to $Builtin.RawPointer |
| 56 | +// CHECK: return [[GLOBL_ADDR]] : $Builtin.RawPointer |
| 57 | +// CHECK: } // end sil function '$trivialglobal_unsafemutableaddressor' |
| 58 | +sil hidden [global_init] @$trivialglobal_unsafemutableaddressor : $@convention(thin) () -> Builtin.RawPointer { |
| 59 | +bb0: |
| 60 | + %0 = global_addr @globalinit_trivialglobal_token : $*Builtin.Word |
| 61 | + %1 = address_to_pointer %0 : $*Builtin.Word to $Builtin.RawPointer |
| 62 | + %2 = function_ref @globalinit_trivialglobal_func : $@convention(c) () -> () |
| 63 | + %3 = builtin "once"(%1 : $Builtin.RawPointer, %2 : $@convention(c) () -> ()) : $() |
| 64 | + %4 = global_addr @$trivialglobal : $*TStruct |
| 65 | + %5 = address_to_pointer %4 : $*TStruct to $Builtin.RawPointer |
| 66 | + return %5 : $Builtin.RawPointer |
| 67 | +} |
| 68 | + |
| 69 | +// $bartrivial's access to the trivial global variable via the accessor is optimized to the rhs of the init value |
| 70 | + |
| 71 | +// CHECK-LABEL: sil hidden [noinline] @$bartrivial : |
| 72 | +// CHECK: [[CONST:%.*]] = integer_literal $Builtin.Int32, 10 |
| 73 | +// CHECK: [[INT:%.*]] = struct $Int32 ([[CONST]] : $Builtin.Int32) |
| 74 | +// CHECK: return [[INT]] : $Int32 |
| 75 | +// CHECK-LABEL: } // end sil function '$bartrivial' |
| 76 | +sil hidden [noinline] @$bartrivial : $@convention(thin) () -> Int32 { |
| 77 | +bb0: |
| 78 | + %0 = function_ref @$trivialglobal_unsafemutableaddressor : $@convention(thin) () -> Builtin.RawPointer |
| 79 | + %1 = apply %0() : $@convention(thin) () -> Builtin.RawPointer |
| 80 | + %2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*TStruct |
| 81 | + %3 = struct_element_addr %2 : $*TStruct, #TStruct.x |
| 82 | + %4 = load %3 : $*Int32 |
| 83 | + return %4 : $Int32 |
| 84 | +} |
| 85 | + |
| 86 | +// CHECK-LABEL: sil private @globalinit_nontrivialglobal_func : |
| 87 | +// CHECK: alloc_global @$nontrivialglobal |
| 88 | +// CHECK: [[GLOBL_ADDR:%.*]] = global_addr @$nontrivialglobal : $*TClass |
| 89 | +// CHECK: [[REF:%.*]] = alloc_ref $TClass |
| 90 | +// CHECK: store [[REF]] to [[GLOBL_ADDR]] : $*TClass |
| 91 | +// CHECK: } // end sil function 'globalinit_nontrivialglobal_func' |
| 92 | +sil private @globalinit_nontrivialglobal_func : $@convention(c) () -> () { |
| 93 | +bb0: |
| 94 | + alloc_global @$nontrivialglobal |
| 95 | + %1 = global_addr @$nontrivialglobal : $*TClass |
| 96 | + %2 = integer_literal $Builtin.Int32, 10 |
| 97 | + %3 = struct $Int32 (%2 : $Builtin.Int32) |
| 98 | + %4 = alloc_ref $TClass |
| 99 | + %7 = ref_element_addr %4 : $TClass, #TClass.x |
| 100 | + store %3 to %7 : $*Int32 |
| 101 | + store %4 to %1 : $*TClass |
| 102 | + %10 = tuple () |
| 103 | + return %10 : $() |
| 104 | +} |
| 105 | + |
| 106 | +sil hidden [global_init] @$nontrivialglobal_unsafemutableaccessor : $@convention(thin) () -> Builtin.RawPointer { |
| 107 | +bb0: |
| 108 | + %0 = global_addr @globalinit_nontrivialglobal_token : $*Builtin.Word |
| 109 | + %1 = address_to_pointer %0 : $*Builtin.Word to $Builtin.RawPointer |
| 110 | + %2 = function_ref @globalinit_nontrivialglobal_func : $@convention(c) () -> () |
| 111 | + %3 = builtin "once"(%1 : $Builtin.RawPointer, %2 : $@convention(c) () -> ()) : $() |
| 112 | + %4 = global_addr @$nontrivialglobal : $*TClass |
| 113 | + %5 = address_to_pointer %4 : $*TClass to $Builtin.RawPointer |
| 114 | + return %5 : $Builtin.RawPointer |
| 115 | +} |
| 116 | + |
| 117 | +// $barnontrivial's access to the non-trivial global variable does not get optimized away |
| 118 | + |
| 119 | +// CHECK-LABEL: sil hidden [noinline] @$barnontrivial : |
| 120 | +// CHECK: [[FUNC_REF:%.*]] = function_ref @$nontrivialglobal_unsafemutableaccessor : |
| 121 | +// CHECK: [[APPLY:%.*]] = apply [[FUNC_REF]]() : |
| 122 | +// CHECK-LABEL: } // end sil function '$barnontrivial' |
| 123 | +sil hidden [noinline] @$barnontrivial : $@convention(thin) () -> Int32 { |
| 124 | +bb0: |
| 125 | + %0 = function_ref @$nontrivialglobal_unsafemutableaccessor : $@convention(thin) () -> Builtin.RawPointer |
| 126 | + %1 = apply %0() : $@convention(thin) () -> Builtin.RawPointer |
| 127 | + %2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*TClass |
| 128 | + %3 = load %2 : $*TClass |
| 129 | + %4 = ref_element_addr %3 : $TClass, #TClass.x |
| 130 | + %5 = load %4 : $*Int32 |
| 131 | + return %5 : $Int32 |
| 132 | +} |
| 133 | + |
0 commit comments