Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 131 additions & 20 deletions cmake/option.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## https://en.wikipedia.org/wiki/List_of_Intel_CPU_microarchitectures
## https://en.wikipedia.org/wiki/List_of_AMD_CPU_microarchitectures
## https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html
## https://en.wikipedia.org/wiki/List_of_Intel_CPU_microarchitectures
## https://en.wikipedia.org/wiki/List_of_AMD_CPU_microarchitectures
## https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html
## https://gcc.gnu.org/onlinedocs/gcc/RISC-V-Options.html

## Intel Microarchitectures
option(ENABLE_NEHALEM "Enable Intel Nehalem CPU microarchitecture" OFF)
Expand Down Expand Up @@ -30,6 +31,12 @@ option(ENABLE_ARMV8.4A "Enable ARMv8.4-a architecture" OFF)
option(ENABLE_ARMV8.5A "Enable ARMv8.5-a architecture" OFF)
option(ENABLE_ARMV8.6A "Enable ARMv8.6-a architecture" OFF)

## RISC-V architectures
option(ENABLE_RISCV64 "Enable RISC-V 64-bit base architecture" OFF)
option(ENABLE_RISCV_VECTOR "Enable RISC-V Vector extension" OFF)
option(ENABLE_RISCV_ZVFH "Enable RISC-V Zvfh extension" OFF)
option(ENABLE_RISCV_ZIHINTPAUSE "Enable RISC-V Zihintpause extension" OFF)

## OpenMP option
option(ENABLE_OPENMP "Enable OpenMP support" OFF)

Expand All @@ -39,6 +46,7 @@ set(ARCH_OPTIONS
ENABLE_ZEN1 ENABLE_ZEN2 ENABLE_ZEN3
ENABLE_ARMV8A ENABLE_ARMV8.1A ENABLE_ARMV8.2A ENABLE_ARMV8.3A ENABLE_ARMV8.4A
ENABLE_ARMV8.5A ENABLE_ARMV8.6A
ENABLE_RISCV64 ENABLE_RISCV_VECTOR ENABLE_RISCV_ZVFH ENABLE_RISCV_ZIHINTPAUSE
ENABLE_NATIVE
)

Expand All @@ -51,6 +59,10 @@ foreach(opt IN LISTS ARCH_OPTIONS)
endif()
endforeach()

if((ENABLE_RISCV_VECTOR OR ENABLE_RISCV_ZVFH OR ENABLE_RISCV_ZIHINTPAUSE) AND NOT ENABLE_RISCV64)
message(FATAL_ERROR "ENABLE_RISCV_VECTOR, ENABLE_RISCV_ZVFH and ENABLE_RISCV_ZIHINTPAUSE require ENABLE_RISCV64")
endif()

include(CheckCCompilerFlag)

function(_AppendFlags _RESULT _FLAG)
Expand Down Expand Up @@ -90,6 +102,73 @@ macro(add_arch_flag FLAG VAR_NAME OPTION_NAME)
endif()
endmacro()

function(_build_riscv64_march RESULT_VAR)
set(_march "rv64gc")

if(ENABLE_RISCV_VECTOR)
string(APPEND _march "v")
endif()

if(ENABLE_RISCV_ZVFH)
if(NOT ENABLE_RISCV_VECTOR)
message(FATAL_ERROR "ENABLE_RISCV_ZVFH requires ENABLE_RISCV_VECTOR")
endif()
string(APPEND _march "_zvfh")
endif()

if(ENABLE_RISCV_ZIHINTPAUSE)
string(APPEND _march "_zihintpause")
endif()

set(${RESULT_VAR} "${_march}" PARENT_SCOPE)
endfunction()

function(_build_riscv64_rvv_march RESULT_VAR)
set(_march "rv64gcv")

if(ENABLE_RISCV_ZVFH)
string(APPEND _march "_zvfh")
endif()

if(ENABLE_RISCV_ZIHINTPAUSE)
string(APPEND _march "_zihintpause")
endif()

set(${RESULT_VAR} "${_march}" PARENT_SCOPE)
endfunction()

function(_setup_riscv64_march)
_build_riscv64_march(_riscv_march)
set(CMAKE_REQUIRED_FLAGS "-mabi=lp64d")
check_c_compiler_flag("-march=${_riscv_march}" _COMP_SUPP_riscv64)
unset(CMAKE_REQUIRED_FLAGS)

if(_COMP_SUPP_riscv64)
_AppendFlags(CMAKE_C_FLAGS "-march=${_riscv_march}")
_AppendFlags(CMAKE_C_FLAGS "-mabi=lp64d")
_AppendFlags(CMAKE_CXX_FLAGS "-march=${_riscv_march}")
_AppendFlags(CMAKE_CXX_FLAGS "-mabi=lp64d")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE)
message(STATUS "RISC-V: enabled -march=${_riscv_march} -mabi=lp64d")
else()
message(WARNING "Compiler does not support -march=${_riscv_march} -mabi=lp64d")
endif()
endfunction()

