Commit Graph

2592 Commits

Author SHA1 Message Date
Meghana Gupta
17664c0c60 Fix store_borrow multi block handling in mem2reg 2022-08-30 12:48:37 -07:00
Meghana Gupta
e0f8711b23 Fix SILMem2Reg while marking end_borrow of store_borrow as deinit point
Fixes rdar://99229461
2022-08-27 21:01:16 -07:00
Meghana Gupta
9a4a21d7eb Merge pull request #59541 from meg-gupta/mem2regstoreborrowpr
Handle store_borrow in SILMem2Reg and some other fixes
2022-08-26 09:36:34 -07:00
Meghana Gupta
ed7788fff1 Get rid of isStorageValid flag on lastStoreInst 2022-08-25 13:50:08 -07:00
Meghana Gupta
8e9117dda3 Handle store_borrow in SILMem2Reg 2022-08-25 13:25:15 -07:00
Slava Pestov
7a16b0275b AST: Allow one-element tuple types to be constructed
These will never appear in the source language, but can arise
after substitution when the original type is a tuple type with
a pack expansion type.

Two examples:
- original type: (Int, T...), substitution T := {}
- original type: (T...), substitution T := {Int}

We need to model these correctly to maintain invariants.

Callers that previously used to rely on TupleType::get()
returning a ParenType now explicitly check for the one-element
case instead.
2022-08-23 11:12:00 -04:00
Nate Chandler
8d55e09bf3 [SSADestroyHoisting] Lifetimes alter arg hoisting.
Arguments whose lifetimes are not lexical should be hoisted without
respect to deinit barriers.  On the other hand, inout arguments
explicitly annotated @_lexical should respect deinit barriers.
2022-08-22 15:27:59 -07:00
Meghana Gupta
13223f6edf Remove fake use of lexical copy 2022-08-22 15:27:52 -07:00
Meghana Gupta
0c2fd6b419 Remove unnecessary edge case 2022-08-22 15:27:52 -07:00
Meghana Gupta
7f6fd0bcf8 Reorganize if to reduce indentation 2022-08-22 15:27:47 -07:00
Michael Gottesman
3e52007562 [builtin] Remove "unsafeGuaranteed" and related code since Unmanaged now has an Ownership SSA based implementation that works completely in SILGen.
This isn't used in the stdlib anymore as well.
2022-08-21 01:22:36 -07:00
Meghana Gupta
8769cbec07 Simplify SILMem2Reg by using computeDominatedBoundaryBlocks api instead of computing in-place 2022-08-19 09:46:53 -07:00
Nate Chandler
7ed618fb5e [TempRValueOpts] Opt some lexical alloc_stacks.
Previously, whenever an alloc_stack [lexical] was seen, optimization
bailed conservatively.

In the fullness of time, we should optimize an alloc_stack [lexical] so
long as:
(1) the root of the source of the store/copy_addr is lexical
(2) the range in which the alloc_stack [lexical] contains the value
    store'd/copy_addr'd into it (i.e. the range ending at
    destroy_addr/etc) is contained within the live range of that lexical
    root

Here, an incremental step in that direction is taken: if the base of the
accessed storage of the source of a copy_addr is a guaranteed function
argument, then we know
(1) the base is lexical--guaranteed function arguments are always
    lexical
(2) the range in which the alloc_stack contains the value copy_addr'd in
    is trivially contained within the range of that lexical root--the
    live range of a guaranteed function argument is the whole function

Added TODOs for the full optimization.
2022-08-12 11:37:19 -07:00
Nate Chandler
896a464ced [AllocBoxToStack] Lexical borrowees get lexical alloc_stacks.
Previously, whenever an alloc_box that was promoted to an alloc_stack,
the new alloc_stack would be lexical.  The result was that alloc_boxes
which didn't need (or explicitly didn't want, in the case of eager move
vars) received lexical alloc_stacks.

Here, only add the lexical flag to the new alloc_stack instruction if
any of the box's uses are `begin_borrow [lexical]`.  That way,
alloc_boxes end up having lexical alloc_stacks only if they were
"lexical alloc_boxes".
2022-08-12 11:37:19 -07:00
Nate Chandler
80ba5b588b [SSADestroyHoisting] Only lexical alloc_stacks respect deinit barriers. 2022-08-10 19:27:57 -07:00
Ben Barham
6de34f37e5 [NFC] Revert SmallVector<T> -> SmallVector<T, N> fixes
With the change to include `SmallVector.h` directly in `LLVM.h` rather
than forward declaring in the only case it matters (ie. Clang <= 5),
these fixes are no longer needed. Since defaulted version is preferred
when there's no better choice (which is presumably the case if that's
how they were originally added), use it instead. Some uses were instead
changed to add `llvm::` so remove that too.
2022-08-05 21:25:55 -07:00
Michael Gottesman
1e6187c4f4 [sil] Update all usages of old API SILValue::getOwnershipKind() in favor of new ValueBase::getOwnershipKind().
Andy some time ago already created the new API but didn't go through and update
the old occurences. I did that in this PR and then deprecated the old API. The
tree is clean, so I could just remove it, but I decided to be nicer to
downstream people by deprecating it first.
2022-07-26 11:46:23 -07:00
nate-chandler
dcea14716c Merge pull request #41943 from nate-chandler/copy_propagation/canonicalize-all
[CopyPropagation] Canonicalize all values.
2022-07-22 16:54:04 -07:00
Nate Chandler
a9038057b9 [Outliner] Add consuming bridged property pattern.
Previously, for the BridgedProperty pattern, there were two flavors of
outlined function that would be formed, varying on the ownership of the
instance whose field is being access:

