Skip to content

Commit 5e88f38

Browse files
committed
Avoid inserting end_cow_mutation_addr in some common cases
1 parent e39a31a commit 5e88f38

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceScopeFixup.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,24 @@ private extension Type {
176176
}
177177
return false
178178
}
179+
180+
// Returns true if a type maybe Array/ArraySlice/ContiguousArray which are optimized COW types.
181+
// The standard library introduces builtins begin_cow_mutation/end_cow_mutation for such types which are then used to optimize uniqueness checks.
182+
func mayHaveOptimizedCOWType(in function: Function) -> Bool {
183+
// Trivial types cannot be Array/ArraySlice/ContiguousArray.
184+
if isTrivial(in: function) {
185+
return false
186+
}
187+
// Builtin types do not contain Array/ArraySlice/ContiguousArray.
188+
if isBuiltinType {
189+
return false
190+
}
191+
// ~Copyable types cannot contain Array/ArraySlice/ContiguousArray.
192+
if isMoveOnly {
193+
return false
194+
}
195+
return true
196+
}
179197
}
180198

181199
/// Insert end_cow_mutation_addr for lifetime dependent values that maybe of type MutableSpan and depend on a mutable address.
@@ -203,7 +221,8 @@ private func createEndCOWMutationIfNeeded(lifetimeDep: LifetimeDependence, _ con
203221
return
204222
}
205223

206-
guard lifetimeDep.dependentValue.type.mayHaveMutableSpan(in: lifetimeDep.dependentValue.parentFunction, context) else {
224+
guard lifetimeDep.dependentValue.type.mayHaveMutableSpan(in: lifetimeDep.dependentValue.parentFunction, context) &&
225+
lifetimeDep.parentValue.type.mayHaveOptimizedCOWType(in: lifetimeDep.dependentValue.parentFunction) else {
207226
return
208227
}
209228

0 commit comments

Comments
 (0)