Files
swift-mirror/test/SILOptimizer/stack_promotion_escaping.swift
Erik Eckstein 71a642e51b stdlib, SIL optimizer: use the SIL copy-on-write representation in the Array types.
Use the new builtins for COW representation in Array, ContiguousArray and ArraySlice.
The basic idea is to strictly separate code which mutates an array buffer from code which reads from an array.
The concept is explained in more detail in docs/SIL.rst, section "Copy-on-Write Representation".

The main change is to use beginCOWMutation() instead of isUniquelyReferenced() and insert endCOWMutation() at the end of all mutating functions. Also, reading from the array buffer must be done differently, depending on if the buffer is in a mutable or immutable state.

All the required invariants are enforced by runtime checks - but only in an assert-build of the library: a bit in the buffer object side-table indicates if the buffer is mutable or not.

Along with the library changes, also two optimizations needed to be updated: COWArrayOpt and ObjectOutliner.
2020-06-08 15:02:22 +02:00

27 lines
859 B
Swift

// RUN: %target-swift-frontend -parse-as-library -O -module-name=test %s -emit-sil | %FileCheck %s
// REQUIRES: optimized_stdlib,swift_stdlib_no_asserts
final class Item {}
final public class Escaper {
var myItem: Item = Item()
@inline(never)
func update(items: [Item]) {
myItem = items[0]
}
// CHECK-LABEL: sil [noinline] @$s4test7EscaperC15badStuffHappensyyF : $@convention(method) (@guaranteed Escaper) -> () {
// CHECK: %2 = alloc_ref $Item
// CHECK: alloc_ref [stack] [tail_elems $Item * %{{[0-9]+}} : $Builtin.Word] $_ContiguousArrayStorage<Item>
// CHECK: return
@inline(never)
public func badStuffHappens() {
// Check that 'item' is not stack promoted, because it escapes to myItem.
let item = Item()
// On the other hand, the array buffer of the array literal should be stack promoted.
update(items:[item])
}
}