Skip to content

Fix the Rust ABI on Windows to use a single return register - #163156

Open
bjorn3 wants to merge 2 commits into
rust-lang:mainfrom
bjorn3:windows_rust_abi_fix
Open

bjorn3 wants to merge 2 commits into
rust-lang:mainfrom
bjorn3:windows_rust_abi_fix

Conversation

@bjorn3

@bjorn3 bjorn3 commented Sep 22, 2026

Copy link
Copy Markdown
Member

The C ABI on Windows also only allows a single return register. Using two return registers is an LLVM ABI extension, which other backends don't support, thus preventing them from using an LLVM compiled standard library on x86_64 Windows.

Fixes rust-lang/rustc_codegen_cranelift#1703

The C ABI on Windows also only allows a single return register. Using
two return registers is an LLVM ABI extension, which other backends
don't support, thus preventing them from using an LLVM compiled standard
library on x86_64 Windows.
@rustbot

rustbot commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

compiletest directives have been modified. Please add or update docs for the
new or modified directive in src/doc/rustc-dev-guide/.

Some changes occurred in src/tools/compiletest

cc @jieyouxu

@rustbot rustbot added A-compiletest Area: The compiletest test runner A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 22, 2026
@rustbot

rustbot commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

r? @TaKO8Ki

rustbot has assigned @TaKO8Ki.
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 77 candidates
  • Random selection from 19 candidates

@bjorn3

bjorn3 commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

Alternative to bytecodealliance/wasmtime#14361 which also fixes this for the GCC backend (I expect it to have the same problem, though I can't check as godbolt doesn't have Windows support for cg_gcc)

@bjorn3

bjorn3 commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

cc @folkertdev

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job test-x86_64-gnu-llvm-21 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- [ui] tests/ui/abi/rust-return.rs#generic stdout ----
Saved the actual stderr to `/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/abi/rust-return.generic/rust-return.generic.stderr`
diff of stderr:

22                        randomization_seed: $SEED,
23                    },
24                },
-                mode: Direct(
-                    ArgAttributes {
-                        regular: NoUndef,
+                mode: Indirect {
+                    attrs: ArgAttributes {
+                        regular: CapturesAddress | NoAlias | NonNull | NoUndef | NoFree,
28                        arg_ext: None,
-                        pointee_size: Size(0 bytes),
-                        pointee_align: None,
+                        pointee_size: Size(16 bytes),
+                        pointee_align: Some(
+                            Align(16 bytes),
+                        ),
31                    },
-                ),
+                    meta_attrs: None,
+                    on_stack: false,
---

47                layout: TyAndLayout {
48                    ty: (usize, usize),
49                    layout: Layout {
-                        size: Size(16 bytes),
+                        size: $SOME_SIZE,
51                        align: AbiAlign {
52                            abi: $SOME_ALIGN,
53                        },

54                        backend_repr: ScalarPair {
-                            a: u64 is ..,
-                            b: u64 is ..,
-                            b_offset: Size(8 bytes),
+                            a: u32 is ..,
+                            b: u32 is ..,
+                            b_offset: Size(4 bytes),
58                        },
59                        fields: Arbitrary {
60                            offsets: [

61                                Size(0 bytes),
---
Note: some mismatched output was normalized before being compared
-                        size: Size(8 bytes),
+                mode: Indirect {
+                    attrs: ArgAttributes {
+                        regular: CapturesAddress | NoAlias | NonNull | NoUndef | NoFree,
+                        pointee_size: Size(16 bytes),
+                        pointee_align: Some(
+                            Align(16 bytes),
+                        ),
+                    meta_attrs: None,
+                    on_stack: false,
+                },
+                        size: $SOME_SIZE,
+                            a: u32 is ..,
+                            b: u32 is ..,
+                            b_offset: Size(4 bytes),
+                                Size(4 bytes),


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args abi/rust-return.rs`

error in revision `generic`: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/abi/rust-return.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=i686-unknown-linux-gnu" "--cfg" "generic" "--check-cfg" "cfg(test,FALSE,generic,win64)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/abi/rust-return.generic" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/i686-unknown-linux-gnu/native/rust-test-helpers" "-Clinker=x86_64-linux-gnu-gcc" "-Cpanic=abort" "-Cforce-unwind-tables=yes" "-O" "--extern" "minicore=/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/abi/rust-return.generic/libminicore.rlib"
stdout: none
--- stderr -------------------------------
error: fn_abi_of(u128_ret) = FnAbi {
           args: [],
           ret: ArgAbi {
               layout: TyAndLayout {
                   ty: u128,
                   layout: Layout {
---
                   },
               },
               mode: Indirect {
                   attrs: ArgAttributes {
                       regular: CapturesAddress | NoAlias | NonNull | NoUndef | NoFree,
                       arg_ext: None,
                       pointee_size: Size(16 bytes),
                       pointee_align: Some(
                           Align(16 bytes),
                       ),
                   },
                   meta_attrs: None,
                   on_stack: false,
               },
---
           can_unwind: false,
       }
  --> /checkout/tests/ui/abi/rust-return.rs:28:1
   |
LL | fn u128_ret() -> u128 {
   | ^^^^^^^^^^^^^^^^^^^^^

error: fn_abi_of(scalar_pair_ret) = FnAbi {
           args: [],
           ret: ArgAbi {
               layout: TyAndLayout {
                   ty: (usize, usize),
                   layout: Layout {
                       size: Size(8 bytes),
                       align: AbiAlign {
                           abi: Align(4 bytes),
                       },
                       backend_repr: ScalarPair {
                           a: u32 is ..,
                           b: u32 is ..,
                           b_offset: Size(4 bytes),
                       },
                       fields: Arbitrary {
                           offsets: [
                               Size(0 bytes),
                               Size(4 bytes),
---
   |
LL | fn scalar_pair_ret() -> (usize, usize) {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: fn_abi_of(scalar_pair_ret2) = FnAbi {
           args: [],
           ret: ArgAbi {
               layout: TyAndLayout {
                   ty: (u32, u32),
                   layout: Layout {
                       size: Size(8 bytes),
                       align: AbiAlign {
                           abi: Align(4 bytes),
                       },
                       backend_repr: ScalarPair {
                           a: u32 is ..,
                           b: u32 is ..,
                           b_offset: Size(4 bytes),
                       },
                       fields: Arbitrary {
                           offsets: [
                               Size(0 bytes),
                               Size(4 bytes),
---
   |
LL | fn scalar_pair_ret2() -> (u32, u32) {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: fn_abi_of(scalar_pair_ret3) = FnAbi {
           args: [],
           ret: ArgAbi {
               layout: TyAndLayout {
                   ty: (u128, u128),
                   layout: Layout {
                       size: Size(32 bytes),
                       align: AbiAlign {
                           abi: Align(16 bytes),
                       },
                       backend_repr: ScalarPair {
                           a: u128 is ..,
                           b: u128 is ..,
                           b_offset: Size(16 bytes),
                       },
                       fields: Arbitrary {
                           offsets: [
                               Size(0 bytes),
                               Size(16 bytes),
---
                   },
               },
               mode: Indirect {
                   attrs: ArgAttributes {
                       regular: CapturesNone | NoAlias | NonNull | NoUndef | NoFree,
                       arg_ext: None,
                       pointee_size: Size(32 bytes),
                       pointee_align: Some(
                           Align(16 bytes),
                       ),
                   },
                   meta_attrs: None,
                   on_stack: false,
               },

@nikic

nikic commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Unlike previous changes like this, this looks like it's going to impact a pretty common case (scalar pair return). I think it would be better to extend other backends to support this psABI variant.

@bjorn3

bjorn3 commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

For Cranelift that is doable. For GCC, as I understand it, only ABIs that are representable in C can be implemented by frontends as common IR of GCC (GIMPLE) uses C types for both function signatures and local variables and has the backend implement the entire lowering from a C function signature to the calling convention for the target. And there is no way to write C code that would use two return registers on x86_64 Windows. Notice how GCC doesn't distinguish at all between by-val and by-ref returns in GIMPLE: https://rust.godbolt.org/z/fKjc4oqTr (the cg_gcc produced GIMPLE is a bit confusing as it calls the tuple type "struct", so you get "struct struct" as type)

@nikic

nikic commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Is compatibility with an LLVM sysroot even a goal for the GCC backend?

@bjorn3

bjorn3 commented Sep 22, 2026 •

Copy link
Copy Markdown
Member Author

We don't ship a separate standard library for GCC, so ABI compat would be necessary for the rustup distributed version of cg_gcc to work. But I don't know if it is a goal beyond that. cc @antoyo @GuillaumeGomez

@antoyo

antoyo commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

For GCC, as I understand it, only ABIs that are representable in C

libgccjit can represent ABIs that are not representable in C: for instance, I'll land the sret fix in the coming weeks which required surfacing to libgccjit ABI stuff from the GCC IR.

But it does indeed seem that GCC cannot currently represent this ABI on Windows.

But I don't know if it is a goal beyond that.

Yes, we would like to be compatible with the LLVM ABI, but I don't think we should block any ABI change on GCC support.
For some other stuff, we'll need to change GCC IR anyway in order to support it, for both cg_gcc and gccrs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-compiletest Area: The compiletest test runner A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) 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.

msvc: small float aggregates are returned incorrectly with extern "Rust"

6 participants