# Why C++ pass 128 bits via registers, while rust uses stack?

**URL:** <https://internals.rust-lang.org/t/why-c-pass-128-bits-via-registers-while-rust-uses-stack/22437>\
**Category:** compiler\
**Created:** [February 22, 2025, 12:16pm UTC](https://internals.rust-lang.org/t/why-c-pass-128-bits-via-registers-while-rust-uses-stack/22437 "2025-02-22T12:16:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davemilter](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/davemilter/32/3493_2.png) [@davemilter](https://internals.rust-lang.org/u/davemilter)\
**Post date:** [February 22, 2025, 12:16pm UTC](https://internals.rust-lang.org/t/why-c-pass-128-bits-via-registers-while-rust-uses-stack/22437/1 "2025-02-22T12:16:13Z")

</div>

If compile such code (linux + amd64):

```c++
#include <cstdint>

struct Small {
  int32_t a, b, c, d;
};

int32_t sum1(Small s)
{
    return s.a + s.d;
}

```

then disassembler show that `sum1` takes arguments in two 64bit registers: [godbolt link](https://godbolt.org/z/nExePPnEz).

But for some reason Rust for similar code:

```rust
pub struct Small {
    a: i32,
    b: i32,
    c: i32,
    d: i32,
}

#[no_mangle]
pub fn sum1(s: Small) -> i32
{
    s.a + s.d
}

```

uses stack instead of registers: [godbolt link](https://godbolt.org/z/dYba916Eh).

Any reasons behind such difference?

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [February 22, 2025, 1:23pm UTC](https://internals.rust-lang.org/t/why-c-pass-128-bits-via-registers-while-rust-uses-stack/22437/2 "2025-02-22T13:23:24Z")

</div>

This is just how the Rust ABI is currently implemented. If you watch to match C then you can define your function as `extern "C"`

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [February 22, 2025, 1:36pm UTC](https://internals.rust-lang.org/t/why-c-pass-128-bits-via-registers-while-rust-uses-stack/22437/3 "2025-02-22T13:36:34Z")

</div>

Note that Rust does not "use stack" in your snippet, instead it generates `sum1` which accepts a pointer to the struct. It may point to any place in memory, not necessary to stack. As for which ABI is better... it depends. Yes, the Rust ABI may result in additional stack usage, but the C++ ABI may result in unnecessary loads and register pollution (e.g. fields `b` and `c` in your example). Overall, I don't think it matters much in practice, since small functions for which the difference could be noticeable are likely to be inlined.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [February 22, 2025, 7:35pm UTC](https://internals.rust-lang.org/t/why-c-pass-128-bits-via-registers-while-rust-uses-stack/22437/4 "2025-02-22T19:35:19Z")

</div>

We do pass aggregate types that fit in a single register in said register: [rust/compiler/rustc\_target/src/callconv/mod.rs at 15469f8f8ae0a77577745cf56d562600fdb6539a · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/blob/15469f8f8ae0a77577745cf56d562600fdb6539a/compiler/rustc_target/src/callconv/mod.rs#L818-L826) In the past we did the same when it fit in two registers but we stopped doing this in [https://github.com/rust-lang/rust/pull/94570](https://github.com/rust-lang/rust/pull/94570) because it caused the autovectorizer to be less effective.

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [February 22, 2025, 9:58pm UTC](https://internals.rust-lang.org/t/why-c-pass-128-bits-via-registers-while-rust-uses-stack/22437/5 "2025-02-22T21:58:26Z")

</div>

Does this mean that the autovectorizer in Rust will do a better job than C++ in the case where C++ is passing some 128 bits parameter via registers? (If both use LLVM)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [August 16, 2026, 9:58pm UTC](https://internals.rust-lang.org/t/why-c-pass-128-bits-via-registers-while-rust-uses-stack/22437/6 "2026-08-16T21:58:31Z")

</div>

Closed on August 16, 2026, 9:58pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
