5

I have a code that uses SSSE3 intrinsic commands (note the triple S) and a runtime check whether to use it, therefore I assumed that the application should execute on CPUs without SSSE3 support. However, when using -mssse3 with -O1 optimization the compiler also inserts SSSE3 instructions which I didn't explicitly call, hence the program crashes.

Is there a way to enable SSSE3 code when I EXPLICITLY call the relevant intrinsic functions, but to stop the compiler from adding its own SSSE3 code?

Note that I cannot disable the -O1 optimization.

jww
  • 97,681
  • 90
  • 411
  • 885
Mark S
  • 235
  • 3
  • 11
  • Related question: http://stackoverflow.com/questions/15584983/whats-the-proper-way-to-use-different-versions-of-sse-intrinsics-in-gcc – yohjp Jul 16 '13 at 08:00
  • compile different TUs with different settings. – PlasmaHH Jul 16 '13 at 08:23
  • 1
    Compile different files with different options, or different functions with different options (pragma, attribute), or use gcc-4.9. – Marc Glisse Jul 16 '13 at 09:51
  • @MarcGlisse - how will gcc 4.9 help me? – Mark S Jul 16 '13 at 10:10
  • 4
    Ah, right, the mention here is quite short: http://gcc.gnu.org/gcc-4.9/changes.html . gcc-4.9 lets you use intrinsics even when the current compilation mode says they are not available. – Marc Glisse Jul 16 '13 at 10:15
  • Also see [GCC Issue 57202 - Please make the intrinsics headers like immintrin.h be usable without compiler flags](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57202). – jww Apr 17 '17 at 13:22

2 Answers2

10

The solution to this problem is to NOT compile ALL the program code with the -mssse3 option, and only compile the portion that actually uses these features with that option. In other words:

 // main.cpp
 ... 

     if (use_ssse3()) 
         do_something_ssse3();
     else
         do_something_traditional();

 // traditional.cpp:
 void do_something_traditional()
 {
     ... 
     code goes here ... 
 }

 // ssse3.cpp:
 void do_something_ssse3()
 {
     ... 
     code goes here ... 
 }

Only "ssse3.cpp" should be compiled with the -mssse3 flag.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Also see [GCC Issue 57202 - Please make the intrinsics headers like immintrin.h be usable without compiler flags](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57202). – jww Apr 17 '17 at 13:22
0

If you use gcc, you can just compile the code without using the -mssse3 switch, and pull in the SSSE3 intrinsics with

#define __SSSE3__ 1
#include <tmmintrin.h>

where you need them.

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • 3
    Note that this won't work, since the __builtin_some_intrinsic will not be present when the compiler expands the inline function declared by the intrinsic functionality, and thus will not compile correctly. The compiler controls which builtin functions are declared by the -msse options. – Mats Petersson Jan 27 '14 at 22:59