Skip to content

Inefficient codegen for eq of enum with identical variants #113506

Open
@adrian17

Description

@adrian17

Given an enum:

type Pointer = *const ();
#[derive(PartialEq, Eq)]
pub enum MyEnum {
    A(Pointer),
    B(Pointer),
    C(Pointer),
    D(Pointer),
    E(Pointer),
    F(Pointer),
    Any,
}

The generated comparison uses jump table despite every jump target being identical:
https://godbolt.org/z/6nWW53eb5
As far as I'm aware, this jump should have been optimized away entirely, probably to something like

example::compare:
        mov     rax, qword ptr [rdi]
        cmp     rax, qword ptr [rsi]
        jne     .LBB0_1
        cmp     rax, 5
        ja      .LBB0_3
-        lea     rcx, [rip + .LJTI0_0]
-        movsxd  rax, dword ptr [rcx + 4*rax]
-        add     rax, rcx
-        jmp     rax
-.LBB0_5:
        mov     rax, qword ptr [rdi + 8]
        cmp     rax, qword ptr [rsi + 8]
        sete    al
        ret

With WASM target, it's even worse, as it doesn't deduplicate the identical match arms at all:
https://godbolt.org/z/Gf16rM4MY

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-heavyIssue: Problems and improvements with respect to binary size of generated code.I-slowIssue: Problems and improvements with respect to performance of generated code.O-wasmTarget: WASM (WebAssembly), http://webassembly.org/

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions