.o文件中缺少汇编功能

时间:2019-10-01 16:17:02

标签: ios assembly static-libraries openblas

我需要构建OpenBLAS静态库,其中包含汇编函数-https://github.com/xianyi/OpenBLAS/releases。就个人而言,我正在尝试构建0.3.6版本。

尽管编译过程成功,但涉及汇编函数时.o文件缺少实际的实现。

我的所有汇编功能都遇到问题,但我将举其中之一-_sdot_k为例。

如何定义缺少的实现?我可以通过以下事实来定义它:执行nm [library_name].a时,此函数的输出如下:

libopenblas_armv8p-r0.3.6.a(sdot_k.o):
0000000000000050 t .Ldot_kernel_F1
0000000000000058 t .Ldot_kernel_F10
0000000000000028 t .Ldot_kernel_F4
000000000000001c t .Ldot_kernel_F_BEGIN
00000000000000d8 t .Ldot_kernel_L999
00000000000000bc t .Ldot_kernel_S1
00000000000000c4 t .Ldot_kernel_S10
0000000000000084 t .Ldot_kernel_S4
0000000000000070 t .Ldot_kernel_S_BEGIN
0000000000000000 t ltmp0

T文件中没有.o标识符,该标识符指示函数实现在此处。当然,如果我将此库放入我的iOS项目中,则会出现这样的错误:

Undefined symbols for architecture arm64:
  "_sdot_k", referenced from:
      _strmv_TLN in libopenblas.a(strmv_TLN.o)
      _strmv_TLU in libopenblas.a(strmv_TLU.o)
      _strmv_TUN in libopenblas.a(strmv_TUN.o)
      _strmv_TUU in libopenblas.a(strmv_TUU.o)
      _trmv_kernel in libopenblas.a(strmv_thread_TLN.o)
      _trmv_kernel in libopenblas.a(strmv_thread_TLU.o)
      _trmv_kernel in libopenblas.a(strmv_thread_TUN.o)
      ...

这是实际的dot.S文件:

#define ASSEMBLER
#include "common.h"

#define N   x0  /* vector length */
#define X   x1  /* X vector address */
#define INC_X   x2  /* X stride */
#define Y   x3  /* Y vector address */
#define INC_Y   x4  /* Y stride */
#define I   x5  /* loop variable */

/*******************************************************************************
* Macro definitions
*******************************************************************************/

#if !defined(DOUBLE)
#if !defined(DSDOT)
#define REG0    wzr
#define DOTF    s0
#else // DSDOT
#define REG0    xzr
#define DOTF    d0
#endif
#define DOTI    s1
#define TMPX    s2
#define LD1VX   {v2.s}[0]
#define TMPY    s3
#define LD1VY   {v3.s}[0]
#define TMPVY   v3.s[0]
#define SZ  4
#else
#define REG0    xzr
#define DOTF    d0
#define DOTI    d1
#define TMPX    d2
#define LD1VX   {v2.d}[0]
#define TMPY    d3
#define LD1VY   {v3.d}[0]
#define TMPVY   v3.d[0]
#define SZ  8
#endif

/******************************************************************************/

.macro KERNEL_F1
    ldr TMPX, [X], #SZ
    ldr TMPY, [Y], #SZ
#if !defined(DSDOT)
    fmadd   DOTF, TMPX, TMPY, DOTF
#else // DSDOT
    fcvt    d3, TMPY
    fcvt    d2, TMPX
    fmul    d2, d2, d3
    fadd    DOTF, DOTF, d2
#endif
.endm

.macro KERNEL_F4
#if !defined(DOUBLE)
    ld1 {v2.4s}, [X], #16
    ld1 {v3.4s}, [Y], #16
#if !defined(DSDOT)
    fmla    v0.4s, v2.4s, v3.4s
#else
    fcvtl2  v5.2d, v3.4s
    fcvtl2  v4.2d, v2.4s
    fcvtl   v3.2d, v3.2s
    fcvtl   v2.2d, v2.2s
    fmul    v4.2d, v4.2d, v5.2d
    fmul    v2.2d, v2.2d, v3.2d
    fadd    v2.2d, v2.2d, v4.2d
    fadd    v0.2d, v0.2d, v2.2d
#endif
#else //DOUBLE
    ld1 {v2.2d, v3.2d}, [X], #32
    ld1 {v4.2d, v5.2d}, [Y], #32
    fmul    v2.2d, v2.2d, v4.2d
    fmul    v3.2d, v3.2d, v5.2d
    fadd    v0.2d, v0.2d, v2.2d
    fadd    v0.2d, v0.2d, v3.2d
#endif
    PRFM    PLDL1KEEP, [X, #1024]
    PRFM    PLDL1KEEP, [Y, #1024]
.endm

.macro KERNEL_F4_FINALIZE
#if !defined(DOUBLE)
#if !defined(DSDOT)
    ext v1.16b, v0.16b, v0.16b, #8
    fadd    v0.2s, v0.2s, v1.2s
    faddp   DOTF, v0.2s
#else
    faddp   DOTF, v0.2d
#endif
#else //DOUBLE
    faddp   DOTF, v0.2d
#endif
.endm

.macro INIT_S
#if !defined(DOUBLE)
    lsl INC_X, INC_X, #2
    lsl INC_Y, INC_Y, #2
#else
    lsl INC_X, INC_X, #3
    lsl INC_Y, INC_Y, #3
#endif
.endm

.macro KERNEL_S1
    ld1 LD1VX, [X], INC_X
    ld1 LD1VY, [Y], INC_Y
#if !defined(DSDOT)
    fmadd   DOTF, TMPX, TMPY, DOTF
#else // DSDOT
    fcvt    d3, TMPY
    fcvt    d2, TMPX
    fmul    d2, d2, d3
    fadd    DOTF, DOTF, d2
#endif
.endm

/*******************************************************************************
* End of macro definitions
*******************************************************************************/

    PROLOGUE

    fmov    DOTF, REG0
#if defined(DOUBLE)
    fmov    d6, DOTF
#endif

    cmp N, xzr
    ble .Ldot_kernel_L999

    cmp INC_X, #1
    bne .Ldot_kernel_S_BEGIN
    cmp INC_Y, #1
    bne .Ldot_kernel_S_BEGIN

.Ldot_kernel_F_BEGIN:

    asr I, N, #2
    cmp I, xzr
    beq .Ldot_kernel_F1

.Ldot_kernel_F4:

    KERNEL_F4

    subs    I, I, #1
    bne .Ldot_kernel_F4

    KERNEL_F4_FINALIZE

.Ldot_kernel_F1:

    ands    I, N, #3
    ble .Ldot_kernel_L999

.Ldot_kernel_F10:

    KERNEL_F1

    subs    I, I, #1
        bne     .Ldot_kernel_F10

    ret

.Ldot_kernel_S_BEGIN:

    INIT_S

    asr I, N, #2
    cmp I, xzr
    ble .Ldot_kernel_S1

.Ldot_kernel_S4:

    KERNEL_S1
    KERNEL_S1
    KERNEL_S1
    KERNEL_S1

    subs    I, I, #1
    bne .Ldot_kernel_S4

.Ldot_kernel_S1:

    ands    I, N, #3
    ble .Ldot_kernel_L999

.Ldot_kernel_S10:

    KERNEL_S1

    subs    I, I, #1
        bne     .Ldot_kernel_S10

.Ldot_kernel_L999:

    ret

    EPILOGUE

如您所见,在C标头中定义了PROLOGUEEPILOGUE这两个宏:

#ifndef F_INTERFACE
#define REALNAME ASMNAME
#else
#define REALNAME ASMFNAME
#endif

#if defined(ASSEMBLER) && !defined(NEEDPARAM)

#define PROLOGUE \
    .text ;\
    .align  2 ;\
    .globl  REALNAME ;\
REALNAME:

#define EPILOGUE

#define PROFCODE

#endif

我的胆量告诉我问题出在EPILOGUEPROLOGUE的某个地方,但是我没有足够的汇编语言知识来独自解决问题。

我也找到了这篇文章-https://developer.apple.com/library/archive/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html,但是对我没有多大帮助。也许是因为我缺乏组装技巧。

注意::如果有人在同一个库中苦苦挣扎,试图使其在iOS上运行,那么我的线程就是Github-https://github.com/xianyi/OpenBLAS/issues/2275#issuecomment-536982253is。它包含我克服的所有问题的描述。

注2::我确实需要OpenBLAS库,不幸的是,我不能选择使用Accelerate.framework

1 个答案:

答案 0 :(得分:0)

我很高兴地说我克服了这个问题。问题确实出在我的PROLOGUE宏中。

我正在使用的

PROLOGUE宏以这种方式转换为汇编代码:

.text; .align 2; .globl REALNAME; REALNAME:

那是一个问题。为了使Assembly标签起作用,它应该如下所示:

   .text; 
   .align 2; 
   .globl REALNAME; 
REALNAME:
   //assembly code

因此,为了获得该结果,我将C宏更改为GAS宏,如下所示:

.macro PROLOGUE
    .text
    .align 2
    .globl REALNAME
REALNAME:
.endm

那个问题消失了!

注意:为了消除任何混乱,;在这里实际上没有发挥作用。重要的是每个逗号后的新行。