Commit Graph

389 Commits

Author SHA1 Message Date
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
Nate Chandler
1eafced6a2 [AllocBoxToStack] Don't destroy in dead-ends.
It is valid to leak a value on paths into dead-end regions.
Specifically, it is valid to leak an `alloc_box`.  Thus, "final
releases" in dead-end regions may not destroy the box and consequently
may not release its contents.  Therefore it's invalid to lower such final
releases to `dealloc_stack`s, let alone `destroy_addr`s.  The in-general
invalidity of that transformation results in miscompiling whenever a box
is leaked and its projected address is used after such final releases.

Fix this by not treating final releases as boundary markers of the
`alloc_box` and not lowering them to `destroy_addr`s and
`dealloc_stack`s.

rdar://158149082
2025-08-27 17:03:48 -07:00
Nate Chandler
74528eef8c [NFC] OptUtils: Promote isInLoop to internal.
It will be used by both AllocBoxToStack in addition to StackPromotion
and for the same reason.
2025-08-27 17:03:47 -07:00
Nate Chandler
f94168c875 [AllocBoxToStack] Restore isDeadEnd check.
The rewrite was missing the intentional omission of `dealloc_stack`s
corresponding to `[dead_end]` `dealloc_box`es.  Add the necessary
bridging to get to parity with the original.

Without this check, `dealloc_box [dead_end]`s are promoted to
`dealloc_stack`s but the memory projected out of such `alloc_box`s need
not be valid.

rdar://159271158
2025-08-27 17:02:27 -07:00
Aidan Hall
53028abea9 Merge pull request #83665 from aidan-hall/fix-effects
ComputeSideEffects: compute properties even if function has an effect attribute
2025-08-27 10:34:53 +01:00
Daniil Kovalev
ae2b46e263 [AutoDiff][gardening] Auto-format ClosureSpecialization.swift (#83925)
Auto-formatting is done as a prerequisite for the ongoing series of
patches resolving #68944.
2025-08-27 06:12:47 +00:00
Aidan Hall
6a263f8ddb ComputeSideEffects: Compute for functions with effect attributes
After computing side effects, we also remove any global or argument effects that
are computed to happen, but are defined not to, based on the effect attribute.

This allows us to compute the deinit_barrier effect for such functions, fixing
the test case here: rdar://155870190

This supercedes #38324.
2025-08-22 11:42:09 +01:00
Doug Gregor
7a196ac3ba Merge pull request #83739 from DougGregor/embedded-weak-definitions-of-imports
[Embedded] Emit weak definitions for imported symbols
2025-08-17 20:25:02 -07:00
Doug Gregor
c91a4822d3 Serialize the "markedAsUsed" bit for SILGlobalVariable 2025-08-17 15:25:50 -07:00
Andrew Trick
288cef1532 LifetimeDependenceScopeFixup: extend store_borrow allocations
Extend temporary allocations (sink dealloc_stacks) initialized by a store_borrow
across lifetime dependent uses.

Fixes rdar://143159873 ([nonescapable] extend rvalue lifetimes when they are the source of a dependency)
2025-08-14 10:13:55 -07:00
Andrew Trick
b3e169be5e [NFC] Rename a variable for clarity. 2025-08-14 09:08:11 -07:00
Andrew Trick
eb1d5f484c [NFC] SwiftCompilerSources: add a correctly named filterUsers API
Rename existing filterUsers to filterUses.
2025-08-14 09:08:11 -07:00
Erik Eckstein
eaf38da903 InitializeStaticGlobals: support non-loadable enums
TODO: we don't support non-loadable enum cases with payload, yet, because IRGen support is missing.
2025-08-03 17:25:43 +02:00
Erik Eckstein
c849c7cdae InitializeStaticGlobals: support initializing globals with @_rawLayout types, like Atomic
Look through `@_rawLayout` projections, which "type casts" a raw-layout struct to it's content, which must match the like-type of the raw-layout, e.g.

```
@_rawLayout(like: T)
struct S {}

  %2 = builtin "addressOfRawLayout"<S>(%1 : $*S) : $Builtin.RawPointer
  %3 = pointer_to_address %2 to $*T
```
2025-08-03 17:25:43 +02:00
Erik Eckstein
41a6b8e257 SwiftCompilerSources: move SIL-related Context APIs from Optimizer to the SIL module 2025-07-28 14:19:11 +02:00
nate-chandler
d6b5c4fb99 Merge pull request #82798 from eeckstein/fix-let-property-lowering
LetPropertyLowering: remove redundant phis after ssa-update
2025-07-08 18:07:08 -07:00
Andrew Trick
14e0fe8d1f Merge branch 'main' into reassign-immortal 2025-07-08 08:20:18 -07:00
Daniil Kovalev
5528cf1cc4 [AutoDiff] Run AutoDiff closure spec pass for all VJPs (#81548)
Previously, AutoDiff closure specialization pass was triggered only on
VJPs containing single basic block. However, the pass logic allows
running on arbitrary VJPs. This PR enables the pass for all VJPs
unconditionally. So, if the pullback corresponding to multiple-BB VJP
accepts some closures directly as arguments, these closures might become
specialized by the pass. Closures passed via payload of branch tracing
enum are not specialized - this is subject for future changes.

The PR contains several commits.
1. The thing named "call site" in the code is partial_apply of pullback
corresponding to the VJP. This might appear only once, so we drop
support for multiple "call sites".
2. Enhance existing SILOptimizer tests for the pass.
3. Add validation-tests for single basic block case.
4. The change itself - delete check against single basic block.
5. Add validation-tests for multiple basic block case.
6. Add SILOptimizer tests for multiple basic block case.
2025-07-07 13:00:14 +00:00
Andrew Trick
ac94d7df1d Fix LifetimeDependenceDiagnostics: allow inout assignment to Void.
Bypess lifetime dependence diagnostics completely for immortal values. We did
not do this initially because we wanted to potentially consider a value with a missing
dependency to mean that it could not escape the current function. But now we use
`Void` as a stand-in for immortal values.

This is needed for reassigning a Span/MutableSpan to an empty, immortal
Span:

    func inoutToImmortal(_ s: inout RawSpan) {
      let tmp = RawSpan(_unsafeBytes: UnsafeRawBufferPointer(start: nil, count: 0))
      s = _overrideLifetime(tmp, borrowing: ())
    }

Fixes rdar://152572002 ([GH:#81976] Cannot reinitialize inout parameter of type
`MutableSpan<T>?`)
2025-07-07 00:16:49 -07:00
Erik Eckstein
81a4f7a84e LetPropertyLowering: remove redundant phis after ssa-update
This is needed after running the SSAUpdater, because the updater can insert unnecessary phis in the middle of the original liverange of a value.

Fixes an ownership error.
rdar://153229472
2025-07-04 11:10:27 +02:00
Erik Eckstein
80ed35b38d MandatoryAllocBoxToStack: also handle new specialized functions
Add new created specializations to the worklist so that those are optimized as well.

rdar://154686063, rdar://154713388
2025-07-02 19:10:47 +02:00