Commit Graph

6319 Commits

Author SHA1 Message Date
Michael Gottesman
ad8e624bc9 Merge pull request #36141 from gottesmm/pr-fa4d423549d67732a05efc03383b0bcc8f15bd99
[sil-combine] Allow for storage casts to be cloned when rebasing an address def-use chain onto a new interior pointer
2021-02-24 16:03:52 -08:00
Michael Gottesman
4d593f44a9 Merge pull request #36092 from gottesmm/pr-ee0a3941f66885b3a8a02d63f8b7a4efa23a63bf
[silgen-cleanup] Ensure that we move nextII past /all/ instructions we are going to delete no matter order of visitation.
2021-02-24 14:02:22 -08:00
swift-ci
17e9dd1938 Merge pull request #36137 from gottesmm/pr-2eaba99e19e769a2e1c5b07980fd6cb5ec33aeff 2021-02-24 13:42:51 -08:00
Andrew Trick
01e11c5059 Merge pull request #36118 from atrick/simplifycfg-trivial
OSSA: simplify-cfg support for trivial block arguments.
2021-02-24 13:35:13 -08:00
Michael Gottesman
658ce47636 [sil-combine] Allow for storage casts to be cloned when rebasing an address def-use chain onto a new interior pointer 2021-02-24 13:19:39 -08:00
Michael Gottesman
248292b9da [cowarrayopt] Instead of always recomputing the loop we are analyzings exiting blocks when checking for dominance, use a cache.
Saves a bunch of compile time when compiling the stdlib. Specifically, when
compiling the iOS 32 bit stdlib + sil-verify-all, this shaves off ~20% of the
compile time.
2021-02-24 11:13:52 -08:00
Slava Pestov
60ae2262b0 Merge pull request #36101 from slavapestov/conformance-access-path-cleanup
Re-implement GenericSignature::getConformanceAccessPath()
2021-02-24 12:40:43 -05:00
Andrew Trick
ba9f52071b OSSA: simplify-cfg support for trivial block arguments.
Enable most simplify-cfg optimizations as long as the block arguments
have trivial types. Enable most simplify CFG unit tests cases.

This massively reduces the size of the CFG during OSSA passes.

Test cases that aren't supported in OSSA yet have been moved to a
separate test file for disabled OSSA tests,

Full simplify-cfg support is currently blocked on OSSA utilities which
I haven't checked in yet.
2021-02-23 22:47:59 -08:00
Andrew Trick
b908b9fe04 EagerSpecializer: only run SIL verification when needed
This was running the SIL verifier in the asserts build unconditionally
on every function even if this pass did nothing.
2021-02-23 22:47:59 -08:00
Andrew Trick
0c09d42447 Comment OSSA APIs makeNewValueAvailable & endLifetimeAtLeakingBlocks 2021-02-23 16:54:37 -08:00
Konrad `ktoso` Malawski
6f4fca8721 Merge branch 'main' into wip-no-escape-group 2021-02-24 08:59:53 +09:00
Michael Gottesman
6189e8e970 Merge pull request #36031 from gottesmm/pr-3c9f3fdecad83d37bd4c97bc5116da95c64279e8
[ownership] Make whether or not we serialize in OSSA form based off of a flag instead of off of whether or not we are processing the stdlib.
2021-02-23 10:42:37 -08: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
Slava Pestov
d492a53bf1 GSB: Remove unused SourceLoc parameter from computeGenericSignature() 2021-02-22 23:06:27 -05:00
Michael Gottesman
91317c723c [ownership] Make whether or not we serialize in OSSA form based off of the flag -enable-ossa-modules.
I added a change to CMake so that the stdlib still gets the option passed in so
should be NFC.
2021-02-22 19:07:45 -08:00
Michael Gottesman
d3aee16db3 [silgen-cleanup] Ensure that we move nextII past /all/ instructions we are going to delete no matter order of visitation.
Specifically, given code like the following:

```
(%1, %2) = multi_result_inst %0   (a)
inst_to_delete %1                 (b)
inst_to_delete %2                 (c)
```

We would sometimes visit (c) before (b). So if nextII was (b) at that point, we
would think that we were safe. Then we process (b), move nextII from (b) ->
(c). Then we delete both (b) and (c). =><=.

The solution to this is each iteration of the delete loop, we track /all/
instructions that eliminateDeadInstruction is going to eliminate and ensure that
after each callback is called we have moved past all visited instructions (even
ones we visited previously). This is done using a small set that I clear each
iteration.

I also re-enabled the dictionary test that exposed this issue when testing
-O/-Osize.

rdar://71933996
2021-02-22 13:33:56 -08:00
Konrad `ktoso` Malawski
a226259d84 [Concurrency] TaskGroup moves out of AsyncTask, non escaping body 2021-02-22 13:26:27 +09:00
swift-ci
a3671e6b68 Merge pull request #36071 from rxwei/pullback-debuginfo 2021-02-21 19:53:38 -08:00
Andrew Trick
81c0d09311 Merge pull request #36046 from atrick/fix-escape
Fix EscapeAnalysis::mayReleaseContent
2021-02-21 18:36:18 -08:00
Richard Wei
e3b480b0c9 [AutoDiff] Enable variable debugging support for pullback functions.
- Properly clone and use debug scopes for all instructions in pullback functions.
- Emit `debug_value` instructions for adjoint values.
- Add debug locations and variable info to adjoint buffer allocations.
- Add `TangentBuilder` (a `SILBuilder` subclass) to unify and simplify special emitter utilities for tangent vector code generation. More simplifications to come.

