Skip to content

Commit 0b0959a

Browse files
authored
Merge pull request #72183 from eeckstein/fix-init-static-globals
InitializeStaticGlobals: don't merge stores for structs with unreferencable storage
2 parents 89b8405 + c5c2688 commit 0b0959a

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/InitializeStaticGlobals.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,14 @@ private func getSequenceOfElementStores(firstStore: StoreInst) -> ([StoreInst],
113113
return nil
114114
}
115115
let structAddr = elementAddr.struct
116-
if structAddr.type.isMoveOnly {
116+
let structType = structAddr.type
117+
if structType.isMoveOnly {
117118
return nil
118119
}
119-
guard let fields = structAddr.type.getNominalFields(in: firstStore.parentFunction) else {
120+
if structType.nominal.isStructWithUnreferenceableStorage {
121+
return nil
122+
}
123+
guard let fields = structType.getNominalFields(in: firstStore.parentFunction) else {
120124
return nil
121125
}
122126
let numElements = fields.count

test/SILOptimizer/Inputs/bitfield.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
struct S {
3+
int a;
4+
int b : 8;
5+
};

test/SILOptimizer/global_init_opt.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -parse-as-library -O -module-name=test %s -emit-sil | %FileCheck %s
1+
// RUN: %target-swift-frontend -parse-as-library -O -import-objc-header %S/Inputs/bitfield.h -module-name=test %s -emit-sil | %FileCheck %s
22

33
// REQUIRES: swift_in_compiler
44

@@ -7,6 +7,9 @@ var gg: Int = {
77
return 27
88
}()
99

10+
// Test that the compiler doesn't crash with a global C bitfield.
11+
var bitfield = S(a: 0, b: 0)
12+
1013
// CHECK-LABEL: sil @$s4test3cseSiyF
1114
// CHECK: builtin "once"
1215
// CHECK-NOT: builtin "once"

0 commit comments

Comments
 (0)