Skip to content

Commit 5ed60ff

Browse files
committed
[mlir][test] Extend CMake logic for e2e tests
Adds two new CMake functions to query the host system: * `check_hwcap`, * `check_emulator`. Together, these functions are used to check whether a given set of MLIR integration tests require an emulator. If yes, then the corresponding CMake var that defies the required emulator executable is also checked. `check_hwcap` relies on ELF_HWCAP for discovering CPU features from userspace on Linux systems. This is the recommended approach for Arm CPUs running on Linux as outlined in this blog post: * https://community.arm.com/arm-community-blogs/b/operating-systems-blog/posts/runtime-detection-of-cpu-features-on-an-armv8-a-cpu Other operating systems (e.g. Android) and CPU architectures will most likely require some other approach. Right now these new hooks are only used for SVE and SME integration tests. This relands #86489 with the following changes: * Replaced: `set(hwcap_test_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/hwcap_check.c)` with: `set(hwcap_test_file ${CMAKE_BINARY_DIR}/temp/hwcap_check.c)` The former would trigger an infinite loop when running `ninja` (after the initial CMake configuration). * Fixed commit msg. Previous one was taken from the initial GH PR commit rather than the final re-worked solution (missed this when merging via GH UI). * A couple more NFCs/tweaks.
1 parent aff197f commit 5ed60ff

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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}/temp/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(STATUS "Checking whether ${hwcap_spec} is supported by the host system: ${test_run_result}")
57+
set(${output} ${test_run_result} PARENT_SCOPE)
58+
else()
59+
message(STATUS "Checking whether ${hwcap_spec} is supported by the host system: FALSE")
60+
endif()
61+
endfunction(check_hwcap)
62+
63+
# For the given group of e2e tests (defined by the `mlir_e2e_tests` flag),
64+
# checks whether an emulator is required. If yes, verifies that the
65+
# corresponding CMake var pointing to an emulator (`emulator_exec`) has been
66+
# set.
67+
#
68+
# check_emulator(
69+
# mlir_e2e_tests
70+
# hwcap_spec
71+
# emulator_exec
72+
# )
73+
#
74+
# mlir_e2e_tests - MLIR CMake variables corresponding to the group of e2e tests
75+
# to check
76+
# hwcap_spec - HWCAP value to check. This should correspond to the hardware
77+
# capabilities required by the tests to be checked. Possible
78+
# values are defined in hwcap.h in the Linux kernel.
79+
# emulator_exec - variable the defines the emulator (ought to be set if
80+
# required, can be empty otherwise).
81+
#
82+
# EXAMPLES:
83+
#
84+
# check_emulator(MLIR_RUN_ARM_SVE_TESTS "HWCAP_SVE" ARM_EMULATOR_EXECUTABLE)
85+
#
86+
function(check_emulator mlir_e2e_tests hwcap_spec emulator_exec)
87+
if (NOT ${mlir_e2e_tests})
88+
return()
89+
endif()
90+
91+
check_hwcap(${hwcap_spec} emulator_not_required)
92+
if (${emulator_not_required})
93+
return()
94+
endif()
95+
96+
if (${emulator_exec} STREQUAL "")
97+
message(FATAL_ERROR "${mlir_e2e_tests} requires an emulator, but ${emulator_exec} is not set")
98+
endif()
99+
100+
endfunction()

mlir/docs/Dialects/ArmSME.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ This dialect defines custom and LLVM IR intrinsic operations that are used to
66
target Arm Scalable Matrix Extension. Through the available conversion and
77
ArmSME passes you can, for example, lower a
88
[linalg.matmul](https://mlir.llvm.org/docs/Dialects/Linalg/#linalgmatmul-linalgmatmulop)
9-
opereation to Arm SME
9+
operation to Arm SME
1010
[FMOPA](https://developer.arm.com/documentation/ddi0602/2023-03/SME-Instructions/FMOPA--widening---Half-precision-floating-point-sum-of-outer-products-and-accumulate-)
1111
(floating-point outer product) operations. See one of the in-tree end-to-end
1212
integration tests for reference:
1313

1414
* [Linalg/CPU/ArmSME/matmul.mlir](https://github.com/llvm/llvm-project/blob/main/mlir/test/Integration/Dialect/Linalg/CPU/ArmSME/matmul.mlir)
1515
* [Vector/CPU/ArmSME/test-outerproduct-f64.mlir](https://github.com/llvm/llvm-project/blob/main/mlir/test/Integration/Dialect/Vector/CPU/ArmSME/test-outerproduct-f64.mlir)
1616

17+
In order to run ArmSME integration tests, include these flags in the CMake
18+
invocation when configuring LLVM and MLIR:
19+
```bash
20+
-DMLIR_INCLUDE_INTEGRATION_TESTS=On
21+
-DMLIR_RUN_ARM_SME_TESTS=On
22+
-DARM_EMULATOR_EXECUTABLE=<path-to-emulator>
23+
```
24+
1725
These tests are run "post-commit" by the
1826
[clang-aarch64-sve-vla](https://lab.llvm.org/buildbot/#/builders/197) LLVM
1927
BuildBot worker.

mlir/test/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
include(MLIRCheckHardwareFeatures)
2+
13
add_subdirectory(CAPI)
24
add_subdirectory(lib)
35

@@ -39,6 +41,10 @@ if (MLIR_INCLUDE_INTEGRATION_TESTS)
3941
option(MLIR_RUN_ARM_SVE_TESTS "Run Arm SVE tests.")
4042
option(MLIR_RUN_ARM_SME_TESTS "Run Arm SME tests.")
4143

44+
# Check whether an emulator is required - if yes then make sure that it's
45+
# been set.
46+
check_emulator(MLIR_RUN_ARM_SVE_TESTS "HWCAP_SVE" ARM_EMULATOR_EXECUTABLE)
47+
check_emulator(MLIR_RUN_ARM_SME_TESTS "HWCAP2_SME" ARM_EMULATOR_EXECUTABLE)
4248

4349
# The native target may not be enabled when cross compiling, raise an error.
4450
if(NOT MLIR_ENABLE_EXECUTION_ENGINE)

0 commit comments

Comments
 (0)