function(setup_compiler_march_for_riscv64_rvv RESULT_VAR)
_build_riscv64_rvv_march(_riscv_rvv_march)
set(CMAKE_REQUIRED_FLAGS "-mabi=lp64d")
check_c_compiler_flag("-march=${_riscv_rvv_march}" _COMP_SUPP_riscv64_rvv)
unset(CMAKE_REQUIRED_FLAGS)

if(_COMP_SUPP_riscv64_rvv)
set(${RESULT_VAR} "-march=${_riscv_rvv_march} -mabi=lp64d" PARENT_SCOPE)
else()
set(${RESULT_VAR} "" PARENT_SCOPE)
endif()
endfunction()

function(_setup_armv8_march)
if(MSVC)
return()
Expand Down Expand Up @@ -126,18 +205,18 @@ endfunction()

function(setup_compiler_march_for_x86 VAR_NAME_SSE VAR_NAME_AVX2 VAR_NAME_AVX512 VAR_NAME_AVX512FP16)
if(MSVC)
#sse
# sse
set(${VAR_NAME_SSE} "" PARENT_SCOPE)

#avx2
# avx2
check_c_compiler_flag("/arch:AVX2" _COMP_SUPP_MSVC_AVX2)
if(_COMP_SUPP_MSVC_AVX2)
set(${VAR_NAME_AVX2} "/arch:AVX2" PARENT_SCOPE)
else()
set(${VAR_NAME_AVX2} "" PARENT_SCOPE)
endif()

