Skip to content

Inefficient assembly for semantically equivalent way of matching on enum #85841

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

Closed
e00E opened this issue May 30, 2021 · 3 comments
Closed

Inefficient assembly for semantically equivalent way of matching on enum #85841

e00E opened this issue May 30, 2021 · 3 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.

Comments

@e00E
Copy link
Contributor

e00E commented May 30, 2021

https://godbolt.org/z/fYfohcsax

#[derive(Clone, Copy)]
pub enum E {
    E0,
    E1,
    E2,
    E3,
}

impl E {
    pub fn next0(self) -> Option<Self> {
        match self {
            E::E0 => Some(E::E1),
            E::E1 => Some(E::E2),
            E::E2 => Some(E::E3),
            E::E3 => None,
        }
    }

    pub fn next1(self) -> Option<Self> {
        Some(match self {
            E::E0 => E::E1,
            E::E1 => E::E2,
            E::E2 => E::E3,
            E::E3 => return None,
        })
    }
}

Optimized assembly on stable 1.52.0 and nightly:

example::E::next0:
        lea     eax, [rdi + 1]
        ret

example::E::next1:
        mov     al, 4
        mov     cl, 1
        movzx   edx, dil
        lea     rsi, [rip + .LJTI1_0]
        movsxd  rdx, dword ptr [rsi + 4*rdx]
        add     rdx, rsi
        jmp     rdx
.LBB1_1:
        mov     cl, 2
        jmp     .LBB1_3
.LBB1_2:
        mov     cl, 3
.LBB1_3:
        mov     eax, ecx
.LBB1_4:
        ret
.LJTI1_0:
        .long   .LBB1_3-.LJTI1_0
        .long   .LBB1_1-.LJTI1_0
        .long   .LBB1_2-.LJTI1_0
        .long   .LBB1_4-.LJTI1_0

I expected next1 to compile to the same assembly as next0.

Edit 2022-12-01: Not fixed on current stable 1.65 and nightly.

@e00E e00E added the C-bug Category: This is a bug. label May 30, 2021
@JulianKnodt
Copy link
Contributor

If you look at the emitted mir, I think what's causing this is that an extra temporary is used to store the enum type and is not const as is marked in next0. I'm curious why the 2nd implementation doesn't have E::E1-3 marked as const?

@nikic nikic added the I-slow Issue: Problems and improvements with respect to performance of generated code. label Jun 7, 2021
@nikic nikic added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Jul 14, 2023
@nikic
Copy link
Contributor

nikic commented Jul 14, 2023

Upstream issue: llvm/llvm-project#63876

@e00E
Copy link
Contributor Author

e00E commented Dec 18, 2024

Fixed on current Rust 1.83.

@e00E e00E closed this as completed Dec 18, 2024
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.
Projects
None yet
Development

No branches or pull requests

3 participants