Commit Graph

2777 Commits

Author SHA1 Message Date
Erik Eckstein
98612171bb Devirtualizer: fix de-virtualization of begin_apply
Handle the special case of an guaranteed return value with no uses.
Fixes an assertion crash.

Unfortunately I don't have an isolated test case for this.
But this problem showed up in the `Interpreter/moveonly_read_modify_coroutines.swift` test with OSSA modules and will therefore be tested by this test once we enable OSSA modules.
2025-01-24 20:17:55 +01:00
Erik Eckstein
3ec5d7de24 SIL: replace the is_escaping_closure instruction with destroy_not_escaped_closure
The problem with `is_escaping_closure` was that it didn't consume its operand and therefore reference count checks were unreliable.
For example, copy-propagation could break it.
As this instruction was always used together with an immediately following `destroy_value` of the closure, it makes sense to combine both into a `destroy_not_escaped_closure`.
It
1. checks the reference count and returns true if it is 1
2. consumes and destroys the operand
2025-01-24 19:23:27 +01:00
Michael Gottesman
7ae56aab2e [sil] Add a new instruction ignored_use.
This is used for synthetic uses like _ = x that do not act as a true use but
instead only suppress unused variable warnings. This patch just adds the
instruction.

Eventually, we can use it to move the unused variable warning from Sema to SIL
slimmming the type checker down a little bit... but for now I am using it so
that other diagnostic passes can have a SIL instruction (with SIL location) so
that we can emit diagnostics on code like _ = x. Today we just do not emit
anything at all for that case so a diagnostic SIL pass would not see any
instruction that it could emit a diagnostic upon. In the next patch of this
series, I am going to add SILGen support to do that.
2025-01-22 21:12:36 -08:00
nate-chandler
b59280c0f0 Merge pull request #78624 from nate-chandler/rdar142520491_2
[OSSACanonicalizeOwned] Fix liveness passed to completion.
2025-01-15 19:38:21 -08:00
Nate Chandler
ebf602803e [OSSACanOwned] Fix liveness passed to completion.
To determine where a lifetime ends within dead-end blocks,
OSSACanonicalizeOwned uses OSSACompleteLifetime's
visitAvailabilityBoundary.  This API takes a liveness which it uses to
walk forward to the availability boundary.  Specifically, the liveness
passed from OSSACanonicalizeOwned to OSSACompleteLifetime is a variation
of OSSACanonicalizeOwned's own liveness (it has destroys added).

There is a mismatch in the characteristics of livenesses created by
OSSACanonicalizeOwned and OSSACompleteLifetime:  The former deals with
not only direct uses of a value but also uses of its copies; that
introduces the possibility for consuming uses in the middle of liveness.
The latter on the other hand deals only with uses of a single value
(nestedly, but at each level of nesting only a single value).  Passing a
liveness from the former to the latter without handling this mismatch
is incorrect: OSSACompleteLifetime understands consuming uses to always
end a lifetime, even when they are in the middle of a copy-extended
liveness.  The result is that OSSACompleteLifetime produces non-sensical
results when provided with such a liveness.

To address this, fixup the liveness passed from OSSACanonicalizeOwned to
OSSACompleteLifetime by demoting consuming uses that appear within
(that's to say _not_ on the boundary) of liveness to non-consuming uses.

rdar://142846936
2025-01-15 11:57:13 -08:00
Meghana Gupta
15733446ed Merge pull request #78581 from meg-gupta/outlinerfix
Outliner: Do not outline if the BridgedArg value is not available at the BridgedCall
2025-01-13 04:15:20 -08:00
nate-chandler
50c4861a04 Merge pull request #78552 from nate-chandler/rdar142520491
[OSSACanonicalizeGuaranteed] Don't rewrite consuming uses of move-only values.
2025-01-10 17:21:32 -08:00
Meghana Gupta
2c256180ce Add new utility swift::areUsesWithinValueLifetime 2025-01-10 16:15:50 -08:00
Nate Chandler
6dd45a70f9 [OSSACanonicalizeGuaranteed] Don't copy MO values.
When visiting a consuming use of a move-only value (which can be
produced by a forwarding operation), the inner rewriter must bail out.
Otherwise, it would produce a copy of that move-only value.

rdar://142520491
2025-01-10 10:14:21 -08:00
Nate Chandler
3b4803028d [NFC] OSSACanonicalizeGuaranteed: Args can bail.
Allow rewriting of arguments to bail out.  This is necessary because not
all forwarding instructions allow rewriting of forward(copy) as
copy(forward) (e.g. when forward produces a move-only value).
2025-01-10 10:14:21 -08:00
Nate Chandler
8803b612ac [NFC] OSSACanonicalizeGuaranteed: Flip condition.
Replace large nested condition with early exit.
2025-01-10 10:14:20 -08:00
Nate Chandler
0f6283c5dd [Gardening] OSSACanGuaranteed: Tweak comment. 2025-01-10 10:14:20 -08:00
Michael Gottesman
c061ee72ec [rbi] Ensure that we infer isolation correctly for direct sending results.
I did the correct thing for indirect parameters, but did not do the correct
thing for direct parameters. This is now fixed with some tests to boot.

rdar://141631655
2025-01-07 14:33:39 -08:00
Michael Gottesman
b74be1433a [concurrency] Fix a small bug in RBI that caused us to not recognize that an @MainActor SILIsolationInfo is from an @MainActor. 2025-01-02 13:18:55 -08:00
Erik Eckstein
48b913af4b Optimizer: make the hasOwnershipOperandsOrResults utility available in OwnershipOptUtils 2025-01-02 10:42:01 +01:00
Meghana Gupta
ed2f43e67d Merge pull request #78354 from meg-gupta/enablesilcombine
Enable silcombine to propagate concrete type of existentials in ossa
2025-01-02 00:01:13 -08:00
Meghana Gupta
5e1f9a37ef Enable silcombine to propagate concrete type of existentials in ossa 2024-12-23 08:34:13 -08:00
Erik Eckstein
e6f161c948 GenericSpecializer: fix an ownership verification failure when removing a partial_apply of a thunk
rdar://141490629
2024-12-23 09:45:21 +01:00
Nate Chandler
62f2ffd794 [NFC] ReachableBlocks: Use StackList.
Replace a SmallVector.
2024-12-18 11:25:42 -08:00
Nate Chandler
631fc4c39e [NFC] ReachableBlocks: Rename isReachable method.
It was previously `isVisited`.  And mark `visit` private, it needn't be
part of the interface for any current clients.
2024-12-18 11:25:42 -08:00
Nate Chandler
f8d782ed42 [NFC] ReachableBlocks: Extract compute method.
In preparation for adding more users.
2024-12-18 11:25:42 -08:00
Erik Eckstein
6990a195a3 Optimizer: rename GuaranteedPhiUpdater -> PhiUpdater
Because it now has the replacePhisWithIncomingValues utility, which works for all kind of phis.
2024-12-12 09:09:11 +01:00
Erik Eckstein
09a5a4487a Optimizer: add a utility to replaces phis with the unique incoming values if all incoming values are the same
This is needed after running the SSAUpdater for an existing OSSA value, because the updater can
insert unnecessary phis in the middle of the original liverange which breaks up the original
liverange into smaller ones:

```
   %1 = def_of_owned_value
   %2 = begin_borrow %1
   ...
   br bb2(%1)
 bb2(%3 : @owned $T): // inserted by SSAUpdater
   ...
   end_borrow %2      // use after end-of-lifetime!
   destroy_value %3
```

It's not needed to run this utility if SSAUpdater is used to create a _new_ OSSA liverange.
2024-12-12 08:57:57 +01:00
Erik Eckstein
301ab4e112 LoopRotate: remove the replace-arg-with-struct peephole optimization
This does not belong to loop-rotate and does not work with OSSA.
This peephole is covered by other optimizations.
2024-12-12 08:35:48 +01:00
Shubham Sandeep Rastogi
5fb3578d70 Merge pull request #77951 from rastogishubham/LostVarsFalse
Add false positive detection to sil lost variables
2024-12-10 19:23:05 -08:00
Erik Eckstein
710c4b1be0 SSAUpdater: fix an ownership violation
It can happen that the SSAUpdater inserts a phi-argument with all incoming values being the same.
If a value is requested in the phi-block we must not use the unique incoming value, but we have to re-use the phi argument, because the lifetime of the incoming values end at in the predecessor blocks.

rdar://129859331
2024-12-10 16:08:40 +01:00
nate-chandler
5637eaf3ca Merge pull request #77968 from nate-chandler/rdar139842132
[OSSACanonicalizeOwned] Record traversed defs and don't traverse copies of guaranteed values.
2024-12-06 07:00:20 -08:00
Shubham Sandeep Rastogi
86e87c2a45 Add false positive detection to sil lost variables
This patch adds false positive detection to sil-stats-lost-variables.
We will now only detect a debug_value as lost if there is a real
instruction which belongs to the same scope or a child scope of the
scope of the debug_value and if they are both inline at the same
location.

//a
2024-12-05 11:52:40 -08:00
Nate Chandler
33135a192f [OSSACanOwned] Don't walk into reborrow copies.
Because discovery of defs walks into reborrows and borrowed-from
instructions, copies may be seen whose underlying value is a guaranteed
value (namely, a reborrow or a borrowed-from instruction). Such copies
may be used beyond the lifetime end of such guaranteed values, so it's
not allowed to sink copies to their consuming uses. Such
canonicalization is the responsibility of the OSSACanonicalizeGuaranteed
utility.

rdar://139842132
2024-12-05 11:25:06 -08:00
Nate Chandler
ebd47790fc [OSSACanonicalizeOwned] Only discover defs once.
The utility performs two def-use traversals.  The first determines
liveness from uses.  The second rewrites copies.

Previously, the defs whose uses were analyzed were discovered twice,
once during each traversal.  The non-triviality of the discovery logic
(i.e. the logic determining when to walk into the values produced by the
instructions which were the users of visited uses) opened the
possibility for a divergence between the two discoveries.  This
possibility had indeed been realized--the two traversals didn't visit
exactly the same uses, and issues ensue.

Here, the defs whose uses are analyzed are discovered only once (and not
discarded as their uses are analyzed) during the first traversal.  The
second traversal reuses the defs discovered in the first traversal,
eliminating the possibility of a def discovery difference.

The second traversal is now done in a different order.  This results in
perturbing the SIL in certain cases.
2024-12-05 11:25:06 -08:00
Nate Chandler
eebe9ac20a [NFC] OSSACanonicalizeOwned: Renamed found defs.
The field is no longer a worklist, just a list of discovered defs.
2024-12-05 08:29:52 -08:00
Nate Chandler
498294efa2 [NFC] OSSACanOwned: Record defs in SmallVector.
In preparation for only recording the defs once, replace the
GraphNodeWorklist of defs with a SetVector.  Preserve the current
visitation order by creating a worklist of indices to be visited.
2024-12-05 08:24:46 -08:00
Nate Chandler
d2f251cd9a [NFC] OSSACanonicalizeOwned: Shadow member.
Add a local variable that shadows the `defUseWorklist` instance member,
enabling further constraints (e.g. `const`ness) to eventually be imposed.
2024-12-05 07:31:20 -08:00
Nate Chandler
aed217437e [NFC] OSSACanOwned: Vary rewrite with def kind.
When rewriting uses, determine how that rewriting should proceed based
on the kind of def whose use is being visited.  Direct uses of reborrows
and borrowed-froms never need to be rewritten because every such use is
a use of a guaranteed value and can never require a copy.
2024-12-05 07:31:19 -08:00
Nate Chandler
77bc114e54 [NFC] OSSACanonicalizeOwned: Record def kinds.
Add a type which distinguishes among the types of defs that are pushed
onto the "def-use worklist".  Note that it's not possible to rely on the
kind of value because the root may itself be a copy_value.  For now, the
distinction is discarded as soon as the def is visited.
2024-12-05 07:31:19 -08:00
nate-chandler
0d76250033 Merge pull request #77908 from nate-chandler/rdar139840307
[BarrierAccessScopes] Handle end_access instructions' barrierness introduced during run.
2024-12-04 15:29:02 -08:00
Michael Gottesman
87495c6b83 Merge pull request #77900 from gottesmm/rdar127477211
[region-isolation] Perform checking of non-Sendable results using rbi rather than Sema.
2024-12-03 22:08:49 -08:00
Michael Gottesman
d9f5ef8d03 Fix small compilation from MSVC. 2024-12-03 15:43:15 -08:00
Kuba Mracek
9c77074cac [Mangling] Establish a new mangling prefix for Embedded Swift: $e 2024-12-02 15:01:24 -08:00
Kuba Mracek
6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00
Nate Chandler
2d1bdb84e6 [Gardening] Add missing word in comment. 2024-12-02 14:05:40 -08:00
Nate Chandler
6e48e05ece [Test] Fix shrink_borrow_scope. 2024-12-02 14:05:40 -08:00
Michael Gottesman
cff835e061 [region-isolation] Perform checking of non-Sendable results using rbi rather than Sema.
In terms of the test suite the only difference is that we allow for non-Sendable
types to be returned from nonisolated functions. This is safe due to the rules
of rbi. We do still error when we return non-Sendable functions across isolation
boundaries though.

The reason that I am doing this now is that I am implementing a prototype that
allows for nonisolated functions to inherit isolation from their caller. This
would have required me to implement support both in Sema for results and
arguments in SIL. Rather than implement results in Sema, I just finished the
work of transitioning the result checking out of Sema and into SIL. The actual
prototype will land in a subsequent change.

rdar://127477211
2024-12-02 16:54:12 -05:00
Doug Gregor
a93e8fd006 Merge pull request #77752 from DougGregor/perf-diag-check-throws
[Performance diagnostics] Enable checking of throw instructions
2024-12-01 22:54:07 -08:00
Doug Gregor
1d3332d471 Remove the now-unused NonErrorHandlingBlocks 2024-11-21 16:06:45 -08:00
Michael Gottesman
e6b4e0f9f1 Merge pull request #77709 from gottesmm/pr-6feaf0c91a7d95d75b36d32cc91a32150d992162
[region-isolation] Some initial NFCI refactoring commits before adding experimental support for inheriting isolation to nonisolated functions
2024-11-19 22:22:50 -08:00
Michael Gottesman
d33f819038 [region-isolation] Move freeform logging on the specific error we are emitting into a method on the error itself.
I am doing this since I discovered that we are not printing certain errors as
early as we used to (due to the refactoring I did here), which makes it harder
to see the errors that we are emitting while processing individual instructions
and before we run the actual dataflow.

A nice side-effect of this is that it will make it easy to dump the error in the
debugger rather than having to wait until the point in the code where the normal
logging takes place.
2024-11-19 12:48:30 -08:00
Andrew Trick
4612728581 [NFC] Add BeginApplyInst::getEndApplyUses() API.
A begin_apply token may be used by operands that do not end the coroutine:
mark_dependence.

We need an API that gives us only the coroutine-ending uses. This blocks
~Escapable accessors.

end_borrow is considered coroutine-ending even though it does not actually
terminate the coroutine.

We cannot simply ask isLifetimeEnding, because end_apply and abort_apply do not
end any lifetime.
2024-11-18 01:37:00 -08:00
Slava Pestov
2c3ee09678 SILOptimizer: Fix invariant violation in usePrespecialized()
The conformances no longer match the replacement types after the
transformation here, so we must look them up again. This is now
flagged by SubstitutionMap::verify().
2024-11-15 17:27:24 -05:00
Nate Chandler
e76dc2d5e4 [Devirtualizer] Replace begin_apply allocations.
When devirtualizing a yield_once_2 begin_apply, replace all uses of the
allocation address, just as is done with the token.
2024-11-13 21:33:07 -08:00