Commit Graph

7601 Commits

Author SHA1 Message Date
Mishal Shah
c110c9570f Merge pull request #83809 from finagolfin/cross-tools
[6.2.1][build] Make it possible to build a cross-compilation toolchain for Android, including Testing
2025-09-18 09:10:48 -07:00
eeckstein
e35a45f7b7 Merge pull request #83965 from eeckstein/fix-simplify-alloc-stack-apply-6.2.1
[6.2.1] Optimizer: fix handling of dependent existential archetypes in `alloc_stack` and `apply` simplification
2025-09-04 17:18:22 +02:00
eeckstein
d7ff11b2c4 Merge pull request #83966 from eeckstein/fix-combine-apply-of-convertfunction-6.2.1
[6.2.1] SILCombine: fix `convert_function` -> `apply` peephole for generic function types
2025-09-04 10:25:54 +02:00
Nate Chandler
5851dcb971 [MemoryLifetimeVerifier] Permit leaks in dead-ends 2025-08-28 19:08:41 -07:00
Nate Chandler
eb9f5b2a92 [AllocBoxToStack] Don't destroy in dead-ends.
It is valid to leak a value on paths into dead-end regions.
Specifically, it is valid to leak an `alloc_box`.  Thus, "final
releases" in dead-end regions may not destroy the box and consequently
may not release its contents.  Therefore it's invalid to lower such final
releases to `dealloc_stack`s, let alone `destroy_addr`s.  The in-general
invalidity of that transformation results in miscompiling whenever a box
is leaked and its projected address is used after such final releases.

Fix this by not treating final releases as boundary markers of the
`alloc_box` and not lowering them to `destroy_addr`s and
`dealloc_stack`s.

rdar://158149082
2025-08-28 19:08:41 -07:00
Erik Eckstein
36d2549bf4 SILCombine: fix convert_function -> apply peephole for generic function types
Currently we cannot deal with generic arguments/returns. Bail in this case.

Fixes a compiler crash
rdar://158809851
2025-08-28 07:29:53 +02:00
Erik Eckstein
fb80d0349c Optimizer: fix handling of dependent existential archetypes in alloc_stack and apply simplification
When replacing an opened existential type with the concrete type, we didn't consider that the existential archetype can also be a "dependent" type of the root archetype.
For now, just bail in this case. In future we can support dependent archetypes as well.

Fixes a compiler crash.
rdar://158594365
2025-08-28 07:17:54 +02:00
finagolfin
2de7f9427a [build][android] Use a CMake toolchain file to cross-compile Testing (#83260)
Also, disable a recently failing test for Android armv7.
2025-08-19 18:45:31 +05:30
finagolfin
d746436d94 Merge pull request #82986 from finagolfin/droid
[6.2][android] Small test and build tweaks
2025-07-18 15:22:47 +05:30
Michael Gottesman
182149978e [concurrency] Make optimize hop to executor more conservative for 6.2 around caller isolation inheriting functions.
Specifically for 6.2, we are making optimize hop to executor more conservative
around caller isolation inheriting functions. This means that we are:

1. No longer treating calls to caller isolation inheriting functions as having a
hop in their prologue. In terms of this pass, it means that when determining
dead hop to executors, we no longer think that a caller isolation inheriting
function means that an earlier hop to executor is not required.

2. Treating returns from caller isolation inheriting callees as requiring a
hop. The reason why we are doing this is that we can no longer assume that our
caller will hop after we return.

Post 6.2, there are three main changes we are going to make:

* Forward Dataflow

Caller isolation inheriting functions will no longer be treated as suspension
points meaning that we will be able to propagate hops over them and can assume
that we know the actor that we are on when we enter the function. Practically
this means that trees of calls that involve just nonisolated(nonsending) async
functions will avoid /all/ hop to executor calls since we will be able to
eliminate all of them since the dataflow will just propagate forward from the
entrance that we are already on the actor.

* Backwards Dataflow

A caller isolation inheriting call site will still cause preceding
hop_to_executor functions to be live. This is because we need to ensure that we
are on the caller isolation inheriting actor before we hit the call site. If we
are already on that actor, the hop will be eliminated by the forward pass. But
if the hop has not been eliminated, then the hop must be needed to return us to
the appropriate actor.

We will also keep the behavior that returns from a caller isolation inheriting
function are considered to keep hop to executors alive. If we were able to
propagate to a hop to executor before the return inst with the forward dataflow,
then we know that we are guaranteed to still be on the relevant actor. If the
hop to executor is still there, then we need it to ensure that our caller can
treat the caller isolation inheriting function as a non-suspension point.

rdar://155905383
(cherry picked from commit b3942424c8)
2025-07-15 17:32:19 -07:00
Meghana Gupta
8f00bc4fe8 Bailout from an illegal transformation in semantic arc opts
tryJoinIfDestroyConsumingUseInSameBlock replaces a copy with its operand
when there is no use of the copy's operand between the copy's forwarded consuming use
and the copy operand's destroy in the same block. It is illegal to do this transformation
when there is a non-consuming use of the copy operand after the forwarded consuming use of the copy.

The code checking this illegal case was not considerin the case where the consuming use of the copy
was in the same instruction as the non-consuming use of the copy operand.

rdar://154712867
2025-07-15 03:41:05 -07:00
Meghana Gupta
cfe2a7030f Merge pull request #82963 from meg-gupta/disablefso2
[6.2] Disable function signature optimization on functions with lifetime dependencies
2025-07-14 15:36:46 -07:00
Michael Gottesman
187b1eedf7 Merge pull request #82776 from gottesmm/release/6.2-154139237
[6.2] [nonisolated-nonsending] Make the AST not consider nonisolated(nonsending) to be an actor isolation crossing point.
2025-07-14 13:19:55 -07:00
Meghana Gupta
6e2065106e Merge pull request #82974 from meg-gupta/disablerrnccp
[6.2] Disable retain and release sinking when rc root values are ~Copyable
2025-07-14 12:48:27 -07:00
finagolfin
21d90c5a41 [test] Fix or disable tests for 32-bit platforms (#82501)
Fix two IRGen tests that are failing on Android armv7 and disable eight ClangImporter, C++ Interop, and SILOptimizer tests, two of which that were already failing on other 32-bit platforms.
2025-07-11 16:20:08 +05:30
Meghana Gupta
212e55e055 [6.2] Fix SILCombine of inject_enum_addr when the enum is non-trivial but payload is trivial 2025-07-10 17:17:25 -07:00
Meghana Gupta
5c85c65278 Disable retain and release sinking when rc root values are ~Copyable 2025-07-10 16:13:13 -07:00
Meghana Gupta
1e6f93e0e1 Disable function signature optimization on functions with lifetime dependencies
If a function has lifetime dependencies, disable FSO's dead param optimization. Dead params maybe dependency sources and we should not delete them. It is also problematic to dead code params that are not dependency sources, since lifetime dependent sources are stored as indices and deleting dead parameters will require recomputation of these indices.
2025-07-10 11:35:23 -07:00
Michael Gottesman
731d605950 Update two tests for 4c1440735b8066d061c668d3b655955c1831f1dd.
I cherry-picked 4c1440735b8066d061c668d3b655955c1831f1dd to prevent merge
conflicts. I think these tests were updated by a subsequent PR. Rather than
cherry-picking more work, I just updated these tests since the change seemed
benign and that this kept the overall amount of cherry-picked commits small
(converting %$NUMBER to %kind$NUMBER for some ValueDecls).
2025-07-09 12:25:06 -07:00
John McCall
c0db6b64f6 Merge pull request #82868 from rjmccall/dont-capture-defer-isolation-6.2
[6.2] Don't force a capture of an isolated parameter in defer bodies
2025-07-08 11:58:33 -04:00
John McCall
fb9e730fc9 Don't force a capture of an isolated parameter in defer bodies.
SILGen already has an exception for this from -enable-actor-data-race-checks,
so there's no need for it, and it causes problems in actor inits.

Fixes rdar://155239032
2025-07-07 23:30:22 -04:00
Andrew Trick
4c115f140b Fix a move-checker diagnostic message (textual change)
The move-checker was assuming that any non-Copyable variable in a box must be
captured by a closure. The underlying problem is that the move-checker relies on
the best-effort AllocBoxToStack optimization to be perfect. But when
non-Escapable values depend on the variable, it remains boxed. That's good for
lifetime diagnostics but caused an incorrect move-checker diagnostic.

Fixes rdar://154519148 (Returning non-copyable type after accessing borrowed
field emits incorrect error about escaped closure capturing the noncopyable)

(cherry picked from commit 1b2fc8cbf9)
2025-07-07 17:47:00 -07:00
Andrew Trick
d03fd679b8 Merge pull request #82797 from atrick/62-local-deadend
[6.2] Improve LocalVariableUtils.gatherKnownLifetimeUses; dead ends
2025-07-07 08:17:15 -07:00
eeckstein
092441e60a Merge pull request #82799 from eeckstein/fix-escape-utils-6.2
[6.2] EscapeUtils: consider that a pointer argument can escape a function call
2025-07-07 16:04:57 +02:00
Erik Eckstein
972fde7e70 LetPropertyLowering: remove redundant phis after ssa-update
This is needed after running the SSAUpdater, because the updater can insert unnecessary phis in the middle of the original liverange of a value.

Fixes an ownership error.
rdar://153229472
2025-07-04 20:37:45 +02:00
Erik Eckstein
fc7c9931b3 EscapeUtils: consider that a pointer argument can escape a function call
Unlike addresses of indirect arguments, a pointer argument (e.g. `UnsafePointer`) can escape a function call.
For example, it can be returned.

Fixes a miscompile
rdar://154124497
2025-07-04 11:56:03 +02:00
nate-chandler
dd6813f1b8 Merge pull request #82788 from nate-chandler/cherrypick/release/6.2/rdar153693915
6.2: [Mem2Reg] Don't promote proj(unchecked_addr_cast).
2025-07-04 00:20:11 -07:00
Andrew Trick
9f7bf2a507 Improve LocalVariableUtils.gatherKnownLifetimeUses; dead ends
Add a fake use for dead-end blocks. This allows gatherKnownLifetimeUses to be
used for local liveness by considering an "unreachable" instruction to generate
liveness. This is important when liveness is used as a boundary within which
access scopes may be extended. Otherwise, we are unable to extend access scopes
into dead-end blocks.

Fixes rdar://154406790 (Lifetime-dependent variable 'X' escapes its
scope but only if actor/class is final)

(cherry picked from commit 239255b8bc)
2025-07-03 21:34:25 -07:00
Nate Chandler
211299d4d2 [Mem2Reg] Don't promote proj(unchecked_addr_cast).
In OSSA, the result of an `unchecked_bitwise_cast` must immediately be
copied or `unchecked_bitwise_cast`'d again.  In particular, it is not
permitted to borrow it.  For example, the result can't be borrowed for
the purpose of performinig additional projections (`struct_extract`,
`tuple_extract`) on the borrowed value.  Consequently, we cannot promote
an address if such a promotion would result in such a pattern.  That
means we can't promote an address `%addr` which is used like
`struct_element_addr(unchecked_addr_cast(%addr))` or
`tuple_element_addr(unchecked_addr_cast(%addr))`.  We can still promote
`unchecked_addr_cast(unchecked_addr_cast(%addr))`.

In ownership-lowered SIL, this doesn't apply and we can still promote
address with such projections.

rdar://153693915
2025-07-03 15:32:41 -07:00
Erik Eckstein
a538252a77 MoveOnlyWrappedTypeEliminator: handle EndCOWMutationAddr
fixes a compiler crash
rdar://154416511
2025-07-03 13:10:00 +02:00
nate-chandler
9fd0e92f7b Merge pull request #82737 from nate-chandler/cherrypick/release/6.2/rdar154499140
6.2: [Test] Re-disable test in asserts builds.
2025-07-02 21:56:22 -07:00
Nate Chandler
99e66c51a9 6.2: [Test] Re-disable test in asserts builds.
The test was reenabled for the `swift_stdlib_asserts` context in
https://github.com/swiftlang/swift/pull/82559/ .  That had the effect of
enabling it for noasserts builds.  But the test uses subpass flags which
are only heeded in asserts builds.  That behavior is disabled--the flags
are made always effective--in
https://github.com/swiftlang/swift/pull/82736 .  But there's no reason
to take that change onto a release branch.  Just disable the test again
in exactly the relevant configuration.

rdar://154499140
2025-07-02 12:14:15 -07:00
Nate Chandler
b730144497 [CSE] Fix combine of type_value.
The instruction has no operands but has a param type.  Hash and compare
the type and the type and the param type.

rdar://154652254
2025-07-01 14:30:59 -07:00
Andrew Trick
9b66d7c7d3 Merge pull request #82603 from atrick/62-local-switch
[6.2] Fix LocalVariableUtils switch_enum_addr.
2025-06-30 10:22:03 -07:00
eeckstein
8c47b59d7d Merge pull request #82560 from eeckstein/fix-closure-specializer-6.2
[6.2] ClosureSpecializer: don't specialize captures of stack-allocated Objective-C blocks
2025-06-30 18:18:10 +02:00
eeckstein
f5f058f49d Merge pull request #82565 from eeckstein/fix-mandatory-perfopt-6.2
[6.2] MandatoryPerformanceOptimization: don't recursive into referenced functions if they are not called
2025-06-28 19:21:50 +02:00
Andrew Trick
43193b6a4d 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)

(cherry picked from commit fe9c0dd735)
2025-06-28 09:30:55 -07:00
nate-chandler
9b8e749027 Merge pull request #82573 from nate-chandler/cherrypick/release/6.2/rdar154407327
6.2: [DestroyAddrHoisting] Don't fold into read access.
2025-06-27 19:35:58 -07:00
Nate Chandler
9acdc921e0 [DestroyAddrHoisting] Don't fold into read access.
Narrowly fix a long-standing bug where destroy_addrs would be hoisted
into read access scopes, leaving the scope as a read despite the fact
that it modifies memory.  This is a problem for utilities which expect
access scopes to provide correct summaries.  Do this by refusing to fold
into access scopes which are marked `[read]`.

In a follow-up, we can reenable this folding, promoting each access
scope to a modify.  Doing so requires checking that there are no access
scopes which overlap with any of the access scopes which would be
promoted to modify.

rdar://154407327
2025-06-27 11:54:19 -07:00
Erik Eckstein
995264913f ClosureSpecializer: don't specialize captures of stack-allocated Objective-C blocks
Bail if the closure captures an ObjectiveC block which might _not_ be copied onto the heap, i.e optimized by SimplifyCopyBlock.
We can't do this because the optimization inserts retains+releases for captured arguments.
That's not possible for stack-allocated blocks.

Fixes a mis-compile
rdar://154241245
2025-06-27 20:08:51 +02:00
Erik Eckstein
8758b557fa MandatoryPerformanceOptimization: don't recursive into referenced functions if they are not called
Fixes a false compiler error when referencing a function from a global with a section attribute.

rdar://154332540
2025-06-27 16:09:53 +02:00
Erik Eckstein
728f37abf0 SemanticARCOpts: don't ignore dead-end blocks in the liverange analysis
This can result in wrong ARC optimizations in dead-end blocks.
Fixes a SIL verification error.

rdar://154356277
2025-06-27 07:03:23 +02:00
Andrew Trick
d3002ed0b2 [nonescapable] remove '@_lifetime' requirement on implicit accessors
This avoids diagnostic errors on synthesized accessors, which are impossible for developers to understand.

Fixes rdar://153793344 (Lifetime-dependent value returned by generated accessor '_read')

(cherry picked from commit 855b3e4446)
2025-06-24 15:06:30 -07:00
Andrew Trick
2052d2c61e Fix MoveOnlyObjectCheckerPImpl::check() for mark_dependence.
Handle the presence of mark_dependence instructions after a begin_apply.

Fixes a compiler crash:
"copy of noncopyable typed value. This is a compiler bug. ..."

(cherry picked from commit 7a29d9d8b6)
2025-06-24 12:54:39 -07:00
Andrew Trick
1fa15f23fe Fix SILCombine of MarkDependenceInst.
Do not eliminate a mark_dependence on a begin_apply scope even though the token
has a trivial type.

Ideally, token would have a non-trivial Builtin type to avoid special cases.

(only relevant on 6.2)
2025-06-24 12:54:39 -07:00
Meghana Gupta
1d1f9b063f Merge pull request #82431 from meg-gupta/lifetimeenumcp
[6.2] Add lifetime dependencies on function types representing ~Escapable enum elements with ~Escapable payloads
2025-06-24 11:06:27 -07:00
Andrew Trick
2c98863499 Fix MutableSpan exclusive access to unsafe pointers
This fix enables exclusive access to a MutableSpan created from an UnsafeMutablePointer.

The compiler has a special case that allows MutableSpan to depend on a mutable
pointer *without* extending that pointer's access scope. That lets us implement
standard library code like this:

    mutating public func extracting(droppingLast k: Int) -> Self {
      //...
      let newSpan = unsafe Self(_unchecked: _pointer, byteCount: newCount)
      return unsafe _overrideLifetime(newSpan, mutating: &self)

Refine this special case so that is does not apply to inout parameters where the
programmer has an expectation that the unsafe pointer is not copied when being
passed as an argument. Now, we safely get an exclusivity violation when creating
two mutable spans from the same pointer field:

    @lifetime(&self)
    mutating func getSpan() -> MutableSpan<T> {
      let span1 = makeMutableSpan(&self.pointer)
      let span2 = makeMutableSpan(&self.pointer) // ERROR: overlapping access
      return span1
    }

If we don't fix this now, it will likely be source breaking in the future.

Fixes rdar://153745332 (Swift allows constructing two MutableSpans to
the same underlying pointer)

(cherry picked from commit 7c5d4b8b6d)

--- CCC ---

Explanation: Fix MutableSpan exclusive access to unsafe pointers This
fix enables exclusive access to a MutableSpan created from an
UnsafeMutablePointer.

Scope: Affects users of MutableSpan when initializing them from an unsafe pointer.

Radar/SR Issue: rdar://153745332 (Swift allows constructing two
MutableSpans to the same underlying pointer)

main PR: https://github.com/swiftlang/swift/pull/82450

Risk: Low. This only affects users of an API that requires lifetime
dependence. Without using an experimental feature, this only applies
to the initializers of Mutable[Raw]Span.

Testing: Added source-level unit tests

Reviewer: TBD
2025-06-24 00:18:29 -07:00
Andrew Trick
3dd1fb2518 Fix lifetime dependence diagnostics on Void types.
Allow a dependence on Void to be considered immortal. This is the ultimate
override in cases where no other code pattern is supported yet.

(cherry picked from commit 36d2b5bee4)
2025-06-24 00:11:47 -07:00
Meghana Gupta
45af6d647f [NFC] Add tests 2025-06-23 13:49:03 -07:00
Tim Kientzle
3bacc327e9 Merge pull request #82186 from finagolfin/droid
[6.2][android] Fix the remaining tests and add a build flag that enables standalone cross-compilation SDKs
2025-06-23 11:41:14 -07:00