Skip to content

Small POD struct equality ineffective #83585

Description

@leonardo-m

From a recent Reddit discussion (https://old.reddit.com/r/rust/comments/medh15/why_are_derived_partialeqimplementations_not_more/ ):

type T = u8;
type T1 = (T, T, T, T, T, T, T, T);
type T2 = [T; 8];

pub fn foo1a(a: T1, b: T1) -> bool {
    a == b
}
pub fn foo1b(a: &T1, b: &T1) -> bool {
    a == b
}

pub fn foo2a(a: T2, b: T2) -> bool {
    a == b
}
pub fn foo2b(a: &T2, b: &T2) -> bool {
    a == b
}

Gives (rustc 1.53.0-nightly 5e65467 2021-03-26):

foo1a:
        cmp     dil, sil
        jne     .LBB0_2
        mov     rax, rdi
        shr     rax, 8
        mov     rcx, rsi
        shr     rcx, 8
        cmp     al, cl
        jne     .LBB0_2
        mov     rax, rsi
        shr     rax, 16
        mov     rcx, rdi
        shr     rcx, 16
        cmp     cl, al
        jne     .LBB0_2
        mov     rax, rsi
        shr     rax, 24
        mov     rcx, rdi
        shr     rcx, 24
        cmp     cl, al
        jne     .LBB0_2
        mov     rax, rdi
        shr     rax, 32
        mov     rcx, rsi
        shr     rcx, 32
        cmp     al, cl
        jne     .LBB0_2
        mov     rax, rdi
        shr     rax, 40
        mov     rcx, rsi
        shr     rcx, 40
        cmp     al, cl
        jne     .LBB0_2
        mov     rax, rdi
        shr     rax, 48
        mov     rcx, rsi
        shr     rcx, 48
        cmp     al, cl
        jne     .LBB0_2
        shr     rdi, 56
        shr     rsi, 56
        mov     al, 1
        cmp     dil, sil
        jne     .LBB0_2
        ret
.LBB0_2:
        xor     eax, eax
        ret

foo1b:
        mov     al, byte ptr [rdi]
        cmp     al, byte ptr [rsi]
        jne     .LBB1_1
        mov     al, byte ptr [rdi + 1]
        cmp     al, byte ptr [rsi + 1]
        jne     .LBB1_1
        mov     al, byte ptr [rdi + 2]
        cmp     al, byte ptr [rsi + 2]
        jne     .LBB1_1
        mov     al, byte ptr [rdi + 3]
        cmp     al, byte ptr [rsi + 3]
        jne     .LBB1_1
        mov     al, byte ptr [rdi + 4]
        cmp     al, byte ptr [rsi + 4]
        jne     .LBB1_1
        mov     al, byte ptr [rdi + 5]
        cmp     al, byte ptr [rsi + 5]
        jne     .LBB1_1
        mov     al, byte ptr [rdi + 6]
        cmp     al, byte ptr [rsi + 6]
        jne     .LBB1_1
        mov     cl, byte ptr [rdi + 7]
        mov     al, 1
        cmp     cl, byte ptr [rsi + 7]
        jne     .LBB1_1
        ret
.LBB1_1:
        xor     eax, eax
        ret

foo2a:
        cmp     rdi, rsi
        sete    al
        ret

foo2b:
        mov     rax, qword ptr [rdi]
        cmp     rax, qword ptr [rsi]
        sete    al
        ret

That Reddit discussion shows other cases.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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