簡體   English   中英

Clang++/g++ 不在 Aarch64 上矢量化代碼

[英]Clang++/g++ not vectorizing code on Aarch64

我有一個帶有四核 Cortex-A57 的 SBC,並且正在嘗試使用編譯器自動矢量化來試驗 Neon。 在 Ubuntu 18.04 上同時使用 clang++ (5.0.1-4) 和 g++ (7.4.0),下面非常簡單的代碼沒有被向量化(即,它在任何時候都不使用 v 寄存器):

#include <iostream>
#include <cstdint>

int main(void)
    {
    const uint32_t  LEN = 16;
    float           input_1[LEN] __attribute__((__aligned__(16))),
                    input_2[LEN] __attribute__((__aligned__(16))),
                    output [LEN] __attribute__((__aligned__(16)));

    for(uint32_t i = 0; i < LEN; i++)
        {
        input_1[i] = i;
        input_2[i] = i * 2;
        }

    for(uint32_t i = 0; i < LEN; i++)
        output[i] = input_1[i] * input_2[i];

    std::cout << output[0] << std::endl;

    return 0;
    }

它只是聲明了 3 個 arrays,填充 2 並將它們乘以 output 數組。 最后的 cout 是為了防止編譯器擺脫一切。 我沒有發布 objdump -d 的結果以避免向人們的屏幕發送垃圾郵件,但如果有人願意,我可以。 編譯行是:clang++ -O3 neon.cpp -o neon(g++也一樣)

我也嘗試過 -mcpu=cortex-a57 但它也不會矢量化。 然后我發現這篇文章https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65951

評論 11 說,根據情況,如果沒有性能優勢,編譯器可能會決定不進行向量化。 根據您的經驗,似乎是這樣,還是我錯過了什么?

====== 編輯 ======

g++為上述代碼制作的程序集為:

    .arch armv8-a
    .file   "neon_test.cpp"
    .text
    .section    .text.startup,"ax",@progbits
    .align  2
    .p2align 3,,7
    .global main
    .type   main, %function
main:
.LFB1563:
    .cfi_startproc
    stp x29, x30, [sp, -112]!
    .cfi_def_cfa_offset 112
    .cfi_offset 29, -112
    .cfi_offset 30, -104
    adrp    x1, .LC0
    adrp    x0, :got:_ZSt4cout
    movi    d0, #0
    add x29, sp, 0
    .cfi_def_cfa_register 29
    str x19, [sp, 16]
    .cfi_offset 19, -96
    adrp    x19, :got:__stack_chk_guard
    ldr q1, [x1, #:lo12:.LC0]
    ldr x19, [x19, #:got_lo12:__stack_chk_guard]
    ldr x0, [x0, #:got_lo12:_ZSt4cout]
    ldr x1, [x19]
    str x1, [x29, 104]
    mov x1,0
    str q1, [x29, 32]
    bl  _ZNSo9_M_insertIdEERSoT_
    bl  _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
    ldr x0, [x29, 104]
    ldr x1, [x19]
    eor x1, x0, x1
    cbnz    x1, .L5
    ldr x19, [sp, 16]
    mov w0, 0
    ldp x29, x30, [sp], 112
    .cfi_remember_state
    .cfi_restore 30
    .cfi_restore 29
    .cfi_restore 19
    .cfi_def_cfa 31, 0
    ret
.L5:
    .cfi_restore_state
    bl  __stack_chk_fail
    .cfi_endproc
.LFE1563:
    .size   main, .-main
    .section    .rodata.cst16,"aM",@progbits,16
    .align  4
.LC0:
    .word   0
    .word   1073741824
    .word   1090519040
    .word   1099956224
    .section    .text.startup
    .align  2
    .p2align 3,,7
    .type   _GLOBAL__sub_I_main, %function
_GLOBAL__sub_I_main:
.LFB2050:
    .cfi_startproc
    stp x29, x30, [sp, -32]!
    .cfi_def_cfa_offset 32
    .cfi_offset 29, -32
    .cfi_offset 30, -24
    add x29, sp, 0
    .cfi_def_cfa_register 29
    str x19, [sp, 16]
    .cfi_offset 19, -16
    adrp    x19, .LANCHOR0
    add x19, x19, :lo12:.LANCHOR0
    mov x0, x19
    bl  _ZNSt8ios_base4InitC1Ev
    adrp    x0, :got:_ZNSt8ios_base4InitD1Ev
    mov x1, x19
    ldr x19, [sp, 16]
    adrp    x2, __dso_handle
    ldr x0, [x0, #:got_lo12:_ZNSt8ios_base4InitD1Ev]
    add x2, x2, :lo12:__dso_handle
    ldp x29, x30, [sp], 32
    .cfi_restore 30
    .cfi_restore 29
    .cfi_restore 19
    .cfi_def_cfa 31, 0
    b   __cxa_atexit
    .cfi_endproc
.LFE2050:
    .size   _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
    .section    .init_array,"aw"
    .align  3
    .xword  _GLOBAL__sub_I_main
    .bss
    .align  3
    .set    .LANCHOR0,. + 0
    .type   _ZStL8__ioinit, %object
    .size   _ZStL8__ioinit, 1
_ZStL8__ioinit:
    .zero   1
    .hidden __dso_handle
    .ident  "GCC: (Ubuntu/Linaro 7.4.0-1ubuntu1~18.04.1) 7.4.0"
    .section    .note.GNU-stack,"",@progbits

您在執行實際數學運算的同一 function 中生成輸入。

問:那么編譯器在-o3下做了什么?

答:它在構建時進行數學運算,並將結果存儲在某種 LUT 中。 ( .LC0 )

您應該在外部 function 中初始化輸入,最好是在不同的文件中,以避免編譯器的這種“構建時間數學”作弊。

0 = 0.0f = 0.0f * 2.0f * 0.0f
1073741824 = 2.0f = 1.0f * 2.0f * 1.0f
1090519040 = 8.0f = 2.0f * 2.0f * 2.0f
1099956224 = 18.0f = 3.0f * 2.0f * 3.0f

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM