@@ -76,7 +76,14 @@ private func optimizeObjectAllocation(allocRef: AllocRefInstBase, _ context: Fun
7676 return nil
7777 }
7878
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 {
8087 return nil
8188 }
8289
@@ -98,15 +105,15 @@ private func optimizeObjectAllocation(allocRef: AllocRefInstBase, _ context: Fun
98105// The end-of-initialization is either an end_cow_mutation, because it guarantees that the originally initialized
99106// object is not mutated (it must be copied before mutation).
100107// 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 ? {
102109 for use in object. uses {
103110 let user = use. instruction
104111 switch user {
105112 case is UpcastInst ,
106113 is UncheckedRefCastInst ,
107114 is MoveValueInst ,
108115 is EndInitLetRefInst :
109- if let ecm = findEndOfInitialization ( of: user as! SingleValueInstruction ) {
116+ if let ecm = findEndOfInitialization ( of: user as! SingleValueInstruction , canStoreToGlobal : canStoreToGlobal ) {
110117 return ecm
111118 }
112119 case let ecm as EndCOWMutationInst :
@@ -115,7 +122,8 @@ private func findEndOfInitialization(of object: Value) -> Instruction? {
115122 }
116123 return ecm
117124 case let store as StoreInst :
118- if let ga = store. destination as? GlobalAddrInst ,
125+ if canStoreToGlobal,
126+ let ga = store. destination as? GlobalAddrInst ,
119127 ga. global. isLet,
120128 ga. parentFunction. initializedGlobal == ga. global
121129 {
0 commit comments