Skip to content

Commit 65c48dd

Browse files
authored
[cmake][LoongArch] Add testsuite framework for SX/ASX (#79)
1 parent 72a946a commit 65c48dd

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

SingleSource/UnitTests/Vector/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@ endif()
1212
if(ARCH STREQUAL "AArch64")
1313
add_subdirectory(AArch64)
1414
endif()
15+
# The value of LOONGARCH_CPU_SUPPORTS_SX means if the target machine
16+
# can run LSX tests. Its value is set automatically by detecting
17+
# process for native compile. While for cross compile, it needs the
18+
# user to set the value when running the cmake command, such as
19+
# "-DLOONGARCH_CPU_SUPPORTS_SX=TRUE" if LSX tests are expected to run.
20+
# LOONGARCH_CPU_SUPPORTS_ASX is used for LASX tests, the other is same.
21+
if(ARCH STREQUAL "LoongArch")
22+
CHECK_C_COMPILER_FLAG(-mlsx COMPILER_SUPPORTS_LSX)
23+
if(NOT DEFINED LOONGARCH_CPU_SUPPORTS_SX AND COMPILER_SUPPORTS_LSX)
24+
include(DetectLoongArchSupportVector)
25+
detect_loongarch_cpu_supports_sx(LOONGARCH_CPU_SUPPORTS_SX)
26+
endif()
27+
if(LOONGARCH_CPU_SUPPORTS_SX AND COMPILER_SUPPORTS_LSX)
28+
add_subdirectory(LSX)
29+
endif()
30+
31+
CHECK_C_COMPILER_FLAG(-mlasx COMPILER_SUPPORTS_LASX)
32+
if(NOT DEFINED LOONGARCH_CPU_SUPPORTS_ASX AND COMPILER_SUPPORTS_LASX)
33+
include(DetectLoongArchSupportVector)
34+
detect_loongarch_cpu_supports_asx(LOONGARCH_CPU_SUPPORTS_ASX)
35+
endif()
36+
if(LOONGARCH_CPU_SUPPORTS_ASX AND COMPILER_SUPPORTS_LASX)
37+
add_subdirectory(LASX)
38+
endif()
39+
endif()
1540

1641
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
1742
if(ARCH STREQUAL "x86")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
list(APPEND CFLAGS -mlasx)
2+
3+
llvm_singlesource(PREFIX "Vector-LASX-")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef LASX_TEST_UTIL_H
2+
#define LASX_TEST_UTIL_H
3+
4+
#include <stdio.h>
5+
#include <string.h>
6+
7+
#define MIN(a, b) ((a) < (b) ? (a) : (b))
8+
9+
__attribute__((noinline)) void check_lasx_out(void *expected, void *got,
10+
int len, const char *fname,
11+
int line) {
12+
int i = 0;
13+
int num = MIN(len, 32);
14+
if (memcmp(expected, got, num) != 0) {
15+
printf("%s:%d: \n", fname, line);
16+
printf("0x");
17+
for (i = 0; i < num; i++) {
18+
printf(" %02x", ((char *)expected)[i] & 0xff);
19+
}
20+
printf(" != \n");
21+
printf("0x");
22+
for (i = 0; i < num; i++) {
23+
printf(" %02x", ((char *)got)[i] & 0xff);
24+
}
25+
printf("\n");
26+
}
27+
}
28+
29+
#endif
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
list(APPEND CFLAGS -mlsx)
2+
3+
llvm_singlesource(PREFIX "Vector-LSX-")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef LSX_TEST_UTIL_H
2+
#define LSX_TEST_UTIL_H
3+
4+
#include <stdio.h>
5+
#include <string.h>
6+
7+
#define MIN(a, b) ((a) < (b) ? (a) : (b))
8+
9+
__attribute__((noinline)) void check_lsx_out(void *expected, void *got, int len,
10+
const char *fname, int line) {
11+
int i = 0;
12+
int num = MIN(len, 16);
13+
if (memcmp(expected, got, num) != 0) {
14+
printf("%s:%d: \n", fname, line);
15+
printf("0x");
16+
for (i = 0; i < num; i++) {
17+
printf(" %02x", ((char *)expected)[i] & 0xff);
18+
}
19+
printf(" != \n");
20+
printf("0x");
21+
for (i = 0; i < num; i++) {
22+
printf(" %02x", ((char *)got)[i] & 0xff);
23+
}
24+
printf("\n");
25+
}
26+
}
27+
28+
#endif
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
##===- DetectLoongArchSupportVector.cmake ---------------------------------===##
2+
#
3+
# Performs a try_run with a simple program calling LSX/LASX builtin
4+
# to determine if the LoongArch CPU supports vector instructions.
5+
# This is not used for cross compile.
6+
#
7+
##===----------------------------------------------------------------------===##
8+
function(detect_loongarch_cpu_supports_sx variable)
9+
file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/SimpleProgramCallLSXBuiltin.c
10+
"#include <lsxintrin.h>
11+
int main(void) {
12+
__m128i a, b, c;
13+
c = __lsx_vand_v(a, b);
14+
return 0;
15+
}")
16+
set(SX_FLAG "-mlsx")
17+
try_run(HAVE_RUN_${variable} HAVE_COMPILE_${variable}
18+
${CMAKE_BINARY_DIR}
19+
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/SimpleProgramCallLSXBuiltin.c
20+
CMAKE_FLAGS "-DCOMPILE_DEFINITIONS=${SX_FLAG}"
21+
COMPILE_OUTPUT_VARIABLE COMP_OUTPUT
22+
RUN_OUTPUT_VARIABLE RUN_OUTPUT)
23+
24+
set(${variable} false PARENT_SCOPE)
25+
if(HAVE_COMPILE_${variable} AND (HAVE_RUN_${variable} EQUAL 0))
26+
message(STATUS "Check if LoongArch cpu supports SX - Success")
27+
set(${variable} true PARENT_SCOPE)
28+
else()
29+
message(STATUS "Check if LoongArch cpu supports SX - Failed")
30+
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
31+
"LoongArch cpu does not support SX with the following output:\n${COMP_OUTPUT}\n${RUN_OUTPUT}")
32+
endif()
33+
34+
endfunction(detect_loongarch_cpu_supports_sx)
35+
36+
function(detect_loongarch_cpu_supports_asx variable)
37+
file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/SimpleProgramCallLASXBuiltin.c
38+
"#include <lasxintrin.h>
39+
int main(void) {
40+
__m256i a, b, c;
41+
c = __lasx_xvand_v(a, b);
42+
return 0;
43+
}")
44+
set(ASX_FLAG "-mlasx")
45+
try_run(HAVE_RUN_${variable} HAVE_COMPILE_${variable}
46+
${CMAKE_BINARY_DIR}
47+
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/SimpleProgramCallLASXBuiltin.c
48+
CMAKE_FLAGS "-DCOMPILE_DEFINITIONS=${ASX_FLAG}"
49+
COMPILE_OUTPUT_VARIABLE COMP_OUTPUT
50+
RUN_OUTPUT_VARIABLE RUN_OUTPUT)
51+
52+
set(${variable} false PARENT_SCOPE)
53+
if(HAVE_COMPILE_${variable} AND (HAVE_RUN_${variable} EQUAL 0))
54+
message(STATUS "Check if LoongArch cpu supports ASX - Success")
55+
set(${variable} true PARENT_SCOPE)
56+
else()
57+
message(STATUS "Check if LoongArch cpu supports ASX - Failed")
58+
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
59+
"LoongArch cpu does not support ASX with the following output:\n${COMP_OUTPUT}\n${RUN_OUTPUT}")
60+
endif()
61+
62+
endfunction(detect_loongarch_cpu_supports_asx)

0 commit comments

Comments
 (0)