Skip to content

Commit

Permalink
Add array.init semantics.
Browse files Browse the repository at this point in the history
This way, array optimization can see initialization as a copy of the
whole array value into a local variable, regardless of whether it's
from a factory method or Array initializer.

I'm making an assumption that when we return an Array by value, we
can't have an alias of the array buffer without retaining it.

Swift SVN r20444
  • Loading branch information
atrick committed Jul 23, 2014
1 parent cd799f6 commit f36ff58
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion docs/HighLevelSILOptimizations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ control dependent because it may trap. Some operations are *guarded*
by others. A guarded operation can never be executed before its
guard.

array.init

Initialize an array with new storage. This currently applies to any
initializer that does not get its storage from an argument. This
semantically writes to every array element and the array's storage
descriptor. ``init`` also implies the guarding semantics of
``make_mutable``. It is not itself guarded by ``make_mutable`` and
may act as a guard to other mutating operations, such as
``set_element``.

array.get_element(index: Int) -> Element

Read an element from the array at the specified index. No other
Expand Down Expand Up @@ -176,7 +186,7 @@ array.make_mutable()
executed safely on any array at any point. i.e. the optimizer can
freely insert calls to make_mutable.

array.mutate_unknown()
array.mutate_unknown

This operation may mutate the array in any way, so it semantically
writes to the entire array state and is naturally control
Expand Down
4 changes: 4 additions & 0 deletions stdlib/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public struct ${Self}<T> : MutableCollectionType, Sliceable {
public
typealias _Buffer = _${Self}Buffer<T>

/// Initialization from an existing buffer does not have "array.init"
/// semantics because the caller may retain an alias to buffer.
public
init(_ buffer: _Buffer) {
self._buffer = buffer
Expand All @@ -148,6 +150,7 @@ extension ${Self} {

extension ${Self} : ArrayType {
/// Construct an empty ${Self}
@semantics("array.init")
public init() {
_buffer = _Buffer()
}
Expand All @@ -160,6 +163,7 @@ extension ${Self} : ArrayType {

/// Construct an array of `count` elements, each initialized to
/// `repeatedValue`.
@semantics("array.init")
public init(count: Int, repeatedValue: T) {
_precondition(count >= 0, "Can't construct ${Self} with count < 0")
_buffer = _Buffer()
Expand Down

0 comments on commit f36ff58

Please sign in to comment.