Skip to content

Commit

Permalink
[enhancement] workload-xsave: Add cmake build support
Browse files Browse the repository at this point in the history
1. Based on the instruction set supported by the CPU, selectively compile the source files.
Signed-off-by: Haoliang Zhu <[email protected]>
  • Loading branch information
haoliang-Zhu committed Jan 3, 2024
1 parent 66778fe commit 8f685b6
Showing 1 changed file with 32 additions and 82 deletions.
114 changes: 32 additions & 82 deletions workload-xsave/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ set(SRC
work_PAUSE.c
work_memcpy.c
work_MEM.c
# The source files here are not needed for now
# run_common.c
# work_GETCPU.c
# worker_init4.c
# worker_init_dotprod.c
# worker_init_amx.c
# worker_init_avx.c
# worker_init_avx2.c
# worker_init_sse.c
)

add_compile_definitions(CMAKE_FLAG=0)

# Get target CPU information
execute_process(
COMMAND ${CMAKE_C_COMPILER} -march=native -Q --help=target
Expand All @@ -44,93 +51,36 @@ execute_process(
# Print information about the target CPU
message(STATUS "Target CPU: ${TARGET_CPU}")

# Check whether the CPU feature is enabled
function(check_cpu_feature feature var_name)
string(REGEX MATCH "${feature}[ \t]+\\[enabled\\]" feature_enabled_match "${TARGET_CPU}")
# Function to check for CPU feature and add source files and compile definition if the feature is enabled
function(check_cpu_feature)
# Separate the feature, compile definition, and source files
set(options "")
set(oneValueArgs FEATURE COMPILE_DEFINITION)
set(multiValueArgs SOURCES)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

string(REGEX MATCH "${ARG_FEATURE}[ \t]+\\[enabled\\]" feature_enabled_match "${TARGET_CPU}")
if(feature_enabled_match)
set(${var_name} TRUE PARENT_SCOPE)
message(STATUS "${ARG_FEATURE} is enabled")
add_compile_definitions(${ARG_COMPILE_DEFINITION}=1)
# Append all source files to the list in the parent scope
set(SRC_LIST ${SRC})
foreach(source_file IN LISTS ARG_SOURCES)
list(APPEND SRC_LIST ${source_file})
endforeach()
set(SRC ${SRC_LIST} PARENT_SCOPE)
else()
set(${var_name} FALSE PARENT_SCOPE)
message(STATUS "${ARG_FEATURE} is disabled or not found")
endif()
endfunction()

add_compile_definitions(CMAKE_FLAG=0)

# Check whether "-mamx" is enabled and get the result
check_cpu_feature("-mamx-tile" mamx_enabled)
if(mamx_enabled)
message(STATUS "-mamx-tile is enabled")
add_compile_definitions(MAMX_ENABLED=1)
list(APPEND SRC work_AMX.c)
# list(APPEND SRC worker_init_amx.c)
else()
message(STATUS "-mamx-tile is disabled or not found")
endif()

# Check whether "-mavx" is enabled and get the result
check_cpu_feature("-mavx" mavx_enabled)
if(mavx_enabled)
message(STATUS "-mavx is enabled")
add_compile_definitions(MAVX_ENABLED=1)
list(APPEND SRC work_AVX.c)
# list(APPEND SRC worker_init_avx.c)
else()
message(STATUS "-mavx is disabled or not found")
endif()

# Check whether "-mavx2" is enabled and get the result
check_cpu_feature("-mavx2" mavx2_enabled)
if(mavx2_enabled)
message(STATUS "-mavx2 is enabled")
add_compile_definitions(MAVX2_ENABLED=1)
list(APPEND SRC work_AVX2.c)
list(APPEND SRC work_DOTPROD.c)
# list(APPEND SRC worker_init_avx2.c)
else()
message(STATUS "-mavx2 is disabled or not found")
endif()

# Check whether "-mavx512f" is enabled and get the result
check_cpu_feature("-mavx512f" mavx512f_enabled)
if(mavx512f_enabled)
message(STATUS "-mavx512f is enabled")
add_compile_definitions(MAVX512F_ENABLED=1)
list(APPEND SRC work_AVX512.c)
else()
message(STATUS "-mavx512f is disabled or not found")
endif()

# Check whether "-msse" is enabled and get the result
check_cpu_feature("-msse" sse_enabled)
if(sse_enabled)
message(STATUS "-msse is enabled")
add_compile_definitions(MSSE_ENABLED=1)
list(APPEND SRC work_SSE.c)
# list(APPEND SRC worker_init_sse.c)
else()
message(STATUS "-msse is disabled or not found")
endif()

# Check whether "-mvnni" is enabled and get the result
check_cpu_feature("-mvnni" vnni_enabled)
if(vnni_enabled)
message(STATUS "-mvnni is enabled")
add_compile_definitions(MVNNI_ENABLED=1)
list(APPEND SRC work_VNNI.c)
else()
message(STATUS "-mvnni is disabled or not found")
endif()

# Check whether "-mvnni512" is enabled and get the result
check_cpu_feature("-mvnni512" vnni512_enabled)
if(vnni512_enabled)
message(STATUS "-mvnni512 is enabled")
add_compile_definitions(MVNNI512_ENABLED=1)
list(APPEND SRC work_VNNI512.c)
else()
message(STATUS "-mvnni512 is disabled or not found")
endif()

# Check CPU feature and add source files and compile definition
check_cpu_feature(FEATURE "-mamx-tile" COMPILE_DEFINITION "MAMX_ENABLED" SOURCES "work_AMX.c")
check_cpu_feature(FEATURE "-mavx" COMPILE_DEFINITION "MAVX_ENABLED" SOURCES "work_AVX.c")
check_cpu_feature(FEATURE "-mavx2" COMPILE_DEFINITION "MAVX2_ENABLED" SOURCES "work_AVX2.c" "work_DOTPROD.c")
check_cpu_feature(FEATURE "-mavx512f" COMPILE_DEFINITION "MAVX512f_ENABLED" SOURCES "work_AVX512.c")
check_cpu_feature(FEATURE "-mvnni" COMPILE_DEFINITION "MVNNI_ENABLED" SOURCES "work_VNNI.c")
check_cpu_feature(FEATURE "-mvnni512" COMPILE_DEFINITION "MVNNSI512_ENABLED" SOURCES "work_VNNI512.c")

# Print the contents of the SRC
message(STATUS "SRC contains the following files: ${SRC}")
Expand Down

0 comments on commit 8f685b6

Please sign in to comment.