Search code examples
iosassemblystatic-librariesopenblas

Assembly function is missing in .o file


I need to build OpenBLAS static library which contains assembly functions - https://github.com/xianyi/OpenBLAS/releases . Personally, I'm trying to build 0.3.6 version.

Although, compilation process is successful, .o files are missing actual implementations when it comes to assembly functions.

I have problems with all my assembly functiosn, but I will give as an example one of them - _sdot_k.

How can I define that implementation is missing? I can define it by that fact that when I do nm [library_name].a, output for this function is following:

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

There's no T identifier which tells that function implementation is here, in .o file. And of course, if I put this library into my iOS project I have such error:

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)
      ...

Here's the actual dot.S file:

#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

As you can see, there's PROLOGUE and EPILOGUE which are macroses, defined in C header:

#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

My guts are telling me that the problem lies somewhere in EPILOGUE and PROLOGUE, but I don't have decent Assembler knowledge to figure out a problem on my own.

Also I found this article - https://developer.apple.com/library/archive/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html, but it didn't help me much. Maybe because of my lack of Assembly skill.

NOTE: If anyone is struggling with the same library trying to make it work on iOS, here's my thread on it's Github - https://github.com/xianyi/OpenBLAS/issues/2275#issuecomment-536982253is. It contains descriptions of all problems I overcomed.

NOTE 2: I really need OpenBLAS library and using Accelerate.framework is not an option in my case, unfortunately.


Solution

  • I'm happy to say that I overcome that issue. The problem was indeed in my PROLOGUE macros.

    PROLOGUE macros I was using was converting to the assembly code in this manner:

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

    And that was an issue. In order to make Assembly label work it should look like this:

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

    So in order to achieve that result I changed C macro to GAS macro as such:

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

    After that issue was gone!

    NOTE: In order to remove any confusions, ; doesn't really play a role here. It's new line after each comma what matters.