Commit Graph

2592 Commits

Author SHA1 Message Date
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
Michael Gottesman
8750b404f3 [opt-remark] Extract out DebugValue inference from a Value's uses into a helper struct to enable code reuse.
I am doing this so I can use the same code with alloc_stack.
2021-03-29 12:10:37 -07:00
Michael Gottesman
360e32bfbd Merge pull request #36622 from gottesmm/pr-f547a5b1d52f9b19efd84b124f089c7df498f07f
[opt-remark-gen] Teach OptRemarkGen how to emit diagnostics about exclusivity.
2021-03-28 18:20:00 -07:00
Michael Gottesman
1227693d29 [opt-remark-gen] Teach OptRemarkGen how to emit diagnostics about exclusivity.
For example, now we get the following diagnostic on globals:

 public func getGlobal() -> Klass {
     return global // expected-remark @:5 {{retain of type 'Klass'}}
                   // expected-note @-5:12 {{of 'global'}}
+                  // expected-remark @-2:12 {{begin exclusive access to value of type 'Klass'}}
+                  // expected-note @-7:12 {{of 'global'}}
+                  // expected-remark @-4 {{end exclusive access to value of type 'Klass'}}
+                  // expected-note @-9:12 {{of 'global'}}
+
 }

and for classes when we can't eliminate the access:

+func simpleInOut() -> Klass {
+    let x = Klass() // expected-remark @:13 {{heap allocated ref of type 'Klass'}}
+                    // expected-note @-1:9 {{of 'x'}}
+    simpleInOutUser(&x.next) // expected-remark @:5 {{begin exclusive access to value of type 'Optional<Klass>'}}
+                             // expected-note @-3:9 {{of 'x.next'}}
+                             // expected-remark @-2:28 {{end exclusive access to value of type 'Optional<Klass>'}}
+                             // expected-note @-5:9 {{of 'x.next'}}
+    return x
+}
2021-03-28 13:39:13 -07:00
John McCall
98711fd628 Revise the continuation ABI.
The immediate desire is to minimize the set of ABI dependencies
on the layout of an ExecutorRef.  In addition to that, however,
I wanted to generally reduce the code size impact of an unsafe
continuation since it now requires accessing thread-local state,
and I wanted resumption to not have to create unnecessary type
metadata for the value type just to do the initialization.

Therefore, I've introduced a swift_continuation_init function
which handles the default initialization of a continuation
and returns a reference to the current task.  I've also moved
the initialization of the normal continuation result into the
caller (out of the runtime), and I've moved the resumption-side
cmpxchg into the runtime (and prior to the task being enqueued).
2021-03-28 12:58:16 -04:00
Erik Eckstein
9b16a94e8d TempRValueOpt: small fixes/improvements for extending access scopes
Don't move an end_access over a (non-aliasing) end_access. This would destroy the proper nesting of accesses.
Also, add some comments, asserts and tests.
2021-03-24 12:51:30 +01:00
Erik Eckstein
7c454e3eed TempRValueOpt: extend access scopes
Try to move an end_access down to extend the access scope over all uses of the temporary.
For example:

   %a = begin_access %src
   copy_addr %a to [initialization] %temp : $*T
   end_access %a
   use %temp

We must not replace %temp with %a after the end_access. Instead we try to move the end_access after "use %temp".

This fixes generation of invalid SIL and/or the invalid removal of access checks.
2021-03-22 21:54:41 +01:00
Doug Gregor
cfa07b60ee Merge pull request #36477 from jckarter/executor-hop-back
Concurrency: Hop back to the previous executor after actor calls.
2021-03-19 10:17:20 -07:00
Joe Groff
79fb05b362 Concurrency: Hop back to the previous executor after actor calls.
Tasks shouldn't normally hog the actor context indefinitely after making a call that's bound to
that actor, since that prevents the actor from potentially taking on other jobs it needs to
be able to address. Set up SILGen so that it saves the current executor (using a new runtime
entry point) and hops back to it after every actor call, not only ones where the caller context
is also actor-bound.

The added executor hopping here also exposed a bug in the runtime implementation while processing
DefaultActor jobs, where if an actor job returned to the processing loop having already yielded
the thread back to a generic executor, we would still attempt to make the actor give up the thread
again, corrupting its state.

rdar://71905765
2021-03-18 11:47:50 -07:00
Andrew Trick
e9d0b08706 Add utilities to support OSSA update after running SSAUpdater.
This directly adds support to BasicBlockCloner for updating OSSA.

It also adds a much more general-purpose GuaranteedPhiBorrowFixup
utility which can be used for more complicated SSA updates, in which
multiple phis need to be created. More generally, it handles adding
nested borrow scopes for guaranteed phis even when that phi is used by
other guaranteed phis.
2021-03-18 00:14:13 -07:00
Erik Eckstein
28a5eee217 SILMem2Reg: don't create an "undef" as a replacement of a load of an empty tuple.
Instead create the empty tuple value.
In general, it's not a good idea to create undef values in SIL.
2021-03-16 11:26:54 +01:00
eeckstein
35cc53ea7e Merge pull request #36384 from eeckstein/memory-lifetime
improvements/fixes in RawSILInstLowering and MemoryLifetimeVerifier
2021-03-14 10:03:27 +01:00
Michael Gottesman
19dd90674d Merge pull request #36167 from gottesmm/pr-761936430320990a32c8dfb2f85d27f171f186ba
[simplify-cfg] Only check if we can remove releases by performing simple jump threading if our block argument is not a trivial type.
2021-03-13 13:00:03 -08:00
Erik Eckstein
b9c8e57d7a MemoryLifetimeVerifier: also verify locations with trivial types.
It helps to catch more problems
2021-03-13 10:41:30 +01:00
Erik Eckstein
1baf009c06 refactoring: Split MemoryLifetime.cpp/h into three separate files
And rename MemoryDataflow -> BitDataflow.

MemoryLifetime contained MemoryLocations, MemoryDataflow and the MemoryLifetimeVerifier.
Three independent things, for which it makes sense to have them in three separated files.

NFC.
2021-03-13 10:41:30 +01:00
Andrew Trick
0b3027a37e Add destroy_value [poison] support to mandatory copy propagation.
Ensure that any object lifetime that will be shortened ends in
destroy_value [poison], indicating that the reference should not be
dereferenced (e.g. by the debugger) beyond this point.

This has no way of knowing whether the object will actually be
deallocated at this point. It conservatively avoids showing garbage to
debuggers.

