Skip to content

Commit b20d154

Browse files
authored
Merge pull request #2159 from swiftwasm/katei/merge-main-2020-11-06
Merge main 2020-11-06
2 parents 01d3115 + 947d5aa commit b20d154

File tree

529 files changed

+11138
-6623
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

529 files changed

+11138
-6623
lines changed

docs/ABI/Mangling.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ Globals
175175

176176
global ::= global 'MJ' // noncanonical specialized generic type metadata instantiation cache associated with global
177177
global ::= global 'MN' // noncanonical specialized generic type metadata for global
178+
global ::= global 'Mz' // canonical specialized generic type metadata caching token
178179

179180
#if SWIFT_RUNTIME_VERSION >= 5.4
180181
global ::= context (decl-name '_')+ 'WZ' // global variable one-time initialization function
@@ -218,6 +219,8 @@ types where the metadata itself has unknown layout.)
218219
global ::= global 'Tm' // merged function
219220
global ::= entity // some identifiable thing
220221
global ::= from-type to-type generic-signature? 'TR' // reabstraction thunk
222+
global ::= from-type to-type generic-signature? 'TR' // reabstraction thunk
223+
global ::= impl-function-type 'Tz' // objc-to-swift-async completion handler block implementation
221224
global ::= from-type to-type self-type generic-signature? 'Ty' // reabstraction thunk with dynamic 'Self' capture
222225
global ::= from-type to-type generic-signature? 'Tr' // obsolete mangling for reabstraction thunk
223226
global ::= entity generic-signature? type type* 'TK' // key path getter

docs/CppInteroperabilityManifesto.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ provide the following guarantees:
307307

308308
- `inout` are backed by initialized memory on function entry and function exit
309309
(an implication of the copy-in/copy-out semantics). Destroying the object in
310-
`inout` requires using unsafe construts. Therefore, in safe Swift `inout`
310+
`inout` requires using unsafe constructs. Therefore, in safe Swift `inout`
311311
parameters are backed by initialized memory throughout function execution.
312312

313313
- `inout` parameters don't alias each other or any other values that program is
@@ -1033,7 +1033,7 @@ in all Swift code, but it is feasible for local variables of concrete types.
10331033
An example where it is not possible to maintain precise destruction semantics
10341034
for C++ classes is in a generic Swift function that is called with a C++ class
10351035
as a type parameter. In this case, we must be able to compile one definition of
1036-
a Swift function, which must work regradless of the nature of the type (be it a
1036+
a Swift function, which must work regardless of the nature of the type (be it a
10371037
Swift type or a C++ class). This limitation does not seem too bad, because RAII
10381038
types are not often passed as type parameters to templates in C++, which creates
10391039
a reason to believe it will not be common in Swift either.

docs/Diagnostics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Educational notes should:
100100
- Explain a single language concept. This makes them easy to reuse across related diagnostics and helps keep them clear, concise, and easy to understand.
101101
- Be written in unabbreviated English. These are longer-form messages compared to the main diagnostic, so there's no need to omit needless words and punctuation.
102102
- Not generally exceed 3-4 paragraphs. Educational notes should be clear and easily digestible. Messages which are too long also have the potential to create UX issues on the command line.
103-
- Be accessible. Educational notes should be beginner friendly and avoid assuming unnecesary prior knowledge. The goal is not only to help users understand what a diagnostic is telling them, but also to turn errors and warnings into "teachable moments".
103+
- Be accessible. Educational notes should be beginner friendly and avoid assuming unnecessary prior knowledge. The goal is not only to help users understand what a diagnostic is telling them, but also to turn errors and warnings into "teachable moments".
104104
- Include references to relevant chapters of _The Swift Programming Language_.
105105
- Be written in Markdown, but avoid excessive markup which negatively impacts the terminal UX.
106106

docs/DifferentiableProgramming.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2542,7 +2542,7 @@ As defined above, the `@differentiable` function type attributes requires all
25422542
non-`@noDerivative` arguments and results to conform to the `@differentiable`
25432543
attribute. However, there is one exception: when the type of an argument or
25442544
result is a function type, e.g. `@differentiable (T) -> @differentiable (U) ->
2545-
V`. This is because we need to differentiate higher-order funtions.
2545+
V`. This is because we need to differentiate higher-order functions.
25462546

25472547
Mathematically, the differentiability of `@differentiable (T, U) -> V` is
25482548
similar to that of `@differentiable (T) -> @differentiable (U) -> V` in that

docs/ExternalResources.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,24 @@ https://medium.com/kinandcartacreated/contributing-to-swift-part-2-efebcf7b6c93
135135

136136
## Code generation, runtime and ABI
137137

138-
- The Swift Runtime ([Part 1: Heap Objects][], [Part 2: Type Layout][])
139-
by Jordan Rose (blog post series, Aug-Sep 2020): This blog post series
140-
describes the runtime layout for classes, closures and structs, as well
141-
as basic runtime functionality such as retain/release that needs to be
142-
handled when porting Swift to a new platform, such as [Mac OS 9][].
138+
- [The Swift Runtime][] by Jordan Rose (blog post series, Aug-Oct 2020):
139+
This blog post series describes the runtime layout for different structures,
140+
runtime handling for metadata, as well as basic runtime functionality such
141+
as retain/release that needs to be handled when porting Swift to a new
142+
platform, such as [Mac OS 9][].
143+
- [Part 1: Heap Objects][]
144+
- [Part 2: Type Layout][]
145+
- [Part 3: Type Metadata][]
146+
- [Part 4: Uniquing Caches][]
147+
- [Part 5: Class Metadata][]
148+
- [Part 6: Class Metadata Initialization][]
149+
- [Part 7: Enums][]
150+
- [Implementing the Swift Runtime in Swift][]
151+
by JP Simard and Jesse Squires with Jordan Rose (podcast episode, Oct 2020):
152+
This episode describes Jordan's effort to port Swift to Mac OS 9.
153+
It touches on a wide variety of topics, such as ObjC interop, memory layout
154+
guarantees, performance, library evolution, writing low-level code in Swift,
155+
the workflow of porting Swift to a new platform and the design of Swift.
143156
- [How Swift Achieved Dynamic Linking Where Rust Couldn't][] by Alexis
144157
Beingessner (blog post, Nov 2019): This blog post describes Swift's approach
145158
for compiling polymorphic functions, contrasting it with the strategy used by
@@ -174,9 +187,16 @@ https://medium.com/kinandcartacreated/contributing-to-swift-part-2-efebcf7b6c93
174187
value witness tables, type metadata, abstraction patterns, reabstraction,
175188
reabstraction thunks and protocol witness tables.
176189

190+
[Mac OS 9]: https://belkadan.com/blog/2020/04/Swift-on-Mac-OS-9/
191+
[The Swift Runtime]: https://belkadan.com/blog/tags/swift-runtime/
177192
[Part 1: Heap Objects]: https://belkadan.com/blog/2020/08/Swift-Runtime-Heap-Objects/
178193
[Part 2: Type Layout]: https://belkadan.com/blog/2020/09/Swift-Runtime-Type-Layout/
179-
[Mac OS 9]: https://belkadan.com/blog/2020/04/Swift-on-Mac-OS-9/
194+
[Part 3: Type Metadata]: https://belkadan.com/blog/2020/09/Swift-Runtime-Type-Metadata/
195+
[Part 4: Uniquing Caches]: https://belkadan.com/blog/2020/09/Swift-Runtime-Uniquing-Caches/
196+
[Part 5: Class Metadata]: https://belkadan.com/blog/2020/09/Swift-Runtime-Class-Metadata/
197+
[Part 6: Class Metadata Initialization]: https://belkadan.com/blog/2020/10/Swift-Runtime-Class-Metadata-Initialization/
198+
[Part 7: Enums]: https://belkadan.com/blog/2020/10/Swift-Runtime-Enums/
199+
[Implementing the Swift Runtime in Swift]: https://spec.fm/podcasts/swift-unwrapped/1DMLbJg5
180200
[How Swift Achieved Dynamic Linking Where Rust Couldn't]: https://gankra.github.io/blah/swift-abi/
181201
[arm64e: An ABI for Pointer Authentication]: https://youtu.be/C1nZvpEBfYA
182202
[Exploiting The Swift ABI]: https://youtu.be/0rHG_Pa86oA

docs/SIL.rst

Lines changed: 155 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,7 @@ Owned
17501750
Owned ownership models "move only" values. We require that each such value is
17511751
consumed exactly once along all program paths. The IR verifier will flag values
17521752
that are not consumed along a path as a leak and any double consumes as
1753-
use-after-frees. We model move operations via "forwarding uses" such as casts
1753+
use-after-frees. We model move operations via `forwarding uses`_ such as casts
17541754
and transforming terminators (e.x.: `switch_enum`_, `checked_cast_br`_) that
17551755
transform the input value, consuming it in the process, and producing a new
17561756
transformed owned value as a result.
@@ -1805,7 +1805,7 @@ derived from the ARC object. As an example, consider the following Swift/SIL::
18051805
}
18061806

18071807
Notice how our individual copy (``%1``) threads its way through the IR using
1808-
forwarding of ``@owned`` ownership. These forwarding operations partition the
1808+
`forwarding uses`_ of ``@owned`` ownership. These `forwarding uses`_ partition the
18091809
lifetime of the result of the copy_value into a set of disjoint individual owned
18101810
lifetimes (``%2``, ``%3``, ``%5``).
18111811

@@ -1847,7 +1847,7 @@ optimizations that they can never shrink the lifetime of ``%0`` by moving
18471847
`destroy_value`_ above ``%1``.
18481848

18491849
Values with guaranteed ownership follow a dataflow rule that states that
1850-
non-consuming "forwarding" uses of the guaranteed value are also guaranteed and
1850+
non-consuming `forwarding uses`_ of the guaranteed value are also guaranteed and
18511851
are recursively validated as being in the original values scope. This was a
18521852
choice we made to reduce idempotent scopes in the IR::
18531853

@@ -1912,6 +1912,137 @@ This is a form of ownership that is used to model two different use cases:
19121912
trivial pointer to a class. In that case, since we have no reason to assume
19131913
that the object will remain alive, we need to make a copy of the value.
19141914

1915+
Forwarding Uses
1916+
~~~~~~~~~~~~~~~
1917+
1918+
NOTE: In the following, we assumed that one read the section above, `Value Ownership Kind`_.
1919+
1920+
A subset of SIL instructions define the value ownership kind of their results in
1921+
terms of the value ownership kind of their operands. Such an instruction is
1922+
called a "forwarding instruction" and any use with such a user instruction a
1923+
"forwarding use". This inference generally occurs upon instruction construction
1924+
and as a result:
1925+
1926+
* When manipulating forwarding instructions programatically, one must manually
1927+
update their forwarded ownership since most of the time the ownership is
1928+
stored in the instruction itself. Don't worry though because the SIL verifier
1929+
will catch this error for you if you forget to do so!
1930+
1931+
* Textual SIL does not represent the ownership of forwarding instructions
1932+
explicitly. Instead, the instruction's ownership is inferred normally from the
1933+
parsed operand. Since the SILVerifier runs on Textual SIL after parsing, you
1934+
can feel confident that ownership constraints were inferred correctly.
1935+
1936+
Forwarding has slightly different ownership semantics depending on the value
1937+
ownership kind of the operand on construction and the result's type. We go
1938+
through each below:
1939+
1940+
* Given an ``@owned`` operand, the forwarding instruction is assumed to end the
1941+
lifetime of the operand and produce an ``@owned`` value if non-trivially typed
1942+
and ``@none`` if trivially typed. Example: This is used to represent the
1943+
semantics of casts::
1944+
1945+
sil @unsafelyCastToSubClass : $@convention(thin) (@owned Klass) -> @owned SubKlass {
1946+
bb0(%0 : @owned $Klass): // %0 is defined here.
1947+
1948+
// %0 is consumed here and can no longer be used after this point.
1949+
// %1 is defined here and after this point must be used to access the object
1950+
// passed in via %0.
1951+
%1 = unchecked_ref_cast %0 : $Klass to $SubKlass
1952+
1953+
// Then %1's lifetime ends here and we return the casted argument to our
1954+
// caller as an @owned result.
1955+
return %1 : $SubKlass
1956+
}
1957+
1958+
* Given a ``@guaranteed`` operand, the forwarding instruction is assumed to
1959+
produce ``@guaranteed`` non-trivially typed values and ``@none`` trivially
1960+
typed values. Given the non-trivial case, the instruction is assumed to begin
1961+
a new implicit borrow scope for the incoming value. Since the borrow scope is
1962+
implicit, we validate the uses of the result as if they were uses of the
1963+
operand (recursively). This of course means that one should never see
1964+
end_borrows on any guaranteed forwarded results, the end_borrow is always on
1965+
the instruction that "introduces" the borrowed value. An example of a
1966+
guaranteed forwarding instruction is ``struct_extract``::
1967+
1968+
// In this function, I have a pair of Klasses and I want to grab some state
1969+
// and then call the hand off function for someone else to continue
1970+
// processing the pair.
1971+
sil @accessLHSStateAndHandOff : $@convention(thin) (@owned KlassPair) -> @owned State {
1972+
bb0(%0 : @owned $KlassPair): // %0 is defined here.
1973+
1974+
// Begin the borrow scope for %0. We want to access %1's subfield in a
1975+
// read only way that doesn't involve destructuring and extra copies. So
1976+
// we construct a guaranteed scope here so we can safely use a
1977+
// struct_extract.
1978+
%1 = begin_borrow %0 : $KlassPair
1979+
1980+
// Now we perform our struct_extract operation. This operation
1981+
// structurally grabs a value out of a struct without safety relying on
1982+
// the guaranteed ownership of its operand to know that %1 is live at all
1983+
// use points of %2, its result.
1984+
%2 = struct_extract %1 : $KlassPair, #KlassPair.lhs
1985+
1986+
// Then grab the state from our left hand side klass and copy it so we
1987+
// can pass off our klass pair to handOff for continued processing.
1988+
%3 = ref_element_addr %2 : $Klass, #Klass.state
1989+
%4 = load [copy] %3 : $*State
1990+
1991+
// Now that we have finished accessing %1, we end the borrow scope for %1.
1992+
end_borrow %1 : $KlassPair
1993+
1994+
%handOff = function_ref @handOff : $@convention(thin) (@owned KlassPair) -> ()
1995+
apply %handOff(%0) : $@convention(thin) (@owned KlassPair) -> ()
1996+
1997+
return %4 : $State
1998+
}
1999+
2000+
* Given an ``@none`` operand, the result value must have ``@none`` ownership.
2001+
2002+
* Given an ``@unowned`` operand, the result value will have ``@unowned``
2003+
ownership. It will be validated just like any other ``@unowned`` value, namely
2004+
that it must be copied before use.
2005+
2006+
An additional wrinkle here is that even though the vast majority of forwarding
2007+
instructions forward all types of ownership, this is not true in general. To see
2008+
why this is necessary, lets compare/contrast `struct_extract`_ (which does not
2009+
forward ``@owned`` ownership) and `unchecked_enum_data`_ (which can forward
2010+
/all/ ownership kinds). The reason for this difference is that `struct_extract`_
2011+
inherently can only extract out a single field of a larger object implying that
2012+
the instruction could only represent consuming a sub-field of a value instead of
2013+
the entire value at once. This violates our constraint that owned values can
2014+
never be partially consumed: a value is either completely alive or completely
2015+
dead. In contrast, enums always represent their payloads as elements in a single
2016+
tuple value. This means that `unchecked_enum_data`_ when it extracts that
2017+
payload from an enum, can consume the entire enum+payload.
2018+
2019+
To handle cases where we want to use `struct_extract`_ in a consuming way, we
2020+
instead are able to use the `destructure_struct`_ instruction that consumes the
2021+
entire struct at once and gives one back the structs individual constituant
2022+
parts::
2023+
2024+
struct KlassPair {
2025+
var fieldOne: Klass
2026+
var fieldTwo: Klass
2027+
}
2028+
2029+
sil @getFirstPairElt : $@convention(thin) (@owned KlassPair) -> @owned Klass {
2030+
bb0(%0 : @owned $KlassPair):
2031+
// If we were to just do this directly and consume KlassPair to access
2032+
// fieldOne... what would happen to fieldTwo? Would it be consumed?
2033+
//
2034+
// %1 = struct_extract %0 : $KlassPair, #KlassPair.fieldOne
2035+
//
2036+
// Instead we need to destructure to ensure we consume the entire owned value at once.
2037+
(%1, %2) = destructure_struct $KlassPair
2038+
2039+
// We only want to return %1, so we need to cleanup %2.
2040+
destroy_value %2 : $Klass
2041+
2042+
// Then return %1 to our caller
2043+
return %1 : $Klass
2044+
}
2045+
19152046
Runtime Failure
19162047
---------------
19172048

@@ -2754,6 +2885,27 @@ initialized with the resume value, and that value is then owned by the current
27542885
function. If ``await_async_continuation`` instead resumes to its ``error``
27552886
successor, then the memory remains uninitialized.
27562887

2888+
hop_to_executor
2889+
```````````````
2890+
2891+
::
2892+
2893+
sil-instruction ::= 'hop_to_executor' sil-operand
2894+
2895+
hop_to_executor %0 : $T
2896+
2897+
// $T must conform to the Actor protocol
2898+
2899+
Ensures that all instructions, which need to run on the actor's executor
2900+
actually run on that executor.
2901+
This instruction can only be used inside an ``@async`` function.
2902+
2903+
Checks if the current executor is the one which is bound to the operand actor.
2904+
If not, begins a suspension point and enqueues the continuation to the executor
2905+
which is bound to the operand actor.
2906+
2907+
The operand is a guaranteed operand, i.e. not consumed.
2908+
27572909
dealloc_stack
27582910
`````````````
27592911
::

0 commit comments

Comments
 (0)