Skip to content

[clr-interp] Add reverse P/Invoke support for the Swift calling convention to the CoreCLR interpreter on arm64#124927

Merged
kotlarmilos merged 14 commits intodotnet:mainfrom
kotlarmilos:feature/swift-reverse-pinvoke
Mar 3, 2026
Merged

[clr-interp] Add reverse P/Invoke support for the Swift calling convention to the CoreCLR interpreter on arm64#124927
kotlarmilos merged 14 commits intodotnet:mainfrom
kotlarmilos:feature/swift-reverse-pinvoke

Conversation

@kotlarmilos
Copy link
Member

@kotlarmilos kotlarmilos commented Feb 26, 2026

Description

This PR adds reverse P/Invoke support for the Swift calling convention to the CoreCLR interpreter on arm64 Apple platforms. It extends the existing Swift call stubs so Swift managed callbacks work correctly under the interpreter, including special registers, argument rewriting, and return handling.

SwiftSelf

A new Store_SwiftSelf routine is added to restores the original x20 value from the transition frame (since it may be clobbered by thread setup) and writes it into the interpreter argument area.

SwiftSelf<T> is unsupported for reverse P/Invoke and throws PlatformNotSupportedException if encountered.

SwiftError

SwiftError* is excluded from the rewritten signature because it does not participate in normal argument register allocation. To preserve the byref error location for managed code, the stub generator emits a Store_SwiftError routine before regular argument processing. This routine stores the address of the local stack error slot into the interpreter argument area. The interpreter stub epilog is updated to correctly propagate the error register.

SwiftIndirectResult

A new Store_SwiftIndirectResult routine writes the x8 value into the interpreter argument area so managed callbacks can handle the indirect result correctly.

Lowering

For Swift-lowered struct arguments, the stub generator now emits store at offset routines when generating reverse stubs. These routines read values from the appropriate GP/FP register or native stack slot and store them into the interpreter buffer using explicit offsets. The implementation handles unaligned fields within lowered structs and emits correctly sized stack loads.

A HasSwiftReturnLowering flag is added to the CallStubHeader. When this flag is set, the interpreter stub epilog skips loading the continuation context because the lowered return path (x2) has the return data.

Fixes #120049

Copilot AI review requested due to automatic review settings February 26, 2026 22:30
@kotlarmilos kotlarmilos added this to the 11.0.0 milestone Feb 26, 2026
@kotlarmilos kotlarmilos self-assigned this Feb 26, 2026
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @BrzVlad, @janvorli, @kg
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds reverse P/Invoke (UnmanagedCallersOnly) support for the Swift calling convention to the CoreCLR interpreter on arm64 Apple platforms. The implementation extends the existing callstub generation infrastructure and arm64 interpreter assembly stubs to handle Swift-specific ABI requirements when native Swift code calls into managed code.

Changes:

  • Removes ActiveIssue attributes from 100+ Swift interop tests, enabling them for the CoreCLR interpreter
  • Extends CallStubHeader and CallStubGenerator with Swift return lowering support for reverse P/Invoke
  • Adds Store_* assembly routines for transferring Swift special parameters (SwiftSelf, SwiftError, SwiftIndirectResult) from native registers to the interpreter stack
  • Implements SwiftReverseReturnLoweredHandler for decomposing Swift lowered struct returns into registers

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
SwiftIndirectResult.cs Enables test for non-frozen struct returns in interpreter
SwiftErrorHandling.cs Enables UnmanagedCallersOnly Swift error handling tests
SwiftCallbackAbiStress.cs Enables 100 callback stress tests covering various struct layouts and Swift ABI combinations
callstubgenerator.h Adds HasSwiftReturnLowering flag and m_isSwiftILStub tracking; increases routine storage size for Swift lowering
callstubgenerator.cpp Implements bidirectional Swift routine selection, SwiftError handling for reverse P/Invoke, signature rewriting exclusions, and return lowering routines
asmhelpers.S Adds Store_SwiftError, Store_SwiftSelf, Store_SwiftIndirectResult, Store_Stack_*_AtOffset, Store_X4-X7/D4-D7_AtOffset routines; implements SwiftReverseReturnLoweredHandler for struct return decomposition
asmconstants.h Defines OFFSETOF__CallStubHeader__HasSwiftReturnLowering constant

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 27, 2026 07:43
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

kotlarmilos and others added 3 commits February 27, 2026 09:00
…s/runtime into feature/swift-reverse-pinvoke
…onv.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 27, 2026 08:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 27, 2026 09:07
@kotlarmilos kotlarmilos marked this pull request as ready for review February 27, 2026 09:12
@kotlarmilos kotlarmilos requested a review from BrzVlad February 27, 2026 09:13
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

Copilot AI review requested due to automatic review settings March 2, 2026 09:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@kotlarmilos kotlarmilos merged commit 744234e into dotnet:main Mar 3, 2026
108 of 111 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clr-ios] Implement Swift calling convention in CoreCLR interpreter

3 participants