]> git.sesse.net Git - stockfish/commitdiff
Use dynamic allocation for evaluation scratch TLS buffer.
authorTomasz Sobczyk <tomasz.sobczyk1997@gmail.com>
Sun, 27 Feb 2022 16:02:13 +0000 (17:02 +0100)
committerJoost VandeVondele <Joost.VandeVondele@gmail.com>
Tue, 1 Mar 2022 16:51:02 +0000 (17:51 +0100)
fixes #3946 an issue related with the toolchain as found in xcode 12 on macOS,
related to previous commit 5f781d36.

closes https://github.com/official-stockfish/Stockfish/pull/3950

No functional change

src/nnue/nnue_architecture.h

index b4f65364c2e1c4bb727721ab3922550b75685e41..4f9596ae949e032a8e1d45d42f4c2312ad7f30c1 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef NNUE_ARCHITECTURE_H_INCLUDED
 #define NNUE_ARCHITECTURE_H_INCLUDED
 
+#include <memory>
+
 #include "nnue_common.h"
 
 #include "features/half_ka_v2_hm.h"
@@ -88,9 +90,7 @@ struct Network
 
   std::int32_t propagate(const TransformedFeatureType* transformedFeatures)
   {
-    constexpr uint64_t alignment = CacheLineSize;
-
-    struct Buffer
+    struct alignas(CacheLineSize) Buffer
     {
       alignas(CacheLineSize) decltype(fc_0)::OutputBuffer fc_0_out;
       alignas(CacheLineSize) decltype(ac_0)::OutputBuffer ac_0_out;
@@ -104,12 +104,13 @@ struct Network
       }
     };
 
-#if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
-    static thread_local char bufferRaw[sizeof(Buffer) + alignment];
-    static thread_local char* bufferRawAligned = align_ptr_up<alignment>(&bufferRaw[0]);
-    static thread_local Buffer& buffer = *(new (bufferRawAligned) Buffer);
+#if defined(__clang__) && (__APPLE__)
+    // workaround for a bug reported with xcode 12
+    static thread_local auto tlsBuffer = std::make_unique<Buffer>();
+    // Access TLS only once, cache result.
+    Buffer& buffer = *tlsBuffer;
 #else
-    alignas(alignment) static thread_local Buffer buffer;
+    alignas(CacheLineSize) static thread_local Buffer buffer;
 #endif
 
     fc_0.propagate(transformedFeatures, buffer.fc_0_out);