Commit Graph

1173 Commits

Author SHA1 Message Date
Adrian Prantl
11356acd44 Merge pull request #85397 from adrian-prantl/163167975
[SILOptimzer] Fix a crash caused by SILCombine mishandling inlined variables
2025-11-10 18:41:07 -08:00
Meghana Gupta
e2123e1b3b Merge pull request #85362 from meg-gupta/moreborrow
Add SIL verification for borrow and mutate accessors and some other minor fixes
2025-11-10 09:04:28 -08:00
Adrian Prantl
99cf35cdce [SILOptimzer] Fix a crash caused by SILCombine mishandling inlined variables
This showed up on and off again on the source-compatibility testsuite project hummingbird.

The gist of the problem is that transformations may not rewrite the
type of an inlined instance of a variable without also createing a
deep copy of the inlined function with a different name (and e.g., a
specialization suffix). Otherwise the modified inlined variable will
cause an inconsistency when later compiler passes try to create the
abstract declaration of that inlined function as there would be
conflicting declarations for that variable.

Since SILDebugScope isn't yet available in the SwiftCompilerSources
this fix just drop these variables, but it would be absolutely
possible to preserve them by using the same mechanism that SILCloner
uses to create a deep copy of the inlined function scopes.

rdar://163167975
2025-11-07 17:06:33 -08:00
Erik Eckstein
62786b01e2 Optimizer: add the mandatory destroy hoisting pass
It hoists `destroy_value` instructions for non-lexical values.

```
  %1 = some_ownedValue
  ...
  last_use(%1)
  ... // other instructions
  destroy_value %1
```
->
```
  %1 = some_ownedValue
  ...
  last_use(%1)
  destroy_value %1    // <- moved after the last use
  ... // other instructions
```

In contrast to non-mandatory optimization passes, this is the only pass which hoists destroys over deinit-barriers.
This ensures consistent behavior in -Onone and optimized builds.
2025-11-06 21:00:44 +01:00
Erik Eckstein
eef769c83a Optimizer: when checking for lexical lifetimes look through all ownership-transitioning instructions
except `copy_value`
2025-11-06 20:26:20 +01:00
Meghana Gupta
a0eb58fa07 Handle return_borrow in a few more places in SwiftCompilerSources 2025-11-06 10:55:31 -08:00
Andrew Trick
382529c73b Merge pull request #85232 from atrick/rdar159793739-lifedep-cast
LifetimeDependenceDiagnostics: handle dynamic casting of Span<T>
2025-10-31 08:35:36 -07:00
Andrew Trick
5943d40edf LifetimeDependenceDiagnostics: handle dynamic casting of Span<T>
Add a special case for checked_cast_addr_br instruction. If it conformed to
SourceDestAddrInstruction, then the diagnostics would already have handled it
naturally, but the instruction's conditional semantics are strange enough that
such a conformance might confuse other passes.

rdar://159793739 (Using `as?` with non-escapable types emits faulty lifetime
diagnostics)
2025-10-30 23:28:25 -07:00
Andrew Trick
e11f7f1f11 LifetimeDependenceDiagnostics: diagnostic error on unknown uses
The common def-use walkers should not return .abortWalk without making a
diagnostic callback.
2025-10-30 23:28:25 -07:00
Aidan Hall
8632e58825 Create Pack Specialisation pass 2025-10-30 14:28:16 +00:00
Aidan Hall
c7af4c584e Bridging: APIs for PackSpecialization pass 2025-10-26 13:44:34 +00:00
Andrew Trick
5494a24899 Merge pull request #85076 from atrick/lifedep-reassign
Lifetimes: handle MutableSpan reassignment and 'inout' arguments
2025-10-24 09:55:34 -07:00
Andrew Trick
305d75187a LocalVariableUtils: precisely handle function live-out
Only record an outgoingArgument access when the current allocation is an
outgoing argument and the function exiting instruction is ReturnInst.

This avoids invalid SIL where we attempt to extend end_access up to an `unwind` but
fail to actually materialize that end_access.
2025-10-23 23:34:26 -07:00
Andrew Trick
62b04caaa7 LocalVariableUtils add non-escaping closure capture support
Don't always consider an inout_aliasable argument to have
escaped. AccessEnforcementSelection has already done that analysis and left
begin_access [dynamic] artifacts if the argument has escaped in any meaningful
way. Use that information to

Essential for supporting autoclosures that call mutating methods on span-like
things. Such as UTF8Span.UnicodeScalarIterator.skipForward():

e.g. while numSkipped < n && skipForward() != 0 { ... }
2025-10-23 23:34:25 -07:00
Andrew Trick
8200a871c9 Fix LifetimeDependenceDefUseWalker to follow inout dependence
Fixes rdar://157796728 ([nonescapable] [miscompile] No diagnostic error when an
inout MutableSpan is reassigned to a different lifetime source)
2025-10-23 23:34:25 -07:00
Andrew Trick
77ee27a0e7 [NFC] LocalVariableAccessInfo.description 2025-10-23 23:34:25 -07:00
Andrew Trick
de34abe3e5 Fix LocalVariableReachableAccess to handle potential reassignment.
Define LocalAccessInfo._isFullyAssigned to mean that the access does not read
the incoming value. Then treat any assignment that isn't full as a potential read.
2025-10-23 23:34:25 -07:00
Andrew Trick
6e0eeb00e3 Fix LifetimeDependenceDefUseWalker for @inout reassignment 2025-10-23 23:34:24 -07:00
Andrew Trick
ff4d053a44 [NFC] LifetimeDependenceDefUseWalker.inoutDependence entry point
Handle cases where there's no relevant operand.
2025-10-23 23:34:24 -07:00
Andrew Trick
a0f0c108b0 [NFC] Add a new entry point for DiagnoseDependence.reportError
Allow diagnosing values that have no relevant users. Such as an @inout argument.
2025-10-23 23:34:24 -07:00
Andrew Trick
ce153a85ea [NFC] extend AddressInitializationWalker to report address reads 2025-10-23 23:34:24 -07:00
Andrew Trick
432e31612a [NFC] Extend AddressInitializationWalker.findSingleInitializer
Handle applies that reassign the lifetime of their operand vs. the value of
their operand.
2025-10-23 23:34:24 -07:00
Andrew Trick
3bb3e4d077 [NFC] Add ApplySite.parameterDependence(target:source:) 2025-10-23 23:34:23 -07:00
Erik Eckstein
6988b03c13 ClosureSpecialization: don't crash for recursive functions
Don't support self-recursive functions because that would result in mapping troubles when cloning.
2025-10-24 06:11:53 +02:00
Erik Eckstein
53f424e5f0 TempRValueOptimization: respect deinit-barriers when hoisting destroys
Fixes a mis-compile which causes a deinit to be called earlier than expected.

rdar://162299994
2025-10-23 20:45:11 +02:00
Erik Eckstein
309b93ca15 Optimizer: add the isBarrierForDestroy utility
It checks if a destroy of a type must not be moved across an instruction.
2025-10-23 20:45:11 +02:00
eeckstein
0e4dc2fe2e Merge pull request #85068 from eeckstein/fix-licm
LoopInvariantCodeMotion: don't extend "read" access scopes over memory writes to the same address
2025-10-23 07:25:38 +02:00
Arnold Schwaighofer
7f36b58942 Merge pull request #85009 from aschwaighofer/embedded_existentials_feature_flag
Add experimental feature flag EmbeddedExistentials
2025-10-22 19:04:42 -07:00
Erik Eckstein
5b8a1a7b68 LoopInvariantCodeMotion: don't extend "read" access scopes over memory writes to the same address.
We must not do this even if the write is _not_ in an access scope, because this would confuse alias analysis.

Fixes a mis-compile.
2025-10-22 20:55:22 +02:00
Erik Eckstein
dafb43b124 LoopInvariantCodeMotion: a small code cleanup
NFC
2025-10-22 20:55:22 +02:00
Arnold Schwaighofer
a59f6c4a3c Fix formatting 2025-10-21 07:54:55 -07:00
eeckstein
9074a1eec3 Merge pull request #85005 from eeckstein/remove-copy-forwarding
Optimizer: remove the CopyForwarding pass
2025-10-21 07:24:42 +02:00
Erik Eckstein
dbeda9612c TempRValueElimination: handle debug_value instructions 2025-10-20 20:19:54 +02:00
Erik Eckstein
62c3850d55 TempRValueElimination: allow elimination of dead alloc_stacks in non-OSSA 2025-10-20 20:19:54 +02:00
Erik Eckstein
8618a39ac5 TempRValueElimination: support consuming indirect apply operands 2025-10-20 20:19:53 +02:00
Arnold Schwaighofer
22c8ea6af6 Add experimental feature flag EmbeddedExistentials
This flag can be used to gradually add the functionility that will allow use of
protocol values in embedded mode.
2025-10-20 10:18:45 -07:00
Meghana Gupta
0bd6825aa2 Update SIL utilities for borrow and mutate accessors under opaque values mode 2025-10-20 09:05:38 -07:00
Erik Eckstein
62e2871b2f MandatoryPerformanceOptimizations: run DeadStoreElimination for global init functions.
Cleanup leftovers from simplification.
The InitializeStaticGlobals pass cannot deal with dead stores in global init functions.
2025-10-17 20:25:19 +02:00
Erik Eckstein
794964adba MandatoryRedundantLoadElimination: remove redundant loads from global variables
This is important in the initializer functions of globals. It can enabled static initialization of globals in more cases.
2025-10-17 20:25:19 +02:00
Erik Eckstein
110010856f AliasAnalysis: handle debug_step in computeMemoryEffect
The `debug_step` instruction does not have a memory effect on any specific address
2025-10-17 20:25:18 +02:00
Daniil Kovalev
75f80410e6 Run closure specialization only if ownership info is present in callee (#84956)
Calling `cloneRecursively` from `SpecializationInfo.cloneClosures`
requires the callee having ownership info. Otherwise, the cloner uses
`recordFoldedValue` instead of `recordClonedInstruction`, and
`postProcess` hook is not called, which leads to an assertion failure in
`BridgedClonerImpl::cloneInst`.
2025-10-17 10:51:57 -07:00
Erik Eckstein
610539a85f SIL: streamline Operand Sequence APIs
* remove `filterUsers(ofType:)`, because it's a duplication of `users(ofType:)`
* rename `filterUses(ofType:)` -> `filter(usersOfType:)`
* rename `ignoreUses(ofType:)` -> `ignore(usersOfType:)`
* rename `getSingleUser` -> `singleUser`
* implement `singleUse` with `Sequence.singleElement`
* implement `ignoreDebugUses` with `ignore(usersOfType:)`

This is a follow-up of eb1d5f484c.
2025-10-16 10:12:33 +02:00
Erik Eckstein
82bffd3b21 Simplification: fold tuple - copy_value - destructure_tuple sequences
We already do this for structs.
Also, refactor the simplification to share the logic between structs and tuples
2025-10-14 10:20:17 +02:00
Erik Eckstein
5104c317ca TempRValueElimination: fix a problem with non-copyable types
In case of a non-copyable type the final destroy (or take) of a stack location can be missing if the value has only trivial fields.
The optimization inserted a `destroy_addr` in this case although it wasn't there before.

Beside fixing this problem I also refactored the code a bit to make it more readable.
2025-10-10 21:37:27 +02:00
Andrew Trick
d653b0ccd0 Merge pull request #84739 from atrick/lifedep-unsafeaddress
LifetimeDependenceDefUseWalker: store to unsafeMutableAddress
2025-10-09 18:40:32 -07:00
Erik Eckstein
181b2f1f6d Optimizer: eliminate struct_extracts of an owned struct where the struct_extracts are inside a borrow scope
This is done by splitting the `begin_borrow` of the whole struct into individual borrows of the fields (for trivial fields no borrow is needed).
And then sinking the `struct` to it's consuming use(s).

```
  %3 = struct $S(%nonTrivialField, %trivialField)  // owned
  ...
  %4 = begin_borrow %3
  %5 = struct_extract %4, #S.nonTrivialField
  %6 = struct_extract %4, #S.trivialField
  use %5, %6
  end_borrow %4
  ...
  end_of_lifetime %3
```
->
```
  ...
  %5 = begin_borrow %nonTrivialField
  use %5, %trivialField
  end_borrow %5
  ...
  %3 = struct $S(%nonTrivialField, %trivialField)
  end_of_lifetime %3
```

This optimization is important for Array code where the Array buffer is constantly wrapped into structs and then extracted again to access the buffer.
2025-10-08 17:48:37 +02:00
Andrew Trick
4378995a78 LifetimeDependenceDefUseWalker: store to unsafeMutableAddress
Handle storing to a mutable property implemented as unsafeMutableAddress. In
SIL, the stored address comes from pointer_to_address. Recognize the addressor
pattern and handle the store as if it writes to a regular property of 'self'.

Required for UnsafePointer<~Escapable>.pointee.
2025-10-07 13:22:27 -07:00
eeckstein
757de6e2a6 Merge pull request #84711 from eeckstein/fix-licm
LoopInvariantCodeMotion: fix check for hoisting `load_borrow` instructions
2025-10-07 11:40:29 +02:00
eeckstein
401a2ac2bc Merge pull request #84704 from eeckstein/closure-specialization
ClosureSpecialization: support for OSSA and a big overhaul
2025-10-07 06:59:08 +02:00
Andrew Trick
c2171ea9b0 Merge pull request #84703 from atrick/lifedep-diag-init
Lifetimes: add a diagnostic note for implicit accessors
2025-10-06 12:29:51 -07:00