Pullback variable inspection example:
```console
(lldb) n
Process 50984 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
    frame #0: 0x0000000100003497 main`pullback of foo(x=0) at main.swift:12:11
   9   	import _Differentiation
   10
   11  	func foo(_ x: Float) -> Float {
-> 12  	  let y = sin(x)
   13  	  let z = cos(y)
   14  	  let k = tanh(z) + cos(y)
   15  	  return k
Target 0: (main) stopped.
(lldb) fr v
(Float) x = 0
(Float) k = 1
(Float) z = 0.495846391
(Float) y = -0.689988375
```

Resolves rdar://68616528 / SR-13535.
2021-02-21 19:00:07 -05:00
Andrew Trick
f6624e355a Add EscapeAnalysis::findObjectKind to mitigate deinit side effects
Deinitializer side effects severely handicap the connection-graph
based EscapeAnalysis. Because it's not flow-sensitive, this approach
falls apart whenever an object is released in the current function,
which makes it seem to have escaped everywhere (it generally doesn't
matter if it doesn't escape until the release point, but the analysis
can't discern that).

This can be slightly mitigated by realizing that releasing an object
can only cause things it points to to escape if the object itself has
references or pointers.

Fix: Don't create a escaping content node when releasing an object
that can't contain any references or pointers.  The previous commit,
"Fix EscapeAnalysis::mayReleaseContent" would defeat release-hoisting
in important cases without doing anything else. Adding this extra
precision to the connection graph avoids some regressions.
2021-02-19 12:35:11 -08:00
Andrew Trick
23696e4dff Fix EscapeAnalysis::mayReleaseContent
The previous implementation made extremely subtle and specific
assumptions about how the API is used which doesn't apply
everywhere. It was trying very hard to avoid regressing performance
relative to an even older implementation that didn't even try to consider deinitializer side effects.

The aggressive logic was based on the idea that a release must have a
corresponding retain somewhere in the same function and we don't care
if the last release happens early if there are no more aliasing
uses. All the unit tests I wrote previously were based on release
hoisting, which happens to work given the way the API is used.

But this logic is incorrect for retain sinking. In that case sinking
past an "unrelated" release could cause the object to be freed
early. See test/SILOptimizer/arc_crash.swift.

With SemanticARC and other SIL improvements being made, I'm afraid
bugs like this will begin to surface.

To fix it, just remove the subtle logic to leave behind a simple and
sound EscapeAnalysis API. To do better, we will need to rewrite the
AliasAnalysis logic for release side effects, which is currently
a tangled web. In the meantime, SemanticARC can handle many cases without EscapeAnalysis.

Fixes rdar://74469299 (ARC miscompile:
EscapeAnalysis::mayReleaseContent; potential use-after-free)

While fixing this, add support for address-type queries too:

Fixes rdar://74360041 (Assertion failed:
(!releasedReference->getType().isAddress() && "an address is never a
reference"), function mayReleaseContent
2021-02-18 19:00:22 -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
Meghana Gupta
222a988f83 Merge pull request #35979 from meg-gupta/csefixes
CSE OSSA: Enable lookThroughOwnershipInsts for some more instructions
2021-02-15 19:19:56 -08:00
Michael Gottesman
8b563d24e3 Merge pull request #35790 from gottesmm/pr-1fe4c56db40fbe22618e6ca72f441f2e2059152d
[capture-promotion] Eliminate non-OSSA support/tests.
2021-02-15 16:26:11 -08:00
Michael Gottesman
12164c9d1b Merge pull request #35974 from gottesmm/interior_pointer_improvements
[ownership] Small code improvements around interior pointers
2021-02-15 16:25:27 -08:00
Meghana Gupta
dd1c37ae91 CSE OSSA: Enable lookThroughOwnershipInsts for some more instructions 2021-02-15 13:43:58 -08:00
eeckstein
841e4ee975 Merge pull request #35918 from eeckstein/diagnose-lifetime-issues
SILOptimizer: add a diagnostics pass to warn about lifetime issues with weak references.
2021-02-15 18:11:19 +01:00
Erik Eckstein
a17f8c2f3f SILOptimizer: add a diagnostics pass to warn about lifetime issues with weak references.
The DiagnoseLifetimeIssuesPass pass prints a warning if an object is stored to a weak property (or is weakly captured) and destroyed before the property (or captured reference) is ever used again.
This can happen if the programmer relies on the lexical scope to keep an object alive, but copy-propagation can shrink the object's lifetime to its last use.
For example:

  func test() {
    let k = Klass()
      // k is deallocated immediately after the closure capture (a store_weak).
      functionWithClosure({ [weak k] in
                            // crash!
                            k!.foo()
                          })
    }

Unfortunately this pass can only catch simple cases, but it's better than nothing.

rdar://73910632
2021-02-15 11:11:35 +01:00
Richard Wei
75088cde3e [AutoDiff] Mangle derivative vtable thunks.
Add the following mangling rule:
```
global ::= global generic-signature? 'TJV' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // autodiff derivative vtable thunk
```

Resolves rdar://74340331.
2021-02-15 00:11:50 -08:00
Michael Gottesman
2c9f9bd908 [ownership] When lowering store_borrow, RAUW its result with its input dest.
A store_borrow is a manner to temporarily "borrow" a guaranteed value into
memory for purposes like reabstraction. To make this safer in OSSA, we treat a
store_borrow's result as an interior pointer into the stored guaranteed value,
causing all uses of that result to be validated as being within the lifetime of
the guaranteed value.

NOTE: This is not the complete store_borrow verification story. We also will
verify that the memory that is being store_borrowed into is not written to while
the store_borrow's result is live.
2021-02-14 21:41:29 -08:00
Michael Gottesman
e33ce4285e [capture-promotion] Eliminate non-OSSA support/tests.
This only runs on OSSA now, so I am deleting the non-OSSA tests and put in an
early bail out since there are some tests that have not been updated yet for
OSSA, that run the diagnostic passes, but for which this pass never kicked in
on.

NOTE: I also while updating the tests noticed a bunch were assuming load_borrow
like semantics. So I updated them with that in mind and implemented support in
the pass for that.
2021-02-14 20:45:36 -08:00
swift-ci
d33c79edaf Merge pull request #35847 from rxwei/72666310-mangle-ad-thunks 2021-02-13 05:07:32 -08:00
Richard Wei
f9ddecf459 [AutoDiff] Mangle linear map self-reordering thunks and subset parameters thunks.
Add the following new mangling rules.
```
global ::= from-type to-type 'TJO' AUTODIFF-FUNCTION-KIND // autodiff self-reordering reabstraction thunk
global ::= from-type 'TJS' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' INDEX-SUBSET 'P' // autodiff linear map subset parameters thunk
global ::= global to-type 'TJS' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' INDEX-SUBSET 'P' // autodiff derivative function subset parameters thunk
```

Example:
```console
$s13TangentVector16_Differentiation14DifferentiablePQzAaDQy_SdAFIegnnnr_TJSdSSSpSrSUSP ---> autodiff subset parameters thunk for differential from @escaping @callee_guaranteed (@in_guaranteed A._Differentiation.Differentiable.TangentVector, @in_guaranteed B._Differentiation.Differentiable.TangentVector, @in_guaranteed Swift.Double) -> (@out B._Differentiation.Differentiable.TangentVector) with respect to parameters {0, 1, 2} and results {0} to parameters {0, 2}
$sS2f8mangling3FooV13TangentVectorVIegydd_SfAESfIegydd_TJOp ---> autodiff self-reordering reabstraction thunk for pullback from @escaping @callee_guaranteed (@unowned Swift.Float) -> (@unowned Swift.Float, @unowned mangling.Foo.TangentVector) to @escaping @callee_guaranteed (@unowned Swift.Float) -> (@unowned mangling.Foo.TangentVector, @unowned Swift.Float)
```

Resolves rdar://72666310 / SR-13508.

Also fix a bug in `AutoDiffFunction` mangling where the original may be a global that contains more than 1 node (rdar://74151229 / SR-14106).
2021-02-13 01:38:35 -08:00
Michael Gottesman
dd6439d31e [ownership] Change the stdlib to serialize code in ossa form on Darwin.
There is some sort of ASAN issue that this exposes on Linux, so I am going to do
this on Darwin and then debug the Linux issue using ASAN over the weekend/next
week.
2021-02-12 23:20:17 -08:00
Meghana Gupta
ff3c7ba4a9 [sil] Add another run of ARCSequenceOpts before inlining in function passes.
This eliminates some regressions by eliminating phase ordering in between
ARCSequenceOpts/inlining with read only functions whose read onlyness is lost
after inlining.
2021-02-12 23:20:17 -08:00
Michael Gottesman
f73276259e [loop-rotate] In OSSA, instead of creating address phis, sneak the address through the phi using a RawPointer.
In OSSA, we do not allow for address phis, but in certain cases the logic of
LoopRotate really wants them. To work around this issue, I added some code in
this PR to loop rotate that as a post-pass fixes up any address phis by
inserting address <-> raw pointer adapters and changing the address phi to
instead be of raw pointer type.
2021-02-12 23:20:17 -08:00
Michael Gottesman
142c3bd1fc [simplify-cfg] Enable some simple opts during ownerships on br, cond_br that do not involve objects directly.
Just to reduce the size of the CFG.
2021-02-12 23:20:17 -08:00
Michael Gottesman
6c255734ba [simplify-cfg] Enable remove unreachable blocks to shrink the CFG a bit. 2021-02-12 23:20:17 -08:00
Michael Gottesman
2827d72029 [pred-dead-alloc] Be more conservative and bail if we did not find a complete available value when cleaning up takes. 2021-02-12 23:20:17 -08:00
Michael Gottesman
705373c205 [pred-deadalloc-elim] Fix memory safety issue and handle promoting paths where there is dynamically no value by inserting compensating destroys.
This commit is fixing two things:

1. In certain cases, we are seeing cases where either SILGen or the optimizer
are eliminating destroy_addr along paths where we know that an enum is
dynamically trivial. This can not be expressed in OSSA, so I added code to
pred-deadalloc-elim so that I check if any of our available values after we
finish promoting away an allocation now need to have their consuming use set
completed.

2. That led me to discover that in certain cases load [take] that we were
promoting were available values of other load [take]. This means that we have a
memory safety issue if we promote one load before the other. Consider the
following SIL:

```
%mem = alloc_stack
store %arg to [init] %mem
%0 = load [take] %mem
store %0 to [init] %mem
%1 = load [take] %mem
destroy_value %1
dealloc_stack %mem
```

In this case, if we eliminate %0 before we eliminate %1, we will have a stale
pointer to %0.

I also took this as an opportunity to turn off predictable mem access opt on SIL
that was deserialized canonicalized and non-OSSA SIL. We evidently need to still
do this for pred mem opts for perf reasons (not sure why). But I am pretty sure
this isn't needed and allows me to avoid some nasty code.
2021-02-12 23:20:17 -08:00
Michael Gottesman
428ab472ac [sil-combine] Turn off try_apply convert_function elimination on both ossa/non-ossa SIL.
In SILCombine, we do not want to add or delete edges. We are ok with swapping
edges or replacing edges when the CFG structure is preserved. This becomes an
issue since by performing this optimization, we are going to get rid of the
error parameter but leave a try_apply, breaking SIL invariants. So to do perform
this optimization, we would need to convert to an apply and eliminate the error
edge, breaking the aforementioned SILCombine invariant. So, just do not perform
this for now and leave it to other passes like SimplifyCFG.
2021-02-12 23:18:29 -08:00
eeckstein
d6a7e9b532 Merge pull request #35935 from eeckstein/basicblock-worklist
SIL: A utility for processing basic blocks in a worklist.
2021-02-13 07:35:44 +01:00
Erik Eckstein
214b7a9929 Use the new BasicBlockWorklist utility in various places in the compiler.
It's a refactoring which simplifies the code.
NFC.
2021-02-12 11:15:55 +01:00
Erik Eckstein
fe10f98cf0 SIL: rename the SILBitfield.h header file to BasicBlockBits.h
NFC
2021-02-12 11:15:55 +01:00
Erik Eckstein
db29d77918 SILCombine: don't create a strong_retain/strong_release with an Optional<reference> as an operand
strong_retain/strong_release with an optional reference as operand are rejected by the verifier and are not supported by IRGen.
SILCombine created such retains/releases when optimizing ref_cast instructions.

This fixes a compiler crash.
rdar://74146617
2021-02-12 10:03:22 +01:00
Meghana Gupta
8807d44211 Remove an llvm_unreachable in ownership rauw
This PR just removes an unnecessary error raised in
OwnershipLifetimeExtender::createPlusOneCopy.
We can ownership rauw a value inside the loop with a value outside the
loop. findJointPostDominatingSet correctly helps create control
equivalent copies inside the loop for replacement.
2021-02-10 15:48:22 -08:00
Meghana Gupta
319e9e0140 Merge pull request #35840 from meg-gupta/fixrpe
Fix redundant phi elimination for OSSA
2021-02-10 13:26:56 -08:00
eeckstein
a2996b7703 Merge pull request #35780 from eeckstein/static-function-arrays
SILOptimizer: allow function pointers in static globals and static global arrays.
2021-02-10 09:02:44 +01:00
Michael Gottesman
ae695b2e88 Merge pull request #35828 from gottesmm/pr-da5b78d526300f0503471e9040d28e518aeb2bea
[ownership] Change store_borrow to be an interior pointer and update interior pointer formalism to use it.
2021-02-09 22:09:48 -08:00