Commit Graph

575 Commits

Author SHA1 Message Date
Ikko Ashimine
7a7d089933 docs: Fix typo in SIL.rst
implemenation -> implementation
2021-04-15 12:40:57 +09:00
Evan Wilde
014b1fe72c Fix the missing title underbar 2021-04-13 17:54:31 -07:00
Doug Gregor
e77a27e8ed [Concurrency] Introduce runtime detection of data races.
Through various means, it is possible for a synchronous actor-isolated
function to escape to another concurrency domain and be called from
outside the actor. The problem existed previously, but has become far
easier to trigger now that `@escaping` closures and local functions
can be actor-isolated.

Introduce runtime detection of such data races, where a synchronous
actor-isolated function ends up being called from the wrong executor.
Do this by emitting an executor check in actor-isolated synchronous
functions, where we query the executor in thread-local storage and
ensure that it is what we expect. If it isn't, the runtime complains.
The runtime's complaints can be controlled with the environment
variable `SWIFT_UNEXPECTED_EXECUTOR_LOG_LEVEL`:

  0 - disable checking
  1 - warn when a data race is detected
  2 - error and abort when a data race is detected

At an implementation level, this introduces a new concurrency runtime
entry point `_checkExpectedExecutor` that checks the given executor
(on which the function should always have been called) against the
executor on which is called (which is in thread-local storage). There
is a special carve-out here for `@MainActor` code, where we check
against the OS's notion of "main thread" as well, so that `@MainActor`
code can be called via (e.g.) the Dispatch library's
`DispatchQueue.main.async`.

The new SIL instruction `extract_executor` performs the lowering of an
actor down to its executor, which is implicit in the `hop_to_executor`
instruction. Extend the LowerHopToExecutor pass to perform said
lowering.
2021-04-12 15:19:51 -07:00
John McCall
4f6f8b3377 Rewrite hop_to_executor so that it takes a Builtin.Executor in IRGen
The comment in LowerHopToActor explains the design here.
We want SILGen to emit hops to actors, ignoring executors,
because it's easier to fully optimize in a world where deriving
an executor is a non-trivial operation.  But we also want something
prior to IRGen to lower the executor derivation because there are
useful static optimizations we can do, such as doing the derivation
exactly once on a dominance path and strength-reducing the derivation
(e.g. exploiting static knowledge that an actor is a default actor).

