Commit Graph

11008 Commits

Author SHA1 Message Date
eeckstein
efe8aeb718 Merge pull request #78317 from eeckstein/pointer-to-address-simplification
Optimizer: re-implement the `pointer_to_address` SILCombine peephole optimizations in swift
2025-01-02 12:54:26 +01:00
Erik Eckstein
53d78abd01 Optimizer: enable SILCodeMotion in OSSA, but only for trivial values.
to-do: we should eventually remove code motion of retain and release instructions and implement them as semantic ARC optimizations in OSSA.
2025-01-02 11:01:39 +01: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
eeckstein
aa2cfd3ed1 Merge pull request #78361 from eeckstein/dead-obj-elimination
DeadObjectElimination: handle OSSA instructions when analyzing class destructors
2025-01-02 08:41:15 +01:00
eeckstein
95f199212d Merge pull request #78351 from eeckstein/fix-specializer
GenericSpecializer: fix an ownership verification failure when removing a partial_apply of a thunk
2025-01-02 08:40:52 +01:00
eeckstein
1bdb51edc3 Merge pull request #78318 from eeckstein/fix-access-enforcement-opts
AccessEnforcementOpts: fix a SIL verifier error caused by a wrong bail-out condition
2025-01-02 08:31:28 +01:00
Erik Eckstein
41dd3dc453 DeadObjectElimination: handle OSSA instructions when analyzing class destructors
Handle typical patterns which are generated by SILGen.
2024-12-24 12:00:22 +01:00
Meghana Gupta
5e1f9a37ef Enable silcombine to propagate concrete type of existentials in ossa 2024-12-23 08:34:13 -08:00
Meghana Gupta
571498289a [NFC] Print final ossa module when enabled 2024-12-23 08:31:31 -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
Erik Eckstein
9aff288be4 Optimizer: re-implement the pointer_to_address SILCombine peephole optimizations in swift
Which consists of
* removing redundant `address_to_pointer`-`pointer_to_address` pairs
* optimize `index_raw_pointer` of a manually computed stride to `index_addr`
* remove or increase the alignment based on a "assumeAlignment" builtin

This is a big code cleanup but also has some functional differences for the `address_to_pointer`-`pointer_to_address` pair removal:

* It's not done if the resulting SIL would result in a (detectable) use-after-dealloc_stack memory lifetime failure.
* It's not done if `copy_value`s must be inserted or borrow-scopes must be extended to comply with ownership rules (this was the task of the OwnershipRAUWHelper).

Inserting copies is bad anyway.
Extending borrow-scopes would only be required if the original lifetime of the pointer extends a borrow scope - which shouldn't happen in save code. Therefore this is a very rare case which is not worth handling.
2024-12-21 08:28:22 +01:00
Ben Barham
8111fe9343 Merge pull request #78262 from bnbarham/skip-non-wmo-diag
[Embedded] Do not produce `cannot_specialize_class` for live issues
2024-12-20 10:40:07 -08:00
Erik Eckstein
413c78e33a AccessEnforcementOpts: fix a SIL verifier error caused by a wrong bail-out condition
In case of a bail-out on failed ownership extension the optimization didn't remove old instructions from previously merged access pairs.
2024-12-20 15:51:37 +01:00
Ben Barham
a2fda1d9f3 [Embedded] Do not produce cannot_specialize_class for live issues
SourceKit explicitly disables WMO, silence the diagnostic in this case
(but leave it enabled for explicit non-WMO builds otherwise).
2024-12-19 15:31:41 -08:00
Nate Chandler
6d9ae19629 [DCE] Don't complete lifetimes of erased values.
DCE may enqueue a value for lifetime completion and later on erase the
instruction that defines that value.  When erasing an instruction, erase
each of its results from the collection of values to complete.

rdar://141560546
2024-12-19 15:16:32 -08:00
Nate Chandler
f75f12b681 [DCE] Add delete handler for phi erasure.
Set CallsChanged when appropriate and increment NumDeletedInsts.
2024-12-19 15:12:24 -08:00
Nate Chandler
00116edcfb [NFC] DCE: TermInsts increment NumDeletedInsts. 2024-12-19 15:12:24 -08:00
Nate Chandler
1851e712f8 [Gardening] DCE: Use bound variable in branch.
Rather than reusing the source of the dyn_cast.
2024-12-19 15:12:24 -08:00
Allan Shortlidge
17dfbf5053 AST: Adopt SemanticAvailableAttr for SpecializeAttr. 2024-12-19 08:40:00 -08:00
nate-chandler
419d87d630 Merge pull request #78105 from nate-chandler/rdar141197164
[SILGenCleanup] Complete lifetimes of defs backwards reachable from dead-end blocks.
2024-12-18 20:28:01 -08:00
Nate Chandler
c327c59460 [SGC] Complete deadend-reachable lifetimes.
For a function to have complete lifetimes, the lifetime of every def in
the function which is backwards-reachable from a dead-end block must be
completed.

Previously, every def in every block which appears in the post-order of
the function's blocks is completed.  This was not enough: defs in
"unreachable blocks" (i.e. those which aren't forwards-reachable from
the function entry) must be completed too, and such blocks do not appear
in the function's post-order.

Here, defs in unreachable blocks are be completed too.  Such defs must
also be completed in relative post-order.  To do this, roots for the
non-entry post-orders must be found.

rdar://141197164
2024-12-18 11:25:43 -08:00
Nate Chandler
7c4036e908 [NFC] SGC: Extract range completion.
In preparation for completing lifetimes in multiple ranges (namely
post-orders of subgraphs of the function), extract a complete-in-range
method.
2024-12-18 11:25:42 -08:00
Nate Chandler
62f2ffd794 [NFC] ReachableBlocks: Use StackList.
Replace a SmallVector.
2024-12-18 11:25:42 -08:00
Nate Chandler
f9b32a5c4d [NFC] CLF: Compute reachable blocks only.
Don't form a set of unreachable blocks, we only need to check whether
any given block is unreachable, which can be done via
`ReachableBlocks::isVisited`.
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
Nate Chandler
e926d22b2d [NFC] SILGenCleanup: Added logging. 2024-12-18 11:25:42 -08:00
Nate Chandler
c53b1a5711 [Gardening] Detypo'd comment. 2024-12-18 11:25:41 -08:00
eeckstein
f1d66e3826 Merge pull request #78233 from eeckstein/ossa-simplify-alloc-stack
SILCombine: enable alloc_stack optimization for OSSA
2024-12-18 06:39:31 +01:00
eeckstein
d5bb068cb9 Merge pull request #78234 from eeckstein/no-use-prespecialized-in-embedded
Optimizer: don't run the UsePrespecialized pass in embedded mode
2024-12-18 06:39:12 +01:00
Meghana Gupta
aaaf5a4916 Merge pull request #78260 from meg-gupta/enableedgecase
Allow SimplifyCFG::simplifyArgument on borrowed values
2024-12-17 20:59:31 -08:00
Meghana Gupta
cda907e06d Merge pull request #78231 from meg-gupta/fixabc
Fix array bounds check optimization for ossa
2024-12-17 16:47:25 -08:00
Meghana Gupta
363731686e Allow SimplifyCFG::simplifyArgument on borrowed values 2024-12-17 15:03:11 -08:00
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