g++ inlining failed in call to always_inline "int _rdrand16_step()"

Viewed 1589

I'm wrote a code using the Intel function _rdrand16_step(), on Windows(Visual Studio 2017) works fine but on Linux(g++) I can't make it work. I call that function 2 times in my code:

#include <immintrin.h>
...
unsigned short val = 0;
if (_rdrand16_step(&val))
...
_rdrand16_step(&val);
...

and the g++ output this:

/usr/lib/gcc/x86_64-linux-gnu/8/include/immintrin.h: In member function ‘int otp_s7c::crypt(std::__cxx11::string, std::__cxx11::string, long long unsigned int)’:
/usr/lib/gcc/x86_64-linux-gnu/8/include/immintrin.h:129:1: error: inlining failed in call to always_inline ‘int _rdrand16_step(short unsigned int*)’: target specific option mismatch
 _rdrand16_step (unsigned short *__P)
 ^~~~~~~~~~~~~~
otp_s7c.cpp:139:24: note: called from here
      if (_rdrand16_step(&val))
          ~~~~~~~~~~~~~~^~~~~~
/usr/lib/gcc/x86_64-linux-gnu/8/include/immintrin.h:129:1: error: inlining failed in call to always_inline ‘int _rdrand16_step(short unsigned int*)’: target specific option mismatch
 _rdrand16_step (unsigned short *__P)
 ^~~~~~~~~~~~~~
otp_s7c.cpp:148:23: note: called from here
         _rdrand16_step(&val);
         ~~~~~~~~~~~~~~^~~~~~
2 Answers

This is a somewhat misleading error message stemming from the fact that you are not actually telling the compiler that the RDRAND instruction is supported on your target architecture (as far as I can tell, the important part here is the "target specific option mismatch" part at the end).

Adding -mrdrnd to the compiler flags seems to fix the issue.


Compare an example on Compiler Explorer with and without the flag

I had the same problem in cmake a week ago. But when the following command is added, this problem disappear.

SET(CMAKE_C_FLAGS "-mrdrnd")
Related