Revert "New SIL instructions to support tail-allocated arrays in SIL."

This commit is contained in:
Dmitri Gribenko
2016-09-15 00:25:25 -07:00
committed by GitHub
parent cd07c2c125
commit fbb3cf35a5
68 changed files with 1145 additions and 1470 deletions

View File

@@ -25,6 +25,8 @@ internal final class _EmptyArrayStorage
_sanityCheckFailure("creating instance of _EmptyArrayStorage")
}
var countAndCapacity: _ArrayBody
#if _runtime(_ObjC)
override func _withVerbatimBridgedUnsafeBuffer<R>(
_ body: (UnsafeBufferPointer<AnyObject>) throws -> R
@@ -99,8 +101,10 @@ class _ContiguousArrayStorage1 : _ContiguousArrayStorageBase {
final class _ContiguousArrayStorage<Element> : _ContiguousArrayStorage1 {
deinit {
_elementPointer.deinitialize(count: countAndCapacity.count)
_fixLifetime(self)
__manager._elementPointer.deinitialize(
count: __manager._headerPointer.pointee.count)
__manager._headerPointer.deinitialize()
_fixLifetime(__manager)
}
#if _runtime(_ObjC)
@@ -110,10 +114,10 @@ final class _ContiguousArrayStorage<Element> : _ContiguousArrayStorage1 {
_ body: (UnsafeBufferPointer<AnyObject>) throws -> Void
) rethrows {
if _isBridgedVerbatimToObjectiveC(Element.self) {
let count = countAndCapacity.count
let elements = UnsafeRawPointer(_elementPointer)
let count = __manager.header.count
let elements = UnsafeRawPointer(__manager._elementPointer)
.assumingMemoryBound(to: AnyObject.self)
defer { _fixLifetime(self) }
defer { _fixLifetime(__manager) }
try body(UnsafeBufferPointer(start: elements, count: count))
}
}
@@ -125,7 +129,7 @@ final class _ContiguousArrayStorage<Element> : _ContiguousArrayStorage1 {
_sanityCheck(
!_isBridgedVerbatimToObjectiveC(Element.self),
"Verbatim bridging should be handled separately")
return countAndCapacity.count
return __manager.header.count
}
/// Bridge array elements and return a new buffer that owns them.
@@ -136,15 +140,15 @@ final class _ContiguousArrayStorage<Element> : _ContiguousArrayStorage1 {
_sanityCheck(
!_isBridgedVerbatimToObjectiveC(Element.self),
"Verbatim bridging should be handled separately")
let count = countAndCapacity.count
let count = __manager.header.count
let result = _HeapBuffer<Int, AnyObject>(
_HeapBufferStorage<Int, AnyObject>.self, count, count)
let resultPtr = result.baseAddress
let p = _elementPointer
let p = __manager._elementPointer
for i in 0..<count {
(resultPtr + i).initialize(to: _bridgeAnythingToObjectiveC(p[i]))
}
_fixLifetime(self)
_fixLifetime(__manager)
return result
}
#endif
@@ -170,8 +174,12 @@ final class _ContiguousArrayStorage<Element> : _ContiguousArrayStorage1 {
return Element.self
}
internal final var _elementPointer : UnsafeMutablePointer<Element> {
return UnsafeMutablePointer(Builtin.projectTailElems(self, Element.self))
internal // private
typealias Manager = ManagedBufferPointer<_ArrayBody, Element>
internal // private
var __manager: Manager {
return Manager(_uncheckedUnsafeBufferObject: self)
}
}
@@ -191,16 +199,14 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
self = _ContiguousArrayBuffer<Element>()
}
else {
_storage = Builtin.allocWithTailElems_1(
_ContiguousArrayStorage<Element>.self,
realMinimumCapacity._builtinWordValue, Element.self)
let storageAddr = UnsafeMutableRawPointer(Builtin.bridgeToRawPointer(_storage))
let endAddr = storageAddr + _swift_stdlib_malloc_size(storageAddr)
let realCapacity = endAddr.assumingMemoryBound(to: Element.self) - firstElementAddress
__bufferPointer = ManagedBufferPointer(
_uncheckedBufferClass: _ContiguousArrayStorage<Element>.self,
minimumCapacity: realMinimumCapacity)
_initStorageHeader(
count: uninitializedCount, capacity: realCapacity)
count: uninitializedCount, capacity: __bufferPointer.capacity)
_fixLifetime(__bufferPointer)
}
}
@@ -213,13 +219,17 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
/// - Warning: storage may have been stack-allocated, so it's
/// crucial not to call, e.g., `malloc_size` on it.
internal init(count: Int, storage: _ContiguousArrayStorage<Element>) {
_storage = storage
__bufferPointer = ManagedBufferPointer(
_uncheckedUnsafeBufferObject: storage)
_initStorageHeader(count: count, capacity: count)
_fixLifetime(__bufferPointer)
}
internal init(_ storage: _ContiguousArrayStorageBase) {
_storage = storage
__bufferPointer = ManagedBufferPointer(
_uncheckedUnsafeBufferObject: storage)
}
/// Initialize the body part of our storage.
@@ -232,12 +242,11 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
let verbatim = false
#endif
// We can initialize by assignment because _ArrayBody is a trivial type,
// i.e. contains no references.
_storage.countAndCapacity = _ArrayBody(
count: count,
capacity: capacity,
elementTypeIsBridgedVerbatim: verbatim)
__bufferPointer._headerPointer.initialize(to:
_ArrayBody(
count: count,
capacity: capacity,
elementTypeIsBridgedVerbatim: verbatim))
}
/// True, if the array is native and does not need a deferred type check.
@@ -247,8 +256,7 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
/// A pointer to the first element.
internal var firstElementAddress: UnsafeMutablePointer<Element> {
return UnsafeMutablePointer(Builtin.projectTailElems(_storage,
Element.self))
return __bufferPointer._elementPointer
}
internal var firstElementAddressIfContiguous: UnsafeMutablePointer<Element>? {
@@ -278,7 +286,8 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
//===--- _ArrayBufferProtocol conformance -----------------------------------===//
/// Create an empty buffer.
internal init() {
_storage = _emptyArrayStorage
__bufferPointer = ManagedBufferPointer(
_uncheckedUnsafeBufferObject: _emptyArrayStorage)
}
internal init(_buffer buffer: _ContiguousArrayBuffer, shiftedToStartIndex: Int) {
@@ -337,7 +346,7 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
/// The number of elements the buffer stores.
internal var count: Int {
get {
return _storage.countAndCapacity.count
return __bufferPointer.header.count
}
nonmutating set {
_sanityCheck(newValue >= 0)
@@ -346,7 +355,7 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
newValue <= capacity,
"Can't grow an array buffer past its capacity")
_storage.countAndCapacity.count = newValue
__bufferPointer._headerPointer.pointee.count = newValue
}
}
@@ -355,14 +364,14 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
@inline(__always)
func _checkValidSubscript(_ index : Int) {
_precondition(
(index >= 0) && (index < count),
(index >= 0) && (index < __bufferPointer.header.count),
"Index out of range"
)
}
/// The number of elements the buffer can store without reallocation.
internal var capacity: Int {
return _storage.countAndCapacity.capacity
return __bufferPointer.header.capacity
}
/// Copy the elements in `bounds` from this buffer into uninitialized
@@ -389,7 +398,7 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
internal subscript(bounds: Range<Int>) -> _SliceBuffer<Element> {
get {
return _SliceBuffer(
owner: _storage,
owner: __bufferPointer.buffer,
subscriptBaseAddress: subscriptBaseAddress,
indices: bounds,
hasNativeBuffer: true)
@@ -405,14 +414,14 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
/// may need to be considered, such as whether the buffer could be
/// some immutable Cocoa container.
internal mutating func isUniquelyReferenced() -> Bool {
return _isUnique(&_storage)
return __bufferPointer.isUniqueReference()
}
/// Returns `true` iff this buffer's storage is either
/// uniquely-referenced or pinned. NOTE: this does not mean
/// the buffer is mutable; see the comment on isUniquelyReferenced.
internal mutating func isUniquelyReferencedOrPinned() -> Bool {
return _isUniqueOrPinned(&_storage)
return __bufferPointer._isUniqueOrPinnedReference()
}
#if _runtime(_ObjC)
@@ -480,7 +489,11 @@ internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
return true
}
internal var _storage: _ContiguousArrayStorageBase
internal var _storage: _ContiguousArrayStorageBase {
return Builtin.castFromNativeObject(__bufferPointer._nativeBuffer)
}
var __bufferPointer: ManagedBufferPointer<_ArrayBody, Element>
}
/// Append the elements of `rhs` to `lhs`.