(1) guaranteed_in -- for use by addresses
(2) unowned -- for use by values which were not destroyed until some
              time after the pattern of interest is matched

Now that the lifetime of the instance may be shortened to just after the
call to the objc method, the latter of these is not appropriate for
values.  We would have to re-extend the lifetime until after the method
returns.

Instead, here, we add a new variant

(3) owned -- for use by values which were destroyed just after the objc
             method call

Doing so requires new mangling.
2022-07-22 08:17:14 -07:00
Nate Chandler
9b290cdfb6 [Outliner] Defer bridged property mangling.
Put off calculating the outlined function name until the point at which
we will have all the inputs for the name.
2022-07-22 08:17:14 -07:00
Nate Chandler
b527649b15 [NFC] [Outliner] Removed unused fields. 2022-07-22 08:17:14 -07:00
Nate Chandler
50a7080c9c [Outliner] Bridged property expects hoisted destroy.
Thanks to CopyPropagation canonicalizing all values, not just those for
which there is a copy_value instruction, the lifetime of the value that
is loaded in the BridgedProperty pattern is shortened and the
destroy_value appears right after its use rather than after the full CFG
for the bridged property.  Updated the BridgedProperty pattern to match
that newly hoisted instruction.
2022-07-22 08:17:14 -07:00
Nate Chandler
50a0cfa406 [CopyPropagation] Canonicalize all values.
Previously, the lifetimes of only values which were copied were
canonicalized during the non-mandatory CopyPropagation pass.  The
mandatory pass (i.e. MandatoryCopyPropagation) has been disabled,
meaning that non-copied values lifetimes were  never canonicalized.  So
enable canonicalization of all lifetimes during regular CopyPropagation.

rdar://54335055
2022-07-22 08:17:13 -07:00
Nate Chandler
fe9920ef7b [Outliner] Fixed bridged property match in OSSA.
Previously, the match failed to find the larger sequence that began with
load [copy] and ended with destroy_value because the iterator advanced
after finding the load [copy].  Advanced the iterator here.

Enables reverting the test outlining test changes introduced in
a52b8966c6.
2022-07-22 08:17:13 -07:00
Erik Eckstein
dc42c4c17d AllocStackToBox: fix a bug which results in a too early released captured variable
In case of a borrowed `alloc_box`, the optimization didn't look through the `begin_borrow` when calculating the final release of the box.
This resulted in inserting the destroy of the inserted `alloc_stack` too early.

rdar://97087762
2022-07-18 17:13:12 +02:00
Erik Eckstein
4818a4605f AccessEnforcementWMO: notify analysis if functions have changed.
This was missing. It's not a big problem, but should be done correctly.
I noticed this because changes didn't get printed when I used `-sil-print-function`.
2022-07-14 18:42:35 +02:00
Erik Eckstein
777b42180d SILOptimizer: use the new set data structures in various optimizations 2022-07-13 14:27:50 +02:00
nate-chandler
aff4e799d9 Merge pull request #59726 from nate-chandler/freestanding/task-to-thread-model
[Freestanding] Disable unstructured tasks.
2022-07-11 07:40:31 -07:00
Nate Chandler
34c08b8344 [TaskToThread] Add Task.runInline.
The new intrinsic, exposed via static functions on Task<T, Never> and
Task<T, Error> (rethrowing), begins an asynchronous context within a
synchronous caller's context.  This is only available for use under the
task-to-thread concurrency model, and even then only under SPI.
2022-07-08 08:44:18 -07:00
Erik Eckstein
da53141ca2 MandatoryGenericSpecializer: always inline co-routines
Co-routines (called by `begin_apply`) may allocate and therefore it’s important to always inline them.

Also, refactor the code a bit to fix the if-changed-then-return-true logic.

