Files
swift-mirror/test/SILOptimizer/init_static_globals.sil
Erik Eckstein afc0f617e0 Optimizer: support statically initialized globals which contain pointers to other globals
For example:
```
  var p = Point(x: 10, y: 20)
  let o = UnsafePointer(&p)
```

Also support outlined arrays with pointers to other globals. For example:
```
var g1 = 1
var g2 = 2

func f() -> [UnsafePointer<Int>] {
  return [UnsafePointer(&g1), UnsafePointer(&g2)]
}
```
2023-08-10 20:50:36 +02:00

274 lines
9.4 KiB
Plaintext

// RUN: %target-sil-opt -enable-sil-verify-all %s -initialize-static-globals | %FileCheck %s
// REQUIRES: swift_in_compiler
sil_stage canonical
import Builtin
import Swift
import SwiftShims
public struct TStruct {
let x: Int32
init(x: Int32)
}
struct Outer {
let a: Int32
let b: TStruct
let c: Int32
}
let trivialglobal: TStruct
public class TClass {
final let x: Int32
init(x: Int32)
deinit
}
struct GenericStruct<T> {
var x: T
}
struct TwoFields {
let a: Int32
let b: Int32
}
let nontrivialglobal: TClass
// CHECK-LABEL: sil_global hidden [let] @$trivialglobal : $TStruct = {
// CHECK: [[CONST:%.*]] = integer_literal $Builtin.Int32, 10
// CHECK: [[INT:%.*]] = struct $Int32 ([[CONST]] : $Builtin.Int32)
// CHECK: %initval = struct $TStruct ([[INT]] : $Int32)
sil_global hidden [let] @$trivialglobal : $TStruct
sil_global private @globalinit_trivialglobal_token : $Builtin.Word
// CHECK-LABEL: sil_global hidden [let] @$nontrivialglobal : $TClass{{$}}
sil_global hidden [let] @$nontrivialglobal : $TClass
sil_global private @globalinit_nontrivialglobal_token : $Builtin.Word
// CHECK-LABEL: sil_global hidden [let] @empty_global : $GenericStruct<()>{{$}}
sil_global hidden [let] @empty_global : $GenericStruct<()>
sil_global private @empty_global_token : $Builtin.Word
// CHECK: sil_global @go : $Outer = {
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 2
// CHECK-NEXT: %1 = struct $Int32 (%0 : $Builtin.Int32)
// CHECK-NEXT: %2 = struct $TStruct (%1 : $Int32)
// CHECK-NEXT: %initval = struct $Outer (%1 : $Int32, %2 : $TStruct, %1 : $Int32)
// CHECK-NEXT: }
sil_global @go : $Outer
sil_global private @globalinit_token0 : $Builtin.Word
// CHECK-LABEL: sil_global [let] @g1 : $Int32{{$}}
sil_global [let] @g1 : $Int32
sil_global private @g1_token : $Builtin.Word
// CHECK-LABEL: sil_global [let] @g2 : $Int32{{$}}
sil_global [let] @g2 : $Int32
sil_global private @g2_token : $Builtin.Word
// CHECK-LABEL: sil_global [let] @g3 : $Optional<UnsafeMutablePointer<Int>>
sil_global [let] @g3 : $Optional<UnsafeMutablePointer<Int>>
sil_global private @g3_token : $Builtin.Word
// CHECK-LABEL: sil_global [let] @g4 : $Optional<UnsafeMutablePointer<Int>>
sil_global [let] @g4 : $Optional<UnsafeMutablePointer<Int>>
sil_global private @g4_token : $Builtin.Word
// CHECK-LABEL: sil_global [let] @g5 : $TwoFields = {
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 11
// CHECK-NEXT: %1 = struct $Int32 (%0 : $Builtin.Int32)
// CHECK-NEXT: %2 = integer_literal $Builtin.Int32, 10
// CHECK-NEXT: %3 = struct $Int32 (%2 : $Builtin.Int32)
// CHECK-NEXT: %initval = struct $TwoFields (%1 : $Int32, %3 : $Int32)
// CHECK-NEXT: }
sil_global [let] @g5 : $TwoFields
sil_global [let] @g6 : $TwoFields
sil_global [let] @g7 : $TwoFields
// CHECK-LABEL: sil_global [let] @g8 : $UnsafePointer<Int32> = {
// CHECK-NEXT: %0 = global_addr @g1 : $*Int32
// CHECK-NEXT: %1 = address_to_pointer %0 : $*Int32 to $Builtin.RawPointer
// CHECK-NEXT: %initval = struct $UnsafePointer<Int32> (%1 : $Builtin.RawPointer)
// CHECK-NEXT: }
sil_global [let] @g8 : $UnsafePointer<Int32>
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_trivialglobal_func :
// CHECK-NOT: alloc_global
// CHECK-NOT: store
// CHECK: } // end sil function 'globalinit_trivialglobal_func'
sil [global_init_once_fn] [ossa] @globalinit_trivialglobal_func : $@convention(c) () -> () {
bb0:
alloc_global @$trivialglobal
%1 = global_addr @$trivialglobal : $*TStruct
%2 = integer_literal $Builtin.Int32, 10
%3 = struct $Int32 (%2 : $Builtin.Int32)
%4 = struct $TStruct (%3 : $Int32)
store %4 to [trivial] %1 : $*TStruct
%6 = tuple ()
return %6 : $()
}
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_nontrivialglobal_func :
// CHECK: alloc_global
// CHECK: store
// CHECK: } // end sil function 'globalinit_nontrivialglobal_func'
sil [global_init_once_fn] [ossa] @globalinit_nontrivialglobal_func : $@convention(c) () -> () {
bb0:
alloc_global @$nontrivialglobal
%1 = global_addr @$nontrivialglobal : $*TClass
%2 = integer_literal $Builtin.Int32, 10
%3 = struct $Int32 (%2 : $Builtin.Int32)
%4 = alloc_ref $TClass
%5 = begin_borrow %4 : $TClass
%6 = ref_element_addr %5 : $TClass, #TClass.x
store %3 to [trivial] %6 : $*Int32
end_borrow %5 : $TClass
store %4 to [init] %1 : $*TClass
%10 = tuple ()
return %10 : $()
}
// Check that we don't crash on an initializer struct with an "undef" operand.
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_with_undef :
// CHECK: alloc_global
// CHECK: store
// CHECK: } // end sil function 'globalinit_with_undef'
sil [global_init_once_fn] [ossa] @globalinit_with_undef : $@convention(c) () -> () {
bb0:
alloc_global @empty_global
%1 = global_addr @empty_global : $*GenericStruct<()>
%2 = struct $GenericStruct<()> (undef : $())
store %2 to [trivial] %1 : $*GenericStruct<()>
%4 = tuple ()
return %4 : $()
}
// CHECK-LABEL: sil [global_init_once_fn] @globalinit_nested_struct :
// CHECK-NOT: alloc_global
// CHECK-NOT: store
// CHECK: } // end sil function 'globalinit_nested_struct'
sil [global_init_once_fn] @globalinit_nested_struct : $@convention(c) () -> () {
bb0:
alloc_global @go
%0 = global_addr @go : $*Outer
%1 = integer_literal $Builtin.Int32, 2
%2 = struct $Int32 (%1 : $Builtin.Int32)
%3 = struct $TStruct (%2 : $Int32)
%4 = struct $Outer (%2 : $Int32, %3 : $TStruct, %2 : $Int32)
store %4 to %0 : $*Outer
%r = tuple ()
return %r : $()
}
// CHECK-LABEL: sil [global_init_once_fn] @globalinit_mismatching_global :
// CHECK: alloc_global
// CHECK: store
// CHECK: } // end sil function 'globalinit_mismatching_global'
sil [global_init_once_fn] @globalinit_mismatching_global : $@convention(c) () -> () {
bb0:
alloc_global @g1
%1 = global_addr @g2 : $*Int32
%2 = integer_literal $Builtin.Int32, 10
%3 = struct $Int32 (%2 : $Builtin.Int32)
store %3 to %1 : $*Int32
%6 = tuple ()
return %6 : $()
}
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_enum :
// CHECK-NOT: alloc_global
// CHECK-NOT: store
// CHECK: } // end sil function 'globalinit_enum'
sil [global_init_once_fn] [ossa] @globalinit_enum : $@convention(c) () -> () {
bb0:
alloc_global @g3
%2 = global_addr @g3 : $*Optional<UnsafeMutablePointer<Int>>
%3 = enum $Optional<UnsafeMutablePointer<Int>>, #Optional.none!enumelt
store %3 to [trivial] %2 : $*Optional<UnsafeMutablePointer<Int>>
%5 = tuple ()
return %5 : $()
}
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_enum_inttoptr :
// CHECK-NOT: alloc_global
// CHECK-NOT: store
// CHECK: } // end sil function 'globalinit_enum_inttoptr'
sil [global_init_once_fn] [ossa] @globalinit_enum_inttoptr : $@convention(c) () -> () {
bb0:
alloc_global @g4
%2 = global_addr @g4 : $*Optional<UnsafeMutablePointer<Int>>
%4 = integer_literal $Builtin.Word, 1111638594
%5 = builtin "inttoptr_Word"(%4 : $Builtin.Word) : $Builtin.RawPointer
%6 = struct $UnsafeMutablePointer<Int> (%5 : $Builtin.RawPointer)
%7 = enum $Optional<UnsafeMutablePointer<Int>>, #Optional.some!enumelt, %6 : $UnsafeMutablePointer<Int>
store %7 to [trivial] %2 : $*Optional<UnsafeMutablePointer<Int>>
%10 = tuple ()
return %10 : $()
}
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_separate_stores :
// CHECK-NOT: alloc_global
// CHECK-NOT: store
// CHECK: } // end sil function 'globalinit_separate_stores'
sil [global_init_once_fn] [ossa] @globalinit_separate_stores : $@convention(c) () -> () {
bb0:
alloc_global @g5
%1 = global_addr @g5 : $*TwoFields
%2 = integer_literal $Builtin.Int32, 10
%3 = struct $Int32 (%2 : $Builtin.Int32)
%4 = struct_element_addr %1 : $*TwoFields, #TwoFields.b
store %3 to [trivial] %4 : $*Int32
%6 = integer_literal $Builtin.Int32, 11
%7 = struct $Int32 (%6 : $Builtin.Int32)
%8 = struct_element_addr %1 : $*TwoFields, #TwoFields.a
store %7 to [trivial] %8 : $*Int32
%10 = tuple ()
return %10 : $()
}
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_wrong_separate_stores :
// CHECK: alloc_global
// CHECK: store
// CHECK: store
// CHECK: } // end sil function 'globalinit_wrong_separate_stores'
sil [global_init_once_fn] [ossa] @globalinit_wrong_separate_stores : $@convention(c) () -> () {
bb0:
alloc_global @g5
%1 = global_addr @g5 : $*TwoFields
%2 = integer_literal $Builtin.Int32, 10
%3 = struct $Int32 (%2 : $Builtin.Int32)
%4 = struct_element_addr %1 : $*TwoFields, #TwoFields.b
store %3 to [trivial] %4 : $*Int32
%6 = integer_literal $Builtin.Int32, 11
%7 = struct $Int32 (%6 : $Builtin.Int32)
store %7 to [trivial] %4 : $*Int32
%10 = tuple ()
return %10 : $()
}
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_pointer_to_other_global :
// CHECK-NOT: alloc_global
// CHECK-NOT: store
// CHECK: } // end sil function 'globalinit_pointer_to_other_global'
sil [global_init_once_fn] [ossa] @globalinit_pointer_to_other_global : $@convention(c) (Builtin.RawPointer) -> () {
bb0(%0 : $Builtin.RawPointer):
alloc_global @g8
%2 = global_addr @g8 : $*UnsafePointer<Int32>
%3 = global_addr @g1 : $*Int32
%4 = begin_access [read] [dynamic] %3 : $*Int32
%5 = address_to_pointer %4 : $*Int32 to $Builtin.RawPointer
end_access %4 : $*Int32
%7 = struct $UnsafePointer<Int32> (%5 : $Builtin.RawPointer)
store %7 to [trivial] %2 : $*UnsafePointer<Int32>
%8 = tuple ()
return %8 : $()
}