Commit Graph

409 Commits

Author SHA1 Message Date
eeckstein
401a2ac2bc Merge pull request #84704 from eeckstein/closure-specialization
ClosureSpecialization: support for OSSA and a big overhaul
2025-10-07 06:59:08 +02:00
Andrew Trick
c2171ea9b0 Merge pull request #84703 from atrick/lifedep-diag-init
Lifetimes: add a diagnostic note for implicit accessors
2025-10-06 12:29:51 -07:00
Erik Eckstein
df20d36255 ClosureSpecialization: support for OSSA and a big overhaul
Beside supporting OSSA, this change significantly simplifies the pass.
The main change is that instead of starting at a closure (e.g. `partial_apply`) and finding all call sites, we now start at a call site and look for closures for all arguments. This makes a lot of things much simpler, e.g. not so many intermediate data structures are required to track all the states.

I needed to remove the 3 unit tests because the things those tests were testing are not there anymore. However, the pass is tested with a lot of sil tests (and I added quite a few), which should give good test coverage.

The old ClosureSpecializer pass is still kept in place, because at that point in the pipeline we don't have OSSA, yet. Once we have that, we can replace the old pass withe the new one.
However, the autodiff closure specializer already runs in the OSSA pipeline and there the new changes take effect.
2025-10-06 12:02:48 +02:00
Erik Eckstein
89bba668e2 Mangling: add a closure specialization mangling for arguments which specialize for the same closure as a previous argument
For example:
```
  %1 = partial_apply %closure
  apply %f(%1, %1)    // first argument: `.closure(%1)`
                      // second argument: `.previousArgumentIndex(0)`
```
2025-10-06 09:47:41 +02:00
Erik Eckstein
2cd625a367 Optimizer: add the Builder.insertCleanupAtFunctionExits utility 2025-10-06 09:47:40 +02:00
Erik Eckstein
c3612bafb8 SIL: make var OperandArray.values available for all kind of operand sequences 2025-10-06 09:47:40 +02:00
Andrew Trick
771e9b522e Lifetimes: add a diagnostic note for implicit accessors
Lifetime diagnostics may report an error within an implicit initializer or
accessor. The source location is misleading in these cases and causes much
consternation.
2025-10-05 20:16:44 -07:00
Andrew Trick
7197f63ce2 Comment LifetimeDependenceScopeFixup. Explain unreachable paths. 2025-10-03 20:44:17 -07:00
Erik Eckstein
171607afa7 DeadStoreElimination: fix a corner case of storing a trivial case of a non-trivial enum
Storing a trivial enum case in a non-trivial enum must be treated like a non-trivial init or assign, e.g.
```
  %1 = enum $Optional<String>, #Optional.none!enumelt
  store %1 to [trivial] %0  // <- cannot delete this store!
  store %2 to [assign] %0
```
2025-10-02 19:20:35 +02:00
Arnold Schwaighofer
7853ba0a7f Merge pull request #84178 from aschwaighofer/inline_always
Add experimental feature `@inline(always)`
2025-10-01 07:23:24 -07:00
Arnold Schwaighofer
25a071efc8 Add experimental feature @inline(always)
The intent for `@inline(always)` is to act as an optimization control.
The user can rely on inlining to happen or the compiler will emit an error
message.

Because function values can be dynamic (closures, protocol/class lookup)
this guarantee can only be upheld for direct function references.

In cases where the optimizer can resolve dynamic function values the
attribute shall be respected.

rdar://148608854
2025-09-30 08:36:26 -07:00
Erik Eckstein
b527020364 LoopInvariantCodeMotion: bail on split load [take]
We currently don't support split `load [take]`, i.e. `load [take]` which does _not_ load all non-trivial fields of the initial value.
2025-09-30 10:39:51 +02:00
Erik Eckstein
fc6302e3e9 LoopInvariantCodeMotion: correctly handle load [copy]
When moving loads and stores out of a loop, a `load [copy]` must be replaced by a `copy_value`.
2025-09-30 10:39:51 +02:00
Erik Eckstein
7bbe5c6fe2 LoopInvariantCodeMotion: don't hoist loads and stores if the memory location is not initialized at loop exits.
If the memory is not initialized at all exits, it would be wrong to insert stores at exit blocks.
2025-09-30 10:39:51 +02:00
Erik Eckstein
273874cd59 ConstantCapturePropagation: don't propagate keypaths with multiple uses in non-OSSA
We cannot do this because we don't know where to insert the compensating release after the propagated `partial_apply`.
A required `strong_retain` may have been moved over the `partial_apply`.
Then we would release the keypath too early.

Fixes a mis-compile
rdar://161321614
2025-09-29 18:42:39 +02:00
Erik Eckstein
12626475c3 ConstantCapturePropagation: refactor Value.isConstant 2025-09-29 18:42:39 +02:00
Erik Eckstein
e3055673f4 ConstantCapturePropagation: support sub-pass bisecting 2025-09-29 18:42:39 +02:00
Kavon Farvardin
32263bb9b8 ManualOwnership: add workaround for vars
For a proof-of-concept quality feature,
we can just skip the pass causing problems until
a proper solution is implemented.
2025-09-25 15:53:18 -07:00
Erik Eckstein
d590b94109 InitializeStaticGlobals: fix handling of nested non-loadable enums
Fixes a compiler crash
rdar://160880083
2025-09-25 10:11:18 +02:00
Erik Eckstein
648aa411a7 Reapply "InitializeStaticGlobals: support non-loadable enums"
This reverts commit 586f8a4694.
2025-09-25 08:22:26 +02:00
Jakub Florek
aebebd9ee2 Merge pull request #84463 from MAJKFL/fix-licm-missing-earlier-materializable-projection-check
LICM fix missing materializable projection check
2025-09-24 17:19:39 +01:00
Jakub Florek
6788017cfb Add earlier check before load projection that bails when it's not materializable. 2025-09-24 11:49:59 +01:00
Jakub Florek
bf16a4248c Merge pull request #84398 from MAJKFL/bring-back-licm-ownership
Reintroduce licm support for ownership
2025-09-22 17:57:15 +01:00
Jakub Florek
b12e0ef554 Don't hoist scoped instructions in dead end loops. 2025-09-22 12:12:19 +01:00
Jakub Florek
38f28c1049 Reapply "Merge pull request #84045 from MAJKFL/new-sil-licm-pass-copy-ownership"
This reverts commit d2cd281d4c.
2025-09-19 16:06:35 +01:00
nate-chandler
d7d4ba391a Merge pull request #84378 from nate-chandler/rdar160742150
[AllocBoxToStack] Fix missing [nothrow] of apply.
2025-09-18 21:27:23 -07:00
Nate Chandler
30098ad3e0 [AllocBoxToStack] Fix missing [nothrow] of apply.
When specializing an apply which is annotated `[nothrow]`, the resulting
function's apply must still be annotated `[nothrow]`.

rdar://160742150
2025-09-18 12:20:28 -07:00
Jakub Florek
d2cd281d4c Revert "Merge pull request #84045 from MAJKFL/new-sil-licm-pass-copy-ownership"
This reverts commit a5c6156525, reversing
changes made to 2b6ea81b9e.
2025-09-17 15:52:48 +01:00
Jakub Florek
a5c6156525 Merge pull request #84045 from MAJKFL/new-sil-licm-pass-copy-ownership
Ownership support for LICM
2025-09-16 11:02:28 +01:00
Meghana Gupta
c764244df0 Merge pull request #84180 from meg-gupta/borrowandmutatepr
Add preliminary support for borrow accessors
2025-09-15 10:01:15 -07:00
Jakub Florek
e84bc084f4 Check for aliasing destroy_addr before hoisting load_borrow - end_borrow pair. 2025-09-15 12:42:30 +01:00
Meghana Gupta
51c64d5fda Update SideEffects for borrow accessors returning addresses
Add a read effect on the self parameter. Without this, the self parameter can get dead code eliminated by the GenericSpecializer.
2025-09-14 23:38:03 -07:00
Nate Chandler
586f8a4694 Revert "InitializeStaticGlobals: support non-loadable enums"
This reverts commit eaf38da903.

rdar://160143339
2025-09-12 11:29:19 -07:00
Andrew Trick
8ea97f31e5 Merge pull request #84245 from atrick/lifedep-log
[NFC] LifetimeDependenceDiagnostics debug output
2025-09-12 04:14:39 -07:00
Andrew Trick
979a2500dd [NFC] LifetimeDependenceDiagnostics debug output
Make the output grep-able for errors since we often have debug output
for many functions.
2025-09-11 09:26:54 -07:00
Andrew Trick
52b0b058a7 LifetimeDependenceScopeFixup: extend temporary stack allocations
When the source of a lifetime dependency is a stack-allocated address, extend
the stack allocation to cover all dependent uses.

This avoids miscompilations for "addressable" dependencies which arise in code
built with -enable-experimental-feature AddressableTypes or
AddressableParameters. It is always an error for SILGen to emit the alloc_stack
in such cases. Nonetheless, we want to handle these unexpected cases gracefully
in SIL as a diagnostic error rather than allowing a miscompile.

Fixes rdar://159680262 ([nonescapable] diagnose dependence on a
temporary copy of a global array)
2025-09-10 20:59:19 -07:00
Jakub Florek
0b75a81b65 Add licm Ownership support. 2025-09-10 16:18:51 +01:00
Jakub Florek
df24019195 Rename Cloner.cloneRecursivelyToGlobal. 2025-09-10 16:18:50 +01:00
Jakub Florek
6955bdf564 Merge pull request #84173 from MAJKFL/fix-licm-not-projecting-load-path
Fix licm not projecting load path before load splitting.
2025-09-09 22:43:53 +01:00
Jakub Florek
be72ad726a Fix licm not projecting load path before load splitting. 2025-09-09 17:02:29 +01:00
Erik Eckstein
efa9f9ebc0 Optimizer: rewrite and improve the ConstantCapturePropagation pass
(old name: CapturePropagation)

The pass is now rewritten in swift which makes the code smaller and simpler.
Compared to the old pass it has two improvements:

* It can constant propagate whole structs (and not only builtin literals). This is important for propagating "real" Swift constants which have a struct type of e.g. `Int`.
* It constant propagates keypaths even if there are other non-constant closure captures which are not propagated. This is something the old pass didn't do.

rdar://151185177
2025-09-04 08:15:46 +02:00
Jakub Florek
e905df42f2 Fix read apply hoisted with conflicting write apply. 2025-09-02 15:53:53 +01:00
Jakub Florek
eae7864370 Merge pull request #83988 from MAJKFL/new-sil-licm-pass-copy
New SIL LICM pass
2025-09-01 10:28:17 +01:00
Erik Eckstein
c790052590 AllocBoxToStack: convert access checks from "dynamic" to "static"
Once we have promoted the box to stack, access violations can be detected statically by the DiagnoseStaticExclusivity pass (which runs after MandatoryAllocBoxToStack).
Therefore we can convert dynamic accesses to static accesses.

rdar://157458037
2025-08-30 07:29:15 +02:00
Erik Eckstein
b6e9c4c5e4 TempLValueElimination: don't propagate alloc_stack which have access scopes
If exclusivity is checked for the alloc_stack we must not replace it with the copy-destination.
If the copy-destination is also in an access-scope this would result in an exclusivity violation which was not there before.

Fixes a miscompile which results in a wrong exclusivity violation error at runtime.
https://github.com/swiftlang/swift/issues/83924
rdar://159220436
2025-08-30 07:29:15 +02:00
Jakub Florek
bab00113b2 Fix licm handling of unreferenceable storage. 2025-08-29 12:46:27 +01:00
nate-chandler
9fe43836f6 Merge pull request #83907 from nate-chandler/rdar158149082
[AllocBoxToStack] Don't destroy in dead-ends.
2025-08-28 13:36:28 -07:00
Jakub Florek
07ac8b3478 Add new loop invariant code motion. 2025-08-28 21:00:33 +01:00
Jakub Florek
e3140e0ae0 Add new generalized cloner. 2025-08-28 20:57:57 +01:00
Erik Eckstein
8e33487471 DeinitDevirtualizer: de-virtualize deinits of builtin "destroyArray" 2025-08-28 08:05:28 +02:00