Part 1/2: rdar://75012368 (-Onone compiler support for early object
deinitialization with sentinel dead references)
2021-03-12 19:32:55 -08:00
Joe Groff
872afda50b Merge pull request #36298 from jckarter/created-task-closure-context-leak
SIL: Clean up ownership handling in `createAsyncTask` builtins.
2021-03-09 14:17:13 -08:00
Joe Groff
d9798c0868 Concurrency: Redo non-_f variants of swift_task_create to accept closures as is.
In their previous form, the non-`_f` variants of these entry points were unused, and IRGen
lowered the `createAsyncTask` builtins to use the `_f` variants with a large amount of caller-side
codegen to manually unpack closure values. Amid all this, it also failed to make anyone responsible
for releasing the closure context after the task completed, causing every task creation to leak.
Redo the `swift_task_create_*` entry points to accept the two words of an async closure value
directly, and unpack the closure to get its invocation entry point and initial context size
inside the runtime. (Also get rid of the non-future `swift_task_create` variant, since it's unused
and it's subtly different in a lot of hairy ways from the future forms. Better to add it later
when it's needed than to have a broken unexercised version now.)
2021-03-08 16:54:19 -08:00
Erik Eckstein
2d73b21b2b StringOptimization: optimize C string literals.
Replace a String initializer followed by String.utf8CString with a (UTF8 encoded) string literal.
This code pattern is generated when calling C functions with string literals, e.g.

  puts("hello!")

rdar://74941849
2021-03-08 08:34:53 +01:00
Erik Eckstein
9f1ccdebe9 MemoryLifetime: support select_enum_addr, existential_metatype, value_metatype, is_unique and fix_lifetime 2021-03-07 09:10:22 +01:00
Erik Eckstein
45f08bd7c4 MemoryLifetime: support init_existential_addr and open_existential_addr
Also, relax the check for enums a bit. Instead of only accepting single-payload enums, just require that the payload type is the same for an enum location.
2021-03-07 09:10:22 +01:00
Erik Eckstein
b73285d20d MemoryLifetime: support unchecked_ref_cast_addr and unconditional_checked_cast_addr instructions 2021-03-07 09:10:22 +01:00
Erik Eckstein
54e0a5403e MemoryLifetime: support checked_cast_addr_br 2021-03-07 09:10:22 +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
Slava Pestov
7ccc41a7b7 SIL: Preliminary support for 'apply [noasync]' calls
Refactor SILGen's ApplyOptions into an OptionSet, add a
DoesNotAwait flag to go with DoesNotThrow, and sink it
all down into SILInstruction.h.

Then, replace the isNonThrowing() flag in ApplyInst and
BeginApplyInst with getApplyOptions(), and plumb it
through to TryApplyInst as well.

Set the flag when SILGen emits a sync call to a reasync
function.

When set, this disables the SIL verifier check against
calling async functions from sync functions.

Finally, this allows us to add end-to-end tests for
rdar://problem/71098795.
2021-03-04 22:41:46 -05:00
Meghana Gupta
eb1c656d0c Remove unnecessary #ifndef in PhiArgumentOptimizations.cpp 2021-03-04 13:24:29 -08:00
Meghana Gupta
1f89d9ff89 Verify critical edges when -sil-verify-all is enabled 2021-03-03 23:45:56 -08:00
Andrew Trick
291467c8a5 Merge branch 'main' into mandatory-copyprop 2021-03-03 05:53:51 -08:00
Andrew Trick
b41e268a7e CopyPropagation: respect -Xfrontend -disable-sil-ownership-verifier 2021-03-02 22:20:13 -08:00
Andrew Trick
a77ced8423 Add frontend flags for developers to easily control copy propagation:
-enable-copy-propagation: enables whatever form of copy propagation
 the current pipeline runs (mandatory-copy-propagation at -Onone,
 regular copy-propation at -O).

-disable-copy-propagation: similarly disables any form of copy
 propagation in the current pipelien.
2021-03-02 22:20:13 -08:00
Andrew Trick
b689b1dabe Rename GuaranteedARCOpts to MandatoryARCOpts.
This bleeds into the implementation where "guaranteed" is used
everywhere to talk about optimization of guaranteed values. We need to
use mandatory to indicate we're talking about the pass pipeline.
2021-03-02 22:20:13 -08:00
Andrew Trick
cb1ed89c9a [NFC] Add support for a mandatory-copy-propagation pass.
It is currently disabled so this commit is NFC.

MandatoryCopyPropagation canonicalizes all all OSSA lifetimes with
either CopyValue or DestroyValue operations. While regular
CopyPropagation only canonicalizes lifetimes with copies. This ensures
that more lifetime program bugs are found in debug builds. Eventually,
regular CopyPropagation will also canonicalize all lifetimes, but for
now, we don't want to expose optimized code to more behavior change
than necessary.

Add frontend flags for developers to easily control copy propagation:

-enable-copy-propagation: enables whatever form of copy propagation
 the current pipeline runs (mandatory-copy-propagation at -Onone,
 regular copy-propation at -O).

-disable-copy-propagation: similarly disables any form of copy
 propagation in the current pipelien.

To control a specific variant of the passes, use
   -Xllvm -disable-pass=mandatory-copy-propagation
or -Xllvm -disable-pass=copy-propagation instead.

The meaning of these flags will stay the same as we adjust the
defaults. Soon mandatory-copy-propagation will be enabled by
default. There are two reasons to do this, both related to predictable
behavior across Debug and Release builds.

1. Shortening object lifetimes can cause observable changes in program
   behavior in the presense of weak/unowned reference and
   deinitializer side effects.

2. Programmers need to know reliably whether a given code pattern will
   copy the storage for copy-on-write types (Array, Set). Eliminating
   the "unexpected" copies the same way at -Onone and -O both makes
   debugging tractable and provides assurance that the code isn't
   relying on the luck of the optimizer in a particular compiler
   release.
2021-03-02 22:19:47 -08:00
Erik Eckstein
aa16864a2c SIL: verify store_borrow
Verify that
* the destination address is an alloc_stack
* the stack location is not modified beside a store_borrow
* the stack location has been initialized when used
2021-03-02 12:02:54 +01:00
Erik Eckstein
d6736e93e0 MemoryLifetime: support partial_apply arguments in memory lifetime verification. 2021-03-02 12:02:54 +01:00
Andrew Trick
ecc7127be8 Merge pull request #36183 from atrick/fix-dce-fixlifetime
Disable fix_lifetime DCE.
2021-02-26 14:37:31 -08:00
Andrew Trick
f470b29747 Disable fix_lifetime DCE.
Unless the type is a trivial scalar. As a follow-up we could try to
prove that the referenced object is unique, but that seems more
natural for DeadObjectElimination.

This optimization was never correct because references may alias. See
rdar://36038096 ([SR-6608] DeadCodeElimination removes fix_lifetime
instructions.)