rdar://94833845
2022-07-07 08:34:54 +02:00
Erik Eckstein
9ef9c154f6 MandatoryGenericSpecializer: run optimizations in a loop
To avoid phase ordering problems of the involved optimizations, iterate until we reach a fixed point.
2022-07-07 08:34:53 +02:00
Meghana Gupta
cf6c177117 Fix mem2reg assert to allow phi arguments (#59796)
rdar://96171577
2022-06-30 19:11:44 -07:00
Erik Eckstein
2eda3b3bcb TempRValueOpt: don't move end_access instructions after a terminator instruction.
When extending access scopes, prevent that we end up with an `end_access` after the block's terminator.

Fixes a verifier crash.

rdar://85020372
2022-06-27 19:55:37 +02:00
eeckstein
51bc273541 Merge pull request #59591 from eeckstein/fix-dead-obj-elim
DeadObjectElimination: fix a bug which caused wrong inserted release instructions.
2022-06-22 15:42:11 +02:00
Meghana Gupta
f99eb1df26 Fix possible iterator invalidation in SILMem2Reg 2022-06-21 14:22:26 -07:00
Meghana Gupta
344ef14796 Fix load_borrow replacement in SILMem2Reg 2022-06-21 14:22:20 -07:00
Erik Eckstein
8fd17764e4 DeadObjectElimination: fix a bug which caused wrong inserted release instructions.
This bug triggered a "instruction isn't dominated by its operand" verifier crash.
Or - if the verifier doesn't run - a crash later in IRGen.

rdar://94376582
2022-06-20 20:24:32 +02:00
Meghana Gupta
a0a0ebb558 Enable Mem2Reg for allocs with load_borrow (#59350) 2022-06-15 14:37:10 -07:00
Nate Chandler
8775148f7b [SILOpt] Used SetVector for fast contains check.
IterableBackwardReachability just requires an iterable list of gens.
ShrinkBorrowScope, LexicalDestroyHoisting, and SSADestroyHoisting all
need to be able to check whether a given instruction is scope ending
quickly.  Use a SmallSetVector rather than a SmallVector for the gens in
all three.
2022-05-24 14:15:27 -07:00
Nate Chandler
24979d48e0 [SSADestroyHoisting] Adopt new utilities.
Instead of doing one or two non-iterative BackwardReachability runs,
do a single run of IterativeBackwardReachability.  During that, pause
after discovery/local dataflow and use VisitBarrierAccessScopes to
determine which end_access instructions in the discovered region are
barriers.  Add those instructions as kills to the dataflow.  Finally run
the global dataflow.

Enables SSADestroyHoisting to hoist destroys over loops.

Addresses a correctness issue where access scopes which were open at
barrier blocks were not promoted to barriers, resulting in destroy_addrs
getting hoisted into unrelated access scopes.
2022-05-21 12:56:51 -07:00
Arnold Schwaighofer
104fd6662f GenericSpecialization: Move once initialization of pre-specializations earlier before the first query for pre-specialziations 2022-05-06 13:13:19 -07:00
Arnold Schwaighofer
bcf24e4715 Eager specializer: Fix pre-specialization of imported code
We must no pre-specialize imported code (except if this was explicitly
called for by the importing module).

Therefore, don't pre-specialize `shared` definitions based on their
pre-specialization attributes.

Rather, only pre-specialize if the pre-specialization is called for
using a `target: "theFunctionToSpecialize"` parameter.

Run OnonePrespecializations before serialization so that module native functions
are not yet marked `shared` and can be identified as native.

rdar://92337361
2022-05-04 08:32:41 -07:00
Anton Korobeynikov
df6e2ebbd5 [NFC] Fix use-after-free issues in debug printing inside SIL Mem2Reg (#42183)
Turned printing of addresses of instructions into instructions while there.
2022-05-02 11:35:09 -07:00
Joe Groff
d2fad71328 Merge pull request #42589 from jckarter/begin-partial-apply-simplification-pass
[SIL] Initial work on PartialApplySimplification pass
2022-05-02 10:20:39 -07:00
Erik Eckstein
cad646b283 re-implement the StackPromotion pass in swift
It uses the new EscapeInfo.
2022-05-02 14:22:27 +02:00
Erik Eckstein
653b6ecc33 fix a crash/hang in SimplifyCFG.
Jump threading in an unreachable CFG region can lead to a crash (in an assert compiler) or hang (in an no-assert compiler) in `ValueBase::replaceAllUsesWith`.

Unfortunately I couldn't come up with an isolated SIL test case.

rdar://92267349
2022-04-25 16:48:59 +02:00
Erik Eckstein
b47afec018 RedundantLoadElimination: support bisecting individual optimized loads when debugging this pass. 2022-04-25 09:19:29 +02:00
Joe Groff
1f30a54b48 Sketch out a SILType::isSingleSwiftRefcounted function. 2022-04-21 14:53:39 -07:00
Joe Groff
56c7547fa3 Treat types obviously word-sized in SIL as simple nonescaping partial_apply contexts 2022-04-21 14:53:39 -07:00
Joe Groff
a416f3d2ac If a partial_apply would be simple by simply changing the convention of the callee, change the convention 2022-04-21 12:51:49 -07:00