Skip to content

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

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
dead-claudia opened this issue Jul 5, 2024 · 3 comments
Open

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

dead-claudia opened this issue Jul 5, 2024 · 3 comments
Assignees
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-heavy Issue: Problems and improvements with respect to binary size of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dead-claudia
Copy link

dead-claudia commented Jul 5, 2024

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
@dead-claudia dead-claudia added the C-bug Category: This is a bug. label Jul 5, 2024
@dead-claudia dead-claudia changed the title Strange jmp 0 emitted in size-optimized assembly Strange jmp 0 emitted in size-optimized x86-64 assembly Jul 5, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 5, 2024
@workingjubilee
Copy link
Member

@rustbot label: +C-optimization -C-bug +I-heavy +A-LLVM +A-codegen

@rustbot rustbot added A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-heavy Issue: Problems and improvements with respect to binary size of generated code. and removed C-bug Category: This is a bug. labels Jul 5, 2024
@workingjubilee workingjubilee added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 5, 2024
@the8472
Copy link
Member

the8472 commented Jul 5, 2024

#126585 looks somewhat similar

@dianqk
Copy link
Member

dianqk commented Jul 9, 2024

#126585 looks somewhat similar

Yes. But it's not exactly duplicated. This should be in the plan I mentioned at llvm/llvm-project#95932 (comment).

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-heavy Issue: Problems and improvements with respect to binary size of generated code. 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

5 participants