Commit Graph

909 Commits

Author SHA1 Message Date
Andrew Trick
c891d8ade4 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)
2025-04-29 15:36:48 -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
cbf5c196b2 SIL: rename protocol BorrowIntroducingInstruction -> BeginBorrowInstruction
to match with `BeginBorrowValue`
2025-04-28 10:07:45 +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
8a48cd979b 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.
2025-04-25 18:52:20 -07:00
Andrew Trick
4f470a1d34 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)
2025-04-25 17:58:23 -07:00
Andrew Trick
92b2a8587f Fix LifetimeDependenceDiagnostics: handle mark_dependence_addr
A small typo meant that scoped dependencies modeled with mark_dependence_addr
were not diagnosed.
2025-04-25 17:58:23 -07:00
Andrew Trick
060364546a [NFC] LifetimeDependenceUtils comment 2025-04-25 17:58:23 -07:00
Andrew Trick
bb9e52c3f2 LifetimeDependenceScopeFixup: cleanup for clarity 2025-04-25 17:58:23 -07:00
Andrew Trick
b4b93ef34c Merge pull request #81061 from atrick/rdar147500193-vector-expectEquals
Fix LifetimeDependenceDiagnostics: handle address-only 'let's
2025-04-24 11:59:48 -07:00
Andrew Trick
5831777407 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)
2025-04-23 22:48:27 -07:00
eeckstein
8226f20ddb Merge pull request #80912 from eeckstein/copyaddr-in-rle
RedundantLoadElimination: support replacing a redundant `copy_addr` with a `store`
2025-04-23 20:59:03 +02:00
Erik Eckstein
8cf4e34cc1 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-23 11:57:28 +02:00
Andrew Trick
3b08592ed2 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)
2025-04-22 22:35:09 -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
Erik Eckstein
6c31eb0c43 embedded: rewrite the diagnostic pass for embedded swift
1. move embedded diagnostics out of the PerformanceDiagnostics pass. It was completely separated from the other logic in this pass, anyway.
2. rewrite it in swift
3. fix several bugs, that means: missed diagnostics, which led to IRGen crashes
  * look at all methods in witness tables, including base protocols and associated conformances
  * visit all functions in the call tree, including generic functions with class bound generic arguments
  * handle all instructions, e.g. concurrency builtins
4. improve error messages by adding meaningful call-site information. For example:
  * if the error is in a specialized function, report where the generic function is originally specialized with concrete types
  * if the error is in a protocol witness method, report where the existential is created
2025-04-18 06:58:40 +02:00
Erik Eckstein
d222cf20f1 MandatoryPerformanceOptimizations: support default methods for class existentials
For example:
```
protocol P: AnyObject {
  func foo()
}
extension P {
  func foo() {}
}
class C: P {}

let e: any P = C()
```

Such default methods are SILGen'd with a generic self argument. Therefore we need to specialize such witness methods, even if the conforming type is not generic.

rdar://145855851
2025-04-18 06:58:40 +02:00
Erik Eckstein
655dae825a Optimizer: add Options.noAllocations 2025-04-18 06:58:39 +02:00
Erik Eckstein
f0f1c3c84b SIL: make SILFunctionConvention constructible without needing a SIL function
Add an initializer which takes the `hasLoweredAddresses` directly and add `Context.moduleHasLoweredAddresses`
2025-04-18 06:58:38 +02:00
Erik Eckstein
75152f27be Swift AST: make Conformance hashable and rename var proto -> var protocol 2025-04-18 06:58:38 +02:00
Erik Eckstein
1c9a7cd562 SwiftCompilerSources: refactor DiagnosticEngine
* move it from the SIL to the AST module (where it belongs)
* change the signature of `diagnose` from `diagnose(location, .some_error)` to `diagnose(.some_error, at: location)`
* add an overload to allow passing a `SIL.Location` directly to `diagnose`
* add a `Diagnostic : Error` utility struct which allows throwing a `Diagnostic`
2025-04-18 06:58:38 +02:00
Andrew Trick
6f8605263d Fix unidentified LifetimeDependence.Scope initialization.
Fixes rdar://149226564 (Compiler crash with non-escapable storing another
non-escapable and having a deinit)
2025-04-16 16:46: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
4e35d255ad 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.
2025-04-16 16:46:38 -07:00
Andrew Trick
b80bd16eea [NFC] Add LifetimeDependenceUseDefWalker utility.
Refactor VariableIntroducerUseDefWalker into a general
LifetimeDependenceUseDefWalker for use with LifetimeDependenceScopeFixup.
2025-04-16 16:43:23 -07:00
Andrew Trick
43debc529f [NFC] minor cleanup 2025-04-16 16:43:23 -07:00
Andrew Trick
28daec01a7 Add InstructionRange.overlapsWithPath() utility. 2025-04-16 16:43:23 -07:00
Andrew Trick
2bdaa68e98 SwiftCompilerSources: add IgnoredUseInst as incidental
Fixes rdar://148540048 (Assigning span value to `_` results in an incorrect escape diagnostic)
2025-04-16 16:42:36 -07:00
eeckstein
a9c6da6c70 Merge pull request #80819 from eeckstein/copy-block-optimization
Optimizer: remove redundant `copy_block` instructions
2025-04-15 20:17:02 +02:00
Anton Korobeynikov
87ca0f8680 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
2025-04-15 07:14:44 -07:00
Erik Eckstein
199eb1d6e7 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-15 11:02:39 +02:00
Andrew Trick
4b5d5197ae Merge pull request #80782 from atrick/rdar143992296-addressliverange
Fix AddressOwnershipLiveRange to include the full range.
2025-04-12 15:02:35 -07:00
Andrew Trick
c9279d9899 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)
2025-04-11 23:49:07 -07:00
Meghana Gupta
2c462ba368 Merge pull request #80733 from meg-gupta/updatectb
Handle a special case of borrowed from instruction in CopyToBorrowOptimization
2025-04-11 21:12:54 -07:00
Meghana Gupta
fe780b670f Handle a special case of borrowed from instruction in CopyToBorrowOptimization 2025-04-11 13:21:22 -07:00
Andrew Trick
ccb26bcd89 LocalVariableUtils: add unit tests 2025-04-10 23:27:55 -07:00
Andrew Trick
37ecd0e9bd 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.
2025-04-10 23:27:55 -07:00
Andrew Trick
6e51dd8534 Merge pull request #80628 from atrick/address-escape-effect
Fix escape analysis: addressable parameters.
2025-04-08 13:40:02 -07:00
Andrew Trick
05a9acbb40 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.
2025-04-08 00:07:30 -07:00
Slava Pestov
ec82f6bf4d SIL: Remove SILWitnessTable::AssociatedConformanceWitness::SubstType 2025-04-07 21:36:06 -04:00
Erik Eckstein
f5ca12d42b Simplification: check for isolated conformances when simplifying metatype casts
rdar://147417762
2025-04-05 09:00:30 +02:00
Erik Eckstein
f8d6efd485 SimplifyAllocStack: bail if the address of the allocated existential escapes
This is not a problem if the alloc_stack already contains the opened existential.
Fixes a miscompile
rdar://148253142
2025-04-03 23:04:49 +02:00
Erik Eckstein
1100bf9bfb SimplifyAllocStack: rename a variable and update a comment
NFC
2025-04-03 06:58:00 +02:00
Erik Eckstein
ee18f8c168 Simplification: fix a crash in alloc_stack simplification
Handle the case that a type dependent operand is a dynamic-self argument and not an open-existential instruction.

rdar://148258964
2025-04-01 10:58:45 +02:00
Artem Chikin
66334f8f1b Merge pull request #79290 from artemcm/ConstProtoSIL
[Compile Time Values] Implement a mandatory SIL pass to verify '@const' values
2025-04-01 00:12:27 -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
Artem Chikin
281f84da0f [Compile Time Values] Rewrite the 'Diagnose Unknown Compile Time Values' diagnostic pass in Swift 2025-03-28 10:30:07 -07:00
Artem Chikin
72a420919a [Compile Time Values] Add mandatory optimization pipeline driver for '@const' globals 2025-03-27 14:33:38 -07:00
Artem Chikin
d484ec7c1f [Compile Time Values] Implement a mandatory SIL pass to verify '@const' values 2025-03-27 14:33:35 -07:00