Commit Graph

50 Commits

Author SHA1 Message Date
Andrew Trick
7197f63ce2 Comment LifetimeDependenceScopeFixup. Explain unreachable paths. 2025-10-03 20:44:17 -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
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
Andrew Trick
fe9c0dd735 Fix LocalVariableUtils switch_enum_addr.
switch_enum_addr was being treated like a store instruction, which killed
the local enum's liveness. This could result local variable analysis reporting a
shorter lifetime for the local.

This showed up as a missing exclusivity diagnostic because an access scope was
not fully extended across a dependent local variable of Optional type.

This prevents the following pattern from miscompiling. It should report an exclusivity violation:

  var mutableView = getOpaqueOptionalView(holder: &holder)!
  mutate(&holder)
  mutableView.modify()

Fixes rdar://151231236 ([~Escapable] Missing 'overlapping acceses' error when
called from client code, but exact same code produces error in same module)
2025-06-28 09:30:17 -07:00
Erik Eckstein
63cb683cb7 SIL: improve some Location APIs
* rename `var autoGenerated` -> `var asAutoGenerated`
* add `var asCleanup`
* add `func withScope`
2025-06-20 08:15:00 +02:00
Andrew Trick
5b5f370ce1 LifetimeDependenceScopeFixup: crash handling dead-end coroutine
When extending a coroutine, handle the end_borrow instruction used to end a
coroutine lifetime at a dead-end block.

Fixes rdar://153479358 (Compiler crash when force-unwrapping optional ~Copyable type)
2025-06-16 21:43:44 -07:00
Valeriy Van
949c2bad67 Fix some typos in SwiftCompilerSources/Sources 2025-06-08 11:22:45 +03:00
Andrew Trick
227f8028e8 LifetimeDependenceScopeFixup: handle yielded value copies.
When extending an access scope over a coroutines, instead of simply
considering the lifetime of the coroutine scope, recurse through all
uses of yielded values. They may be copyable, non-Escapable values
that depend on the coroutine operand.

Fixes rdar://152693622 (Extend coroutines over copied yields)
2025-06-05 20:21:40 -07:00
Andrew Trick
95f7a12c98 [NFC] LifetimeDependence: fix internal debug output 2025-06-05 20:09:06 -07:00
Meghana Gupta
ba845de874 Fix newly introduced warnings in LifetimeDependenceScopeFixup 2025-05-05 11:22:16 -07:00
Meghana Gupta
3cad5c5924 Merge pull request #81043 from meg-gupta/fixcow
Insert end_cow_mutation_addr for lifetime dependent values dependent on mutable addresses
2025-05-01 07:26:28 -07:00
Meghana Gupta
4951f628ad 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 13:37:45 -07:00
Andrew Trick
4a65be8074 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)
2025-04-30 11:27:57 -07:00
Erik Eckstein
d80f7a3560 SIL: make BeginBorrowInstruction.endOperands and BeginBorrowValue.scopeEndingOperands consistent.
* rename `ScopedInstruction.endOperands` -> `scopeEndingOperands`
* let them behave the same way. For `load_borrow` there was a difference because `endOperands` didn't consider branches to re-borrow phis.
2025-04-28 10:24:56 +02:00
Erik Eckstein
0dee86590d SIL: make protocol ScopeInstruction an Instruction protocol
This allows to remove the `var instruction` property.

NFC
2025-04-28 09:40:06 +02:00
Andrew Trick
bb9e52c3f2 LifetimeDependenceScopeFixup: cleanup for clarity 2025-04-25 17:58:23 -07:00
Andrew Trick
33fbe11bf8 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)
2025-04-22 11:54:53 -07:00
Andrew Trick
a1aaed9159 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)
2025-04-21 22:33:38 -07:00
Andrew Trick
62454f42fd 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.
2025-04-16 16:46:38 -07:00
Andrew Trick
81ca8fd3ec Enable NonEscapableType support on Windows.
SwiftCompilerSources for Windows is now fully enabled, so there is no
longer any reason to gate these passes under an OS check.

commit d482ab73bc
Author: Hiroshi Yamauchi <hjyamauchi@gmail.com>
Date:   Fri Dec 13 09:24:44 2024 -0800

Update the pinned toolchain for Windows and enable SwiftCompilerSources for Win/ARM64
2025-03-31 11:21:06 -07:00
Andrew Trick
d9dd93560d Support mark_dependence_addr in SIL passes. 2025-03-25 23:02:45 -07:00
Andrew Trick
b3f401d346 LifetimeDependenceScopeFixup: handle indirect yields & fix bugs
Handle coroutines that yield an address. Fix bugs involving extension of
multiple nested scopes that depend on multiple coroutine operands.
2025-03-05 02:12:21 -08:00
Andrew Trick
1b51aa8829 LifetimeDependenceScopeFixup: extend store_borrow scopes
This is require to handle '@'_addressable arguments on the caller side.
2025-03-03 16:21:48 -08:00
Andrew Trick
bc6200e592 SwiftCompilerSources: bridge '@'_addressable dependencies 2025-03-03 11:56:37 -08:00
Andrew Trick
c3de120ca5 LifetimeDependence: simplify and fix multiple bugs.
Functional changes:

Improved modeling of dependence on local variable scopes.

For nested modify->read accesses, only extend the read accesses.

Avoid making a read access dependent on an inout argument.
The following needs to be an error to prevent span storage from being modified:

  @lifetime(owner)
  foo(owner: inout Owner) -> Span {
    owner.span
  }

Improve usability of borrowing trivial values (UnsafePointer). Allow:

  let span = Span(buffer.baseAddress)

Ignore access scopes for trivial values.

Structural changes:

Delete the LifetimeDependenceUseDefWalker.

Encapsulate all logic for variable introducers within the LifetimeDependenceInsertion pass. Once mark_dependence instructions are inserted, no subsequent pass needs to think about the "root" of a dependence.

Fixes: rdar://142451725 (Escape analysis fails with mutations)
2025-02-10 09:11:22 -08:00
Andrew Trick
b0f2ca03a7 AccessUtils: allow mark_deps to be tracked by the EnclosingScope.
This encourages AccessPathWalker clients to handle enclosing mark_deps. In
some cases, it is necessary. The accessBaseWithScopes API now provides both
nested begin_access and mark_dependence.
2025-01-11 15:40:22 -08:00
Andrew Trick
49136f237f Fix LifetimeDependenceScopeFixup to avoid rewriting mark_dependence.
This pass rewrites mark_depenendence to ignore "useless" borrow scopes. It was
also accidentally rewriting a dependence on a loaded value, which may redirect the
dependence to the access scope used to load that value. That access scope may be
narrower than the lifetime of the loaded value which could result in invalid
SIL. Do not rewrite this mark_dependence:

  %access = begin_access [read] [unknown] %base
  %load = load [trivial] %access
  end_access %access
  %adr = pointer_to_address
  %md = mark_dependence [unresolved] %adr on %load

Fixes rdar://142424000 (Swift compiler crashes with Assertion failed
(isa<UnreachableInst>(block->getTerminator())))
2025-01-07 15:36:52 -08:00
Andrew Trick
3897929e4a LifetimeDependence: handle dependence on trivial values. 2024-12-16 16:09:37 -08:00
Andrew Trick
9f74282680 Redesign LifetimeDependenceScopeFixup to handle accessors.
Handle all combinations of nested dependence scopes: access scopes, coroutines,
and borrow scopes.

This is required to enforce ~Escapable _read accessors and unsafeAddress addressors.

