Commit Graph

6583 Commits

Author SHA1 Message Date
Nate Chandler
5851dcb971 [MemoryLifetimeVerifier] Permit leaks in dead-ends 2025-08-28 19:08:41 -07:00
Michael Gottesman
dcb3e5c48e [concurrency] Emit nonisolated(nonsending) async throw initializers correctly.
Specifically, we were not inserting the implicit isolated parameter and were not
setting up the actor prologue. To keep this specific to nonisolated(nonsending)
code, I only setup the actor prologue if we know that we have something that is
nonisolated(nonsending).

I also ported some async initializer tests to run with/without
nonisolated(nonsending) just to increase code coverage.

rdar://156919493
(cherry picked from commit 3871d22257)
2025-07-29 20:10:44 -07:00
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
Joe Groff
6f4748c5ef SILGen: Bitcast indirect returns that differ only in concurrency annotations.
A call to a `@preconcurrency` function goes through a function conversion
that removes `Sendable` from existentials among other things. Implement
support for this by bitcasting indirect return slots whose type differs
from the formal indirect return type in concurrency markings only.

Fixes rdar://154240007
2025-07-09 07:56:23 -07:00
Jamie
11525cabf8 [SILOptimizer]: slow OSSA lifetime canonicalization mitigation
OSSA lifetime canonicalization can take a very long time in certain
cases in which there are large basic blocks. to mitigate this, add logic
to skip walking the liveness boundary for extending liveness to dead
ends when there aren't any dead ends in the function.

Updates `DeadEndBlocks` with a new `isEmpty` method and cache to
determine if there are any dead-end blocks in a given function.

(cherry picked from commit 1f3f830fc7)
2025-06-22 18:08:06 -05:00
Meghana Gupta
d5b82a3b7c [6.2] Fix an inliner crash when inlining begin_apply with scoped lifetime dependence 2025-06-13 14:18:29 -07:00
Erik Eckstein
0bdd827237 GenericSpecializer: remove some kind of instructions if their operands become trivial after specialization
Fixes a SIL verifier crash.

https://github.com/swiftlang/swift/issues/82093
rdar://152807200
2025-06-11 17:31:27 +02:00
Nate Chandler
5af84375f2 [MemAccessUtils] Add to strip access storage casts
The new function stripAccessAndAccessStorageCasts is analogous to the
existing function stripAccessAndIdentityCasts but differs in that the
latter uses isAccessStorageIdentityCast whereas the new function uses
isAccessStorageCast.
2025-06-03 15:54:11 -07:00
Nate Chandler
f99d542351 [MemAccessUtils] This instruction changes the type
moveonlywrapper_to_copyable_box changes the type so it's a type cast not
an identity cast.
2025-06-03 15:54:11 -07:00
Nate Chandler
c894bdffab [NFC] SIL: This utility takes a func not a module.
In preparation to use the function in the implementation.
2025-05-30 13:27:45 -07:00
Erik Eckstein
30e138c48b SIL: define mark_dependence_addr to read and write to its address operand
This prevents simplification and SILCombine passes to remove (alive) `mark_dependence_addr`.
The instruction is conceptually equivalent to
```
  %v = load %addr
  %d = mark_dependence %v on %base
  store %d to %addr
```

Therefore the address operand has to be defined as writing to the address.
2025-05-23 07:53:14 +02:00
Michael Gottesman
63a436ec04 Merge pull request #81615 from gottesmm/release/6.2-rdar151394209
[6.2] Fix a few issues around nonisolated(nonsending) and  protocol witness thunks/vtable thunks
2025-05-22 00:04:01 -07:00
Joe Groff
6aea9660ac SIL: Resilient types don't need to be treated as addressable-for-dependencies inside their resilience domain.
Outside of the resilience domain, they have to be treated as opaque and therefore potentially
addressable-for-dependencies, but inside of the resilience domain, we may take advantage of
knowing the type layout to load indirect parameters out of memory and break the (unnecessary)
dependency on a fixed memory location. Fixes rdar://151268401.

We do still however have problems when the type is actually `@_addressableForDependencies`
inside of its resilience domain (rdar://151500074). I'll fix that in a follow up.
2025-05-19 18:04:17 -07:00
Michael Gottesman
d3145b2380 [silgen] Begin placing isolation on protocol witness thunks.
This will cause tests today to crash since even though we are placing the
isolation now, to make it easier to read, I left in the old isolation selecting
code. This code uses the witness's isolation instead of the requirement's
isolation which is incorrect since the protocol witness thunk needs to look the
requirement from an ABI perspective since the two must be substitutable. The
crash comes from the ABI verification I added in earlier commits.

(cherry picked from commit ff1cbea576)
2025-05-19 10:33:46 -07:00
Erik Eckstein
21303a5d3d SIL: Make the verifier's ImmutableAddressUseVerifier generally available
Also, make it more tolerant to instructions and builtins, which are not explicitly handled.
This avoids crashes when new instructions are added. We got lucky that this didn't happen so far.
2025-05-14 07:09:32 +02:00
Erik Eckstein
2f34dae56f Swift SIL: add Builder.createUncheckedOwnershipConversion 2025-05-06 17:57:37 +02:00
Meghana Gupta
cc72edb119 Introduce end_cow_mutation_addr instruction 2025-04-30 14:38:48 -07:00
Andrew Trick
7756a322fa LifetimeDependenceDiagnostics: note for unsupported closure capture
Add a note explaining that dependence on closure captures is not
supported. Otherwise, the diagnostics are very confusing:
"it depends on a closure capture; this is not yet supported"

(cherry picked from commit 83b0ce1098)
2025-04-30 00:09:40 -07:00
Andrew Trick
9b9a4056bd [NFC] SwiftCompilerSource: bridge Function.accessorKindName
(cherry picked from commit 25e9cbf3f1)
2025-04-30 00:09:39 -07:00
Nate Chandler
b794e21dd3 [SIL] Add open_existential_addr.accessKind setter. 2025-04-23 13:28:49 -07:00
Nate Chandler
ce510590d9 NFC: [OSSACompleteLifetime] Allow forced verifying 2025-04-11 17:24:50 -07:00
Nate Chandler
cb0dafba55 [Gardening] OSSACompleteLifetime: Add slash to doc 2025-04-11 17:24:50 -07:00
Andrew Trick
78c9fe4c56 Add SILFunctionType::isAddressable & ApplySite::isAddressable.
(cherry picked from commit 501abb0975)
2025-04-08 14:02:19 -07:00
nate-chandler
88fb10ed3b Merge pull request #80589 from nate-chandler/cherrypick/release/6.2/rdar148744816
6.2: [SIL] Fix bridged begin_apply results.
2025-04-07 16:46:48 -07:00
Nate Chandler
b7ca5672c5 [SIL] Fix bridged begin_apply results.
The `yield_once_2` adds an extra result at the end, the deallocation.
Fix the indexing for the token and yielded results.
2025-04-07 07:36:43 -07:00
Erik Eckstein
8f2c17a149 CastOptimizer: pass the SIL function to the cast-classify APIs, instead of the module decl
NFC
2025-04-07 10:58:02 +02:00
Erik Eckstein
6987748517 SIL: add matchesActorIsolation
Returns true if the conformance is not isolated or if its isolation matches the isolation `inFunction`.
2025-04-07 10:58:02 +02:00
Erik Eckstein
f4955978d8 SIL: fix AllocStackInst.isLexical
If wrongly returned false.
2025-04-03 23:04:49 +02:00
Erik Eckstein
0b9f5eb86c SIL: add some asserts to catch broken enum ASTs 2025-04-03 08:14:42 +02:00
Joe Groff
562d7dc832 Merge pull request #80438 from jckarter/substitute-away-escapable-lifetime-deps
Type substitution eliminates dependencies with Escapable targets.
2025-04-02 16:56:54 -07:00
Joe Groff
6b605f41cb Type substitution eliminates dependencies with Escapable targets.
When a generic function has potentially Escapable outputs, those outputs
declare lifetime dependencies, which have no effect when substitution
leads to those types becoming `Escapable` in a concrete context.
This means that type substitution should canonically eliminate lifetime
dependencies targeting Escapable parameters or returns, and that
type checking should allow a function value with potentially-Escapable
lifetime dependencies to bind to a function type without those dependencies
when the target of the dependencies is Escapable.

Fixes rdar://147533059.
2025-04-02 08:54:45 -07: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
Shubham Sandeep Rastogi
65a515b2d4 Merge pull request #80079 from rastogishubham/EnhanceDump
Add dump overloads to print debug info for SIL.
2025-03-31 11:41:55 -07:00
Michael Gottesman
918756f854 Merge pull request #80374 from gottesmm/pr-ee297e6b432cd07483ec86444ffd6a8b5334c203
[concurrency] Fix a few issues with @execution(caller)/@execution(concurrent).
2025-03-31 09:20:01 -07:00
Michael Gottesman
f245389bb3 [concurrency] Fix a few issues with @execution(caller)/@execution(concurrent).
Specifically:

1. I made it so that thunks from caller -> concurrent properly ignore the
isolated parameter of the thunk when calling the concurrent function.

rdar://148112362

2. I made it so that thunks from concurrent -> caller properly create a
Optional<any Actor>.none and pass that into the caller function.

rdar://148112384

3. I made it so that in cases where we are assigning an @Sendable caller to a
non-sendable caller variable, we allow for the conversion as long as the
parameters/results are sendable as well.

rdar://148112532

4. I made it so that when we generate a thunk from @execution(caller) ->
@GlobalActor, we mangle in @GlobalActor into the thunk.

rdar://148112569

5. I discovered that due to the way we handle function conversion expr/decl ref
expr, we were emitted two thunks when we assigned a global @caller function to a
local @caller variable. The result is that we would first cast from @caller ->
@concurrent and then back to @caller. The result of this would be that the
@caller function would always be called on the global queue.

rdar://148112646

I also added a bunch of basic tests as well that showed that this behavior was
broken.
2025-03-30 20:00:05 -07:00
Allan Shortlidge
43fcb2cecd SIL/SILOptimizer: Fix unused variable warnings. 2025-03-28 12:33:39 -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
Doug Gregor
e0b52cd20e [SIL] Extend checked-cast instructions with "prohibit isolated conformances" flag
When performing a dynamic cast to an existential type that satisfies
(Metatype)Sendable, it is unsafe to allow isolated conformances of any
kind to satisfy protocol requirements for the existential. Identify
these cases and mark the corresponding cast instructions with a new flag,
`[prohibit_isolated_conformances]` that will be used to indicate to the
runtime that isolated conformances need to be rejected.
2025-03-26 22:31:47 -07:00
Michael Gottesman
de7a62e13e Merge pull request #80312 from gottesmm/pr-126bc735b2d01c0f2f35f27268ff26d404b2fb16
[sil] Make SILFunctionTypeInfo a struct enum.
2025-03-26 18:05:57 -07:00
Shubham Sandeep Rastogi
b016c742c6 Add dump overloads to print debug info for SIL.
This patch adds dump overload methods to SILInstruction, SILBasicBlock,
SILFunction, and SILModule that prints the debug information as well.
2025-03-26 10:47:45 -07:00
Andrew Trick
97b249bd11 Merge pull request #80263 from atrick/markdep-addr
SIL: add mark_dependence_addr
2025-03-26 10:33:42 -07:00
Michael Gottesman
8cfb029b5c [sil] Make SILFunctionTypeInfo a struct enum.
I am doing this in preparation for adding the ability to represent in the SIL
type system that a function is global actor isolated. Since we have isolated
parameters in SIL, we do not need to represent parameter, nonisolated, or
nonisolated caller in the type system. So this should be sufficient for our
purposes.

I am adding this since I need to ensure that we mangle into thunks that convert
execution(caller) functions to `global actor` functions what the global actor
is. Otherwise, we cannot tell the difference in between such a thunk and a thunk
that converts execution(caller) to execution(concurrent).
2025-03-26 10:23:44 -07:00
John McCall
4da795fd61 Merge pull request #80297 from rjmccall/zero-objc-async-result-on-error-path
Pass a zero normal result to ObjC async completion handlers on the error path
2025-03-26 12:42:38 -04:00
Erik Eckstein
85a6df30d6 SIL: add lookup(method:) in VTable and WitnessTable 2025-03-26 08:45:23 +01:00
Erik Eckstein
1844c14c44 SIL: add var ClassMethodInst.member 2025-03-26 08:45:07 +01:00
Erik Eckstein
67556cfbf4 SIL: add some Function APIs
* `var wasDeserializedCanonical`
* `var genericSignature`
* `func mapTypeIntoContext`
* `var forwardingSubstitutionMap`
2025-03-26 08:45:07 +01:00
Erik Eckstein
1ce545ba3d SIL: add some DeclRef APIs
* `var decl`
* `func ==`
* `func calleesAreStaticallyKnowable`
2025-03-26 08:43:55 +01:00
Erik Eckstein
d523e303c3 SIL/AST: move some SIL.Type APIs to the TypeProperties protocol which makes them also available for AST.Type and AST.CanonicalType 2025-03-26 07:30:10 +01:00