Commit Graph

55 Commits

Author SHA1 Message Date
Andrew Trick
c839e265df [NFC] Update an incomplete comment. 2025-01-15 16:53:03 -08:00
Andrew Trick
c8bec28e6a LifetimeDependence.Scope: recognize @in_guaranteed dependencies.
Unlike @in, treat @in_guaranteed like a caller-side dependence
scope because there is not need to look for the end of the lifetime in the
current function.

Completely fixes rdar://142847915 (Crash during lifetime checking
while building new swift standard library `Span`-related features)
2025-01-14 15:38:54 -08:00
Andrew Trick
6849b66bb3 LifetimeDependenceDiagnostics; remove a bootstrapping hack.
This temporary hack was preventing diagnostics from kicking in. This could even
result in invalid SIL after the diagnostic failed to trigger.
2025-01-13 08:30:41 -08:00
Andrew Trick
0326c98f72 LifetimeDependenceScopeFixup: handle unsafe addressors
Handle unsafe addressors just like projections of stored properties rather than
as separate variable accesses.
2025-01-11 15:40: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
195ca7ceae Improve LifetimeDependence.Scope for mutable trivial variables.
Recognize dependence on the address of a trivial 'var' as an "access" dependence
instead of an "unknown" dependence. This allows the mark_dependence to be
resolved as "[nonescaping]".
2025-01-07 22:21:15 -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
Erik Eckstein
c5b14c2b93 BorrowUtils: make unchecked_ownership_conversion to guaranteed ownership a BeginBorrowValue
BeginBorrowValue needs to handle all cases where a guaranteed value is produced.
Fixes a compiler crash.
2024-12-21 08:28:21 +01:00
Andrew Trick
3897929e4a LifetimeDependence: handle dependence on trivial values. 2024-12-16 16:09:37 -08:00
Andrew Trick
eab9a3599f [NFC] Add LifetimeDependence.Scope.ignoreBorrowScope utility.
Skip borrow scopes that do not actually require lifetime dependence. This
will improve LifetimeDependenceScopeFixup.
2024-12-16 15:28:00 -08:00
Andrew Trick
7811a63233 Extend LifetimeDependence to allow borrowed value base. 2024-12-16 15:28:00 -08:00
Andrew Trick
f5e16b698e [NFC] LifeDependenceUtils: fix a merge redundancy. 2024-12-16 15:27:25 -08:00
Andrew Trick
c4a2200669 Fix lifetime_dependence_borrow_fail diagnostic.
Report the correct variable name.
2024-12-14 22:46:55 -08:00
Andrew Trick
46db7ce24e LifetimeDependenceDefUseWalker: recognize ConvertPointerToPointer
Look through a call to the ConvertPointerToPointerArgument compiler intrinsic
just like it is a copy of the pointer. At the source level, that's all it is:

Treat this like a direct use of the argument 'p' rather than the result of the
invisible pointer conversion call:

func foo(_: UnsafeRawPointer)

func bar(p: UnsafePointer<T>) {
  foo(p)
}
2024-12-14 22:46:54 -08:00
Andrew Trick
f5fc17804e [NFC] SwiftCompilerSources: Add VariableScopeInstruction utility
To diagnose dependence on trivial variables.
2024-12-14 22:46:54 -08:00
Erik Eckstein
f0633d5638 AccessUtils: support computing "constant" access paths
Add `Value.constantAccessPath`. It is like `accessPath`, but ensures that the projectionPath only contains "constant" elements.
This means: if the access contains an `index_addr` projection with a non-constant index, the `projectionPath` does _not_ contain the `index_addr`.
Instead, the `base` is an `AccessBase.index` which refers to the `index_addr`.
2024-11-04 19:26:44 +01:00
Allan Shortlidge
9062271185 SILGen: Skip SIL verification and optimization if errors were emitted.
In lazy typechecking mode, errors in the program may only be discovered during
SILGen, which can leave the SIL in a bad state for subsequent stages of
compilation. If errors were detected, skip SIL verification and optimization to
prevent knock-on failures.

Partially reverts https://github.com/swiftlang/swift/pull/75428, which included
a more targeted fix for one of the possible knock-on effects of bad SIL coming
out of SILGen.

Resolves rdar://132107752.
2024-08-12 15:34:59 -07:00
Alexander Cyon
c18a24e499 [SwiftCompilerSources] Fix typos 2024-08-08 22:22:39 -07:00
Andrew Trick
3e98d2e232 Fix an assert in LifetimeDependenceUtils to allow all valid cases. 2024-07-31 22:58:39 -07:00
Andrew Trick
11ba799cd4 LifetimeDependence: diagnose yield and store-to-yield. 2024-07-30 16:27:48 -07:00
Andrew Trick
11640461ef Lifetime dependence: add inferrence for setters.
A nonescapable computed property should always depend on 'newValue'. We simply
infer that now. There's no way to explicitly spell the dependence.
2024-07-30 15:57:57 -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
f3cf529b5d [SwiftCompilerSources] use debugLog in diagnostic passes 2024-07-30 15:57:56 -07:00
Andrew Trick
b4cdbd276b LifetimeDependenceUtils comment typo. 2024-07-29 23:44:02 -07:00
Andrew Trick
c32c238720 Fix LifetimeDependenceDiagnostics to handle invalid SIL types
Invalid types are not considered Escapable. This makes it difficult to make any
assumptions about nonescapable types.

Fixes rdar://132348528 (Fix LifetimeDependenceDiagnostics to handle invalid SIL types)
2024-07-23 16:03:32 -07:00
Andrew Trick
ad2e58ed43 Assert that mark_dependence operands are distinct.
Various downstream passes assume that the dependent value is not the same as the
base value.
2024-07-05 10:42:31 -07:00
Nate Chandler
06921cfe84 [SIL] Hollow out Builtin.copy, deprecate _copy.
The copy operator has been implemented and doesn't use it.  Remove
`Builtin.copy` and `_copy` as much as currently possible.

Source compatibility requires that `_copy` remain in the stdlib.  It is
deprecated here and just uses the copy operator.

Handling old swiftinterfaces requires that `Builtin.copy` be defined.
Redefine it here as a passthrough--SILGen machinery will produce the
necessary copy_addr.

rdar://127502242
2024-05-03 15:56:25 -07:00
Erik Eckstein
e14c1d1f62 SIL, Optimizer: update and handle borrowed-from instructions
Compute, update and handle borrowed-from instruction in various utilities and passes.
Also, used borrowed-from to simplify `gatherBorrowIntroducers` and `gatherEnclosingValues`.
Replace those utilities by `Value.getBorrowIntroducers` and `Value.getEnclosingValues`, which return a lazily computed Sequence of borrowed/enclosing values.
2024-04-10 13:38:10 +02:00
Andrew Trick
bac1dc9699 Cleanup LifetimeDependenceUtils comments. 2024-04-03 10:46:09 -07:00
Andrew Trick
fbae21ff21 LifetimeDependence: handle dependent values in throwing calls.
Fixes rdar://125564278 (~Escapable: crash in LifetimeDependenceInsertion)
2024-04-03 10:46:09 -07:00
Andrew Trick
1b6be740e8 Fix LifetimeDependenceDefUseWalker for address yields.
Do not treat address yields as escapes.

Fixes rdar://125752476 (`UnsafeRawPointer` property in non-escapable type doesn't compile)
2024-04-01 22:16:45 -07:00
Andrew Trick
a0b2ae9c2c Add AccessBase.storeBorrow.
Don't treat StoreBorrow addresses as unknown bases. While they are never the base of a formal access, they are returned
as the AccessBase when querying the enclosing scope of an address.
2024-03-22 11:51:58 -07:00
Andrew Trick
f037b635fb LifetimeDependenceUtils: cleanup Scope initialization.
And fix a couple latent bugs.
2024-03-22 11:51:58 -07:00
Andrew Trick
b7eaee9448 Disable verbose LifetimeDependenceUtils 2024-03-18 17:45:28 -07:00
Andrew Trick
5e9e282b62 LifetimeDependenceUtils comment clarification and cleanup. 2024-03-18 17:38:12 -07:00
Andrew Trick
95a100a9c0 Add a comment in LifetimeDependenceUseDefWalker. 2024-03-18 17:38:12 -07:00
Andrew Trick
df7326d790 Fix SILType::isEscapable for box types.
SILBoxTypes have their own generic signature and substitution
map. This means that every time we query isEscapable or mayEscape, we
need to extract the type of the box's field and perform type
substitution so that the AST query only sees types from the function's
generic environment.

Fixes rdar://124179106 (Assertion failed in SIL:
(!type->hasTypeParameter() && "caller forgot to mapTypeIntoContext!"))
2024-03-07 13:34:46 -08:00
Andrew Trick
3bee32639b LifetimeDependenceDefUseWalker: use LocalVariableReachableUses. 2024-03-05 18:08:16 -08:00
Andrew Trick
a3fe978cc4 LifetimeDependenceUtils: handle borrowed ProjectBox 2024-03-05 17:08:13 -08:00
Andrew Trick
ef940c5297 Redesign gatherBorrowIntroducers to return BeginBorrowValues.
This avoids a lot of confusion because the callers expect this type. Fixing it just required some redundancy and
bridging in the EnclosigValues implementation.
2024-03-05 17:08:13 -08:00
Erik Eckstein
8137adf89e SwiftCompilerSources: support Undef.parentFunction and PlaceholderValue.parentFunction
After support in the C++ SIL, we can implement parentFunction for those values in swift, too.
It simplifies a few things.
2024-03-01 09:07:12 +01:00
Andrew Trick
a7e4e900c4 Fix LifetimeDependenceUtils to use OwnershipTransitionInstruction 2024-02-26 00:20:46 -08:00
Meghana Gupta
12ac8042ab Handle YieldInst in LifetimeDependenceUtils 2024-02-19 15:28:04 -08:00
Andrew Trick
41043948c1 LifetimeDependenceDiagnostics: enable mutably borrowed lifetimes.
This allows basic prototyping. A separate analysis will ensure no
mutation of the original value during the borrow.
2024-02-15 02:39:34 -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
Andrew Trick
f0b04004ff LifetimeDepenenceUtils; improve VariableIntroducerUseDefWalker
Stop at formal access to a mutable variable.
2024-02-13 20:57:02 -08:00
Andrew Trick
1cdf9d56ac LifetimeDefUseWalker handles inherited dependencies.
This is needed when we don't insert mark_dependence.
2024-02-12 20:05:24 -08:00
Andrew Trick
2e7c29cdf5 Restrict findSingleInitializer @out arguments.
The client should handle @in/@inout arguments differently.
2024-02-12 20:03:38 -08:00
Andrew Trick
af39b0fb1f LifetimeDependenceUtils improvements
Improve noescape function handling. Use the mayEscape API.

Improve handling of @_unsafeNonescapableResult.

Improve handling of initialized scopes. Use findSingleInitializer.
2024-02-12 09:57:14 -08:00
Andrew Trick
ac457abab9 LifetimeDependenceUseDefWalker: track the owner of the value. 2024-02-12 09:57:14 -08:00