Recently I met a problem that my avx2 optimized program may crash on old machines like 2010 mac, which does not support avx2 intruction set. At the same time, I can ensure that all my avx2 code is surrounded by dynamically detection of instruction, which will not be run on an avx2-free machine. So i digged into this problem and found that the crash is caused by auto-vectorization conducted by llvm itself. I tried -fno-vectorize and -fno-slp-vectorize but found that once -mavx2 is set, the program will be auto vectorized. Is there a way to disable auto-vectorization in llvm with -mavx2 set? Because without -mavx2, my handwritten avx2 code may not be compiled successfully.
Asked
Active
Viewed 197 times
3
-
Did you try to self-defense & autodetect the AVX2 presence during the code runtime, before branching into AVX2? – user3666197 Nov 25 '20 at 14:17
-
Can you put the AVX2 code in a separate compilation unit? – chtz Nov 25 '20 at 15:28
-
yep,my handwritten avx2 is surrounded by autodetect condition. – Richard Yao Nov 26 '20 at 03:04
-
i wrote some inline assemably code, which mixed with normal c/c++ code, i have to used -mavx2 to compile them. The normal c/c++ code, which is necessary in any condition, will be auto-vectorized, may still cause crash on some old machines – Richard Yao Nov 26 '20 at 05:55
-
avx2 inline assemably does not need -mavx2 flag in compilation, checked. It seems that i have to aviod writing avx2 intrinsic code, which need -mavx2 flag – Richard Yao Nov 26 '20 at 07:34
1 Answers
1
An alternative to specifying the -mavx2
flag generally would be to use function attributes specifying avx2
just on the relevant functions.
void __attribute__ ((__target__ ("avx2"))) function_with_avx2(...) {
...
}
void function_without_avx2(...) {
...
}

dannyadam
- 3,950
- 2
- 22
- 19
-
It's a solution. But a little inconvenience in organizing my code. At last, I decide to avoid avx2 intrinsic and replace all avx2 intrinsic with handwritten assembly code – Richard Yao Nov 26 '20 at 07:37