Commit Graph

11812 Commits

Author SHA1 Message Date
Andrew Trick
16699e8110 Merge pull request #87015 from atrick/fix-ossa-rauw
Another fix for the OSSA RAUW utility for unowned phis
2026-02-06 14:13:26 -08:00
Meghana Gupta
617deca5ea Merge pull request #87011 from meg-gupta/oslogopt
Fix OSLogOptimization for complete lifetimes in SIL
2026-02-06 09:12:58 -08:00
Meghana Gupta
280057b8d8 Fix OSLogOptimization for complete lifetimes in SIL 2026-02-05 17:01:40 -08:00
Andrew Trick
2120c0502a Another fix for the OSSA RAUW utility for unowned phis
Fixes rdar://169556118 (Assertion: (succ->getSinglePredecessorBlock() == predBB)
function visitInsertionPoints at ValueLifetime.cpp:37.)
2026-02-05 15:01:11 -08:00
Erik Eckstein
cbf1c24233 LoopUnroll: avoid iterating over an llvm::DenseMap
Use `llvm::MapVector` instead.
Fixes a potential non-determinism bug.
2026-02-05 13:32:11 +01:00
eeckstein
dcad02e22b Merge pull request #86979 from eeckstein/di-closures
DefiniteInitialization: handle inout-self uses when analyzing closures
2026-02-05 07:27:04 +01:00
Michael Gottesman
82fa88a855 [rbi] Fix logic around tracking of Sendable/non-Sendable field projections from Sendable/non-Sendable values
Previously, region-based isolation was not diagnosing cases where a non-Sendable
value projected from a Sendable base that was MainActor isolated. For example:

```swift
@MainActor
struct MainActorBox {
  var value: NonSendableKlass?

  mutating func take() -> sending NonSendableKlass {
    if let value {
      self.value = nil
      return value // Should warn: main actor-isolated value cannot be 'sending'
    } else {
      preconditionFailure("Consumed twice")
    }
  }
}
```

This was caused by two issues:

1. A logic bug in `AddressBaseComputingVisitor::visitAll` where we overwrote an
   already-found Sendable value when recursing. The visitor should only record
   the first Sendable value found, not subsequent ones. This caused us to
   incorrectly return the `@MainActor` `MainActorBox` as the base instead of
   emitting a require for the projected value.

2. struct_element_addr being transparent unconditionally causing us to not even
   emit a require at all.

This commit has two parts:

1. I fixed the first two issues by fixing the logic bug and by making
   struct_element_addr only transparent if its operand and result are
   non-Sendable. I added logic that we have used in other such cases to handle
   non-Sendable operand/Sendable result as well as Sendable operand and
   non-Sendable result. I then added the same support for tuple_element_addr and
   unchecked_take_enum_data_addr since they suffered from a similar problem.

2. Adding the logic to handle variants where the operand was non-Sendable and
   the result was Sendable, caused a bunch of tests to break so I had to fix
   that.

To fix the broken test cases, I had to make the compiler smarter about how it
was inserting this require. To do this:

1. I checked if the access base of the projection was actually immutable or if
   we are an alloc_stack that there was a prefix path in the projection path of
   immutable projections that end in the alloc_stack. In such a case, we know
   that we do not need to require that the operand is available in the current
   isolation domain since we will never write to that piece of memory and once
   we have loaded the result from memory, we know that the value is Sendable so
   nothing bad can happen.

2. If the access base of the projection was mutable, I used the same logic that
   we used for alloc_box that were non-Sendable that stored a Sendable thing by
   changing operand to be a `require [mutable_base_of_sending]` on the result of
   the projection instead of requiring the projection's operand. This ensures
   that we handled important flow sensitive cases.

rdar://169626088
2026-02-04 09:33:58 -08:00
Erik Eckstein
259f5403b8 DefiniteInitialization: handle inout-self uses when analyzing closures
Fixes a false "used before being initialized" error when using self-mutating functions inside conditional operators (`&&`, `||`) in initializers.
This is a follow-up on https://github.com/swiftlang/swift/pull/35276.

rdar://168784050
2026-02-04 15:04:37 +01:00
Meghana Gupta
c47610bf3b Merge pull request #86947 from meg-gupta/bailoutforeachloopunroll
Bailout of ForEachLoopUnroll pass when sources of element stores cannot be extended
2026-02-04 02:59:05 -08:00
Michael Gottesman
52a33beec0 Merge pull request #86946 from gottesmm/pr-fb033d6ec27597054d09c768a52a3a97dd3d81e0
[rbi] Use indirect assignment for stores to no-alias destinations to fix a common data race hole
2026-02-03 21:21:43 -08:00
Michael Gottesman
192344076f [rbi] Use translateSILAssignIndirect for stores to no-alias destinations
For stores to unaliased, non-aggregate-projected destinations, switch from
translateSILAssignDirect(destValue, src) to translateSILAssignIndirect(dest, src).
This passes the Operand* rather than just the SILValue, enabling proper tracking
of the destination address in region analysis.

First in a series migrating instruction handlers to indirect assignment.

NOTE: I originally thought that pack_element_set would also have this property,
but alloc_pack today is always assumed to be to be MayAlias and thus we emit a
merge. I think this is fine since one can never actually assign over a pack
variable like one can other things (that is I think they are generally just used
for marshalling and the like). If I am wrong, I can just fix it later.

rdar://156024613
https://github.com/swiftlang/swift/issues/83121
2026-02-03 09:00:27 -08:00
Meghana Gupta
fbaa30b426 Bailout of ForEachLoopUnroll pass when sources of element stores cannot be extended 2026-02-02 16:13:10 -08:00
Michael Gottesman
33f2172761 [rbi] Update partial_apply translation to use new translateSILMultiAssign signature
Explicitly pass an empty indirect results array to translateSILMultiAssign when
translating partial_apply instructions, since partial_apply does not produce
indirect results. This transitions the call site from the backward-compatible
overload to the full signature introduced in bc8eadad12f.
2026-02-02 10:38:03 -08:00
Michael Gottesman
2016d952bb [rbi] Make select_enum and select_enum_addr both assign instructions.
I looked at the implementation and it was basically an assign. There is no
reason not to just eliminate the special implementation.
2026-02-02 10:38:03 -08:00
Michael Gottesman
460c2edf30 [rbi] Rename TranslationSemantics::Assign{,Direct}.
Just a refactoring commit to make other later commits easier to review.
2026-02-02 10:38:03 -08:00
Michael Gottesman
2a33ce59ef [rbi] Get rid of the last invocation of translateSILAssignDirect(SILInstruction *inst).
This is important to do since we want to ensure that when new instructions are
added, users have to think about their direct or indirect results.
2026-02-02 10:38:03 -08:00
Michael Gottesman
79723769b0 [rbi] Add translateSILAssignIndirect helpers for single-destination indirect parameter assigns
Add two template overloads of translateSILAssignIndirect that wrap
translateSILMultiAssign for the common case of assigning to a single
indirect parameter destination:

1. A generic template accepting any collection of source operands
2. A specialization for a single source operand

These simplify call sites that only need to handle one destination.
2026-02-02 10:38:03 -08:00
Michael Gottesman
5fd48879e3 [rbi] Extend translateSILMultiAssign to handle indirect result addresses
Add a new IndirectResultsRangeTy template parameter and indirectResultAddresses
parameter to translateSILMultiAssign, allowing it to process both direct result
values and indirect result operands.

I also added overload that takes only direct results for backward
compatibility. I am going to get rid of this eventually once we have finished
the transition.
2026-02-02 10:38:03 -08:00
Michael Gottesman
d1239facc9 [rbi] Refactor addAssignFresh so it can process arbitrary ranges of TrackableValues.
It previously just accepted ArrayRef<TrackableValue>. I am going to be using
this with a transform + concat range in a subsequent change to pass a combined
ArrayRef<TrackableValue> + ArrayRef<std::pair<Operand, TrackableValue>>.second.
2026-02-02 10:38:03 -08:00
Michael Gottesman
05af162ded [rbi] Rename translateSILAssign to translateSILAssignDirect in preparation for adding translateSILAssignIndirect 2026-02-02 10:38:03 -08:00
Erik Eckstein
5cd70731da LifetimeCompletion/DiagnoseUnreachable: fix a wrong "will never be executed" warning
Make sure that debug locations are correct so that instruction which are inserted by DI after a no-return call doesn't cause DiagnoseUnreachable to print a wrong warning.
Also, add `extend_lifetime` to the list of excluded instructions in DiagnoseUnreachable.
2026-01-30 11:52:49 +01:00
Alejandro Alonso
0c67ab0fe1 Merge pull request #86860 from Azoy/change-inline-bb-ownership
[SILOptimizer] Update the inliner to account for different result ownerships
2026-01-29 08:47:41 -08:00
Meghana Gupta
16a2d7e18d Merge pull request #86812 from meg-gupta/fixsimplifycfg
Fix SimplifyCFG::simplifyTryApplyBlock for owned arguments in ossa
2026-01-29 00:16:30 -08:00
eeckstein
ad787dc6d4 Merge pull request #86840 from eeckstein/deinit-opts
Optimizer: fix two problems related to value-type deinits
2026-01-29 07:33:36 +01:00
Meghana Gupta
1144aa33d4 Fix SimplifyCFG::simplifyTryApplyBlock for owned arguments in ossa 2026-01-28 19:21:18 -08:00
Alejandro Alonso
1e85809f32 Update the inliner to account for different result ownerships 2026-01-28 15:11:34 -08:00
Hamish Knight
0f968df26e Merge pull request #86791 from hamishknight/compat-hack
[CS] Add `for` loop compatibility hack for `makeIterator`/`next` ranking
2026-01-28 22:47:56 +00:00
Erik Eckstein
144b4b910d SROA: don't try to destructure a non-copyable type with a deinit
This check was only done for the top-level alloc_stack, but not for any nested types.

rdar://169022052
2026-01-28 13:10:30 +01:00
Hamish Knight
1bff51d40d [AST] NFC: Introduce isAtLeastFutureMajorLanguageMode
And use it for clients checking for the next future major language mode.
2026-01-27 23:30:43 +00:00
Alejandro Alonso
a4fdb7d9ec Simplify the identity for dereference borrows 2026-01-27 11:04:25 -08:00
Alejandro Alonso
9f95dc7695 Simplify init_borrow_addr when the borrow type is statically known
make sure we register as well as add test
2026-01-27 11:03:53 -08:00
eeckstein
9297de8d9e Merge pull request #86781 from eeckstein/fix-ownership-verifier
SIL: fix a complexity problem in the ownership verifier
2026-01-27 14:35:25 +01:00
Meghana Gupta
501eada8ef [NFC] Reduce indentation in SimplifyCFG::simplifyTryApplyBlock 2026-01-27 05:18:58 -08:00
eeckstein
d01f9da13b Merge pull request #86788 from eeckstein/check-incremental-2
CSE/check_incremental: add some more logging to detect non-determinisms in CSE
2026-01-27 12:42:53 +01:00
Erik Eckstein
4a1950d292 SIL: fix a complexity problem in the ownership verifier
Computing dominance relation between instructions in the same block was done with linear search, e.g. when checking if a value-use is before its lifetime ending instruction.
This resulted in quadratic complexity and very long compile times for very large basic blocks.
Now we do the dominance check with pre-computed instruction indices, which is O(0) instead of O(n).

https://github.com/swiftlang/swift/issues/86663
rdar://168511262
2026-01-27 08:56:13 +01:00
Joe Groff
5d95daae39 Remove unused variable 2026-01-26 12:28:49 -08:00
Joe Groff
0f3ddfbcc8 Merge pull request #86545 from jckarter/builtin-borrow
`Builtin.Borrow` implementation
2026-01-26 07:32:31 -08:00
Erik Eckstein
903142d7cf CSE/check_incremental: add some more logging to detect non-determinisms in CSE 2026-01-26 15:43:49 +01:00
Meghana Gupta
a2a2374410 Merge pull request #86744 from meg-gupta/dcefix
Remove unnecessary code from DeadCodeElimination
2026-01-26 06:42:52 -08:00
Andrew Trick
eb012174e8 Merge pull request #86767 from atrick/fix-ossa-rauw
Fix the OSSA RAUW utility for unowned phis
2026-01-24 09:24:03 -08:00
elsa
5e9f215f31 Merge pull request #86010 from elsakeirouz/rework-for-each-desugar
Rework ForEachStmt Desugaring
2026-01-24 13:55:51 +00:00
Andrew Trick
96bc9aa90c Fix the OSSA RAUW utility for unowned phis
The OwnershipRAUW utility is called by CSE, SILCombine, etc. whenever OSSA
values are substituted or combined. It handles ownership corner cases by
creating new copies. Destroys need to be insert for those new copies after all
original uses. It is impossible to do that when phis are involved. The utility
already checks for phis involving owned or guaranteed values, but unowned phis
were not anticipated.

Fixes rdar://168620481 swift-frontend crash: While running pass
SILFunctionTransform "GenericSpecializer"
2026-01-23 21:56:30 -08:00
eeckstein
19b52bd3b7 Merge pull request #86748 from eeckstein/check_incremental
check_incremental: better diagnostics for non-determinism checks
2026-01-24 03:30:48 +01:00
eeckstein
06f7bea2ba Merge pull request #86746 from eeckstein/harden_asserts
SIL: replace some `assert`s with `ASSERT`s
2026-01-23 23:48:07 +01:00
Meghana Gupta
bfdfe8f948 Remove DCE support for end_lifetime 2026-01-23 14:04:12 -08:00
eeckstein
be5e89a9bb Merge pull request #86702 from eeckstein/mandatory-inliner
Optimizer: fix a few problems where destroys are illegally hoisted across deinit barriers
2026-01-23 20:35:31 +01:00
Erik Eckstein
58c46dc025 PassManager: add an option -sil-print-pass-md5 to print MD5 hashes of SIL after each pass
With this option an MD5 hash of the SIL is printed after each function or module pass - if the pass has changed the SIL.
This is useful for finding non-determinisms in the optimizer.
2026-01-23 19:20:20 +01:00
Erik Eckstein
49df9fae70 SIL: replace some asserts with ASSERTs
To catch those errors also in a non-assert build of the compiler.
I recently investigated a compiler crash with a non-assert compiler and would have found the root cause much faster if those asserts would have triggered.
2026-01-23 18:39:33 +01:00
Meghana Gupta
13a044dc05 [NFC] Remove unused variable 2026-01-23 09:07:05 -08:00
Meghana Gupta
db77c83cad Remove unnecessary calls to DCE::endLifetimeOfLiveValue 2026-01-23 09:01:49 -08:00