Fixes rdar://74759728 (The compiler/optimizer seems to be shortening
the lifetime too early)
2021-02-26 11:06:10 -08:00
swift-ci
08638f23b5 Merge pull request #36182 from eeckstein/fix-destroyhoisting-debuginfo 2021-02-26 10:24:21 -08:00
Doug Gregor
3dec40cc05 Merge pull request #35874 from ktoso/wip-no-escape-group
[Concurrency] Reimplement Task.Group with accordance to Pitch #3
2021-02-26 08:57:26 -08:00
Erik Eckstein
9262c23b63 DestroyHoisting: create the correct debug info location kind for inserted destroys
Such locations should be cleanup locations and not regular locations.
Otherwise it could trigger a false unreachable-code warning.

rdar://74241814
2021-02-26 16:39:31 +01:00
Meghana Gupta
5e16e75e4f Merge pull request #36056 from meg-gupta/rlefixes
Fix ownershipKind of the phi's that the SILSSAUpdater creates for RLE
2021-02-25 19:13:51 -08:00
Michael Gottesman
34e4e42642 [simplify-cfg] Only check if we can remove releases by performing simple jump threading if our block argument is not a trivial type.
Eliminates unnecessary work and reduces compile time.
2021-02-25 12:21:49 -08:00
Meghana Gupta
7445c5d7bb Fix ownershipKind of the phi's that the SILSSAUpdater creates for RLE
When the available values have none ownership on one side and owned on the other,
we need to merge their ownershipKind for their phi argument
2021-02-25 11:48:00 -08:00
Konrad `ktoso` Malawski
6e525d7469 Merge branch 'main' into wip-no-escape-group 2021-02-25 10:37:20 +09: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
Konrad `ktoso` Malawski
6f4fca8721 Merge branch 'main' into wip-no-escape-group 2021-02-24 08:59:53 +09: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
Konrad `ktoso` Malawski
a226259d84 [Concurrency] TaskGroup moves out of AsyncTask, non escaping body 2021-02-22 13:26:27 +09:00
Meghana Gupta
dd1c37ae91 CSE OSSA: Enable lookThroughOwnershipInsts for some more instructions 2021-02-15 13:43:58 -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