Skip to content

Codegen Optimization for Non-Consecutive Enum Match Arms #140849

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

Open
ray90514 opened this issue May 9, 2025 · 0 comments
Open

Codegen Optimization for Non-Consecutive Enum Match Arms #140849

ray90514 opened this issue May 9, 2025 · 0 comments
Labels
C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ray90514
Copy link

ray90514 commented May 9, 2025

I tried this code:

enum Foo {
    Field1 = 10,
    Field2 = 11,
    Field3 = 12,
    Field4 = 20,
}


#[no_mangle]
fn f(value: u32) -> Foo {
    match value {
        10 => Foo::Field1,
        11 => Foo::Field2,
        12 => Foo::Field3,
        20 => Foo::Field4,
        _ => unreachable!(),
    }
}

It was compiled as jump table.

f:
        add     edi, -10
        cmp     edi, 10
        ja      .LBB0_3
        lea     rax, [rip + .LJTI0_0]
        movsxd  rcx, dword ptr [rax + 4*rdi]
        add     rcx, rax
        jmp     rcx
.LBB0_2:
        mov     al, 10
        ret
.LBB0_5:
        mov     al, 12
        ret
.LBB0_6:
        mov     al, 20
        ret
.LBB0_4:
        mov     al, 11
        ret

For the case of smaller range, the compiler used another optimization.

#[no_mangle]
fn f(value: u32) -> Foo {
    match value {
        10 => Foo::Field1,
        11 => Foo::Field2,
        12 => Foo::Field3,
        14 => Foo::Field4,
        _ => unreachable!(),
    }
}
f:
        add     edi, -10
        cmp     edi, 5
        jae     .LBB0_3
        mov     eax, 23
        bt      eax, edi
        jae     .LBB0_3
        shl     edi, 3
        movabs  rax, 86067907338
        mov     ecx, edi
        shr     rax, cl
        ret

For the case taht matching arms are more than 4, the compiler used lookup table.

enum Foo {
    Field1 = 10,
    Field2 = 11,
    Field3 = 12,
    Field4 = 13,
    Field5 = 20,
}


#[no_mangle]
fn f(value: u32) -> Foo {
    match value {
        10 => Foo::Field1,
        11 => Foo::Field2,
        12 => Foo::Field3,
        13 => Foo::Field4,
        20 => Foo::Field5,
        _ => unreachable!(),
    }
}
f:
        add     edi, -10
        cmp     edi, 11
        jae     .LBB0_3
        mov     eax, 1039
        bt      eax, edi
        jae     .LBB0_3
        mov     eax, edi
        lea     rcx, [rip + .Lswitch.table.f]
        movzx   eax, byte ptr [rax + rcx]
        ret

I expected this, which can be attained only when the arm values are consecutive.

enum Foo {
    Field1 = 10,
    Field2 = 11,
    Field3 = 12,
    Field4 = 13,
    Field5 = 14,
}


#[no_mangle]
fn f(value: u32) -> Foo {
    match value {
        10 => Foo::Field1,
        11 => Foo::Field2,
        12 => Foo::Field3,
        13 => Foo::Field4,
        14 => Foo::Field5,
        _ => unreachable!(),
    }
}
f:
        add     edi, -10
        cmp     edi, 5
        jae     .LBB0_2
        add     dil, 10
        mov     eax, edi
        ret

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
Internal compiler ID: r1860

Compiler options:

-Copt-level=3

@ray90514 ray90514 added the C-bug Category: This is a bug. label May 9, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 9, 2025
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such 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