3

Is there a way to enable vectorization only for some part of the code, like a pragma directive? Basically having as if the -ftree-vectorize is enabled only while compiling some part of the code? Pragma simd for example is not available with gcc...

The reason is that from benchmarking we saw that with -O3 (which enables vectorization) the timings were worse than with -O2. But there are some part of the code for which we would like the compiler to try vectorizing loops.

One solution I could use would be to restrict the compiler directive to one file.

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • Typically vectorization should *speed up* your code, **if** it has been applied. Are you sure of that? How did you find out it slowed down the code? – edmz Sep 17 '14 at 18:22
  • @black We are speaking about a big project, where there's, however, not so much to vectorize. -O3 introduces some overhead that can in some cases generate slower code http://stackoverflow.com/a/19985801/2436175. It was the case in our timing benchmarking. Turning on tree-vectorize globally has the same risk, while I am in the situation to guess very well which part of code it would be worth to ask to the compiler to vectorize. – Antonio Sep 17 '14 at 18:44

1 Answers1

4

Yes, this is possible. You can either disable it for the whole module or individual functions. You can't however do this for particular loops.

For individual functions use __attribute__((optimize("no-tree-vectorize"))).

For whole modules -O3 automatic enables -ftree-vectorize. I'm not sure how to disable it once it's enabled but you can use -O2 instead. If you want to use all of -O3 except -ftree-vectorize then do this

gcc -c -Q -O3 --help=optimizers > /tmp/O3-opts
gcc -c -Q -O2 --help=optimizers > /tmp/O2-opts
diff /tmp/O2-opts /tmp/O3-opts | grep enabled

And then include all the options except for -ftree-vectorize.

Edit: I don't see -fno-tree-vectorize in the man pages but it works anyway so you can do -O3 -fno-tree-vectorize.

Edit: The OP actually wants to enable vectorization for particular functions or whole modules. In that case for individual functions __attribute__((optimize("tree-vectorize"))) can be used and for whole modules -O2 -ftree-vectorize.

Edit (from Antonio): In theory there is a pragma directive to enable tree-vectorizing all functions that follow

#pragma GCC optimize("tree-vectorize")

But it seems not to work with my g++ compiler, maybe because of the bug mentioned here: How to enable optimization in G++ with #pragma. On the other hand, the function attribute works.

Community
  • 1
  • 1
Z boson
  • 32,619
  • 11
  • 123
  • 226
  • Thanks! I was reading about "attribute" exactly now :) BTW, I did not explain myself very clearly: we would like to stay with -O2, and enable vectorization locally (on some files, or on some functions). – Antonio Sep 17 '14 at 18:56
  • @Antonio, actually you were clear. It's my fault, I read your question wrong. I think you can use `__attribute__((optimize("tree-vectorize")))` or `-O2 -ftree-vectorize` for whole modules. – Z boson Sep 17 '14 at 19:01
  • @Antonio, I'm slightly confused by your request to enable vectorizing all functions. You say `#pragma GCC optimize("tree-vectorize")` does not work but what's wrong with `-ftree-vectorize`. Shouldn’t that do what you want? – Z boson Sep 18 '14 at 11:45
  • I am working in a project where adding a specific flag for only one file would be difficult. So, if it was the file itself that made this request it would be better. – Antonio Sep 18 '14 at 11:54
  • @Antonio, oh, I understand. I'm glad that `__attribute__((optimize("tree-vectorize")))` still works then. – Z boson Sep 18 '14 at 11:56