[libc-commits] [PATCH] D74897: [libc] Add CMake script to check host cpu features

Guillaume Chatelet via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Feb 26 02:34:30 PST 2020


gchatelet updated this revision to Diff 246655.
gchatelet marked 2 inline comments as done.
gchatelet added a comment.

- Address comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74897/new/

https://reviews.llvm.org/D74897

Files:
  libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
  libc/cmake/modules/cpu_features/check_avx.cpp
  libc/cmake/modules/cpu_features/check_avx512f.cpp
  libc/cmake/modules/cpu_features/check_sse.cpp
  libc/cmake/modules/cpu_features/check_sse2.cpp


Index: libc/cmake/modules/cpu_features/check_sse2.cpp
===================================================================
--- /dev/null
+++ libc/cmake/modules/cpu_features/check_sse2.cpp
@@ -0,0 +1,8 @@
+#if !defined __SSE2__
+#error "missing __SSE2__"
+#endif
+#include <immintrin.h>
+int main() {
+  (void)_mm_set1_epi8('0');
+  return 0;
+}
Index: libc/cmake/modules/cpu_features/check_sse.cpp
===================================================================
--- /dev/null
+++ libc/cmake/modules/cpu_features/check_sse.cpp
@@ -0,0 +1,8 @@
+#if !defined __SSE__
+#error "missing __SSE__"
+#endif
+#include <immintrin.h>
+int main() {
+  (void)_mm_set_ss(1.0f);
+  return 0;
+}
Index: libc/cmake/modules/cpu_features/check_avx512f.cpp
===================================================================
--- /dev/null
+++ libc/cmake/modules/cpu_features/check_avx512f.cpp
@@ -0,0 +1,8 @@
+#if !defined __AVX512F__
+#error "missing __AVX512F__"
+#endif
+#include <immintrin.h>
+int main() {
+  (void)_mm512_undefined();
+  return 0;
+}
Index: libc/cmake/modules/cpu_features/check_avx.cpp
===================================================================
--- /dev/null
+++ libc/cmake/modules/cpu_features/check_avx.cpp
@@ -0,0 +1,8 @@
+#if !defined __AVX__
+#error "missing __AVX__"
+#endif
+#include <immintrin.h>
+int main() {
+  (void)_mm256_set1_epi8('0');
+  return 0;
+}
Index: libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
===================================================================
--- /dev/null
+++ libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
@@ -0,0 +1,38 @@
+if(${LIBC_TARGET_MACHINE} MATCHES "x86|x86_64")
+  set(ALL_CPU_FEATURES SSE SSE2 AVX AVX512F)
+endif()
+
+function(define_cpu_feature_flags feature)
+  if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
+    string(TOLOWER ${feature} lowercase_feature)
+    set(CPU_FEATURE_${feature}_ENABLE_FLAG "-m${lowercase_feature}" PARENT_SCOPE)
+    set(CPU_FEATURE_${feature}_DISABLE_FLAG "-mno-${lowercase_feature}" PARENT_SCOPE)
+  else()
+    # In future, we can extend for other compilers.
+    message(FATAL_ERROR "Unkown compiler ${CMAKE_CXX_COMPILER_ID}.")
+  endif()
+endfunction()
+
+function(check_host_cpu_feature feature)
+    if(NOT ${feature} IN_LIST ALL_CPU_FEATURES)
+        message(FATAL_ERROR "${feature} is not part of available ${ALL_CPU_FEATURES}")
+    endif()
+    string(TOLOWER ${feature} lowercase_feature)
+    try_run(
+        run_result
+        compile_result
+        "${CMAKE_CURRENT_BINARY_DIR}/check_${lowercase_feature}"
+        "${CMAKE_MODULE_PATH}/cpu_features/check_${lowercase_feature}.cpp"
+        COMPILE_DEFINITIONS ${CPU_FEATURE_${feature}_ENABLE_FLAG}
+        OUTPUT_VARIABLE compile_output
+    )
+    if(${compile_result} AND ("${run_result}" EQUAL 0))
+        list(APPEND HOST_CPU_FEATURES ${feature})
+        set(HOST_CPU_FEATURES ${HOST_CPU_FEATURES} PARENT_SCOPE)
+    endif()
+endfunction()
+
+foreach(feature IN LISTS ALL_CPU_FEATURES)
+    define_cpu_feature_flags(${feature})
+    check_host_cpu_feature(${feature})
+endforeach(feature)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74897.246655.patch
Type: text/x-patch
Size: 3077 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20200226/57523386/attachment-0001.bin>


More information about the libc-commits mailing list