There are probably phase-ordering problems with doing this so late,
but hopefully they're restricted to situations like actors that
share an executor.  We'll want to optimize that eventually, but
in the meantime, this unblocks the executor work.
2021-03-30 20:08:41 -04:00
Andrew Trick
6ed58c6380 Add debug_value [poison] flag.
If the '[poison]' flag is set, then all references within this debug
value will be overwritten with a sentinel at this point in the
program. This is used in debug builds when shortening non-trivial
value lifetimes to ensure the debugger cannot inspect invalid
memory. `debug_value` instructions with the poison flag are not
generated until OSSA islowered. They are not expected to be serialized
within the module, and the pipeline is not expected to do any
significant code motion after lowering.
2021-03-12 19:33:23 -08:00
Erik Eckstein
2b96a913aa docs: fix doc build error in SIL.rst 2021-03-07 09:56:28 +01:00
Meghana Gupta
6e0e4dcb88 Merge pull request #36174 from meg-gupta/fixcastoptimizer
Fix verifier error in CastOptimizer
2021-03-05 21:52:27 -08:00
Andrew Trick
73cde15abe Merge pull request #36299 from atrick/poison-destroy
[NFC] Add a poison flag to SIL destroy_value.
2021-03-04 22:55:54 -08:00
Andrew Trick
e0a440c036 Add a poison flag to SIL destroy_value.
When the IRGen side is implemented, this will overwrite shadow debug
variables with a poison sentinel for all references within the value.
2021-03-04 17:26:18 -08:00
Meghana Gupta
e6da389a81 Add documentation for end_lifetime 2021-03-04 13:24:29 -08:00
eeckstein
c4f6fc32c5 Merge pull request #36253 from eeckstein/fix-assign-by-wrapper
SIL: some improvements/fixes around assign_by_wrapper
2021-03-04 20:07:14 +01:00
Meghana Gupta
bb375c398b Merge pull request #36263 from meg-gupta/fixparser
Fix SILParsing for forwarding ownership kind
2021-03-04 08:27:58 -08:00
Meghana Gupta
8f7978b183 Fix SILParsing for forwarding ownership kind 2021-03-03 16:58:23 -08:00
Erik Eckstein
9055e93af9 SIL: some improvements/fixes around assign_by_wrapper
* Refactoring: replace "Destination" and the ownership qualifier by a single "Mode".  This represents much better the mode how the instruction is to be lowered. NFC
* Make assign_by_wrapper printable and parseable.
* Fix lowering of the assign modes for indirect results of the init-closure: The indirect result was initialized and not assigned to. The fix is to insert a destroy_addr before calling the init closure. This fixes a memory lifetime error and/or a memory leak. Found by inspection.
* Fix an iterator-invalidation crash in RawSILInstLowering
* Add tests for lowering assign_by_wrapper.
2021-03-03 18:57:02 +01:00
Erik Eckstein
e064ff32f4 docs: Document the store_borrow instruction 2021-03-02 12:02:54 +01:00
Michael Gottesman
c1a9f6f3eb Merge pull request #36078 from gottesmm/silrst-improvements
[sil.rst] Just added some docs for begin_borrow, updated end_borrow syntax, adding more inline links.
2021-02-24 16:05:03 -08:00
Ben Rimmington
8ae1b768f1 [docs] Update SIL.rst (#36131) 2021-02-24 20:40:31 +00:00
Erik Eckstein
65a6d9a22a MemoryLifetimeVerifier: support verifying enum memory locations.
This is kind of complicated, because an enum can be trivial for one case and not trivial for another case. We need to check at which parts of the function we can prove that the enum does (or could) have a trivial case. In such a branch, it's not required in SIL to destroy the enum location.

Also, document the rules and requirements for enum memory locations in SIL.rst.

rdar://73770085
2021-02-23 08:08:34 +01:00
Michael Gottesman
a78458c987 [sil.rst] Use inline links instead of literals to refer to SILInstructions. 2021-02-21 16:40:40 -08:00
Michael Gottesman
946c1e716e [sil.rst] Add some docs for begin_borrow and update end_borrow to its current syntactic form. 2021-02-21 16:38:31 -08:00
Richard Wei
e494df2ee6 [AutoDiff] Add differentiability kind to differentiability witnesses and mangle them.
Differentiability witnesses are now keyed by the original function name, the differentiability kind, and the autodiff config.

Updated SIL syntax:
```
differentiability-kind ::= 'forward' | 'reverse' | 'normal' | 'linear'
sil-differentiability-witness ::=
    'sil_differentiability_witness'
    sil-linkage?
    '[' differentiability-kind ']'
    '[' 'parameters' sil-differentiability-witness-function-index-list ']'
    '[' 'results' sil-differentiability-witness-function-index-list ']'
    generic-parameter-clause?
    sil-function-name ':' sil-type
    sil-differentiability-witness-body?
sil-instruction ::=
    'differentiability_witness_function'
    '[' sil-differentiability-witness-function-kind ']'
    '[' differentiability-kind ']'
    '[' 'parameters' sil-differentiability-witness-function-index-list ']'
    '[' 'results' sil-differentiability-witness-function-index-list ']'
    generic-parameter-clause?
    sil-function-name ':' sil-type
```
```console
sil_differentiability_witness [reverse] [parameters 0 1] [results 0] <T where T: Differentiable> @foo : <T> $(T) -> T
differentiability_witness_function [vjp] [reverse] [parameters 0] [results 0] <T where T: Differentiable> @foo : $(T) -> T
```

New mangling:
```swift
  global ::= global generic-signature? 'WJ' DIFFERENTIABILITY-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // differentiability witness
```
```console
$s13test_mangling3fooyS2f_S2ftFWJrSpSr ---> reverse differentiability witness for test_mangling.foo(Swift.Float, Swift.Float, Swift.Float) -> Swift.Float with respect to parameters {0} and results {0}
```

Resolves rdar://74380324.
2021-02-17 18:27:42 -05:00
Richard Wei
af8942d940 [AutoDiff] Rename '@differentiable' to '@differentiable(reverse)'.
Compiler:
- Add `Forward` and `Reverse` to `DifferentiabilityKind`.
- Expand `DifferentiabilityMask` in `ExtInfo` to 3 bits so that it now holds all 4 cases of `DifferentiabilityKind`.
- Parse `@differentiable(reverse)` and `@differentiable(_forward)` declaration attributes and type attributes.
- Emit a warning for `@differentiable` without `reverse`.
- Emit an error for `@differentiable(_forward)`.
- Rename `@differentiable(linear)` to `@differentiable(_linear)`.
- Make `@differentiable(reverse)` type lowering go through today's `@differentiable` code path. We will specialize it to reverse-mode in a follow-up patch.

ABI:
- Add `Forward` and `Reverse` to `FunctionMetadataDifferentiabilityKind`.
- Extend `TargetFunctionTypeFlags` by 1 bit to store the highest bit of differentiability kind (linear). Note that there is a 2-bit gap in `DifferentiabilityMask` which is reserved for `AsyncMask` and `ConcurrentMask`; `AsyncMask` is ABI-stable so we cannot change that.

_Differentiation module:
- Replace all occurrences of `@differentiable` with `@differentiable(reverse)`.
- Delete `_transpose(of:)`.

Resolves rdar://69980056.
2021-02-07 14:09:46 -08:00
Slava Pestov
c997016bc6 Update some docs to talk about 'main' instead of 'master'
Fixes https://bugs.swift.org/browse/SR-14015 / rdar://problem/72819056
2021-01-11 22:57:51 -05:00
Michael Gottesman
0171e753d8 Merge pull request #35257 from apple/gottesmm-patch-1-sil.rst
[sil.rst] Fix some formatting
2021-01-05 12:08:10 -08:00
Michael Gottesman
ed2e1f63a2 [sil.rst] Fix another formatting error
Another case where spacing was off so the rendering on GitHub looks bad.
2021-01-04 18:22:07 -08:00
Michael Gottesman
f5e34b3d30 [sil.rst] Fix some formatting
Just a mistake I noticed.
2021-01-04 18:13:08 -08:00
Michael Gottesman
a7422b7aac [sil.rst] Add a discussion of "Dead End Blocks" and its implications upon OSSA.
The main implication being that we allow for values to be leaked along dead end
blocks.

NOTE: For those who are unaware, a dead end block in SIL is a block that is
post-dominated in the same function by program terminating blocks (which we
model using unreachables).
2020-12-29 13:32:17 -08:00
Michael Gottesman
a0679a40a6 [sil.rst] Use consistent white space margin around boxes in ascii figure. 2020-12-19 00:09:49 -08:00
Michael Gottesman
414b82eb89 [sil.rst] Update Ownership SSA section given further feedback/small rst fixes. 2020-12-18 17:48:11 -08:00
Raphael Isemann
60f8efe393 [sil.rst] Fix invalid reStructuredText syntax
* 'Any' isn't defined anywhere, so the references to that are invalid.
* Starting a literal block but not having any leading whitespace in the next line
   causes "Literal block expected; none found."

This breaks the build when sphinx is installed.
2020-12-18 17:48:11 -08:00
Michael Gottesman
c80856c737 [sil.rst] Update OSSA section for the introduction of an explicit "Ownership Kind Lattice". 2020-12-17 11:58:42 -08:00
Andrew Trick
257b7e60b8 Specify the unchecked_ownership_conversion instruction.
All SIL instructions need their semantics specified in SIL.rst and their
constraints specified in SILVerifier.
2020-11-13 16:33:30 -08:00
Michael Gottesman
9712e453b6 [ownership] Add a section to SIL.rst that describes the semantics of safe interior pointers in Ownership SIL.
This is just part of my ongoing effort to document more explicitly Ownership SSA
in SIL.rst.

NOTE: I realized while writing this, we might be able toalso do this form of
verification with owned values. I don't know how easy/hard it would be
though. Its nice to have that owned values do not need to look for these when
optimizing. So that would need to be balanced against expanding this.
2020-11-08 15:19:45 -08:00
Erik Eckstein
f18a9b8ee8 [Concurrency] SIL: add hop_to_executor instruction
This instructions ensures that all instructions, which need to run on the specified executor actually run on that executor.
For details see the description in SIL.rst.
2020-10-28 18:17:43 +01:00
Michael Gottesman
a37e7b95dc Add an Ownership SSA sub-section about forwarding uses. 2020-10-26 18:03:43 -07:00
Joe Groff
e4b6f8bcca More feedback: ownership of bbargs, begin -> get_async_continuation 2020-09-22 15:22:58 -07:00
Joe Groff
e2dfe3a334 Add example of withUnsafeContinuation lowering to SIL 2020-09-21 15:29:28 -07:00
Joe Groff
29587ac766 Revise with feedback from John and Andy 2020-09-21 14:21:47 -07:00
Joe Groff
57215cb17f SIL.rst: Add documentation for async function representation.
Unlike our existing coroutines, async functions run independently within an async coroutine context, and don't
directly yield values back and forth. They therefore mostly behave like normal functions with only an `@async` annotation
to indicate the presence of async suspend points. The `withUnsafeContinuation` primitive requires some instructions
to represent the operation that prepares the continuation to be resumed, which will be represented by
`begin_async_continuation`...`await_async_continuation` regions.
2020-09-18 16:57:07 -07:00
Michael Gottesman
8ea966319b [SIL.rst] Small bit of copy editing. 2020-07-16 13:12:20 -07:00
Erik Eckstein
5e21e46961 docs: add a dummy section for "begin_borrow" in SIL.rst
It fixes a doc build error
2020-07-16 18:51:19 +02:00
Michael Gottesman
8da561692b [docs] Begin adding section describing OSSA.
This just introduces ValueOwnershipKind and the partitioning of value users into
consuming and non-consuming users. I have more to add, but this I think is a
good start.
2020-07-15 15:30:27 -07:00
Michael Gottesman
6f101b88b4 Merge pull request #32830 from gottesmm/pr-3b0bbc9394dcb9de08d4961ea09ff4f7d918e6dd
[SIL.rst] Convert a bunch of bolded instruction names to inline links to the instructions docs.
2020-07-15 12:26:15 -07:00
Michael Gottesman
3c95a19bd4 [SIL.rst] Convert a bunch of bolded instruction names to inline links to the instructions docs.
Seems useful to allow for quick navigation. I did not get all of them, but did a
bunch of them.
2020-07-10 15:01:33 -07:00
Michael Gottesman
5e36ae1c7c [sil] Add a forwarding cast called unchecked_value_cast.
Today unchecked_bitwise_cast returns a value with ObjCUnowned ownership. This is
important to do since the instruction can truncate memory meaning we want to
treat it as a new object that must be copied before use.

This means that in OSSA we do not have a purely ossa forwarding unchecked
layout-compatible assuming cast. This role is filled by unchecked_value_cast.
2020-07-09 21:14:32 -07:00
Erik Eckstein
67605553df SIL: a new instruction 'base_addr_for_offset' for field offset calculations.
The ``base_addr_for_offset`` instruction creates a base address for offset calculations.
The result can be used by address projections, like ``struct_element_addr``, which themselves return the offset of the projected fields.
IR generation simply creates a null pointer for ``base_addr_for_offset``.
2020-07-01 15:10:08 +02:00
freddi(Yuki Aki)
e3259c4ef8 Fixed desc of unconditional_checked_cast_addr 2020-05-17 13:23:58 +09:00
Erik Eckstein
d57ee1316b docs: add a SIL COW support section in SIL.rst 2020-05-14 08:39:54 +02:00
Erik Eckstein
f5a8f600ea SIL: new instructions for copy-on-write support
* a new [immutable] attribute on ref_element_addr and ref_tail_addr
* new instructions: begin_cow_mutation and end_cow_mutation

These new instructions are intended to be used for the stdlib's COW containers, e.g. Array.
They allow more aggressive optimizations, especially for Array.
2020-05-14 08:39:54 +02:00
Dmitri Gribenko
b384bf9ac9 Addressed code review comments 2020-05-08 10:13:57 +02:00