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

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.

Reimplements #64582 on top of current main. The main objection to it 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.

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.

/// Swift calling convention, exposed via LLVM's `swiftcc`. Cross-platform
/// and not tied to a specific target architecture.
Swift,
Copy link
Copy Markdown
Member

@bjorn3 bjorn3 Apr 26, 2026

Choose a reason for hiding this comment

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

Do I understand it correctly that the Swift ABI isn't stable yet outside of Apple platforms?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think that's right. How do you think that should affect this PR? Should we only allow this on Apple targets?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, I think it makes sense to only allow on Apple targets for now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But on Apple platforms it is stable? We're not chasing a moving target here, are we?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, it's stable on Apple platforms since 2019 and Swift 5:
https://www.swift.org/blog/abi-stability-and-more/

There's a lot of good details in this article:
https://faultlore.com/blah/swift-abi/#resilient-type-layout

There's also Embedded Swift, which has an unstable ABI regardless of platform:
https://docs.swift.org/embedded/documentation/embedded/abi/

CanonAbi::Custom => default_call_conv,

// Cranelift has no Swift calling convention; fall back to the platform default.
CanonAbi::Swift => default_call_conv,
Copy link
Copy Markdown
Member

@bjorn3 bjorn3 Apr 26, 2026

Choose a reason for hiding this comment

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

Please emit an error instead. Silently changing the calling convention is a miscompilations.

View changes since the review

// convention for declaring foreign functions.
CanonAbi::Custom => return None,
// gcc/gccjit does not have anything for Swift's calling convention.
CanonAbi::Swift => return None,
Copy link
Copy Markdown
Member

@bjorn3 bjorn3 Apr 26, 2026

Choose a reason for hiding this comment

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

@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.

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-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

7 participants