[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
Thu Feb 20 06:04:22 PST 2020


gchatelet created this revision.
gchatelet added reviewers: sivachandra, abrachet.
Herald added subscribers: libc-commits, tschuett, MaskRay, mgorny.
Herald added a project: libc-project.

Tested on MacOSX and Linux.
For robustness we can go the OpenCV way and add individual c++ files with intrinsics.
https://github.com/opencv/opencv/blob/master/cmake/checks/cpu_avx2.cpp


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74897

Files:
  libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake


Index: libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
===================================================================
--- /dev/null
+++ libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
@@ -0,0 +1,36 @@
+# Sets variables of the form CPU_FEATURES_XXX
+# Where XXX is the uppercase name of the feature.
+# Usage
+#
+# include(LLVMLibCCheckCpuFeatures)
+# if(${CPU_FEATURES_AVX2})
+#    ...
+# endif()
+
+function(check_cpu_features)
+    if(${CMAKE_HOST_APPLE})
+        execute_process(
+            COMMAND sysctl -a
+            COMMAND grep machdep.cpu.features
+            COMMAND cut -f2 -d:
+            COMMAND tr . _
+            OUTPUT_VARIABLE flags_string
+        )
+    elseif(${CMAKE_HOST_UNIX})
+        execute_process(
+            COMMAND grep -h -m1 \\w*flags\\w* /proc/cpuinfo
+            COMMAND cut -f2 -d:
+            OUTPUT_VARIABLE flags_string
+        )
+    else()
+        message(FATAL "Unsupported system")
+    endif()
+
+    string(REPLACE " " ";" flags "${flags_string}")
+    foreach(flag ${flags})
+        string(TOUPPER ${flag} uppercase_flag)
+        set(CPU_FEATURES_${uppercase_flag} true PARENT_SCOPE)
+    endforeach()
+endfunction()
+
+check_cpu_features()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74897.245632.patch
Type: text/x-patch
Size: 1211 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20200220/9ab26ffd/attachment.bin>


More information about the libc-commits mailing list