Commit Graph

1996 Commits

Author SHA1 Message Date
Erik Eckstein
46035305aa Optimizer: improve simplification of alloc_stack
* Reimplement most of the logic in Swift as an Instruction simplification and remove the old code from SILCombine
* support more cases of existential archetype replacements:

For example:
```
  %0 = alloc_stack $any P
  %1 = init_existential_addr %0, $T
  use %1
```
is transformed to
```
  %0 = alloc_stack $T
  use %0
```

Also, if the alloc_stack is already an opened existential and the concrete type is known,
replace it as well:
```
  %0 = metatype $@thick T.Type
  %1 = init_existential_metatype %0, $@thick any P.Type
  %2 = open_existential_metatype %1 : $@thick any P.Type to $@thick (@opened("X", P) Self).Type
  ...
  %3 = alloc_stack $@opened("X", any P) Self
  use %3
```
is transformed to
```
  ...
  %3 = alloc_stack $T
  use %3
```
2025-03-07 15:59:34 +01:00
Erik Eckstein
5572c832e1 SILOptimizer: replace existential archetypes with concrete types in witness_method instructions 2025-03-07 15:59:34 +01:00
Erik Eckstein
71de3d90e0 Optimizer: replace existential archetypes with concrete types in apply instructions
If an apply uses an existential archetype (`@opened("...")`) and the concrete type is known, replace the existential archetype with the concrete type
  1. in the apply's substitution map
  2. in the arguments, e.g. by inserting address casts
For example:
```
  %5 = apply %1<@opend("...")>(%2) : <τ_0_0> (τ_0_0) -> ()
```
->
```
  %4 = unchecked_addr_cast %2 to $*ConcreteType
  %5 = apply %1<ConcreteType>(%4) : <τ_0_0> (τ_0_0) -> ()
```
2025-03-07 15:59:34 +01:00
Erik Eckstein
35097b5a71 Optimizer: simplify unconditional_checked_cast to existential metatypes.
Replace `unconditional_checked_cast` to an existential metatype with an `init_existential_metatype`, it the source is a conforming type.
Note that init_existential_metatype is better than unconditional_checked_cast because it does not need to do any runtime casting.
2025-03-07 15:59:33 +01:00
Erik Eckstein
df81283b97 SILCombine: make it possible to add a Swift simplification to an existing SILCombine visit function.
So far a `SILCombineSimplifiable` could only replace a SILCombine visit implementation.
With the `SWIFT_SILCOMBINE_PASS_WITH_LEGACY` (to be used in Passes.def) it's possible to keep an existing C++ implementation and on top of that add a Swift Simplification pass.
2025-03-07 15:59:33 +01:00
Erik Eckstein
de841fdcd5 Fix a comment about SILCombine "passes" in Passes.def 2025-03-07 15:59:33 +01:00
Andrew Trick
5783b497e5 Merge pull request #79758 from atrick/addressable-deps
Support enforcement of '@'_addressable under -enable-address-dependencies
2025-03-03 23:51:56 -08:00
Andrew Trick
fa64a362a2 Add -Xfrontend -enable-address-dependencies
Temporary option to bootstrap '@'_addressable enforcement.

Once all the SILGen cases are handled, we won't need this option.
2025-03-03 16:21:48 -08:00
Meghana Gupta
3fe1029ef8 [NFC] Reorganize and rename ArrayBoundsCheckOpts.cpp 2025-02-28 09:50:58 -08:00
Nate Chandler
f1bc3b5205 [OSSACanOwned] Dead-end extend base lifetime.
Regardless of consumes of copies, if the original lexical value is not
consumed on a dead-end path, it must remain live to that dead-end.

rdar://145226757
2025-02-27 07:20:27 -08:00
Nate Chandler
5ffe092fa5 [OSSACanOwned] Extend all lifetimes to dead-ends.
Uses of borrows may occur between the borrow and a dead-end regardless
of lexicality, so always extend lifetimes to dead-ends.
2025-02-27 07:20:25 -08:00
Nate Chandler
22fdeff83c [NFC] OSSACanOwned: Record function as member. 2025-02-27 07:19:48 -08:00
Michael Gottesman
44052e5c28 [rbi] Respect nonisolated(unsafe) when assigning or merging into a sending out parameter.
rdar://143714959
2025-02-25 08:39:17 -08:00
Michael Gottesman
b286373631 [rbi] Make a confusing comment clearer.
From talking with @dgregor, it became clear that this comment was easily
interpreted as saying that AssignFresh always introduced a disconnected value...
which is not the case. Instead, AssignFresh just introduces a new value that
could have any form of isolation. The actual isolation of the value is assigned
via tryToTrackValue and eventually SILIsolationInfo::get().
2025-02-24 10:09:32 -08:00
Meghana Gupta
4561658799 Avoid creating unoptimizable copies in CSE
CSE uses OSSA rauw which creates copies and copies that are created to optimize
across borrow scopes are unoptimizable. This PR avoids this situation for now.
2025-02-19 11:20:43 -08:00
Erik Eckstein
83aaccc188 remove the "array.copy_into_vector" array-semantic
It's not needed anymore, because the "FixedArray" experimental feature is replaced by inline-arrays.
2025-02-12 10:51:14 +01:00
Erik Eckstein
5b93eb31bf Optimizer: remove the AllocVectorLowering pass
It's not needed anymore, because the "FixedArray" experimental feature is replaced by inline-arrays.
2025-02-12 10:51:14 +01:00
Arnold Schwaighofer
ed32270d72 Merge pull request #79191 from aschwaighofer/access_enforcement_fixes
AccessEnforcement: Fix analysis to include mayReleases as potentially executing unknown code
2025-02-10 16:57:31 -08:00
eeckstein
c4af3b3a8e Merge pull request #79186 from eeckstein/remove-predictable-memopt
Optimizer: replace PredictableMemoryAccessOptimizations with a "mandatory" redundant load elimination pass
2025-02-08 08:05:33 +01:00
eeckstein
e22c09c055 Merge pull request #79212 from eeckstein/remove-type-expanstion-analysis
Optimizer: Remove the TypeExpansionAnalysis
2025-02-08 00:59:40 +01:00
Arnold Schwaighofer
7a251af60c AccessEnforcement: Fix analysis to include mayReleases as potentially
executing unknown code

