Search code examples
armclangcross-compilingintrinsicsneon

How to translate neon intrinsics to llvm-IR using llvm-clang on x86


Using clang we can generate IR with compile C program:

clang -S -emit-llvm hello.c -o hello.ll

I would like to translate neon intrinsic to llvm-IR, code like this:

/* neon_example.c - Neon intrinsics example program */
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <arm_neon.h>
/* fill array with increasing integers beginning with 0 */
void fill_array(int16_t *array, int size)
{    int i;
    for (i = 0; i < size; i++)
    {
         array[i] = i;
    }
}
/* return the sum of all elements in an array. This works by calculating 4 totals (one for each lane) and adding those at the end to get the final total */
int sum_array(int16_t *array, int size)
{
     /* initialize the accumulator vector to zero */
     int16x4_t acc = vdup_n_s16(0);
     int32x2_t acc1;
     int64x1_t acc2;
     /* this implementation assumes the size of the array is a multiple of 4 */
     assert((size % 4) == 0);
     /* counting backwards gives better code */
     for (; size != 0; size -= 4)
     {
          int16x4_t vec;
          /* load 4 values in parallel from the array */
          vec = vld1_s16(array);
          /* increment the array pointer to the next element */
          array += 4;
          /* add the vector to the accumulator vector */
          acc = vadd_s16(acc, vec);
      }
      /* calculate the total */
      acc1 = vpaddl_s16(acc);
      acc2 = vpaddl_s32(acc1);
      /* return the total as an integer */
      return (int)vget_lane_s64(acc2, 0);
}
/* main function */
int main()
{
      int16_t my_array[100];
      fill_array(my_array, 100);
      printf("Sum was %d\n", sum_array(my_array, 100));
      return 0;
}

But It doesn't support neon intrinsic, and print error messages like this:

    /home/user/llvm-proj/build/bin/../lib/clang/4.0.0/include/arm_neon.h:65:24: error:
      'neon_vector_type' attribute is not supported for this target
typedef __attribute__((neon_vector_type(8))) float16_t float16x8_t;
                       ^

I think the reason is my host is on x86, but target is on ARM. And I have no idea how to Cross-compilation using Clang to translate to llvm-IR(clang version is 4.0 on ubuntu 14.04). Is there any target option commands or other tools helpful? And any difference between SSE and neon llvm-IR?


Solution

  • Using ELLCC, a pre-packaged clang based tool chain (http://ellcc.org), I was able to compile and run your program by adding -mfpu=neon:

        rich@dev:~$ ~/ellcc/bin/ecc -target arm32v7-linux -mfpu=neon neon.c
        rich@dev:~$ ./a.
        a.exe  a.out  
        rich@dev:~$ file a.out 
    a.out: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, BuildID[sha1]=613c22f6bbc277a8d577dab7bb27cd64443eb390, not stripped
    rich@dev:~$ ./a.out 
        Sum was 4950
        rich@dev:~$ 
    

    It was compiled on an x86 and I ran it using QEMU.

    Using normal clang, you'll also need the appropriate -target option for ARM. ELLCC uses slightly different -target options.