Commit Graph

11193 Commits

Author SHA1 Message Date
Erik Eckstein
d71f36be19 GlobalPropertyOpt: handle load_borrow and destroy_addr 2024-12-11 12:32:32 +01:00
Erik Eckstein
66621d8f2b Optimizer: temporarily disable AccessPathVerification in the late pipeline.
It triggers a false alarm when building SwiftDocC on linux
rdar://141270464
2024-12-11 12:32:32 +01:00
eeckstein
81c65758e3 Merge pull request #78059 from eeckstein/destroy-hoisting
Optimizer: add a new destroy-hoisting optimization
2024-12-11 06:18:05 +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
Meghana Gupta
984f9f6cd2 Remove unreachable blocks after inlining 2024-12-10 17:01:11 -08:00
Meghana Gupta
02b4bac7fc Merge pull request #78083 from eeckstein/fix-ssa-updater
SSAUpdater: fix an ownership violation
2024-12-10 16:21:25 -08:00
Erik Eckstein
5be781a9a0 Optimizer: add a new destroy-hoisting optimization
It hoists `destroy_value` instructions  without shrinking an object's lifetime.
This is done if it can be proved that another copy of a value (either in an SSA value or in memory) keeps the referenced object(s) alive until the original position of the `destroy_value`.
```
  %1 = copy_value %0
  ...
  last_use_of %0
  // other instructions
  destroy_value %0       // %1 is still alive here
```
->
```
  %1 = copy_value %0
  ...
  last_use_of %0
  destroy_value %0
  // other instructions
```

The benefit of this optimization is that it can enable copy-propagation by moving destroys above deinit barries and access scopes.
2024-12-10 16:28:11 +01: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
Erik Eckstein
dd78dc722b Optimizer: add an optimization to remove copy_value of a borrowed value.
It removes a `copy_value` where the source is a guaranteed value, if possible:

```
  %1 = copy_value %0   // %0 = a guaranteed value
  // uses of %1
  destroy_value %1     // borrow scope of %0 is still valid here
```
->
```
  // uses of %0
```

This optimization is very similar to the LoadCopyToBorrow optimization.
Therefore I merged both optimizations into a single file and renamed it to "CopyToBorrowOptimization".
2024-12-09 20:01:07 +01:00
Meghana Gupta
f7d1c59df5 Merge pull request #78053 from meg-gupta/fixdce
Fix DCE for ownership forwarding instructions
2024-12-09 09:56:30 -08:00
Meghana Gupta
daa9b161d0 Fix DCE for ownership forwarding instructions
DCE deletes ownership forwarding instructions when it doesn’t have useful users.
It inserts destroy_value/end_borrow for its operands to compensate their lifetimes.

DCE also deletes branches when its successor blocks does not have useful instructions.
It deletes blocks and creates a jump to the nearest post dominating block.

When DCE needs to delete a forwarding instruction in a dead block, it cannot just create
lifetime ends of its operands at its position. Use LifetimeCompletion utility in such cases.

rdar://140428721
2024-12-09 03:22:57 -08:00
Meghana Gupta
adf1eae287 Bring in DominanceInfo and DeadEndBlocks to DCE 2024-12-09 03:00:18 -08: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
Meghana Gupta
cd0d8d1de0 Merge pull request #77992 from meg-gupta/delassert
Remove incorrect assert in ConditionForwarding
2024-12-05 21:47:52 -08:00
Meghana Gupta
23498595bf Remove incorrect assert in ConditionForwarding
ConditionForwarding is able to handle owned values and non-local guaranteed values.
Remove incorrect assertion about enum trivialiaty

Fixes rdar://140977875
2024-12-05 13:02:50 -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
Erik Eckstein
e328eee05c DefiniteInitialization: correctly handle upcasts in delegating initializers
Fixes a crash due to a wrong class type when creating a value_metatype instruction

rdar://140926647
2024-12-05 13:12:51 +01: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
Meghana Gupta
db89c6a753 Merge pull request #77933 from meg-gupta/fixexistential
Fix existential specializer for unowned ownership
2024-12-04 10:40:09 -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
eeckstein
9e7fa1a023 Merge pull request #77918 from eeckstein/remove-dead-code
ArraySemantics: remove some unused code
2024-12-03 21:30:39 +01:00
Meghana Gupta
d033fe8224 Fix existential specializer for unowned ownership 2024-12-03 12:19:52 -08:00
Gábor Horváth
b744793716 Merge pull request #77878 from swiftlang/gaborh/import-rval-ref
[cxx-interop] Import rvalue references as consuming parameters
2024-12-03 16:14:02 +00:00
Kuba (Brecka) Mracek
8792efedf0 Merge pull request #77115 from kubamracek/embedded-mangling-prefix
[Mangling] [NFC] Prepare for a new mangling prefix for Embedded Swift: $e
2024-12-03 08:10:49 -08:00
Erik Eckstein
f166f4b4df ArraySemantics: remove some unused code
The code is not used anymore because the ArrayElementPropagation pass was removed: https://github.com/swiftlang/swift/pull/77806
2024-12-03 11:45:54 +01:00
Anton Korobeynikov
216111172e Explicitly use minimal type expansion for autodiff-related types (e.g. parameters and pullback result types) (#77831)
As autodiff happens on function types it is not in general possible to determine the real expansion context of the function being differentiated. Use of minimal context is a conservative approach that should work even when libraty evolution mode is enabled.

Fixes #55179
2024-12-02 15:02:09 -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
Allan Shortlidge
3e50a90c45 AST: Introduce Decl::getUnavailableAttr().
It replaces `DeclAttr::getUnavailable()` and `AvailableAttr::isUnavailable()`
as the designated way to query for the attribute that makes a decl unavailable.
2024-12-02 07:35:58 -08:00
Gabor Horvath
1601564342 [cxx-interop] Import rvalue references as consuming parameters
Unfortunately, importing them as is results in ambiguous call sites.
E.g., std::vector::push_back has overloads for lvalue reference and
rvalue reference and we have no way to distinguish them at the call site
in Swift. To overcome this issue, functions with rvalue reference
parameters are imported with 'consuming:' argument labels.

Note that, in general, move only types and consuming is not properly
supported in Swift yet. We do not invoke the dtor for the moved-from
objects. This is a preexisting problem that can be observed with move
only types before this PR, so the fix will be done in a separate PR.
Fortunately, for most types, the moved-from objects do not require
additional cleanups.

rdar://125816354
2024-12-02 13:09:21 +00: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
eeckstein
ca50c55eb5 Merge pull request #77806 from eeckstein/rle-of-array-elements
Optimizer: remove the ArrayElementPropagation optimization
2024-12-02 07:13:08 +01:00
Erik Eckstein
63f6a2f30d Optimizer: remove the ArrayElementPropagation optimization
Propagating array element values is done by load-simplification and redundant-load-elimination.
So ArrayElementPropagation is not needed anymore.

ArrayElementPropagation also replaced `Array.append(contentsOf:)` with individual `Array.append` calls.
This optimization is removed, because the benefit is questionably, anyway.
In most cases it resulted in a code size increase.
2024-11-28 10:35:40 +01:00
Erik Eckstein
6a0b7d1f8c ObjectOutliner: create outlined arrays as let variables
This will allow load-simplification to replace a load of such an array.
2024-11-28 09:40:12 +01:00
Erik Eckstein
ca09336154 ConditionForwarding: fix a wrong assert
Some terminator instructions can have type-dependent operands.
Therefore we need to use `getNumRealOperands` instead of `getNumOperands`.

Fixes a compiler crash.
2024-11-27 13:19:12 +01:00
Doug Gregor
1d3332d471 Remove the now-unused NonErrorHandlingBlocks 2024-11-21 16:06:45 -08:00
Doug Gregor
c09bbf4c10 [Performance diagnostics] Enable checking of throw instructions
When performance diagnostics were introduced, typed throws didn't exist
so it was not generally possible to have throws anywhere without
triggering performance diagnostics. As a short term hack, we disabled
checking of `throw` instructions and the basic blocks that terminate
in a `throw`.

Now that typed throws is available and can be used to eliminate
allocations with error handling, remove all of the hacks. We'll now
diagnose attempts to throw or catch existential values (e.g., the `any
Error` used for untyped throws), but typed throws are fine.
2024-11-21 16:06:44 -08:00
Anton Korobeynikov
55d51b782d Closure specialization might create functions with lots of arguments. (#77629)
Increase inlining benefits for functions with more than 5 arguments and / or results.
We assume that each argument beyond these 5 would be passed on stack and therefore would incur a pair of load and store.
2024-11-20 16:31:47 -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