[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 Mar 3, 2026
Conversation
Contributor
|
Tagging subscribers to this area: @BrzVlad, @janvorli, @kg |
Contributor
There was a problem hiding this comment.
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>
src/tests/Interop/Swift/SwiftInvalidCallConv/SwiftInvalidCallConv.cs
Outdated
Show resolved
Hide resolved
…s/runtime into feature/swift-reverse-pinvoke
…onv.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…s/runtime into feature/swift-reverse-pinvoke
2 tasks
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This was referenced Feb 27, 2026
Open
janvorli
reviewed
Feb 27, 2026
…s/runtime into feature/swift-reverse-pinvoke
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_SwiftSelfroutine is added to restores the originalx20value 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 throwsPlatformNotSupportedExceptionif 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 aStore_SwiftErrorroutine 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_SwiftIndirectResultroutine writes thex8value 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
HasSwiftReturnLoweringflag is added to theCallStubHeader. 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