Skip to content

Inline asm! is unnecessarily duplicated #149832

@ChrisDenton

Description

@ChrisDenton

The following example code:

unsafe extern "system" {
    safe fn finish_ffi_stuff(handle: isize) -> bool;
    safe fn do_ffi_stuff(path: *const u8) -> isize;
    safe fn get_ffi_result() -> u32;
}

//#[inline(never)]
fn get_result() -> u32 {
    let code: u32;
    #[cfg(target_arch = "x86_64")]
    unsafe {
        core::arch::asm!(
            "call {}", // yes, I know. this is just an example.
            sym get_ffi_result,
            out("eax") code,
            options(nostack, preserves_flags, pure, readonly)
        );
    }
    code
}

fn finish_stuff(handle: isize) -> Result<(), u32> {
    match finish_ffi_stuff(handle) {
        false => Err(get_result()),
        true => Ok(())
    }
}

fn do_stuff(path: &str) -> Result<isize, u32> {
    let handle = do_ffi_stuff(path.as_ptr());
    match handle {
        -1 => Err(get_result()),
        _ => Ok(handle)
    }
}

pub fn my_main() -> u32 {
    match do_stuff("meow") {
        Ok(meow) => finish_stuff(meow).err().unwrap_or(0),
        Err(e) => e
    }
}

Produces the following x86_64 assembly:

example[46d8dd5c5912c656]::my_main:
        push    rax
        lea     rdi, [rip + .Lanon.014ce376276f773fc8cbe61d4d1296c1.0]
        call    qword ptr [rip + do_ffi_stuff@GOTPCREL]
        cmp     rax, -1
        je      .LBB0_3
        mov     rdi, rax
        call    qword ptr [rip + finish_ffi_stuff@GOTPCREL]
        test    al, al
        je      .LBB0_4
        xor     eax, eax
        pop     rcx
        ret
.LBB0_3:

        call    get_ffi_result

        pop     rcx
        ret
.LBB0_4:

        call    get_ffi_result

        pop     rcx
        ret

.Lanon.014ce376276f773fc8cbe61d4d1296c1.0:
        .ascii  "meow"

Note that .LBB0_3 and .LBB0_4 are exact duplicates. While the compiler can't see inside asm blocks, I'd assume it at least knows that they are the same in this case.

Meta

rustc --version --verbose:

rustc 1.94.0-nightly (ba86c0460 2025-12-06)
binary: rustc
commit-hash: ba86c0460b0233319e01fd789a42a7276eade805
commit-date: 2025-12-06
host: x86_64-unknown-linux-gnu
release: 1.94.0-nightly
LLVM version: 21.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inline-assemblyArea: Inline assembly (`asm!(…)`)C-bugCategory: This is a bug.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions