Commit Graph

604 Commits

Author SHA1 Message Date
Kavon Farvardin 5d7d187bfb SILGen: adjust special case in expandInnerTupleOuterIndirect
The hack added here doesn't quite work right, so let's throw another on top.

ResultPlanner::expandInnerTupleOuterIndirect's narrow whole-tuple-into-opaque guard
to redirect to planSingleIntoIndirect checked only AllInnerResults.size() == 1,
so when the dispatcher reached the function recursively from expandParallelTuples
for an empty-tuple element, it mis-claimed the lone residual inner slot, which
actually belonged to a sibling tuple element, and the real later element then
tripped claimNext on an empty list.

If we just ensure the types are the same, we can avoid that.

resolves rdar://178736957
2026-06-05 14:54:44 -07:00
Slava Pestov 1cca97c5c2 SILGen: Re-organize a bit of code to avoid an ExistentialLayout::getSuperclass() call 2026-05-27 23:06:48 -04:00
Ben Cohen 6b9d5d5255 SILGen: fix reabstraction crash for typed-throws closure into generic @out (#88926)
When passing a `(T) throws(E) -> R` closure to a parameter of type `(T)
throws -> U` where `U` is a generic type variable substituted to `R`
(which is a tuple — possibly `Void`), SILGen builds a reabstraction
thunk whose inner result is `@out R` and whose outer result is `@out T`
(opaque archetype that happens to substitute to `R`).

`ResultPlanner::expandInnerTupleOuterIndirect` dispatches between
"single-slot injection" (optional/existential) and "parallel tuple walk"
based on whether `outerSubstType` is a tuple — but that's the wrong
dimension. The abstraction pattern determines how the value is *passed*,
not the substituted type. When `outerOrigType` is opaque the outer is a
single `@out` slot regardless of the substituted type.

Routing to the parallel tuple walk caused:
- Empty-tuple case: the walk iterates 0 times, leaves the inner `@out`
slot unclaimed, and trips `assert(AllInnerResults.empty())` in
`ResultPlanner::plan`. (Previously crashed in IRGen's
`SyncCallEmission::setArgs` as `SmallVector grow_pod` before the
IR-level symptom was fixed.)
- Non-empty-tuple case: the walk tries to claim per-element slots that
don't exist and trips the `claimNext` assertion.

Detect the "inner has a single whole-tuple indirect slot, outer is
opaque" case in `expandInnerTupleOuterIndirect` and dispatch to
`planSingleIntoIndirect`. Gate on `AllInnerResults.size() == 1` so the
fix does not fire when the inner has per-element slots (where the
parallel walk's per-element claims remain correct).

A more principled fix dispatches the whole function on
`outerOrigType.isTuple()` and handles 1-to-N result-slot mappings — left
as a FIXME for a separate change.

Fixes https://github.com/swiftlang/swift/issues/77880. Fixes
https://github.com/swiftlang/swift/issues/88027.
2026-05-24 07:00:56 -07:00
Konrad Malawski 003028c17c more rename followups for isNonisolated and the enum change 2026-04-28 09:21:23 -07:00
Konrad Malawski 77ac31d946 Rename to ActorIsolation::Kind::Nonisolated*Concurrent* 2026-04-28 09:21:23 -07:00
Konrad Malawski 3beefe5bd8 Rename CallerIsolationInheriting -> NonisolatedNonsending 2026-04-28 09:21:23 -07:00
Pavel Yaskevich 60ea598e7f [SIL] Set actor isolation when constructing/initializing SILFunction
Prevents situations when actor isolation ends up not being set
un-intentionally i.e. when cloning, specializating, or creating
thunks.

The thunks get `unspecified` isolation at the moment.
2026-04-21 16:03:35 -07:00
Doug Gregor f2eb7cb1a8 [SIL] Model @export(interface) and @export(implementation) on SIL functions
The `@export(interface)` and `@export(implementation)` attributes
SE-0497 are queried directly on AST nodes in several places within the
SIL pipeline. However, they don't persist when SIL functions are
serialized, meaning that clients of the original module might make
different assumptions about the availability of a given function's
definition.

Represent these attributes in a SIL function (as an optional
CodeGenerationModel), (de-)serialize them into the module, and add a
textual representation as SIL function attributes `[export_interface]`
and `[export_implementation]`.
2026-04-15 13:04:10 -07:00
Pavel Yaskevich 1e7c38082d [SILGen] Forwarding arguments to withoutActuallyEscaping thunk should account for implicit isolation parameter
This was already half done by adding the parameter to the arguments
but the flag to skip it while matching was missing.

Resolves: https://github.com/swiftlang/swift/issues/87537
Resolves: rdar://171274515
2026-04-02 16:32:51 -07:00
Slava Pestov 9ea7587a3f SILGen: Workaround for mismatch in expected level of opaque archetype erasure when emitting function conversions
The TypeExpansionContext of a function that emits a reabstraction thunk
might differ from the TypeExpansionContext of the thunk itself.

This seems to cause a problem in one case at least, when emitting a
function conversion from () -> some P to () -> any P where we can see the
underlying type of the some P, we end up with a mismatch where the SIL type
of the value was unwrapped, but the lowering of the formal type in the context
of the thunk remains an opaque archetype.

One of the examples in the test was uncovered by binding inference changes
producing a different but equivalent constraint system solution, but the
remaining cases were already broken prior.

My change here is just a horrible workaround for this mismatch; the
representational problem needs to be addressed properly elsewhere.

Fixes rdar://172130660.
2026-03-20 14:51:05 -04:00
Meghana Gupta 085ea1aaf7 Merge pull request #87347 from meg-gupta/diagnoseborrowreabs
Add additional diagnostics for borrow/mutate accessors
2026-02-20 02:37:40 -08:00
Meghana Gupta 1959e0b23e Add a diagnostic when borrow protocol requirement cannot be witnessed 2026-02-18 15:56:36 -08:00
Anton Korobeynikov eb067cb455 Ensure we are adding T : Differentiable conformance from protocol conditional conformance (#77446)
Fixes #75711
2026-02-17 20:56:03 -08:00
Kavon Farvardin ae77ba81e3 Merge pull request #87013 from kavon/revert-84789
Back out #84789 after additional testing
2026-02-06 02:58:00 -08:00
Kavon Farvardin d5f9a1eeeb Revert "Merge pull request #84789 from nickolas-pohilets/mpokhylets/fix-82618"
This reverts commit b633bd37ac, reversing
changes made to b27bb64b03.
2026-02-05 11:34:59 -08:00
Meghana Gupta 0fb21c9156 Update unchecked_ownership insertion in the protocol witness of borrow accessors 2026-02-04 07:16:31 -08:00
Meghana Gupta 4c18d2ebdc Merge pull request #86496 from meg-gupta/borrowprotopr
Add support for borrow and mutate protocol constraints
2026-01-21 10:11:52 -08:00
Meghana Gupta e3f8d123f2 Update protocol witness SILGen for borrow accessors 2026-01-20 11:44:09 -08:00
Meghana Gupta 686eb94ad7 Synthesize borrow/mutate accessors from stored properties when borrow/mutate
protocol requirements are present.
2026-01-20 11:44:07 -08:00
Mykola Pokhylets 170004b5c0 Applied review comments 2026-01-20 14:49:56 +01:00
Mykola Pokhylets b56e00ac2d Moved emitting closure value inside emitCollectionConversion() 2026-01-20 14:49:56 +01:00
Mykola Pokhylets 62b4a684cd WIP 2026-01-20 14:49:56 +01:00
Slava Pestov 819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05:00
Slava Pestov 522a6b7c80 SILGen: Fix break/continue inside 'for ... in ... repeat' loop
We were creating the JumpDests too early, so lowering a 'break' or 'continue'
statement would perform cleanups that were recorded while evaluating the
pack expansion expression, which would cause SIL verifier errors and
runtime crashes.

- Fixes https://github.com/swiftlang/swift/issues/78598
- Fixes rdar://131847933
2025-11-10 20:51:45 -05:00
Meghana Gupta a0c939bd3b Use @inout result convention for mutate accessors 2025-10-20 09:05:32 -07:00
Michael Gottesman 788abd0b96 [silgen] Use Builtin.ImplicitActor instead of Optional<any Actor> to represent the implicit isolated parameter.
NOTE: We are not performing any bitmasking at all now. This is so that we can
transition the code base/tests to expect Builtin.ImplicitActor instead
of Optional<any Actor>.

NOTE: The actual test changes are in the next commit. I did this to make it
easier to review the changes.

This should not have any user visible changes.
2025-10-16 10:52:05 -07:00
Gabor Horvath 5a63808235 Change the convention for reabstractions as well.
Remove SIL tests that are testing result conventions that are never
emitted by the compiler.
2025-10-03 14:58:48 +01:00
Arnold Schwaighofer 25a071efc8 Add experimental feature @inline(always)
The intent for `@inline(always)` is to act as an optimization control.
The user can rely on inlining to happen or the compiler will emit an error
message.

Because function values can be dynamic (closures, protocol/class lookup)
this guarantee can only be upheld for direct function references.

In cases where the optimizer can resolve dynamic function values the
attribute shall be respected.

rdar://148608854
2025-09-30 08:36:26 -07:00
Meghana Gupta 1cff471c57 Introduce ResultConvention::Guaranteed and ResultConvention::GuaranteedAddress
ResultConvention::Guaranteed will be used by borrow accessors when the storage type can be returned by value.

ResultConvention::GuaranteedAddress will be used by mutate accessors and borrow accessors when the storage type
cannot be returned by value.
2025-09-09 14:45:40 -07:00
Pavel Yaskevich 4e445d1483 Merge pull request #83556 from xedin/rename-nonisolated-caller-to-nonsending
[AST] NFC: Rename function type isolation `NonisolatedCaller` to `Non…
2025-08-08 09:37:17 -07:00
John McCall 8ad4aae6e0 Fix some major SILGen bugs with pack handling:
- Calls to variadic-generic protocol requirements weren't applying
  substitutions properly, so expansion-sensitive types in the callee
  signature weren't pairing properly with their expansions in the
  caller.

- emitPackTransform had an over-destroy if the transformation function
  actually emitted into the temporary element directly.

- There were some MV ownership assertions that were wrong, which
  revealed that the corresponding code really didn't handle consuming/
  borrowing mismatches properly at all.

- We were completely mishandled consuming packs.

Fixes #81002, #80995, and #81600.
2025-08-06 12:38:50 -04:00
Pavel Yaskevich 53ef38e31b [AST] NFC: Rename function type isolation NonisolatedCaller to NonisolatedNonsending
This reduces the number of ways we refer to caller isolated async
functions and matches the name to the attribute spelling.
2025-08-05 17:22:10 -07:00
John McCall bee053f1f0 Switch InitializationPtr to use PossiblyUniquePtr so that we can just
forward existing initializations around when necessary.
2025-08-02 02:13:25 -04:00
Anthony Latsis fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01:00
Michael Gottesman 0da22388cb [silgen] Make sure that thunks that convert to/from nonisolated(nonsending) handle hopping correctly.
Specifically:

1. When we convert a function to nonisolated(nonsending), we need to
make sure that in the thunk we hop upon return since nonisolated(nonsending)
functions are assumed to preserve the caller's isolation.

2. When we convert a function from nonisolated(nonsending), we need to
make sure that in the thunk we hop onto the actor that we are passing in as the
isolated parameter of the nonisolated(nonsending) function. This ensures that
the nonisolated(nonsending) function can assume that it is already on its
isolated parameter's actor at function entry.

rdar://155905383
2025-07-18 10:08:21 -07:00
John McCall 834b606c13 Correctly forward the implicit nonisolated(nonsending) parameter in thunks (#82705)
Also, I discovered that we don't apply nonisolated(nonsending) to
function types in the new mode. That's one for a different patch.

Fixes rdar://154401813 and rdar://154137740
2025-07-02 12:03:59 -07:00
John McCall 2eee30dfbe [NFC] Encapsulate the parameter index of an ActorIsolation 2025-06-27 19:48:12 -04:00
Saleem Abdulrasool 3269c0649b SILGen: adjust check for optional-to-optional conversion (#82099)
The new macro aliasing uncovered a latent issue where we would attempt
to perform an optional-to-optional conversion on a type that is
non-optional though may be aliased to an optional. `CVaList` is
sometimes an optional pointer and would be interpreted as an optional
type which would fail the assertion in the optional-to-optional
conversion.
2025-06-18 09:52:41 -07:00
Daniil Kovalev 1e403ecf5c [AutoDiff] Support custom derivatives for @_alwaysEmitIntoClient functions (#78908)
Consider an `@_alwaysEmitIntoClient` function and a custom derivative
defined
for it. Previously, such a combination resulted different errors under
different
circumstances.

Sometimes, there were linker errors due to missing derivative function
symbol -
these occurred when we tried to find the derivative in a module, while
it
should have been emitted into client's code (and it did not happen).

Sometimes, there were SIL verification failures like this:

```
SIL verification failed: internal/private function cannot be serialized or serializable: !F->isAnySerialized() || embedded
```

Linkage and serialization options for the derivative were not handled
properly,
and, instead of PublicNonABI linkage, we had Private one which is
unsupported
for serialization - but we need to serialize `@_alwaysEmitIntoClient`
functions
so the client's code is able to see them.

This patch resolves the issue and adds proper handling of custom
derivatives
of `@_alwaysEmitIntoClient` functions. Note that either both the
function and
its custom derivative or none of them should have
`@_alwaysEmitIntoClient`
attribute, mismatch in this attribute is not supported.

The following cases are handled (assume that in each case client's code
uses
the derivative).

1. Both the function and its derivative are defined in a single file in
   one module.

2. Both the function and its derivative are defined in different files
which
   are compiled to a single module.

3. The function is defined in one module, its derivative is defined in
another
   module.

4. The function and the derivative are defined as members of a protocol
extension in two separate modules - one for the function and one for the
   derivative. A struct conforming the protocol is defined in the third
   module.

5. The function and the derivative are defined as members of a struct
extension in two separate modules - one for the function and one for the
   derivative.

The changes allow to define derivatives for methods of `SIMD`.

Fixes #54445
<!--
If this pull request is targeting a release branch, please fill out the
following form:

https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1

Otherwise, replace this comment with a description of your changes and
rationale. Provide links to external references/discussions if
appropriate.
If this pull request resolves any GitHub issues, link them like so:

  Resolves <link to issue>, resolves <link to another issue>.

For more information about linking a pull request to an issue, see:

https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->

<!--
Before merging this pull request, you must run the Swift continuous
integration tests.
For information about triggering CI builds via @swift-ci, see:

https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci

Thank you for your contribution to Swift!
-->
2025-05-25 09:47:15 -04:00
Michael Gottesman ef23f97071 [silgen] Teach SILGen how to handle nonisolated(nonsending) thunking for vtables
This ensures that when we generate the vtable thunk for a
nonisolated(nonsending) override (or vis-a-versa), we get the ABI correct. I
also added tests for all of the relevant cases for vtables that we check for
protocols.

rdar://151394209
2025-05-15 14:11:55 -07:00
nate-chandler 2e1494df9f Merge pull request #80701 from nate-chandler/rdar148941214
[CoroutineAccessors] Use yield_once_2 on Darwin and Linux.
2025-04-10 14:46:05 -07:00
Nate Chandler b76a76bdf1 [CoroutineAccessors] Unwind based on feature.
Now that coroutine kind (and consequently ABI) for the accessors is
keyed off a SIL option, it's no longer possible to read whether a given
SILFunction arose from a read/modify coroutine just by checking its
coroutine kind.  Regardless of ABI, read/modify coroutines may only
unwind (i.e. are only permitted not to "run to completion") if the
relevant experimental (soon to be deleted) feature is enabled.
2025-04-10 07:41:37 -07:00
Michael Gottesman 04b845cc97 [concurrency] Implement protocol witness thunking in SILGen for @execution(caller)/@execution(concurrent).
This involved cleaning up and generalizing the work from
f245389bb3. I followed the same approach: hide the
implicit parameter while we run things through translateArgument and friends and
then put it back in manually.

As an additional benefit, I think I found a good place to put FunctionIsolation
onto lowered AnyFunctionTypes that will not cause cycles in the evaluator since
it is done in TypeLowering.

rdar://148785846
2025-04-08 12:43:28 -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
Michael Gottesman 25c0aa3da4 [silgen] Add dump/print methods to IndirectSlot and ParamInfo.
This just makes it easier to work with these types in the debugger. Just slicing
off commits from a larger patch.
2025-03-28 11:21:43 -07:00
Michael Gottesman 67730c1969 [silgen] If SILVerifyAll is enabled verify the body of thunks in buildThunkBody.
This just ensures that all thunks are verified right when they are created. This
is necessary since some thunks do not go through function post processing.
2025-03-26 10:20:27 -07:00
Erik Eckstein d225c47d25 AST: rename OpenArchetypeType -> ExistentialArchetypeType
NFC
2025-03-11 20:21:46 +01:00
Pavel Yaskevich cdb5396c00 [AST] Declare NonIsolatedCaller as kind of FunctionTypeIsolation
This would make sure that async function types marked as `@execution(caller)`
have correct isolation.

Also defines all of the possible conversions to and from `caller`
isolated function types.
2025-02-13 14:35:54 -08:00
Anton Korobeynikov 1e7a1d91fc Emit reabstraction thunks for implicit conversions between T.TangentType and Optional<T>.TangentType (#78076) 2025-02-05 20:57:52 -08:00
Kuba Mracek 6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00