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

Missed optimization: regression 1.29 -> 1.30, makes unnecessary load from memory #65876

Closed
safinaskar opened this issue Oct 27, 2019 · 2 comments
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@safinaskar
Copy link
Contributor

Consider this code: https://godbolt.org/z/QpiBK3 .
Code:

pub fn f(a: &mut i32, b: &mut i32) -> (i32, i32, i32) {
    let c = *a;
    let d = *b;
    *a = 0;
    (c, d, *b)
}

Asm (1.30):

example::f:
        movl    (%rsi), %eax
        movl    (%rdx), %ecx
        movl    $0, (%rsi)
        movl    (%rdx), %edx
        movl    %eax, (%rdi)
        movl    %ecx, 4(%rdi)
        movl    %edx, 8(%rdi)
        movq    %rdi, %rax
        retq

We read from memory at line let d = *b (from %rdx) and then we read from memory again at line (c, d, *b) (again from %rdx).

But in Rust (unlike C and C++) we know that two mutable references cannot point to same location. So, we missed optimization. This is regression

@jonas-schievink
Copy link
Contributor

This is likely due to the known issues with LLVM's noalias annotation. See #54878.

@jonas-schievink jonas-schievink added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 17, 2019
@Mark-Simulacrum
Copy link
Member

This has since been fixed. Current nightly output doesn't load from memory twice for rdx.

f:
        mov     rax, rdi
        mov     ecx, dword ptr [rsi]
        mov     edx, dword ptr [rdx]
        mov     dword ptr [rsi], 0
        mov     dword ptr [rdi], ecx
        mov     dword ptr [rdi + 4], edx
        mov     dword ptr [rdi + 8], edx
        ret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-slow Issue: Problems and improvements with respect to performance of generated code. 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

3 participants