Skip to content

Conversation

@kovdan01
Copy link
Contributor

@kovdan01 kovdan01 commented Nov 3, 2025

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

…tions (swiftlang#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 swiftlang#54445
@kovdan01 kovdan01 requested review from asl and clackary November 3, 2025 20:13
@kovdan01
Copy link
Contributor Author

kovdan01 commented Nov 3, 2025

Tagging @JaapWijnen

@kovdan01 kovdan01 marked this pull request as ready for review November 3, 2025 20:13
@kovdan01 kovdan01 requested a review from a team as a code owner November 3, 2025 20:13
@kovdan01
Copy link
Contributor Author

kovdan01 commented Nov 3, 2025

@kovdan01
Copy link
Contributor Author

kovdan01 commented Nov 3, 2025

@swift-ci please test

@kovdan01 kovdan01 added 🍒 release cherry pick Flag: Release branch cherry picks swift 6.2 labels Nov 3, 2025
@kovdan01 kovdan01 changed the title [AutoDiff] Support custom derivatives for @_alwaysEmitIntoClient functions [6.2][AutoDiff] Support custom derivatives for @_alwaysEmitIntoClient funcs Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AutoDiff 🍒 release cherry pick Flag: Release branch cherry picks swift 6.2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant