Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime cpu detection in dynamic linker (ld.so)

I want to integrate a runtime CPU Dispatch into my library. I have several version of some functions, optimized for sse2/sse3/avx and a x87 generic variant. I want to compile all versions into single .so library and I think how to implement a cpu dispatcher.

A fastest way as I think, is to get cpu dispatching at linking step (dynamic linking), so when ld.so will load my library, I want it to check, does cpu supports sse2, sse3 or avx, and then I want ld.so to select right set of functions.

For example (using gcc target attribute):

Library:

float* func3_generic(float *a, float *b)  __attribute__ ((__target__ ("fpmath=387")));
float* func3_sse2(float *a, float *b)  __attribute__ ((__target__ ("sse2")));
float* func3_sse3(float *a, float *b)  __attribute__ ((__target__ ("sse3")));
float* func3_avx(float *a, float *b)  __attribute__ ((__target__ ("avx")));

I want to have some special symbol func3(), which will be setted by linker (ld.so) to the most advanced of func3_generic, func3_sse2, func3_sse3, func3_avx. So, if the cpu is Core i7-xxxx, I want that every call to func3 will be the call to func3_avx, and if cpu is PentiumPro, call to func3 will be call to the func3_generic.

At same time I want not to write a lot of dispatching code manually and I want the right variant selected with minimum overhead (no extra indirect jump). This means that I can afford extra time in application startup, but no anything extra in calling this function (there are very high number of calls in some cases).

UPDATE. Linker can do dispatching based on AUXV vector, AT_HWCAP: field:

$ LD_SHOW_AUXV=1 /bin/echo
...
AT_HWCAP:    fpu ... mmx fxsr sse sse2
like image 246
osgx Avatar asked Nov 27 '22 11:11

osgx


1 Answers

… Would it be acceptable to simply load one of several .so's using dlopen? You could query the CPU type in any of a variety of methods and then choose the appropriate library to bind func3

like image 167
BRPocock Avatar answered Nov 29 '22 01:11

BRPocock