This means we have to claw back some performance by recognizing harmless
releases.

Such as releases on types we known don't call a deinit with unknown
side-effects.

rdar://143497196
rdar://143141695
2025-02-07 15:10:13 -08:00
Erik Eckstein
319258fea2 Optimizer: Remove the TypeExpansionAnalysis
It was used in the old redundant-load- and redundant-store-elimination passes which were replaced by new implementations.
TypeExpansionAnalysis is not used anymore.
2025-02-07 11:55:27 +01:00
Erik Eckstein
0a011cddd8 Remove the old (and now obsolete) PredictableMemoryAccessOptimizations pass 2025-02-07 11:30:35 +01:00
Erik Eckstein
33761454b7 MandatoryPerformanceOptimizations: use eliminateRedundantLoads instead of optimizeMemoryAccesses to optimize redundant loads 2025-02-07 11:30:35 +01:00
Erik Eckstein
ce73deebc4 RedundantLoadElimination: add a "mandatory" redundant load elimination pass
And make `eliminateRedundantLoads` callable from other optimizations
2025-02-07 11:30:35 +01:00
Nate Chandler
c6e0480e1c [NFC] OSSACanOwned: Simplify Def definition.
It's sufficient just to have a struct with a kind and a value.  There
aren't any cases where the payload's original type benefits from being
statically preserved--they're only ever obtained as `SILValue`s.  Keep
the type safety by way of constructors.
2025-02-06 17:58:20 -08:00
Nate Chandler
2a43494a01 [Gardening] OSSACanOwned: Removed dead friend.
There's no longer a DenseMap of Defs, so no corresponding DenseMapInfo.
2025-02-06 13:16:09 -08:00
nate-chandler
8c63134570 Merge pull request #78682 from nate-chandler/rdar142636711_2
[TempRValueOpt] Invalidate insts when completing.
2025-01-27 12:53:57 -08:00
Michael Gottesman
082b824a8e [rbi] Change Region Based Isolation for closures to not use the AST and instead just use SIL.
The reason why I am doing this is that in certain cases the AST captures indices
will never actually line up with partial apply capture indices since we seem to
"smush" together closures and locally defined functions.

NOTE: The reason for the really small amount of test changes is that this change
does not change the actual output by design. The only cases I had to change were
a case where we began to emit a better diagnostic and also where I added code
coverage around _ and let _ since those require ignored_use to be implemented so
that they would be diagnosed (previously we just did not emit anything so we
couldn't emit the diagnostic at the SIL level).

rdar://142661388
2025-01-22 21:12:36 -08:00
Nate Chandler
1d22288f24 [NFC] SIL: Subpass runs can take values.
Allow continueWithNextSubpassRun to take a SILValue.
2025-01-16 08:18:29 -08:00
Meghana Gupta
2c256180ce Add new utility swift::areUsesWithinValueLifetime 2025-01-10 16:15:50 -08:00
Meghana Gupta
ed0483b4ce Fix SILCombine of existential applies
Don't use previously found owned concrete values in ossa. They will consumed by
forwarding operations like init_existential_ref. Instead create an unconditional cast
of the opened existential to concrete type and use that to create a concrete apply.
2025-01-07 10:38:25 -08:00
eeckstein
f096787871 Merge pull request #78384 from eeckstein/enable-code-motion-in-ossa
Optimizer: enable SILCodeMotion in OSSA, but only for trivial values.
2025-01-02 20:36:29 +01:00
Erik Eckstein
48b913af4b Optimizer: make the hasOwnershipOperandsOrResults utility available in OwnershipOptUtils 2025-01-02 10:42:01 +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
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
bace1a60ce [NFC] ReachableBlocks: Add ::hasUnreachableBlocks.
The method walks over the blocks of the function testing each for
reachability and returns false on the first that is not.
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
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
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
Erik Eckstein
3e35df0983 Simplification: run begin_borrow simplification in SILCombine 2024-12-11 12:32:34 +01: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
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
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
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
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