We compile our code with g++ -march=ivybridge -mtune=skylake
. In case somebody runs on older/incompatible architecture I want app to inform and exit gracefully. How do I do this? How about AMD processors? Is there some sort of parity of architectures/instructions?
Asked
Active
Viewed 183 times
4

MateuszL
- 2,751
- 25
- 38
-
Do you mean [cpuid](https://stackoverflow.com/questions/17758409/intrinsics-for-cpuid-like-informations)? – rustyx Sep 18 '20 at 09:25
-
2You would need to have separate runner compiled for any architecture that will either report error or run your specifically compiled program. This seems like safe generic solution – bartop Sep 18 '20 at 09:25
-
`/proc/cpuinfo` might have some useful info – bobah Sep 18 '20 at 09:25
-
On x86, you can maybe use `__builtin_cpu_supports` in `main` in a source file compiled without any `-march` (i.e. baseline). As @bartop points out, you can't *safely* run any code compiled with `-march=xyz` on a machine that might be older than xyz. See [does gcc's \_\_builtin\_cpu\_supports check for OS support?](https://stackoverflow.com/q/48677575) . But in theory you'd have to check every single feature, not just SIMD ones like AVX. – Peter Cordes Sep 18 '20 at 15:46
-
`__builtin_cpu_is("ivybridge")` is only true on IvB, not IvB-and-later (false on Skylake for example). https://gcc.gnu.org/onlinedocs/gcc/x86-Built-in-Functions.html. Enumerating all known CPU models now would create a binary that refuses to work on next year's new CPU, so that's terrible, ruling out that idea. – Peter Cordes Sep 18 '20 at 15:51
-
(Also, note that Haswell is the ISA that introduced AVX2 and FMA for Intel, and BMI1 / BMI2. IvyBridge is missing a *lot* of really useful stuff. So you might want some multiversioned functions, depending on your application, if you can't target an AVX2 + FMA + BMI2 baseline.) – Peter Cordes Sep 18 '20 at 15:52