Skip to content

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

Closed
@safinaskar

Description

@safinaskar

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.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