Skip to content

Add Swift function call ABI#155815

Open
djc wants to merge 1 commit intorust-lang:mainfrom
djc:swift-cc
Open

Add Swift function call ABI#155815
djc wants to merge 1 commit intorust-lang:mainfrom
djc:swift-cc

Conversation

@djc
Copy link
Copy Markdown
Contributor

@djc djc commented Apr 26, 2026

View all comments

Adds an unstable extern "Swift" ABI behind the abi_swift feature gate, mapping to LLVM's swiftcc calling convention. This is only allowed (a) for is_darwin_like targets, since the ABI is only stable for those platforms and (b) with the LLVM backend, since the other backends don't support it.

Current approaches to interoperability with Swift lower to Objective-C (or require a Swift stub exposing a C ABI), but that is an optional mapping on the Swift side that some newer Apple frameworks omit. It would be great to be able to more directly/natively be able to call into Swift code directly via its stable API (on Apple platforms at least).

Reimplements #64582 on top of current main. The main objection to the previous PR seemed to be that it needed an RFC, but there was pushback (which seems sensible to me) that an RFC could be deferred until stabilization.

I think this needs a tracking issue? Would be happy to write one up if/when there is a consensus that this will be merged.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 26, 2026

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

These commits modify compiler targets.
(See the Target Tier Policy.)

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 26, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 26, 2026

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 19 candidates

@rustbot

This comment has been minimized.

Comment thread compiler/rustc_abi/src/canon_abi.rs
Comment thread compiler/rustc_codegen_cranelift/src/abi/mod.rs Outdated
Comment thread compiler/rustc_codegen_gcc/src/abi.rs Outdated
@bjorn3
Copy link
Copy Markdown
Member

bjorn3 commented Apr 26, 2026

When will arguments be marked as swiftself and swifterror in LLVM IR? Without these it is impossible to call class methods and fallible functions respectively afaik. Also how useful is this without repr(Swift)? Delaying implementation of that to another PR would be fine I think, but I would personally like to see at least a plan for how to soundly implement repr(Swift) given that it breaks the stride=size assumption that is true for all current memory layouts.

@djc
Copy link
Copy Markdown
Contributor Author

djc commented Apr 26, 2026

Delaying implementation of that to another PR would be fine I think, but I would personally like to see at least a plan for how to soundly implement repr(Swift) given that it breaks the stride=size assumption that is true for all current memory layouts.

So this is a bit experimental. I plan on looking into all the things (though in my free time, and thus slowly) needed to call into Swift from Rust, but I don't have a plan today. If you want a more concrete plan before merging this, I'd like hear some more detail (in the sense of, maybe a bullet list) of things that need to be done.

@bjorn3
Copy link
Copy Markdown
Member

bjorn3 commented Apr 26, 2026

To be clear, I'm not going to block this PR on figuring out repr(Swift). I'm just a bit worried that we don't figure out repr(Swift) making extern "Swift" not that useful.

By the way I noticed that a question of mine in the previous attempt hadn't been answered yet: #64582 (comment) https://github.com/swiftlang/swift/blob/main/docs/ABI/CallingConventionSummary.rst seems to suggest like the answer to my question is no it doesn't need a separate classify_arg on Apple platforms, but I'm not sure.

@jieyouxu
Copy link
Copy Markdown
Member

For something like adding a new ABI, it'd definitely need a RFC accepted prior to stabilization. There are mechanisms to support "land something unstably to gather impl experience to feed RFC design / explore edge cases", in this case I would expect to take the form of a lang experiment. That is, if there is a lang champion happy to champion this.

Asked in #t-lang > Swift call ABI.

@jieyouxu jieyouxu added the A-ABI Area: Concerning the application binary interface (ABI) label Apr 27, 2026
@madsmtm
Copy link
Copy Markdown
Contributor

madsmtm commented Apr 27, 2026

To spell it out explicitly, the primary reason this is interesting is because there's a bunch of platform APIs that are only exposed over the Swift ABI, such as RealityKit and WidgetKit.

Existing solutions need users to write @c wrappers in Swift to expose the functionality over a C ABI, which is inefficient (cross lang LTO is hard) and cumbersome.

Copy link
Copy Markdown
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

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

Also, can you please expand on the motivations (in PR description or elsewhere) for adding a new Swift fn call ABI (unstable or not)? There was an earlier PR which this PR revives yes, but we would definitely like to know if the motivations and circumstances have changed since then.

E.g. Mads shared one such motivation in comment above, I'd be curious if you had the same motivations or additional/different ones.

It would also help a lot if the PR description could also be updated to include details like #155815 (comment).

@rustbot author

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 27, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 27, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@djc
Copy link
Copy Markdown
Contributor Author

djc commented May 2, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 2, 2026
@rust-log-analyzer

This comment has been minimized.

Adds an unstable `extern "Swift"` ABI behind the `abi_swift` feature
gate, mapping to LLVM's `swiftcc` calling convention. Cranelift and
GCC backends fall back to the platform default since they have no
equivalent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ABI Area: Concerning the application binary interface (ABI) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants