Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct Layout Optimization in Rust 1.18 regressed C ABI #48433

Closed
CryZe opened this issue Feb 22, 2018 · 2 comments
Closed

Struct Layout Optimization in Rust 1.18 regressed C ABI #48433

CryZe opened this issue Feb 22, 2018 · 2 comments
Labels
A-codegen Area: Code generation C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CryZe
Copy link
Contributor

CryZe commented Feb 22, 2018

In addition to the general question raised by #48426 of whether a pair of floats should use a LLVM vector in the Rust ABI, it seems like the C ABI regressed in Rust 1.18 in this regard too. Before Rust 1.18 it used to actually represent the repr(C) equivalent type as a LLVM vector, but since 1.18 the type gets passed as a double instead and then bitcast to the individual fields inside, completely disabling all the vectorization possible.

Code:

#[repr(C)]
pub struct Vec2 {
    pub x: f32,
    pub y: f32
}

#[no_mangle]
pub extern "C" fn add (a: Vec2, b: Vec2) -> Vec2 {
    Vec2 {
        x: a.x + b.x,
        y: a.y + b.y
    }
}

Rust 1.17:

define <2 x float> @add(<2 x float>, <2 x float>) unnamed_addr #0
add:
  addps xmm0, xmm1
  ret

Rust 1.18:

define double @add(double, double) unnamed_addr #0
add:
  movq rax, xmm0
  movd xmm0, eax
  shr rax, 32
  movd xmm2, eax
  movq rax, xmm1
  movd xmm1, eax
  shr rax, 32
  movd xmm3, eax
  addss xmm1, xmm0
  addss xmm3, xmm2
  movss dword ptr [rsp - 8], xmm1
  movss dword ptr [rsp - 4], xmm3
  movsd xmm0, qword ptr [rsp - 8]
  ret

Here's the code on Godbolt compiled with Rust 1.17 and 1.18: https://godbolt.org/g/1iu8n2

@eddyb
Copy link
Member

eddyb commented Feb 22, 2018

Oh, that's why extern "C" isn't that great! Note that ABI-wise these are equivalent, and choosing <2 x float> over double is a matter of heuristics (for clang too, e.g. if you use an union).

@eddyb eddyb added the A-codegen Area: Code generation label Feb 22, 2018
@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 14, 2018
@Mark-Simulacrum
Copy link
Member

Seems to be equivalent to #48426, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants