rust-lang / rust-lang/rust

Strange `jmp 0` emitted in size-optimized x86-64 assembly

Open
#127,375 3 comments 0 reactions 1 assignee View on GitHub

@dianqk is already working on this.

Since Jul 9, 2024.

A-codegen A-LLVM C-optimization I-heavy T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.