pacak / cargo-show-asm

cargo subcommand showing the assembly, LLVM-IR and MIR generated for Rust code
Apache License 2.0
712 stars 35 forks source link

ASM output removing labels used by `cbz` and `cbnz` instructions on aarch64 #318

Closed GrayJack closed 1 month ago

GrayJack commented 1 month ago

I noticed that the output of cargo asm on aarch64 removes the labels used on the branching instructions cbz and cbnz.

I made a simple strchr example:

#[no_mangle]
pub unsafe extern "C" fn strchr(mut str: *const c_char, ch: c_int) -> *mut c_char {
    let ch = ch as c_char;
    loop {
        if *str == 0 {
            break ptr::null_mut();
        }

        if *str == ch {
            break str as *mut c_char;
        }

        str = str.add(1);
    }
}

Output:

    .globl  _strchr
    .p2align    2
_strchr:
Lfunc_begin439:
    .cfi_startproc
    ldrb w9, [x0]
    cbz w9, LBB439_4
    and w8, w1, #0xff
    cmp w9, w8
    b.eq LBB439_5
    ldrb w9, [x0, #1]!
    cbnz w9, LBB439_2
    mov x0, #0
LBB439_5:
    ret

Expected result should be:

    .globl  _strchr
    .p2align    2
_strchr:
    ldrb    w9, [x0]
    cbz w9, LBB439_4
    and w8, w1, #0xff
LBB439_2:
    cmp w9, w8
    b.eq    LBB439_5
    ldrb    w9, [x0, #1]!
    cbnz    w9, LBB439_2
LBB439_4:
    mov x0, #0
LBB439_5:
    ret

I tested this on aarch64 macOS and Linux and this happens to these instruction on both.

pacak commented 1 month ago

Is this actual or expected output? And what should the other output should be?

GrayJack commented 1 month ago

Sorry, I forgot to include that.

The expected result would be:

    .globl  _strchr
    .p2align    2
_strchr:
    ldrb    w9, [x0]
    cbz w9, LBB439_4
    and w8, w1, #0xff
LBB439_2:
    cmp w9, w8
    b.eq    LBB439_5
    ldrb    w9, [x0, #1]!
    cbnz    w9, LBB439_2
LBB439_4:
    mov x0, #0
LBB439_5:
    ret
pacak commented 1 month ago

Will take a look. Can you include the .s file from target folder? You should be able to find exact name if you pass one or more -v flags - this way I should be able to test if it worked without having to mess with cross-compilation.

GrayJack commented 1 month ago

Sure! Here it is:

tests-02b2c38f374f3d00.s.zip

pacak commented 1 month ago

0.2.41 is out, this issue should be fixed.