Commit Graph

1280 Commits

Author SHA1 Message Date
eeckstein
11b77bfe3c Merge pull request #81328 from eeckstein/copy-block-opt2-6.2
[6.2] Optimizer: support partial_apply of thunks in the `copy_block` simplification
2025-05-07 01:19:57 +02:00
Meghana Gupta
65cda30e54 Merge pull request #81298 from meg-gupta/fixwarncp
[6.2] Fix newly introduced warnings in LifetimeDependenceScopeFixup
2025-05-06 09:03:38 -07:00
Erik Eckstein
cc92319c3d Optimizer: support partial_apply of thunks in the copy_block simplification.
If the block is passed to another function - either as closure argument or as closure capture - it's "converted" to a swift closure with the help of a thunk. The thunk just calls the block.
If this is done with a non-escaping partial_apply, the block does not escape.

rdar://149095630
2025-05-06 17:57:37 +02:00
Erik Eckstein
2f34dae56f Swift SIL: add Builder.createUncheckedOwnershipConversion 2025-05-06 17:57:37 +02:00
Meghana Gupta
f9f709b7c3 Fix newly introduced warnings in LifetimeDependenceScopeFixup 2025-05-05 11:26:38 -07:00
Erik Eckstein
7e212a8580 embedded: avoid false error "Deinit of non-copyable type not visible in the current module" in SourceKit
As SourceKit explicitly disables WMO, silence the diagnostic in this case (but leave it enabled for explicit non-WMO builds otherwise).

rdar://150596807
2025-05-05 19:11:46 +02:00
Zachary 'Clack' Cole
f3380efa9d Merge pull request #81240 from clackary/cherrypick/ossa-partial-apply-80662
[6.2 🍒] Fix ownership issues with sequences of partial_apply's in AutoDiff closure specialization pass
2025-05-02 13:20:34 -06:00
Andrew Trick
6f05d80161 LifetimeDependenceDefUseAddressWalker: avoid infinite recursion.
This utility is used by DependentAddressUseDefWalker which now conservatively
follows all possible uses. This could result in the same address being reached
multiple times during a def-use walk. Ensure that we don't infinitely recurse.

There is no small test case for this, but the fix is trivial and standard
practice for such walkers, and this is hit quickly in real usage, so there is no
danger of it regressing.

Fixes rdar://150403948 ([nonescapable] Infinite recursion compiler crash in
lifetime dependence checking)

(cherry picked from commit 4512927d2b)
2025-05-01 17:10:59 -07:00
Anton Korobeynikov
35678bffc2 [6.2 🍒]
Fix ownership issues with sequences of partial_apply's in AutoDiff closure specialization pass (#80662)

Each partial_apply consumes its arguments, therefore we should never release intermediate ones in the sequence of closures.

Fixes #78847

(cherry picked from commit 87ca0f8680)
2025-05-01 14:48:46 -06:00
Andrew Trick
5643f53dca Merge pull request #81223 from atrick/62-missing-markdep
[6.2] Fix LifetimeDependenceInsertion: handle boxed indirect out arguments
2025-05-01 00:51:13 -07:00
Meghana Gupta
7b0ac8b5b6 Merge pull request #81216 from meg-gupta/fixcowcp
[6.2] Insert end_cow_mutation_addr for lifetime dependent values dependent on mutable addresses
2025-05-01 00:44:41 -07:00
Andrew Trick
1e0cec5065 Fix LifetimeDependenceInsertion: handle boxed indirect out arguments
Correctly generate dependency tracking for functions that return a non-Escapable
existential, such as:

func getMutableSpanWithOpaqueReturn(_ array: inout [Int]) -> any PAny & ~Copyable & ~Escapable

Previously, dependency insertion assumed that @out storage always initialized an
alloc_stack. But existentials are always boxed.

First, add a diagnostic to catch any missing dependency insertions now that
we're past the bootstrapping phase.

Then, generalize handling of dependency insertion to handle any access base as
long as it has a recognizable address source.

Fixes rdar://150388126 (Missing mark_dependence for opaque lifetime dependent
value)

(cherry picked from commit 9d8e8d3f05)
2025-04-30 16:55:55 -07:00
Meghana Gupta
5885421c06 Bridge getSwiftMutableSpanDecl() and isBuiltinType() 2025-04-30 14:39:44 -07:00
Meghana Gupta
cc72edb119 Introduce end_cow_mutation_addr instruction 2025-04-30 14:38:48 -07:00
Meghana Gupta
ed3f6b4c35 Insert end_cow_mutation_addr for lifetime dependent values dependent on mutable addresses
Array/ArraySlice/ContiguousArray have support for mutableSpan property.
These types are "optimized COW" types, we use compiler builtins begin_cow_mutation/end_cow_mutation to
optimize uniqueness checks. Since mutableSpan is a property and not a coroutine there is
no way to schedule the end_cow_mutaton operation at the end of the access.
This can lead to miscompiles in rare cases where we can end up using a stale storage buffer after a cow.

This PR inserts end_cow_mutation_addr to avoid this issue.

Note: We can end up with unnecessary end_cow_mutation_addr. But it is just a barrier to prevent invalid optimizations
and has no impact.
2025-04-30 14:37:59 -07:00
Andrew Trick
6e19b06acc Fix LifetimeDependenceScopeFixup: extend scopes enclosing coroutines
When a coroutine is extended because of a dependent lifetime, extend all scopes
that enclose that coroutine even if the coroutine itself has no lifetime
dependencies.

Fixes rdar://150275147 (Invalid SIL after lifetime dependence fixup involving
coroutines)

(cherry picked from commit 4a65be8074)
2025-04-30 11:40:37 -07:00
Andrew Trick
7756a322fa LifetimeDependenceDiagnostics: note for unsupported closure capture
Add a note explaining that dependence on closure captures is not
supported. Otherwise, the diagnostics are very confusing:
"it depends on a closure capture; this is not yet supported"

(cherry picked from commit 83b0ce1098)
2025-04-30 00:09:40 -07:00
Andrew Trick
4c78ece643 LifetimeDependence: clarify diagnostics for many unusual cases.
Ensure that we always issue a diagnostic on error, but avoid emitting any notes that don't have source locations.

With implicit accessors and thunks, report the correct line number and indicate which accessor generates the error.

Always check for debug_value users.

Consistently handle access scopes across diagnostic analysis and diagnostic messages.

(cherry picked from commit ec512864eb)
2025-04-30 00:09:40 -07:00
Andrew Trick
9b9a4056bd [NFC] SwiftCompilerSource: bridge Function.accessorKindName
(cherry picked from commit 25e9cbf3f1)
2025-04-30 00:09:39 -07:00
Andrew Trick
8bce9b2c4f Refactor debugVarDecl so arguments also support debug_value users.
(cherry picked from commit 49755bd0ed)
2025-04-30 00:09:39 -07:00
Andrew Trick
63619e8a34 LifetimeDependenceDiagnostics: avoid infinite recursion on error
Fix a simple typo that results in infinite recursion on invalid code.

Fixes rdar://147470493 ([nonescapable] LifetimeDependenceInsertion: infinite
recursion in VariableUseDefWalker.walkup with immortal setter)

(cherry picked from commit c891d8ade4)
2025-04-30 00:09:39 -07:00
Erik Eckstein
78bfc9f955 RedundantLoadElimination: support replacing a redundant copy_addr with a store
For example:
```
  %0 = load %1
  copy_addr %1 to %2
```
->
```
  %0 = load %1
  store %0 to %2
```

This is important for MandatoryRedundantLoadElimination to be able to create statically initialized globals in the mandatory pipeline.
For example:
```
public struct MyStruct {
  public static let r: Range<Int> = 1 ..< 3
}

```
gets a statically initialized global, even at Onone, with this improvement.

rdar://149356742
2025-04-28 08:01:49 +02:00
Andrew Trick
52ddb4d377 Bypass lifetime diagnostics in interfaces for copied arguments.
Briefly (April 2025), RawSpan._extracting, Span._extracting, and UTF8Span.span
returned a borrowed value that depended on a copied argument. Continue to
support those interfaces. The implementations were correct but needed an
explicit _overrideLifetime.

(cherry picked from commit 8a48cd979b)
2025-04-25 19:21:16 -07:00
Andrew Trick
5ddb6cebcc Fix LifetimeDependenceDiagnostics: scoped dependence on a copy
Diagnose a scoped dependence on an argument that inherits its lifetime as an
error:

@lifetime(borrow arg)
func reborrowSpan<T>(_ arg: Span<T>) -> Span<T> { arg }

@lifetime(copy span)
public func testBorrowInheritedArg<T>(_ span: Span<T>) -> Span<T> {
  reborrowSpan(span) // expected-error {{lifetime-dependent value escapes its scope}}
}

Fixes: rdar://146319009 ([nonescapable] enforce borrow constraint narrowing of inherited lifetime)
(cherry picked from commit 4f470a1d34)
2025-04-25 19:21:15 -07:00
Andrew Trick
805aaa3b66 Fix LifetimeDependenceDiagnostics: handle mark_dependence_addr
A small typo meant that scoped dependencies modeled with mark_dependence_addr
were not diagnosed.

(cherry picked from commit 92b2a8587f)
2025-04-25 19:21:15 -07:00
Andrew Trick
4fbf0db621 [NFC] LifetimeDependenceUtils comment
(cherry picked from commit 060364546a)
2025-04-25 19:21:15 -07:00
Andrew Trick
e04289abe2 LifetimeDependenceScopeFixup: cleanup for clarity
(cherry picked from commit bb9e52c3f2)
2025-04-25 19:21:15 -07:00
Andrew Trick
3674d08c2b Fix LifetimeDependenceDiagnostics: handle address-only 'let's
Add a case to LifetimeDependence.Scope to support dependencies on address-only
'let' variables. This comes up with C++ interop.

Fixes rdar://147500193 (Spurious lifetime error with closures)

(cherry picked from commit 5831777407)
2025-04-23 23:01:31 -07:00
Andrew Trick
bb6b5b3645 Fix LifetimeDependenceDiagnostics: handle drop_deinit unsafeAddress
This adds support to handle unsafe addressors, which generate mark_dependence
instructions with an address base. This case does not arise with lifetime
dependencies. Nonetheless, the lifetime dependence diagnostics kick in to
attempt to promote the unsafe addressor's mark_dependence to noescape, so we
need to handle it.

rdar://149784450 (Compiler crash with non-escapable deinit dereferencing)
(cherry picked from commit 3b08592ed2)
2025-04-22 22:46:41 -07:00
Andrew Trick
759d109a74 Fix LifetimeDependenceScopeFixup: read access can depend on 'inout'
Allow a dependency on a local [read] access scope to be transfered to the
caller's [modify] access.

Fixes rdar://149560133 (Invalid lifetime dependence error when
trying to return Span from an inout argument)

(cherry picked from commit 33fbe11bf8)
2025-04-22 12:33:01 -07:00
Andrew Trick
7df225e935 Merge pull request #80980 from atrick/62-rdar149397018-optional-return
[6.2] LifetimeDependenceScopeFixup: handle returning dependent Optional
2025-04-22 12:27:28 -07:00
Andrew Trick
161158806e LifetimeDependenceScopeFixup: handle returning dependent Optional
Add support for returnValue phis (e.g. to return an Optional .some or .none).

Fixes rdar://149397018 (Wrapping non escapable in an Optional
(or any copy lifetime wrapper) is an escape)

(cherry picked from commit a1aaed9159)
2025-04-21 22:57:06 -07:00
Meghana Gupta
2a314d0fab Handle a special case of borrowed from instruction in CopyToBorrowOptimization 2025-04-21 15:25:33 -07:00
Andrew Trick
8504b7f584 Merge pull request #80880 from atrick/62-read-accessor
[6.2] LifetimeDependenceScopeFixup: _read accessor: extend temporaries
2025-04-17 08:13:35 -07:00
Andrew Trick
ff7cc60923 Fix unidentified LifetimeDependence.Scope initialization.
Fixes rdar://149226564 (Compiler crash with non-escapable storing another
non-escapable and having a deinit)

(cherry picked from commit 6f8605263d)
2025-04-16 23:32:00 -07:00
Andrew Trick
a794a39a12 LifetimeDependenceScopeFixup: _read accessor: extend temporaries
Add support for extending owned temporary values in addition to access scopes
and borrow scopes. Required for _read accessors in which the coroutine depends
on an owned value.

(cherry picked from commit 62454f42fd)
2025-04-16 23:32:00 -07:00
Andrew Trick
2fbc9032f5 LocalVariableUtils: add store_borrow access tracking.
We sometimes use LocalVariableUtils to analyze temporary storage in which the
store_borrow is not already enclosed by an access scope.

(cherry picked from commit 4e35d255ad)
2025-04-16 23:31:53 -07:00
Andrew Trick
5269c0c8e7 [NFC] Add LifetimeDependenceUseDefWalker utility.
Refactor VariableIntroducerUseDefWalker into a general
LifetimeDependenceUseDefWalker for use with LifetimeDependenceScopeFixup.

(cherry picked from commit b80bd16eea)
2025-04-16 23:23:55 -07:00
Andrew Trick
cc949bdaa7 [NFC] minor cleanup
(cherry picked from commit 43debc529f)
2025-04-16 23:23:55 -07:00
Andrew Trick
32fc6ad623 Add InstructionRange.overlapsWithPath() utility.
(cherry picked from commit 28daec01a7)
2025-04-16 23:23:54 -07:00
Andrew Trick
e60e65538a SwiftCompilerSources: add IgnoredUseInst as incidental
Fixes rdar://148540048 (Assigning span value to `_` results in an incorrect escape diagnostic)

(cherry picked from commit 2bdaa68e98)
2025-04-16 23:23:54 -07:00
Andrew Trick
fa39b4172b Fix AddressOwnershipLiveRange to include the full range.
This allows further extension of access scopes.

Fixes rdar://143992296 (Use of `RawSpan` in switch context causes compiler crash
in AddressOwnershipLiveRange)

(cherry picked from commit c9279d9899)
2025-04-16 23:23:54 -07:00
Erik Eckstein
9f9942855a Guard the copy_block optimization with an experimental feature flag CopyBlockOptimization 2025-04-16 14:33:27 +02:00
Erik Eckstein
d8fddc9dcc Optimizer: remove redundant copy_block instructions
Removes a `copy_block` if its only uses, beside ownership instructions, are callees of function calls
```
  %2 = copy_block %0
  %3 = begin_borrow [lexical] %2
  %4 = apply %3() : $@convention(block) @noescape () -> ()
  end_borrow %3
  destroy_value %2
```
->
```
  %4 = apply %0() : $@convention(block) @noescape () -> ()
```

rdar://118521396
2025-04-16 14:33:26 +02:00
Erik Eckstein
a80a006ec6 SIL: make CopyBlockInst conform to UnaryInstruction 2025-04-16 14:33:26 +02:00
Andrew Trick
e01a167db1 LocalVariableUtils: add unit tests
(cherry picked from commit ccb26bcd89)
2025-04-11 00:29:02 -07:00
Andrew Trick
c9c0ef843c LocalVariableUtils: fix data flow propagation of escapes.
This is only used for lifetime dependence diagnostics.

Fix a couple of obvious problems with data flow propagation that crept in during
"cleanup" because no unit tests existed.

(cherry picked from commit 37ecd0e9bd)
2025-04-11 00:28:52 -07:00
Andrew Trick
bf6ed014dd Fix escape analysis: addressable parameters.
An address-type parameter may escape via an indirect argument if the function's
result depends on the argument's address.

(cherry picked from commit 05a9acbb40)
2025-04-08 14:09:54 -07:00
nate-chandler
88fb10ed3b Merge pull request #80589 from nate-chandler/cherrypick/release/6.2/rdar148744816
6.2: [SIL] Fix bridged begin_apply results.
2025-04-07 16:46:48 -07:00
Nate Chandler
b7ca5672c5 [SIL] Fix bridged begin_apply results.
The `yield_once_2` adds an extra result at the end, the deallocation.
Fix the indexing for the token and yielded results.
2025-04-07 07:36:43 -07:00