Alignment - Alignment - 2022.1 English - UG1079

AI Engine Kernel Coding Best Practices Guide (UG1079)

Document_ID
UG1079
Release_Date
2022-05-25
Version
2022.1 English

Applications can load from data memory (DM) into vector registers and store the contents of vector registers into DM. Memory instructions in the AI Engine that operate on vectors have alignment requirements. Therefore, functions are provided for both aligned and unaligned accesses.

The following functions are assumed to operate on pointers that have met the alignment requirements for a vector load or store of the size.

  • aie::load_v
  • aie::store_v
Note: The 128-bit or 256-bit vector load and store operations in the AI Engine require that data be aligned on 128 bits. If the aligned vector load and store operations are used on unaligned pointers, the result data may be incorrect.

While the following functions are assumed to operate on pointers that have only aligned to the element of the vector.

  • aie::load_unaligned_v
  • aie::store_unaligned_v

For performance perspective, vector load and store must operate on memory that has met the vector operation alignment requirement. Unaligned accesses can incur additional overhead depending on the amount of misalignment. Following is an example of using aligned vector load instead of unaligned load for performance.

//assume factor is vector aligned
__attribute__((noinline)) void vect_mul(input_window<int32>* __restrict data1, output_window<int32>* __restrict out){
  int32* __restrict pv=(int32*)factor+1; //pv is unaligned vector pointer
  for(int i=0;i<64;i++)
  chess_prepare_for_pipelining
  {
    aie::vector<int32,8> va=window_readincr_v<8>(data1);
    aie::vector<int32,8> v1=aie::load_v<8>(pv-1);
    aie::vector<int32,8> v2=aie::load_v<8>(pv+7);
    pv+=8;
    //equivalent: aie::vector<int32,8> v3=aie::load_unaligned_v<8>(pv);
    aie::vector<int32,8> v3=aie::shuffle_down_fill(v1,v2,1); 
    auto vm=aie::mul(va,v3);
    window_writeincr(out,vm.to_vector(0));
  }
}
Note: Window interfaces ensure that internal buffers have the required alignment for vector loads.

The alignas standard C specifier can be used to ensure proper alignment of local memory. In the following example, the reals is aligned to 16-byte boundary.

alignas(16) const int32 reals[8] = 
       {32767, 23170, 0, -23170, -32768, -23170, 0, 23170};
       //align to 16 bytes boundary, equivalent to "alignas(aie::vector<int16,8>)"

The API provides a global constant value (aie::vector_decl_align) that can be used to align the buffer to a boundary that works for any vector size.

alignas(aie::vector_decl_align) static cint16 my_buffer[8]={{0,0},{1,-1},{2,-2},{3,-3},{4,-4},{5,-5},{6,-6},{7,-7}};
Note: The alignment requirement on AI Engine is 16 bytes. Because this requirement can change with future architectures, it is recommended to use aie::vector_decl_align for portability.