@@ -76,7 +76,14 @@ private func optimizeObjectAllocation(allocRef: AllocRefInstBase, _ context: Fun
76
76
return nil
77
77
}
78
78
79
- guard let endOfInitInst = findEndOfInitialization ( of: allocRef) else {
79
+ guard let endOfInitInst = findEndOfInitialization (
80
+ of: allocRef,
81
+ // An object with tail allocated elements is in risk of being passed to malloc_size, which does
82
+ // not work for non-heap allocated objects. Conservatively, disable objects with tail allocations.
83
+ // Note, that this does not affect Array because Array always has an end_cow_mutation at the end of
84
+ // initialization.
85
+ canStoreToGlobal: allocRef. tailAllocatedCounts. count == 0 )
86
+ else {
80
87
return nil
81
88
}
82
89
@@ -98,15 +105,15 @@ private func optimizeObjectAllocation(allocRef: AllocRefInstBase, _ context: Fun
98
105
// The end-of-initialization is either an end_cow_mutation, because it guarantees that the originally initialized
99
106
// object is not mutated (it must be copied before mutation).
100
107
// Or it is the store to a global let variable in the global's initializer function.
101
- private func findEndOfInitialization( of object: Value ) -> Instruction ? {
108
+ private func findEndOfInitialization( of object: Value , canStoreToGlobal : Bool ) -> Instruction ? {
102
109
for use in object. uses {
103
110
let user = use. instruction
104
111
switch user {
105
112
case is UpcastInst ,
106
113
is UncheckedRefCastInst ,
107
114
is MoveValueInst ,
108
115
is EndInitLetRefInst :
109
- if let ecm = findEndOfInitialization ( of: user as! SingleValueInstruction ) {
116
+ if let ecm = findEndOfInitialization ( of: user as! SingleValueInstruction , canStoreToGlobal : canStoreToGlobal ) {
110
117
return ecm
111
118
}
112
119
case let ecm as EndCOWMutationInst :
@@ -115,7 +122,8 @@ private func findEndOfInitialization(of object: Value) -> Instruction? {
115
122
}
116
123
return ecm
117
124
case let store as StoreInst :
118
- if let ga = store. destination as? GlobalAddrInst ,
125
+ if canStoreToGlobal,
126
+ let ga = store. destination as? GlobalAddrInst ,
119
127
ga. global. isLet,
120
128
ga. parentFunction. initializedGlobal == ga. global
121
129
{
0 commit comments