#avx512
# avx512
check_c_compiler_flag("/arch:AVX512" _COMP_SUPP_MSVC_AVX512)
if(_COMP_SUPP_MSVC_AVX512)
set(${VAR_NAME_AVX512} "/arch:AVX512" PARENT_SCOPE)
Expand All @@ -147,7 +226,7 @@ function(setup_compiler_march_for_x86 VAR_NAME_SSE VAR_NAME_AVX2 VAR_NAME_AVX512
set(${VAR_NAME_AVX512} "" PARENT_SCOPE)
endif()

#avx512fp16
# avx512fp16
if(_COMP_SUPP_MSVC_AVX512)
set(${VAR_NAME_AVX512FP16} "/arch:AVX512" PARENT_SCOPE)
elseif(_COMP_SUPP_MSVC_AVX2)
Expand All @@ -159,13 +238,13 @@ function(setup_compiler_march_for_x86 VAR_NAME_SSE VAR_NAME_AVX2 VAR_NAME_AVX512
return()
endif()

#sse
# sse
set(${VAR_NAME_SSE} "-march=corei7" PARENT_SCOPE)

#avx 2
# avx 2
set(${VAR_NAME_AVX2} "-march=core-avx2" PARENT_SCOPE)

#avx512
# avx512
set(_x86_flags_avx512 "icelake-server" "skylake-avx512" "core-avx2" "x86-64")
foreach(_arch_avx512 IN LISTS _x86_flags_avx512)
check_c_compiler_flag("-march=${_arch_avx512}" _COMP_SUPP_${_arch_avx512})
Expand All @@ -175,7 +254,7 @@ function(setup_compiler_march_for_x86 VAR_NAME_SSE VAR_NAME_AVX2 VAR_NAME_AVX512
endif()
endforeach()

#avx512fp16
# avx512fp16
set(_x86_flags_avx512fp16
"sapphirerapids" "icelake-server" "skylake-avx512" "core-avx2" "x86-64"
)
Expand All @@ -195,9 +274,9 @@ endif()

if(NOT AUTO_DETECT_ARCH)
if(ENABLE_NATIVE)
if (NOT MSVC)
if(NOT MSVC)
add_arch_flag("-march=native" NATIVE ENABLE_NATIVE)
endif ()
endif()
endif()

if(ENABLE_ZEN3)
Expand Down Expand Up @@ -252,30 +331,55 @@ if(NOT AUTO_DETECT_ARCH)
add_arch_flag("-march=nehalem" NEHALEM ENABLE_NEHALEM)
endif()

# ARM (newest first — allow multiple? usually only one)
# But GCC allows only one -march=, so honor highest enabled
# ARM (newest first)
if(ENABLE_ARMV8.6A)
add_arch_flag("-march=armv8.6-a" ARMV86A ENABLE_ARMV8.6A)
endif()

if(ENABLE_ARMV8.5A)
add_arch_flag("-march=armv8.5-a" ARMV85A ENABLE_ARMV8.5A)
endif()

if(ENABLE_ARMV8.4A)
add_arch_flag("-march=armv8.4-a" ARMV84A ENABLE_ARMV8.4A)
endif()

if(ENABLE_ARMV8.3A)
add_arch_flag("-march=armv8.3-a" ARMV83A ENABLE_ARMV8.3A)
endif()

if(ENABLE_ARMV8.2A)
add_arch_flag("-march=armv8.2-a" ARMV82A ENABLE_ARMV8.2A)
endif()

if(ENABLE_ARMV8.1A)
add_arch_flag("-march=armv8.1-a" ARMV81A ENABLE_ARMV8.1A)
endif()

if(ENABLE_ARMV8A)
add_arch_flag("-march=armv8-a" ARMV8A ENABLE_ARMV8A)
endif()

# RISC-V 64-bit
if(ENABLE_RISCV64)
_build_riscv64_march(_riscv_march)
set(CMAKE_REQUIRED_FLAGS "-mabi=lp64d")
check_c_compiler_flag("-march=${_riscv_march}" COMPILER_SUPPORT_RISCV64)
unset(CMAKE_REQUIRED_FLAGS)

if(COMPILER_SUPPORT_RISCV64)
_AppendFlags(CMAKE_C_FLAGS "-march=${_riscv_march}")
_AppendFlags(CMAKE_C_FLAGS "-mabi=lp64d")
_AppendFlags(CMAKE_CXX_FLAGS "-march=${_riscv_march}")
_AppendFlags(CMAKE_CXX_FLAGS "-mabi=lp64d")
message(STATUS "RISC-V: enabled -march=${_riscv_march} -mabi=lp64d")
else()
message(FATAL_ERROR
"Compiler does not support required flags: "
"-march=${_riscv_march} -mabi=lp64d for ENABLE_RISCV64")
endif()
endif()

else()
# AUTO DETECT
# Heuristic: detect host architecture and probe appropriate flags
Expand All @@ -289,29 +393,36 @@ else()
set(HOST_ARCH arm64)
elseif(SYSTEM_PROC_LOWER MATCHES "^(arm|armv7|armv7-a|armv7l)$")
set(HOST_ARCH arm)
elseif(SYSTEM_PROC_LOWER MATCHES "^(riscv64)$")
set(HOST_ARCH riscv64)
else()
set(HOST_ARCH unknown)
message(WARNING "unknown host arch: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
# message(STATUS "host arch: ${HOST_ARCH}")

if (HOST_ARCH MATCHES "^(arm|arm64)$")
# message(STATUS "host arch: ${HOST_ARCH}")

if(HOST_ARCH MATCHES "^(arm|arm64)$")
_setup_armv8_march()
elseif (HOST_ARCH MATCHES "^(x86|x64)$")
elseif(HOST_ARCH MATCHES "^(x86|x64)$")
_setup_x86_march()
else ()
elseif(HOST_ARCH STREQUAL "riscv64")
_setup_riscv64_march()
else()
message(WARNING "unknown host arch - no -march set")
endif ()
endif()
endif()

# -----------------------------
# OpenMP
# -----------------------------
if(ENABLE_OPENMP)
find_package(OpenMP REQUIRED)

if(OpenMP_C_FLAGS)
_AppendFlags(CMAKE_C_FLAGS "${OpenMP_C_FLAGS}")
endif()

if(OpenMP_CXX_FLAGS)
_AppendFlags(CMAKE_CXX_FLAGS "${OpenMP_CXX_FLAGS}")
endif()
Expand Down
37 changes: 30 additions & 7 deletions src/ailego/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ if(NOT ANDROID AND AUTO_DETECT_ARCH)
endforeach()

foreach(MATH_FILE ${MATH_FILES_AVX512FP16})
set_source_files_properties(
${MATH_FILE}
PROPERTIES
COMPILE_FLAGS "${MATH_MARCH_FLAG_AVX512FP16}"
)
endforeach()
elseif (HOST_ARCH MATCHES "^(arm|arm64)$")
set_source_files_properties(
${MATH_FILE}
PROPERTIES
COMPILE_FLAGS "${MATH_MARCH_FLAG_AVX512FP16}"
)
endforeach()

elseif(HOST_ARCH MATCHES "^(arm|arm64)$")
if(MSVC)
return()
endif()
Expand All @@ -114,6 +115,28 @@ if(NOT ANDROID AND AUTO_DETECT_ARCH)
COMPILE_FLAGS "${MATH_MARCH_FLAG_NEON}"
)
endforeach()

elseif(HOST_ARCH STREQUAL "riscv64")
setup_compiler_march_for_riscv64_rvv(MATH_MARCH_FLAG_RVV)

if(MATH_MARCH_FLAG_RVV)
file(GLOB_RECURSE MATH_FILES_RVV
${CMAKE_CURRENT_SOURCE_DIR}/math/*_rvv.cc
${CMAKE_CURRENT_SOURCE_DIR}/math/*_rvv.c
${CMAKE_CURRENT_SOURCE_DIR}/math_batch/*_rvv.cc
${CMAKE_CURRENT_SOURCE_DIR}/math_batch/*_rvv.c
)

foreach(MATH_FILE ${MATH_FILES_RVV})
set_source_files_properties(
${MATH_FILE}
PROPERTIES
COMPILE_FLAGS "${MATH_MARCH_FLAG_RVV}"
)
endforeach()
else()
message(STATUS "RISC-V RVV-specific march is not supported by the compiler; skipping *_rvv sources")
endif()
endif()
endif()

Expand Down
Loading