Skip to content

Field ordering still causes extra memcpy #58082

Open
@jrmuizel

Description

@jrmuizel

This is similar to #56356 except this time with more fields:

use std::mem;

struct SV {
    capacity: usize,
    disc: usize,
    data: [usize; 40],
}

impl SV {
    fn new() -> SV {
        SV { data: unsafe { mem::uninitialized() },
            disc: 0,
            capacity: 0 }
    }
}

pub struct L {
    a: SV,
    b: SV
}

pub struct Allocation<T> {
    f: *mut T,
}

impl<T> Allocation<T> {
    pub fn init(self, value: T) {
        use std::ptr;
        unsafe {
        ptr::write(self.f, value);
        }
    }
}

#[inline(never)]
pub fn foo(a: Allocation<L>) {
    a.init(L {
        a: SV::new(),
        b: SV::new()
    });
}

gives:

example::foo:
        sub     rsp, 680
        xorps   xmm0, xmm0
        movaps  xmmword ptr [rsp], xmm0
        movaps  xmmword ptr [rsp + 336], xmm0
        mov     rsi, rsp
        mov     edx, 672
        call    qword ptr [rip + memcpy@GOTPCREL]
        add     rsp, 680
        ret

vs moving capacity to the end of the struct:

example::foo:
        mov     qword ptr [rdi], 0
        xorps   xmm0, xmm0
        movups  xmmword ptr [rdi + 328], xmm0
        mov     qword ptr [rdi + 664], 0
        ret

The problem is reproducible with C++ so I'll file a new llvm bug too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-mir-optArea: MIR optimizationsA-mir-opt-nrvoFixed by the Named Return Value Opt. (NRVO)C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions