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

Useless copies of alignment padding #56297

Closed
glandium opened this issue Nov 27, 2018 · 3 comments · Fixed by #115096
Closed

Useless copies of alignment padding #56297

glandium opened this issue Nov 27, 2018 · 3 comments · Fixed by #115096
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@glandium
Copy link
Contributor

See the following extreme test-case:

#[repr(align(1024))]
pub struct Foo(usize);

pub fn foo() -> Box<Foo> {
    Box::new(Foo(42))
}

This is what godbolt says rust stable does with it with -O:

        push    rbx
        sub     rsp, 1024
        mov     edi, 1024
        mov     esi, 1024
        call    __rust_alloc@PLT
        test    rax, rax
        je      .LBB0_1
        mov     rbx, rax
        mov     qword ptr [rax], 42
        mov     rdi, rax
        add     rdi, 8
        lea     rsi, [rsp + 8]
        mov     edx, 1016
        call    memcpy@PLT
        mov     rax, rbx
        add     rsp, 1024
        pop     rbx
        ret
.LBB0_1:
        mov     edi, 1024
        mov     esi, 1024
        call    alloc::alloc::handle_alloc_error@PLT

That is, it allocates 1024 bytes (expected), writes 42 directly at the allocated location, and then proceeds to copy 1016 (uninitialized) bytes from the stack.

@nikic
Copy link
Contributor

nikic commented Nov 27, 2018

Similar to #56191, but not quite the same issue.

@nikic nikic added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-slow Issue: Problems and improvements with respect to performance of generated code. labels Dec 21, 2018
@klensy
Copy link
Contributor

klensy commented Aug 28, 2021

Looks resolved around 1.52 https://rust.godbolt.org/z/n14acj8sG

@kadiwa4
Copy link
Contributor

kadiwa4 commented Jan 25, 2023

@rustbot label: +E-needs-test -A-LLVM
(--emit=llvm-ir already has the optimization and as I understand that hasn't been touched by LLVM's optimizer yet. So it isn't LLVM-specific, right?)

@rustbot rustbot added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. labels Jan 25, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 22, 2023
Add regression test for not `memcpy`ing padding bytes

Closes rust-lang#56297

See this comparison: https://rust.godbolt.org/z/jjzfonfcE

I don't have any experience with codegen tests, I hope this is correct
@bors bors closed this as completed in 391cbda Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants