Commit Graph

11224 Commits

Author SHA1 Message Date
Meghana Gupta
2a5ddfbe2d Fix array bounds check optimization for ossa
While hoisting check_subscript call in ossa, isNativeTypeChecked call is also hoisted.
The array value used in the isNativeTypeChecked may not be available if it's lifetime
had ended before. Proactively set the array value of the isNativeTypeChecked call to
the array value in the check_subscript call.
2024-12-17 10:57:11 -08:00
Erik Eckstein
35af29aaa0 Optimizer: don't run the UsePrespecialized pass in embedded mode
There are not pre-specialized parts of the stdlib in embedded mode.

Fixes a compiler crash.
Unfortunately I con't have a test case for this.

https://github.com/swiftlang/swift/issues/78167
2024-12-17 11:36:21 +01:00
Erik Eckstein
69b16faa04 SILCombine: enable alloc_stack optimization for OSSA 2024-12-17 09:55:39 +01:00
Meghana Gupta
a62112bd1c Merge pull request #78200 from meg-gupta/fixdce
Look through borrowed from instructions before calling lifetime completion in DCE
2024-12-15 21:06:05 -08:00
Andrew Trick
1d1b260c8f Merge pull request #78199 from atrick/lifedep-cleanup
LifetimeDependence: minor diagnostic quality fixes and related utilities
2024-12-15 04:45:08 -08:00
Meghana Gupta
50bde829b4 Look through borrowed from instructions before calling lifetime completion
Lifetime completion will insert end_borrows only on borrow introducers.

Look through "borrowed from" instructions before calling it.

rdar://141490551
2024-12-15 01:53:19 -08:00
Meghana Gupta
3701f01de4 Mark ownership fixup instructions as live 2024-12-15 01:53:17 -08:00
Andrew Trick
b7f010570d Merge pull request #78197 from atrick/silcombine-markdep
Improve SILCombine mark_dependence: handle address dependence.
2024-12-14 23:37:55 -08:00
Andrew Trick
98da813f02 [NFC] SwiftCompilerSources: add isConvertPointerToPointerArgument 2024-12-14 22:46:54 -08:00
Andrew Trick
e56dbb4695 Improve SILCombine mark_dependence: handle address dependence. 2024-12-14 16:26:58 -08:00
Meghana Gupta
01da59f370 Merge pull request #78176 from meg-gupta/fixstropt
Fix StringOptimization to handle load_borrow
2024-12-13 19:15:46 -08:00
eeckstein
55009bf718 Merge pull request #78163 from eeckstein/improve-dead-object-elimination
Two optimization improvements to fix dead-object-elimination with OSSA
2024-12-13 22:56:24 +01:00
Meghana Gupta
d351623df0 Fix StringOptimization to handle load_borrow
rdar://140229560
2024-12-13 13:16:22 -08:00
Meghana Gupta
28e33af799 Merge pull request #78123 from meg-gupta/newflag
Add new flag -sil-ownership-verify-all
2024-12-13 10:00:41 -08:00
Erik Eckstein
bf496aa4f6 Optimizer: add simplification for fix_lifetime
Canonicalize a `fix_lifetime` from an address to a `load` + `fix_lifetime`:
```
   %1 = alloc_stack $T
   ...
   fix_lifetime %1
```
->
```
   %1 = alloc_stack $T
   ...
   %2 = load %1
   fix_lifetime %2
```

This transformation is done for `alloc_stack` and `store_borrow` (which always has an `alloc_stack` operand).
The benefit of this transformation is that it enables other optimizations, like mem2reg.

This peephole optimization was already done in SILCombine, but it didn't handle store_borrow.
A good opportunity to make an instruction simplification out of it.

This is part of fixing regressions when enabling OSSA modules:
rdar://140229560
2024-12-13 12:06:20 +01:00
Erik Eckstein
9781e99544 DeadObjectElimination: handle begin_borrow/end_borrow when deleting dead arrays
This is part of fixing regressions when enabling OSSA modules:
rdar://140229560
2024-12-13 10:49:01 +01:00
Meghana Gupta
8b1ecb8a71 Add new flag -sil-ownership-verify-all
This flag enables ownership verification after every transform.
2024-12-12 23:55:37 -08:00
Erik Eckstein
750d6ec81b CapturePropagation: handle keypaths which are upcast.
Deal with upcast instructions which cast keypath instructions before they are passed to a partial_apply.

rdar://141370412
2024-12-13 07:51:36 +01:00
eeckstein
0c860f8aba Merge pull request #78136 from eeckstein/fix-ssa-updater-2
LoopRotate: remove redundant phis after ssa-update
2024-12-12 20:59:02 +01:00
Usama Hameed
203f906364 Serialize/Deserialize source locations for instructions (#77281)
This commit adds support for serializing and deserializing source locations for instructions.
2024-12-12 16:15:44 +05:00
Erik Eckstein
8439d0bc46 LoopRotate: remove redundant phis after ssa-update
Fixes an ownership error
2024-12-12 09:22:20 +01: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
eeckstein
b7485467e9 Merge pull request #78054 from eeckstein/ossa-opt-improvements
Some OSSA related optimization improvements
2024-12-11 20:07:24 +01:00
Meghana Gupta
252a57a3fd Merge pull request #78096 from meg-gupta/removeunreachable
Remove unreachable blocks after inlining
2024-12-11 05:07:58 -08:00
Erik Eckstein
6b38f2aab4 Optimizer: simplify load_borrow
* Remove dead `load_borrow` instructions (replaces the old peephole optimization in SILCombine)
* If the `load_borrow` is followed by a `copy_value`, combine both into a `load [copy]`
2024-12-11 12:32:33 +01:00
Erik Eckstein
1bd74d1fc3 LICM: (limited) support for OSSA
The first step: allow hoisting instructions which only have trivial operands and results.
2024-12-11 12:32:33 +01:00
Erik Eckstein
074a99cc39 LoopRotate: don't rotate a loop if the new header is loop exiting as well
This doesn't give any performance benefit.
2024-12-11 12:32:33 +01:00
Erik Eckstein
8000802481 LoopRotate: handle copy_value and begin_borrow correctly 2024-12-11 12:32:32 +01:00
Erik Eckstein
7e8410ebc1 COWArrayOpt: handle load_borrow 2024-12-11 12:32:32 +01:00
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