|
| 1 | +# A collection of helper CMake functions to detect hardware capabilities. At |
| 2 | +# the moment these are used when configuring MLIR integration tests. |
| 3 | + |
| 4 | +# Checks whether the specified hardware capability is supported by the host |
| 5 | +# Linux system. This is implemented by checking auxiliary vector feature |
| 6 | +# provided by the Linux kernel. |
| 7 | +# |
| 8 | +# check_hwcap( |
| 9 | +# hwcap_spec |
| 10 | +# output_var |
| 11 | +# ) |
| 12 | +# |
| 13 | +# hwcap_spec - HWCAP value to check - these are defined in hwcap.h in the Linux |
| 14 | +# kernel. |
| 15 | +# |
| 16 | +# output_var - Output variable to use to save the results (TRUE for supported, |
| 17 | +# FALSE for not supported). |
| 18 | +# |
| 19 | +# EXAMPLES: |
| 20 | +# |
| 21 | +# check_hwcap("HWCAP2_SME" SME_EMULATOR_REQUIRED) |
| 22 | +# |
| 23 | +function(check_hwcap hwcap_spec output) |
| 24 | + set(hwcap_test_src |
| 25 | + [====[ |
| 26 | + #include <asm/hwcap.h> |
| 27 | + #include <sys/auxv.h> |
| 28 | +
|
| 29 | + int main(void) |
| 30 | + { |
| 31 | + long hwcaps = getauxval(AT_<HWCAP_VEC>); |
| 32 | + return (hwcaps & <HWCAP_SPEC>) != 0; |
| 33 | + } |
| 34 | + ]====] |
| 35 | + ) |
| 36 | + |
| 37 | + # Extract from $hwcap_spec whether this is AT_HWCAP or AT_HWCAP2 |
| 38 | + string(FIND ${hwcap_spec} "_" wsloc) |
| 39 | + string(SUBSTRING ${hwcap_spec} 0 ${wsloc} hwcap_vec) |
| 40 | + |
| 41 | + string(REPLACE "<HWCAP_VEC>" ${hwcap_vec} hwcap_test_src "${hwcap_test_src}") |
| 42 | + string(REPLACE "<HWCAP_SPEC>" ${hwcap_spec} hwcap_test_src "${hwcap_test_src}") |
| 43 | + |
| 44 | + set(hwcap_test_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/hwcap_check.c) |
| 45 | + file(WRITE ${hwcap_test_file} "${hwcap_test_src}") |
| 46 | + |
| 47 | + # Compile _and_ run |
| 48 | + try_run( |
| 49 | + test_run_result test_compile_result |
| 50 | + ${CMAKE_BINARY_DIR} |
| 51 | + ${hwcap_test_file} |
| 52 | + ) |
| 53 | + # Compilation will fail if hwcap_spec is not defined - this usually means |
| 54 | + # that your Linux kernel is too old. |
| 55 | + if(${test_compile_result} AND (DEFINED test_run_result)) |
| 56 | + message(${test_run_result}) |
| 57 | + message(STATUS "Checking whether ${hwcap_spec} is supported by the host system: ${test_run_result}") |
| 58 | + set(${output} ${test_run_result} PARENT_SCOPE) |
| 59 | + else() |
| 60 | + message(STATUS "Checking whether ${hwcap_spec} is supported by the host system: FALSE") |
| 61 | + endif() |
| 62 | +endfunction(check_hwcap) |
| 63 | + |
| 64 | +# For the given group of e2e tests (defined by the `mlir_e2e_tests` flag), |
| 65 | +# checks whether an emulator is required. If yes, verifies that the |
| 66 | +# corresponding CMake var pointing to an emulator (`emulator_exec`) has been |
| 67 | +# set. |
| 68 | +# |
| 69 | +# check_emulator( |
| 70 | +# mlir_e2e_tests |
| 71 | +# hwcap_spec |
| 72 | +# emulator_exec |
| 73 | +# ) |
| 74 | +# |
| 75 | +# mlir_e2e_tests - MLIR CMake variables corresponding to the group of e2e tests |
| 76 | +# to check |
| 77 | +# hwcap_spec - HWCAP value to check. This should correspond to the hardware |
| 78 | +# capabilities required by the tests to be checked. Possible |
| 79 | +# values are defined in hwcap.h in the Linux kernel. |
| 80 | +# emulator_exec - variable the defines the emulator (ought to be set if |
| 81 | +# required, can be empty otherwise). |
| 82 | +# |
| 83 | +# EXAMPLES: |
| 84 | +# |
| 85 | +# check_emulator(MLIR_RUN_ARM_SVE_TESTS "HWCAP_SVE" ARM_EMULATOR_EXECUTABLE) |
| 86 | +# |
| 87 | +function(check_emulator mlir_e2e_tests hwcap_spec emulator_exec) |
| 88 | + if (NOT ${mlir_e2e_tests}) |
| 89 | + return() |
| 90 | + endif() |
| 91 | + |
| 92 | + check_hwcap(${hwcap_spec} emulator_not_required) |
| 93 | + if (${emulator_not_required}) |
| 94 | + return() |
| 95 | + endif() |
| 96 | + |
| 97 | + if (${emulator_exec} STREQUAL "") |
| 98 | + message(FATAL_ERROR "${mlir_e2e_tests} requires an emulator, but ${emulator_exec} is not set") |
| 99 | + endif() |
| 100 | + |
| 101 | +endfunction() |
0 commit comments