Files
swift-mirror/test/SILOptimizer/globalopt_resilience_testing.swift
Slava Pestov a86f174445 SILOptimizer: Don't optimize initializers for dynamically-sized globals
If the size of a global variable is not known at compile time, we emit
a fixed size buffer together with initialization code when the global
variable is initialized.

Make sure the SIL optimizer does not convert this into a static
initialization, even if the size of the type is known inside the
module where the global is declared, because we don't have a way to
statically initialize something that looks like a fixed-size buffer
to client code.

Fixes <https://bugs.swift.org/browse/SR-11709>.
2019-11-07 17:16:40 -05:00

28 lines
1.1 KiB
Swift

// RUN: %target-swift-frontend -emit-sil -O -enable-library-evolution -primary-file %s | %FileCheck %s
// RUN: %target-swift-frontend -emit-sil -O -enable-library-evolution -enable-testing -primary-file %s | %FileCheck %s --check-prefix=CHECK-TESTING
// If a global variable with a resilient type has public linkage, we have to
// allocate a buffer for it even if the type has a fixed size in its
// defining module.
//
// There are two cases where this can occur:
//
// - An internal property is defined in a resilient module built with
// -enable-testing
//
// - A public property is defined to have the @_fixed_layout attribute
public struct Wrapper {
var x: Int32
static let usefulConstant = Wrapper(x: 321)
}
// CHECK-LABEL: sil_global hidden [let] @$s28globalopt_resilience_testing7WrapperV14usefulConstantACvpZ : $Wrapper = {
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 321
// CHECK-NEXT: %1 = struct $Int32 (%0 : $Builtin.Int32)
// CHECK-NEXT: %initval = struct $Wrapper (%1 : $Int32)
// CHECK-NEXT: }
// CHECK-TESTING-LABEL: sil_global [let] @$s28globalopt_resilience_testing7WrapperV14usefulConstantACvpZ : $Wrapper{{$}}