From 723bf8b1718fce88a6abed569a0e148d4060ef4e Mon Sep 17 00:00:00 2001 From: accopeland Date: Wed, 30 Mar 2022 18:47:05 +0000 Subject: [PATCH 1/4] Minimum changes to compile on graviton2 (no bmi2, no popcnt) --- CMakeLists.txt | 4 ++-- src/kmlib/kmrns.h | 1 - src/utils/cpu_dispatch.h | 29 ++--------------------------- 3 files changed, 4 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 172a03d..658325f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,8 +73,8 @@ add_executable(megahit_core_popcnt ${OTHER_SOURCE} ${ASMBL_SOURCE} ${IDBA_SOURCE ${CX1_SOURCE} ${TOOLKIT_SOURCE}) add_executable(megahit_core_no_hw_accel ${OTHER_SOURCE} ${ASMBL_SOURCE} ${IDBA_SOURCE} ${SDBG_SOURCE} ${LCASM_SOURCE} ${SEQ_SOURCE} ${CX1_SOURCE} ${TOOLKIT_SOURCE}) -set_target_properties(megahit_core PROPERTIES COMPILE_FLAGS "-mbmi2 -DUSE_BMI2 -mpopcnt") -set_target_properties(megahit_core_popcnt PROPERTIES COMPILE_FLAGS "-mpopcnt") +#set_target_properties(megahit_core PROPERTIES COMPILE_FLAGS "-mbmi2 -DUSE_BMI2 -mpopcnt") +#set_target_properties(megahit_core_popcnt PROPERTIES COMPILE_FLAGS "-mpopcnt") if (STATIC_BUILD) # TODO dirty diff --git a/src/kmlib/kmrns.h b/src/kmlib/kmrns.h index 9f2e9dd..ab9709f 100644 --- a/src/kmlib/kmrns.h +++ b/src/kmlib/kmrns.h @@ -8,7 +8,6 @@ #include #include #include -#include #include namespace kmlib { diff --git a/src/utils/cpu_dispatch.h b/src/utils/cpu_dispatch.h index ec01bb6..75ecc87 100644 --- a/src/utils/cpu_dispatch.h +++ b/src/utils/cpu_dispatch.h @@ -5,32 +5,7 @@ #ifndef MEGAHIT_CPU_DISPATCH_H #define MEGAHIT_CPU_DISPATCH_H -inline bool HasPopcnt() { - unsigned eax, ebx, ecx, edx; -#ifdef _MSC_VER - int cpuid[4]; - __cpuid(cpuid, 1); - eax = cpuid[0], ebx = cpuid[1], ecx = cpuid[2], edx = cpuid[3]; -#else - asm volatile("cpuid\n\t" - : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) - : "0"(1)); -#endif - return ecx >> 23U & 1U; -} - -inline bool HasBmi2() { - unsigned eax, ebx, ecx, edx; -#ifdef _MSC_VER - int cpuid[4]; - __cpuidex(cpuid, 7, 0); - eax = cpuid[0], ebx = cpuid[1], ecx = cpuid[2], edx = cpuid[3]; -#else - asm volatile("cpuid\n\t" - : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) - : "0"(7), "2"(0)); -#endif - return ebx >> 8U & 1U; -} +inline bool HasPopcnt() { return false; } +inline bool HasBmi2() { return false; } #endif // MEGAHIT_CPU_DISPATCH_H From 31ecedb0237b911f25295ba854ede53837d8c3ef Mon Sep 17 00:00:00 2001 From: Alex Copeland Date: Mon, 4 Apr 2022 15:08:55 -0700 Subject: [PATCH 2/4] CMakeLists.txt changes to enable/disable bmi2 and popcount at config time --- CMakeLists.txt | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 172a03d..1308d1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ if (CMAKE_VERSION VERSION_LESS "3.1") else () set(CMAKE_CXX_STANDARD 11) endif () +# target_compile_features( PUBLIC cxx_std_11) option(COVERAGE "Generate coverage report" OFF) option(STATIC_BUILD "Build static executation" OFF) @@ -34,7 +35,7 @@ LIST(APPEND OTHER_SOURCE if (STATIC_BUILD) set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") -endif (STATIC_BUILD) +endif () find_package(ZLIB REQUIRED) find_package(OpenMP REQUIRED) @@ -54,18 +55,19 @@ set(CMAKE_CXX_FLAGS_DEBUG "-g -ggdb -O1 -D_LIBCPP_DEBUG -D_GLIBCXX_DEBUG") if (COVERAGE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage") set(COV_PY "coverage run") -endif (COVERAGE) +endif () if (SANITIZER) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=leak -fsanitize=undefined") -endif (SANITIZER) +endif () if (TSAN) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") -endif (TSAN) +endif () message(STATUS "Build type: ${CMAKE_BUILD_TYPE}: ${CMAKE_CXX_FLAGS}") +message(STATUS "Compiler: ${CMAKE_CXX_COMPILER_ID}" ) add_executable(megahit_core ${OTHER_SOURCE} ${ASMBL_SOURCE} ${IDBA_SOURCE} ${SDBG_SOURCE} ${LCASM_SOURCE} ${SEQ_SOURCE} ${CX1_SOURCE} ${TOOLKIT_SOURCE}) @@ -73,15 +75,32 @@ add_executable(megahit_core_popcnt ${OTHER_SOURCE} ${ASMBL_SOURCE} ${IDBA_SOURCE ${CX1_SOURCE} ${TOOLKIT_SOURCE}) add_executable(megahit_core_no_hw_accel ${OTHER_SOURCE} ${ASMBL_SOURCE} ${IDBA_SOURCE} ${SDBG_SOURCE} ${LCASM_SOURCE} ${SEQ_SOURCE} ${CX1_SOURCE} ${TOOLKIT_SOURCE}) -set_target_properties(megahit_core PROPERTIES COMPILE_FLAGS "-mbmi2 -DUSE_BMI2 -mpopcnt") -set_target_properties(megahit_core_popcnt PROPERTIES COMPILE_FLAGS "-mpopcnt") + +include(CheckCXXCompilerFlag) +#check_cxx_compiler_flag("-march=native" USE_ARCH_NATIVE) +#if(USE_ARCH_NATIVE) +# message(STATUS "Using native tuning (-march=native compiler flag set)") +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") +#endif() + +check_cxx_compiler_flag("-mpopcnt" USE_POPCNT) +if(USE_POPCNT) + message(STATUS "Using popcnt instructions (-mpopcnt compiler flag set)") + set_target_properties(megahit_core_popcnt PROPERTIES COMPILE_FLAGS "-mpopcnt") +endif() + +check_cxx_compiler_flag("-mbmi2" USE_BMI2) +if(USE_BMI2 AND USE_POPCNT) + message(STATUS "Using bmi2 instructions (-mbmi2 compiler flag set)") + set_target_properties(megahit_core PROPERTIES COMPILE_FLAGS "-mbmi2 -DUSE_BMI2 -mpopcnt") +endif() if (STATIC_BUILD) # TODO dirty set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--whole-archive -lpthread -Wl,--no-whole-archive -static") set_target_properties(megahit_core megahit_core_popcnt megahit_core_no_hw_accel PROPERTIES LINK_SEARCH_START_STATIC ON) set_target_properties(megahit_core megahit_core_popcnt megahit_core_no_hw_accel PROPERTIES LINK_SEARCH_END_STATIC ON) -endif (STATIC_BUILD) +endif () target_link_libraries(megahit_core ${ZLIB_LIBRARIES}) target_link_libraries(megahit_core_popcnt ${ZLIB_LIBRARIES}) From a410d48f1a84dc9af78a2ff7171f1bf36534c785 Mon Sep 17 00:00:00 2001 From: accopeland Date: Mon, 4 Apr 2022 22:26:26 +0000 Subject: [PATCH 3/4] In CMakeLists.txt, report aarch64/graviton2 case of missing bmi2 and popcount --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1308d1f..28513bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,12 +87,16 @@ check_cxx_compiler_flag("-mpopcnt" USE_POPCNT) if(USE_POPCNT) message(STATUS "Using popcnt instructions (-mpopcnt compiler flag set)") set_target_properties(megahit_core_popcnt PROPERTIES COMPILE_FLAGS "-mpopcnt") +else() + message(STATUS "popcnt not found ") endif() check_cxx_compiler_flag("-mbmi2" USE_BMI2) if(USE_BMI2 AND USE_POPCNT) message(STATUS "Using bmi2 instructions (-mbmi2 compiler flag set)") set_target_properties(megahit_core PROPERTIES COMPILE_FLAGS "-mbmi2 -DUSE_BMI2 -mpopcnt") +else() + message(STATUS "bmi2 not found") endif() if (STATIC_BUILD) From 3ec40583563074fbb8b6d9956b446732214d05e0 Mon Sep 17 00:00:00 2001 From: accopeland Date: Mon, 4 Apr 2022 23:41:24 +0000 Subject: [PATCH 4/4] In CMakeLists.txt, report aarch64/graviton2 case of missing bmi2 and popcount --- CMakeLists.txt | 25 +++++++++++++++++++++++-- src/kmlib/kmrns.h | 6 ++++++ src/utils/cpu_dispatch.h | 31 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 658325f..e4b29a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,8 +73,29 @@ add_executable(megahit_core_popcnt ${OTHER_SOURCE} ${ASMBL_SOURCE} ${IDBA_SOURCE ${CX1_SOURCE} ${TOOLKIT_SOURCE}) add_executable(megahit_core_no_hw_accel ${OTHER_SOURCE} ${ASMBL_SOURCE} ${IDBA_SOURCE} ${SDBG_SOURCE} ${LCASM_SOURCE} ${SEQ_SOURCE} ${CX1_SOURCE} ${TOOLKIT_SOURCE}) -#set_target_properties(megahit_core PROPERTIES COMPILE_FLAGS "-mbmi2 -DUSE_BMI2 -mpopcnt") -#set_target_properties(megahit_core_popcnt PROPERTIES COMPILE_FLAGS "-mpopcnt") + +include(CheckCXXCompilerFlag) +#check_cxx_compiler_flag("-march=native" USE_ARCH_NATIVE) +#if(USE_ARCH_NATIVE) +# message(STATUS "Using native tuning (-march=native compiler flag set)") +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") +#endif() + +check_cxx_compiler_flag("-mpopcnt" USE_POPCNT) +if(USE_POPCNT) + message(STATUS "Using popcnt instructions (-mpopcnt compiler flag set)") + set_target_properties(megahit_core_popcnt PROPERTIES COMPILE_FLAGS "-mpopcnt") +else() + message(STATUS "popcnt not found ") +endif() + +check_cxx_compiler_flag("-mbmi2" USE_BMI2) +if(USE_BMI2 AND USE_POPCNT) + message(STATUS "Using bmi2 instructions (-mbmi2 compiler flag set)") + set_target_properties(megahit_core PROPERTIES COMPILE_FLAGS "-mbmi2 -DUSE_BMI2 -mpopcnt") +else() + message(STATUS "bmi2 not found") +endif() if (STATIC_BUILD) # TODO dirty diff --git a/src/kmlib/kmrns.h b/src/kmlib/kmrns.h index ab9709f..60e7e6b 100644 --- a/src/kmlib/kmrns.h +++ b/src/kmlib/kmrns.h @@ -8,6 +8,12 @@ #include #include #include +#if defined(__GNUC__) && defined(__aarch64__) + #define SIMDE_ENABLE_NATIVE_ALIASES + #include "../simde/x86/avx2.h" +#else + #include +#endif #include namespace kmlib { diff --git a/src/utils/cpu_dispatch.h b/src/utils/cpu_dispatch.h index 75ecc87..7d9c62e 100644 --- a/src/utils/cpu_dispatch.h +++ b/src/utils/cpu_dispatch.h @@ -5,7 +5,38 @@ #ifndef MEGAHIT_CPU_DISPATCH_H #define MEGAHIT_CPU_DISPATCH_H +#if defined(__GNUC__) && defined(__aarch64__) inline bool HasPopcnt() { return false; } inline bool HasBmi2() { return false; } +#else + +inline bool HasPopcnt() { + unsigned eax, ebx, ecx, edx; +#ifdef _MSC_VER + int cpuid[4]; + __cpuid(cpuid, 1); + eax = cpuid[0], ebx = cpuid[1], ecx = cpuid[2], edx = cpuid[3]; +#else + asm volatile("cpuid\n\t" + : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) + : "0"(1)); +#endif + return ecx >> 23U & 1U; +} + +inline bool HasBmi2() { + unsigned eax, ebx, ecx, edx; +#ifdef _MSC_VER + int cpuid[4]; + __cpuidex(cpuid, 7, 0); + eax = cpuid[0], ebx = cpuid[1], ecx = cpuid[2], edx = cpuid[3]; +#else + asm volatile("cpuid\n\t" + : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) + : "0"(7), "2"(0)); +#endif + return ebx >> 8U & 1U; +} +#endif #endif // MEGAHIT_CPU_DISPATCH_H