Fixes rdar://140424699 (Invalid SIL is generated by some passes for certain
@lifetime annotations)
2024-12-16 15:28:26 -08:00
Alexander Cyon
c18a24e499 [SwiftCompilerSources] Fix typos 2024-08-08 22:22:39 -07:00
Andrew Trick
11ba799cd4 LifetimeDependence: diagnose yield and store-to-yield. 2024-07-30 16:27:48 -07:00
Andrew Trick
ba9f12a80f LifetimeDependenceDiagnostics support for inout reassignment.
This relies on parameter dependence targets.

i.e. dependsOn in parameter position instead of result position.
2024-07-30 15:57:57 -07:00
Andrew Trick
058d22b2b6 Fix LifetimeDependenceScopeFixup for loops.
Handle situations in which the uses of the new scope are inside a loop so the
new range has no end instructions.
2024-07-30 15:57:56 -07:00
Andrew Trick
f3cf529b5d [SwiftCompilerSources] use debugLog in diagnostic passes 2024-07-30 15:57:56 -07:00
Andrew Trick
279eb78d27 [windows] Temporarily guard lifetime dependence diagnostics
by -enable-experimental-feature NonescapableTypes
on the Windows platform

These passes do nothing unless the above feature flag is enabled, so
the only reason to run the pass is to exercise SwiftCompilerSources
and catch invalid SIL.

These passes rely on fundamental SwiftCompilerSources abstractions
which have not yet been tested outside of the passes. They don't yet
handle all SIL patterns, and SIL continues to evolve. We would like to
can these issues quickly as we hit them, but only if we have a way of
reproducing the failure. Currently, we don't have a way of reproducing
Windows-arm64 failures.

Workaround for:
rdar://128434000 ([nonescapable] [LifetimeDependenceInsertion]
Package resolution fails with arm64 Windows toolchain)
2024-06-20 16:51:21 -07:00
Andrew Trick
f19d94ce3f Always-enable lifetime-depenence diagnostics.
-enable-experimental-feature NonescapableTypes now only controls syntax and some type inferrence features.
2024-03-27 13:57:11 -07:00
Andrew Trick
08be9aebf2 LifetimeDependence cleanup logging 2024-03-22 11:51:58 -07:00
Andrew Trick
352724ea0d LifetimeDependenceScopeFixup logging 2024-03-18 17:45:33 -07:00
Andrew Trick
8419cc599f Guard LifetimeDependenceScopeFixup under experimental NonescapableTypes
Out of paranoia, don't even try to run this path unless we see
-enable-experimental-feature NonescapableTypes.
2024-03-07 15:13:50 -08:00
Andrew Trick
3bee32639b LifetimeDependenceDefUseWalker: use LocalVariableReachableUses. 2024-03-05 18:08:16 -08:00
Andrew Trick
5c956f7437 Review feedback; only rewrite access scopes when necessary 2024-03-05 17:08:13 -08:00
Andrew Trick
7dea051bf6 Review feedback; Builder.createEndAccess() 2024-03-05 17:08:13 -08:00
Andrew Trick
d1a75e725c LifetimeDependenceScopeFixup: handle non-dominated dependent uses. 2024-03-05 17:08:13 -08:00
Andrew Trick
1122cc445f LifetimeDependenceScopeFixup: fix handling of returned dependence
Only rewrite the mark_dependence to depend on the function argument when the
dependent value is actually returned.

Also, find all uses even if an escaping use is seen.
2024-03-05 17:08:13 -08:00
Andrew Trick
8aa1d91a2c LifetimeDependence: clarify log headers 2024-03-05 17:08:13 -08:00
Meghana Gupta
12ac8042ab Handle YieldInst in LifetimeDependenceUtils 2024-02-19 15:28:04 -08:00
Andrew Trick
68037faa61 LifetimeDependenceScopeFixup: handle nested access
After extending access scopes, rewrite the mark_depenendence to be on
the outermost scope.
2024-02-13 22:22:28 -08:00
Meghana Gupta
75bd5b08dd Add LifetimeDependenceScopeFixup pass 2024-02-13 16:52:01 -08:00