Search code examples
c++armcompiler-flags

What does "-flax-vector-conversions" exactly mean for ARM compiler?


I am tring to write a xxx.toolchain.cmake from arm-linux-gnueabihf gcc/g++ compiler.

What confused me is, whether should I use -flax-vector-conversions compilation flag or not. I read the doc/man page of the compiler, and it tells:

-flax-vector-conversions

Allow implicit conversions between vectors with differing numbers of elements and/or incompatible element types. This option should not be used for new code.

(via https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html)

My two confusions:

  1. What does "vectors" mean in this explanation? Is there any example illustrate this?

  2. What does "new code" mean? Why "new code" should not use with this compilation option?


Solution

  • GCC offers vector extensions that are meant to provide a way to access SIMD instructions in a machine-independent way (as opposed to intrinsics). This involves special vector types defined with __attribute__((vector_size(n))) to help the compiler understand the packed multiple-element data types that SIMD instructions use. Note that this has nothing to do with C++'s std::vector container.

    Consider the following code:

    typedef short eight_short __attribute__((vector_size(16)));
    typedef int four_int __attribute__((vector_size(16)));
    
    eight_short v1;
    four_int v2;
    
    void foo(void) {
        v2 = v1;
    }
    

    Here four_int and eight_short are vectors of the corresponding number of elements and types. They are both 16 bytes and thus suitable to store in a 128-bit SIMD register. Assigning one to the other is clearly meant to "reinterpret" (aka bit-cast), but it also violates type safety. Presumably older versions of the compiler used to accept such code, and there may be code like this out there, but the compiler authors want to discourage it. So such code now causes an error by default, but they provide the option -flax-vector-conversions that you can use when compiling old code like this to suppress the error.

    "New code" means code you are writing for the first time, where you have a choice as to how to write it. For such code you are most likely expected to use v2 = reinterpret_cast<four_int>(v1);, and not use -flax-vector-conversions. Then the compiler will flag any place where you forgot to cast (since it could be a bug where you actually meant something else).

    If you're compiling legacy code, your best bet would be to first try building without this option. If it builds successfully, then the option is not needed, so don't use it. If it gets errors about conversions of vector types, you could consider using this option, or else rewrite the code with explicit casts where needed.