简体   繁体   English

为什么矢量化失败?

[英]Why does vectorization fail?

I want to optimize my code for vectorization using 我想使用优化我的代码进行矢量化

-msse2 -ftree-vectorizer-verbose=2.

I have the following simple code: 我有以下简单的代码:

int main(){
  int a[2048], b[2048], c[2048];
  int i;

  for (i=0; i<2048; i++){
      b[i]=0;
      c[i]=0;
  }

  for (i=0; i<2048; i++){
    a[i] = b[i] + c[i];
  }
  return 0;
}

Why do I get the note 为什么我会收到这个说明

 test.cpp:10: note: not vectorized: not enough data-refs in basic block.

Thanks! 谢谢!

For what it's worth, after adding an asm volatile("": "+m"(a), "+m"(b), "+m"(c)::"memory"); 为了它的价值,在添加asm volatile("": "+m"(a), "+m"(b), "+m"(c)::"memory"); near the end of main , my copy of gcc emits this: main的末尾附近,我的gcc副本发出:

400610:       48 81 ec 08 60 00 00    sub    $0x6008,%rsp
400617:       ba 00 20 00 00          mov    $0x2000,%edx
40061c:       31 f6                   xor    %esi,%esi
40061e:       48 8d bc 24 00 20 00    lea    0x2000(%rsp),%rdi
400625:       00
400626:       e8 b5 ff ff ff          callq  4005e0 <memset@plt>
40062b:       ba 00 20 00 00          mov    $0x2000,%edx
400630:       31 f6                   xor    %esi,%esi
400632:       48 8d bc 24 00 40 00    lea    0x4000(%rsp),%rdi
400639:       00
40063a:       e8 a1 ff ff ff          callq  4005e0 <memset@plt>
40063f:       31 c0                   xor    %eax,%eax
400641:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)
400648:       c5 f9 6f 84 04 00 20    vmovdqa 0x2000(%rsp,%rax,1),%xmm0
40064f:       00 00
400651:       c5 f9 fe 84 04 00 40    vpaddd 0x4000(%rsp,%rax,1),%xmm0,%xmm0
400658:       00 00
40065a:       c5 f8 29 04 04          vmovaps %xmm0,(%rsp,%rax,1)
40065f:       48 83 c0 10             add    $0x10,%rax
400663:       48 3d 00 20 00 00       cmp    $0x2000,%rax
400669:       75 dd                   jne    400648 <main+0x38>

So it recognised that the first loop was just doing memset to a couple arrays and the second loop was doing a vector addition, which it appropriately vectorised. 所以它认识到第一个循环只是对几个数组进行memset而第二个循环正在进行向量加法,它适当地进行了矢量化。

I'm using gcc version 4.9.0 20140521 (prerelease) (GCC) . 我正在使用gcc version 4.9.0 20140521 (prerelease) (GCC)

An older machine with gcc version 4.7.2 (Debian 4.7.2-5) also vectorises the loop, but in a different way. 使用gcc version 4.7.2 (Debian 4.7.2-5)的旧机器也会以不同的方式对循环进行矢量化。 Your -ftree-vectorizer-verbose=2 setting makes it emit the following output: 你的-ftree-vectorizer-verbose=2设置使它发出以下输出:

Analyzing loop at foo155.cc:10


Vectorizing loop at foo155.cc:10

10: LOOP VECTORIZED.
foo155.cc:1: note: vectorized 1 loops in function.

You probably goofed your compiler flags (I used g++ -O3 -ftree-vectorize -ftree-vectorizer-verbose=2 -march=native foo155.cc -o foo155 to build) or have a really old compiler. 你可能会搞砸你的编译器标志(我使用g++ -O3 -ftree-vectorize -ftree-vectorizer-verbose=2 -march=native foo155.cc -o foo155来构建)或者有一个非常古老的编译器。

remove the first loop and do this 删除第一个循环并执行此操作

int a[2048], b[2048], c[2048] = {0}; int [2048],b [2048],c [2048] = {0};

also try this tag 也尝试这个标签

-ftree-vectorize 

instead of 代替

-msse2 -ftree-vectorizer-verbose=2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM