Coding best practice for auto-vectorization
To encourage the Arm Compiler for HPC to produce optimal auto-vectorized output, code can be structured and hints can be provided to inform the compiler of program features that it would otherwise not be able to determine. This allows the compiler to produce optimal auto-vectorized output.
Use the restrict keyword if appropriate when using C/C++ code
restrict keyword (or the
__restrict__ keyword) indicates
to the compiler that a specified pointer does not alias with any other pointers for
the lifetime of that pointer. This guidance allows the compiler to vectorize loops
more aggressively, since it becomes possible to prove that loop iterations are
independent and can be executed in parallel.
Note: C code may use either the
__restrict__ keywords. C++ code must use only the
If these keywords are used erroneously (that is, if another pointer is used to access the same memory) then the behavior is undefined. It is possible that the results of optimized code will differ from that of its unoptimized equivalent.
The compiler supports pragmas that you can use to explicitly indicate that loop iterations are completely independent from each other.
Use < to construct loops
Where possible, use
!= when constructing loops. This helps the compiler to prove that a
loop terminates before the index variable wraps.
The compiler might also be able to perform more loop optimizations if signed integers are used, because the C standard allows for undefined behavior in the case of signed integer overflow. This is not the case for unsigned integers.
Use the -ffast-math option if it is safe to do so
This can significantly improve the performance of generated code, but it does so at the expense of strict compliance with IEEE and ISO standards for mathematical operations. Ensure that your algorithms are tolerant of potential inaccuracies that could be introduced by the use of this option.