]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Implement AVX2 runtime detection instead of using blake3's
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 30 Mar 2024 09:02:27 +0000 (10:02 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 27 Apr 2024 08:14:29 +0000 (10:14 +0200)
13 files changed:
ci/build
cmake/GenerateConfigurationFile.cmake
cmake/config.h.in
src/ccache/core/mainoptions.cpp
src/ccache/hashutil.cpp
src/ccache/storage/Storage.cpp
src/ccache/storage/Storage.hpp
src/ccache/util/CMakeLists.txt
src/ccache/util/cpu.cpp [new file with mode: 0644]
src/ccache/util/cpu.hpp [new file with mode: 0644]
src/third_party/blake3/CMakeLists.txt
src/third_party/blake3/blake3_cpu_supports_avx2.h [deleted file]
src/third_party/blake3/blake3_dispatch_ccache.c [deleted file]

index 4ed04ebb9aab1d768cb95ebfcde038446b83acbc..f86ca82c0373768a7b0119488248a4adf261086b 100755 (executable)
--- a/ci/build
+++ b/ci/build
@@ -35,12 +35,17 @@ else
             ;;
     esac
 
+    if [ -x ccache ]; then
+        ./ccache --version
+    elif command -v wine >/dev/null && [ -f ccache.exe ]; then
+        wine ccache.exe --version
+    fi
+
     case "${RUN_TESTS:-all}" in
         all)
             CC="${TEST_CC}" ctest --output-on-failure -j"${JOBS}" "$@"
             ;;
         unittest-in-wine)
-            wine ccache.exe --version
             wine unittest/unittest.exe
             ;;
         none)
index 7caf21b58054ce5424eb7f419188d65e07908ce0..a3d24cb7fd71d6a0be4a5f08fa343e899ebb835b 100644 (file)
@@ -1,5 +1,6 @@
 include(CheckIncludeFile)
 set(include_files
+    cpuid.h
     dirent.h
     linux/fs.h
     pwd.h
@@ -70,8 +71,8 @@ include(CheckCXXSourceCompiles)
 check_cxx_source_compiles(
   [=[
     #include <immintrin.h>
-    #ifndef _MSC_VER // MSVC does not need explicit enabling of AVX2.
-    void func() __attribute__((target("avx2")));
+    #ifndef _MSC_VER
+    __attribute__((target("avx2")))
     #endif
     void func() { _mm256_abs_epi8(_mm256_set1_epi32(42)); }
     int main()
index 44027dfb88ecb10124b9b430b1f24d6e6a32a158..24f2038f21072c3421f475b52babad52a97ef8cd 100644 (file)
@@ -69,6 +69,9 @@
 
 // === Header files ===
 
+// Define if you have the <cpuid.h> header file.
+#cmakedefine HAVE_CPUID_H
+
 // Define if you have the <dirent.h> header file.
 #cmakedefine HAVE_DIRENT_H
 
index 28d34e07bf5099e41c399e1c2c41bf0ca2d2d241..f180dccf71e3906954b7b42a9dfc7369b556fb28 100644 (file)
@@ -44,6 +44,7 @@
 #include <ccache/util/UmaskScope.hpp>
 #include <ccache/util/XXH3_128.hpp>
 #include <ccache/util/assertions.hpp>
+#include <ccache/util/cpu.hpp>
 #include <ccache/util/environment.hpp>
 #include <ccache/util/expected.hpp>
 #include <ccache/util/file.hpp>
@@ -417,8 +418,13 @@ parse_compression_level(std::string_view level)
 static std::string
 get_version_text(const std::string_view ccache_name)
 {
+  auto features = storage::get_features();
+  if (util::cpu_supports_avx2()) {
+    features.emplace_back("avx2");
+  }
+  std::sort(features.begin(), features.end());
   return FMT(
-    VERSION_TEXT, ccache_name, CCACHE_VERSION, storage::get_features());
+    VERSION_TEXT, ccache_name, CCACHE_VERSION, util::join(features, " "));
 }
 
 std::string
index c22f7b344f64a6611982a2ce05a145975759c14e..e5c9a2489a8c6d2a9a6eb90c3efbd35753c693f4 100644 (file)
@@ -25,6 +25,7 @@
 #include <ccache/execute.hpp>
 #include <ccache/macroskip.hpp>
 #include <ccache/util/DirEntry.hpp>
+#include <ccache/util/cpu.hpp>
 #include <ccache/util/file.hpp>
 #include <ccache/util/format.hpp>
 #include <ccache/util/logging.hpp>
@@ -36,8 +37,6 @@
 #  include "InodeCache.hpp"
 #endif
 
-#include <blake3_cpu_supports_avx2.h>
-
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
 #endif
@@ -112,14 +111,12 @@ check_for_temporal_macros_bmh(std::string_view str, size_t start = 0)
 }
 
 #ifdef HAVE_AVX2
-#  ifndef _MSC_VER // MSVC does not need explicit enabling of AVX2.
-HashSourceCodeResult check_for_temporal_macros_avx2(std::string_view str)
-  __attribute__((target("avx2")));
-#  endif
-
 // The following algorithm, which uses AVX2 instructions to find __DATE__,
 // __TIME__ and __TIMESTAMP__, is heavily inspired by
 // <http://0x80.pl/articles/simd-strfind.html>.
+#  ifndef _MSC_VER
+__attribute__((target("avx2")))
+#  endif
 HashSourceCodeResult
 check_for_temporal_macros_avx2(std::string_view str)
 {
@@ -222,7 +219,7 @@ HashSourceCodeResult
 check_for_temporal_macros(std::string_view str)
 {
 #ifdef HAVE_AVX2
-  if (blake3_cpu_supports_avx2()) {
+  if (util::cpu_supports_avx2()) {
     return check_for_temporal_macros_avx2(str);
   }
 #endif
index 1149940c9620c4ac692e52f64143bda8b29b94c5..3d6f82f5a84e83b7577a0b26624c4d9c6114a4b3 100644 (file)
@@ -60,7 +60,7 @@ const std::unordered_map<std::string /*scheme*/,
 #endif
 };
 
-std::string
+std::vector<std::string>
 get_features()
 {
   std::vector<std::string> features;
@@ -69,8 +69,7 @@ get_features()
                  k_remote_storage_implementations.end(),
                  std::back_inserter(features),
                  [](auto& entry) { return FMT("{}-storage", entry.first); });
-  std::sort(features.begin(), features.end());
-  return util::join(features, " ");
+  return features;
 }
 
 // Representation of one shard configuration.
index 3d46486d0e9a7c1fbe64c452dabfe8de12530afb..49733e990b2fcda82ebd9b5e0b0bb57456a9589e 100644 (file)
@@ -37,7 +37,7 @@ namespace storage {
 
 constexpr auto k_redacted_password = "********";
 
-std::string get_features();
+std::vector<std::string> get_features();
 
 struct RemoteStorageBackendEntry;
 struct RemoteStorageEntry;
index 63c936b3f6abe54c990ff1d3a165aa824d205cdb..085494910b94ecedf8becdcc33de4c69ff257a89 100644 (file)
@@ -12,6 +12,7 @@ set(
   Tokenizer.cpp
   UmaskScope.cpp
   assertions.cpp
+  cpu.cpp
   environment.cpp
   error.cpp
   file.cpp
diff --git a/src/ccache/util/cpu.cpp b/src/ccache/util/cpu.cpp
new file mode 100644 (file)
index 0000000..18408a6
--- /dev/null
@@ -0,0 +1,53 @@
+// Copyright (C) 2024 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "cpu.hpp"
+
+#ifdef _MSC_VER
+#  include <intrin.h>
+#endif
+
+#ifdef HAVE_CPUID_H
+#  include <cpuid.h>
+#endif
+
+namespace util {
+
+bool
+cpu_supports_avx2()
+{
+  // CPUID with EAX=7 ECX=0 returns AVX2 support in bit 5 of EBX.
+  int registers[4]; // EAX, EBX, ECX, EDX
+#if defined(_MSC_VER) && defined(_M_X64)
+  __cpuidex(registers, 7, 0);
+#elif defined(HAVE_CPUID_H)
+  __cpuid_count(7, 0, registers[0], registers[1], registers[2], registers[3]);
+#elif __x86_64__
+  __asm__ __volatile__("cpuid"
+                       : "=a"(registers[0]),
+                         "=b"(registers[1]),
+                         "=c"(registers[2]),
+                         "=d"(registers[3])
+                       : "a"(7), "c"(0));
+#else
+  registers[1] = 0;
+#endif
+  return registers[1] & (1 << 5);
+}
+
+} // namespace util
diff --git a/src/ccache/util/cpu.hpp b/src/ccache/util/cpu.hpp
new file mode 100644 (file)
index 0000000..3a065b0
--- /dev/null
@@ -0,0 +1,27 @@
+// Copyright (C) 2024 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#pragma once
+
+namespace util {
+
+// --- Interface ---
+
+bool cpu_supports_avx2();
+
+} // namespace util
index 1d4ad94427dfd77068f1bde6e5d7f1187cf38dd5..7ab255f2111f374cff5815ce9f67c40b96a0911a 100644 (file)
@@ -1,4 +1,4 @@
-add_library(dep_blake3 STATIC blake3.c blake3_dispatch_ccache.c blake3_portable.c)
+add_library(dep_blake3 STATIC blake3.c blake3_dispatch.c blake3_portable.c)
 
 target_include_directories(dep_blake3 INTERFACE "${CMAKE_SOURCE_DIR}/src/third_party/blake3")
 target_link_libraries(dep_blake3 PRIVATE standard_settings)
diff --git a/src/third_party/blake3/blake3_cpu_supports_avx2.h b/src/third_party/blake3/blake3_cpu_supports_avx2.h
deleted file mode 100644 (file)
index 5d52cae..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-#ifndef BLAKE3_CPU_SUPPORTS_AVX2_H
-#define BLAKE3_CPU_SUPPORTS_AVX2_H
-
-// This file is a ccache modification to BLAKE3
-
-#include <stdbool.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-bool blake3_cpu_supports_avx2();
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/src/third_party/blake3/blake3_dispatch_ccache.c b/src/third_party/blake3/blake3_dispatch_ccache.c
deleted file mode 100644 (file)
index 86496e7..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-// This file is a ccache modification to BLAKE3
-
-#include "blake3_dispatch.c"
-
-#include "blake3_cpu_supports_avx2.h"
-
-bool blake3_cpu_supports_avx2()
-{
-  return get_cpu_features() & AVX2;
-}