|
| 1 | +include(CheckCXXCompilerFlag) |
| 2 | +include(CheckCXXSourceRuns) |
| 3 | + |
| 4 | +# Helper to test: compiler accepts flag AND the produced binary actually runs. |
| 5 | +function(check_isa_runs ISA_NAME ISA_FLAG TEST_SRC OUT_VAR) |
| 6 | + set(${OUT_VAR} FALSE PARENT_SCOPE) |
| 7 | + |
| 8 | + # 1) Does the compiler accept the flag? |
| 9 | + |
| 10 | + #if("${ISA_FLAG}" STREQUAL "") |
| 11 | + # # No flag to test, assume true (e.g. BMI2 on MSVC) |
| 12 | + # message(STATUS " ${ISA_NAME}: no special compiler flag to #test") |
| 13 | + # set(HAVE_FLAG TRUE) |
| 14 | + #else() |
| 15 | + # message(STATUS " ${ISA_NAME}: testing compiler flag: #${ISA_FLAG}") |
| 16 | + # check_cxx_compiler_flag(${ISA_FLAG} HAVE_FLAG) |
| 17 | + #endif() |
| 18 | + # |
| 19 | + #if (NOT HAVE_FLAG) |
| 20 | + # message(STATUS " ${ISA_NAME}: compiler does not accept #flag: ${ISA_FLAG}") |
| 21 | + # return() |
| 22 | + #endif() |
| 23 | + |
| 24 | + # 2) On cross builds we can’t run the test — be conservative |
| 25 | + if (CMAKE_CROSSCOMPILING) |
| 26 | + message(STATUS "${ISA_NAME}: cross-compiling; skipping run test -> FALSE") |
| 27 | + return() |
| 28 | + endif() |
| 29 | + |
| 30 | + # 3) Unique cache key per (ISA, flag, source) |
| 31 | + string(MD5 _hash "${TEST_SRC}||${ISA_FLAG}") |
| 32 | + set(_KEY "RUN_${ISA_NAME}_${_hash}") |
| 33 | + |
| 34 | + # Ensure a fresh check whenever source/flag changes |
| 35 | + #unset(${_KEY} CACHE) |
| 36 | + |
| 37 | + # Pass the ISA flag to the try_run compile |
| 38 | + set(_SAVE_REQ_FLAGS "${CMAKE_REQUIRED_FLAGS}") |
| 39 | + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${ISA_FLAG}") |
| 40 | + |
| 41 | + # Optional: see compiler errors from try_compile/try_run |
| 42 | + set(CMAKE_REQUIRED_QUIET TRUE) |
| 43 | + |
| 44 | + |
| 45 | + check_cxx_source_runs("${TEST_SRC}" ${_KEY}) |
| 46 | + |
| 47 | + #message(STATUS "${CMAKE_REQUIRED_FLAGS}\n\n${TEST_SRC}") |
| 48 | + |
| 49 | + set(CMAKE_REQUIRED_FLAGS "${_SAVE_REQ_FLAGS}") |
| 50 | + |
| 51 | + set(${OUT_VAR} ${${_KEY}} PARENT_SCOPE) |
| 52 | + if(${${_KEY}}) |
| 53 | + set(RESULT TRUE) |
| 54 | + else() |
| 55 | + set(RESULT FALSE) |
| 56 | + endif() |
| 57 | + message(STATUS "${ISA_NAME}: runtime support ${RESULT} (flag=${ISA_FLAG})") |
| 58 | +endfunction() |
| 59 | + |
| 60 | +# ---- Test sources (each *executes* a distinctive intrinsic) ---- |
| 61 | +set(TEST_SSE2 " |
| 62 | + #include <emmintrin.h> |
| 63 | + int main(){ |
| 64 | + __m128i a = _mm_set1_epi32(1), b = _mm_set1_epi32(2); |
| 65 | + __m128i c = _mm_add_epi32(a,b); |
| 66 | + volatile int v = _mm_cvtsi128_si32(c); |
| 67 | + (void)v; return 0; |
| 68 | + }") |
| 69 | + |
| 70 | +set(TEST_AVX " |
| 71 | + #include <immintrin.h> |
| 72 | + int main(){ |
| 73 | + __m256 a = _mm256_set1_ps(1.0f), b = _mm256_set1_ps(2.0f); |
| 74 | + __m256 c = _mm256_add_ps(a,b); |
| 75 | + volatile float v = ((float*)&c)[0]; |
| 76 | + (void)v; return 0; |
| 77 | + }") |
| 78 | + |
| 79 | +set(TEST_AVX2 " |
| 80 | + #include <immintrin.h> |
| 81 | + int main(){ |
| 82 | + __m256i a = _mm256_set1_epi32(3), b = _mm256_set1_epi32(4); |
| 83 | + __m256i c = _mm256_mullo_epi32(a,b); // AVX2-specific integer op |
| 84 | + volatile int v = ((int*)&c)[0]; |
| 85 | + (void)v; return 0; |
| 86 | + }") |
| 87 | + |
| 88 | +set(TEST_AVX512 [[ |
| 89 | + #include <immintrin.h> |
| 90 | + int main(){ |
| 91 | + alignas(64) int d[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; |
| 92 | + volatile int u = d[0]; |
| 93 | + __m512i X = _mm512_loadu_si512((const void*)d); |
| 94 | + __m512i Y = _mm512_add_epi32(X, _mm512_set1_epi32(u)); |
| 95 | + _mm512_storeu_si512((void*)d, Y); |
| 96 | + return d[0]==0; |
| 97 | + }]]) |
| 98 | + |
| 99 | + set(TEST_BMI2 [[ |
| 100 | + #include <immintrin.h> |
| 101 | + #include <cstdlib> |
| 102 | + int main(){ |
| 103 | + long long unsigned int x = 0b1100; |
| 104 | + volatile unsigned long long y = _mulx_u64(rand(), rand(), &x); |
| 105 | + (void)y; return y==0b10; |
| 106 | + }]]) |
| 107 | + |
| 108 | +# ---- Run the checks (MSVC flags; add GCC/Clang alternates if you use them) ---- |
| 109 | +if (MSVC) |
| 110 | + set(FLAG_SSE2 "/arch:SSE2") # default on x64 but the test is harmless |
| 111 | + set(FLAG_AVX "/arch:AVX") |
| 112 | + set(FLAG_AVX2 "/arch:AVX2") |
| 113 | + set(FLAG_AVX512 "/arch:AVX512") |
| 114 | + set(FLAG_BMI2 "/arch:AVX2") # no /arch flag for BMI2 |
| 115 | +else() |
| 116 | + # clang/gcc (helps when using clang-cl or mingw) |
| 117 | + set(FLAG_SSE2 "-msse2") |
| 118 | + set(FLAG_AVX "-mavx") |
| 119 | + set(FLAG_AVX2 "-mavx2") |
| 120 | + set(FLAG_AVX512 "-mavx512f") |
| 121 | + set(FLAG_BMI2 "-mbmi2") |
| 122 | +endif() |
| 123 | + |
| 124 | +check_isa_runs("SSE2" "${FLAG_SSE2}" "${TEST_SSE2}" SSE2_RUNS) |
| 125 | +check_isa_runs("AVX" "${FLAG_AVX}" "${TEST_AVX}" AVX_RUNS) |
| 126 | +check_isa_runs("AVX2" "${FLAG_AVX2}" "${TEST_AVX2}" AVX2_RUNS) |
| 127 | +check_isa_runs("AVX512" "${FLAG_AVX512}" "${TEST_AVX512}" AVX512_RUNS) |
| 128 | +check_isa_runs("BMI2" "${FLAG_BMI2}" "${TEST_BMI2}" BMI2_RUNS) |
| 129 | + |
| 130 | + |
| 131 | +# Expose as cache options so users can override if they *really* want. |
| 132 | +set(ENABLE_SSE_DEFAULT ${SSE2_RUNS} CACHE BOOL "Enable SSE2 codepaths by default") |
| 133 | +set(ENABLE_AVX_DEFAULT ${AVX_RUNS} CACHE BOOL "Enable AVX codepaths by default") |
| 134 | +set(ENABLE_AVX2_DEFAULT ${AVX2_RUNS} CACHE BOOL "Enable AVX2 codepaths by default") |
| 135 | +set(ENABLE_AVX512_DEFAULT ${AVX512_RUNS} CACHE BOOL "Enable AVX-512 codepaths by default") |
| 136 | +set(ENABLE_BMI2_DEFAULT ${AVX512_RUNS} CACHE BOOL "Enable AVX-512 codepaths by default") |
0 commit comments