Skip to content

Strange jmp 0 emitted in size-optimized x86-64 assembly #127375

Open
@dead-claudia

Description

@dead-claudia

Godbolt link (includes this and another buggy example)

I tried this code:

pub fn parse_u32_digit(acc: u32, byte: u8) -> Option<u32> {
    match (acc.checked_mul(10), byte.checked_sub(b'0')) {
        (Some(next), Some(v @ 0..=9)) => next.checked_add(v as u32),
        _ => None,
    }
}

I expected to see this happen: assembly that looks something like this:

parse_u32_digit:
        mov     eax, edi
        xor     ecx, ecx
        mov     edx, 10
        mul     edx
        jo      .LBB0_1
        mov     edx, eax
        lea     eax, [rsi - 58]
        cmp     al, -10
        jb      .LBB0_4
        add     sil, -48
        movzx   eax, sil
        xor     ecx, ecx
        add     edx, eax
        setae   cl
.LBB0_1:
.LBB0_4:
        mov     eax, ecx
        ret

Instead, this happened: the following assembly:

parse_u32_digit:
        mov     eax, edi
        xor     ecx, ecx
        mov     edx, 10
        mul     edx
        jo      .LBB0_1
        mov     edx, eax
        lea     eax, [rsi - 58]
        cmp     al, -10
        jb      .LBB0_4
        add     sil, -48
        movzx   eax, sil
        xor     ecx, ecx
        add     edx, eax
        setae   cl
        jmp     .LBB0_4
.LBB0_1:
.LBB0_4:
        mov     eax, ecx
        ret

Meta

rustc --version --verbose:

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7
Compiler returned: 0

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-heavyIssue: Problems and improvements with respect to binary size 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