From 55df0ee00914f6bb5e9ecce4ace47f00b758098c Mon Sep 17 00:00:00 2001 From: Disservin Date: Tue, 12 Mar 2024 18:20:19 +0100 Subject: [PATCH] Fix Raspberry Pi Compilation Reported by @Torom over discord. > dev build fails on Raspberry Pi 5 with clang ``` clang++ -o stockfish benchmark.o bitboard.o evaluate.o main.o misc.o movegen.o movepick.o position.o search.o thread.o timeman.o tt.o uci.o ucioption.o tune.o tbprobe.o nnue_misc.o half_ka_v2_hm.o network.o -fprofile-instr-generate -latomic -lpthread -Wall -Wcast-qual -fno-exceptions -std=c++17 -fprofile-instr-generate -pedantic -Wextra -Wshadow -Wmissing-prototypes -Wconditional-uninitialized -DUSE_PTHREADS -DNDEBUG -O3 -funroll-loops -DIS_64BIT -DUSE_POPCNT -DUSE_NEON=8 -march=armv8.2-a+dotprod -DUSE_NEON_DOTPROD -DGIT_SHA=627974c9 -DGIT_DATE=20240312 -DARCH=armv8-dotprod -flto=full /tmp/lto-llvm-e9300e.o: in function `_GLOBAL__sub_I_network.cpp': ld-temp.o:(.text.startup+0x704c): relocation truncated to fit: R_AARCH64_LDST64_ABS_LO12_NC against symbol `gEmbeddedNNUEBigEnd' defined in .rodata section in /tmp/lto-llvm-e9300e.o /usr/bin/ld: ld-temp.o:(.text.startup+0x704c): warning: one possible cause of this error is that the symbol is being referenced in the indicated code as if it had a larger alignment than was declared where it was defined ld-temp.o:(.text.startup+0x7068): relocation truncated to fit: R_AARCH64_LDST64_ABS_LO12_NC against symbol `gEmbeddedNNUESmallEnd' defined in .rodata section in /tmp/lto-llvm-e9300e.o /usr/bin/ld: ld-temp.o:(.text.startup+0x7068): warning: one possible cause of this error is that the symbol is being referenced in the indicated code as if it had a larger alignment than was declared where it was defined clang: error: linker command failed with exit code 1 (use -v to see invocation) make[2]: *** [Makefile:1051: stockfish] Error 1 make[2]: Leaving directory '/home/torsten/chess/Stockfish_master/src' make[1]: *** [Makefile:1058: clang-profile-make] Error 2 make[1]: Leaving directory '/home/torsten/chess/Stockfish_master/src' make: *** [Makefile:886: profile-build] Error 2 ``` closes https://github.com/official-stockfish/Stockfish/pull/5106 No functional change --- src/Makefile | 2 +- src/nnue/network.cpp | 28 ++++++++++++++++++++++++---- src/nnue/network.h | 24 ++++++++---------------- src/uci.cpp | 4 ++-- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/Makefile b/src/Makefile index bd04d2c410e..75f31108159 100644 --- a/src/Makefile +++ b/src/Makefile @@ -502,7 +502,7 @@ endif # In earlier NDK versions, you'll need to pass -fno-addrsig if using GNU binutils. # Currently we don't know how to make PGO builds with the NDK yet. ifeq ($(COMP),ndk) - CXXFLAGS += -stdlib=libc++ -fPIE -mcmodel=large + CXXFLAGS += -stdlib=libc++ -fPIE comp=clang ifeq ($(arch),armv7) CXX=armv7a-linux-androideabi16-clang++ diff --git a/src/nnue/network.cpp b/src/nnue/network.cpp index 5d4e0954d78..bea3e7cb398 100644 --- a/src/nnue/network.cpp +++ b/src/nnue/network.cpp @@ -55,15 +55,33 @@ const unsigned char gEmbeddedNNUESmallData[1] = {0x0}; const unsigned char* const gEmbeddedNNUESmallEnd = &gEmbeddedNNUESmallData[1]; const unsigned int gEmbeddedNNUESmallSize = 1; #endif + +struct EmbeddedNNUE { + EmbeddedNNUE(const unsigned char* embeddedData, + const unsigned char* embeddedEnd, + const unsigned int embeddedSize) : + data(embeddedData), + end(embeddedEnd), + size(embeddedSize) {} + const unsigned char* data; + const unsigned char* end; + const unsigned int size; +}; + +using namespace Stockfish::Eval::NNUE; + +EmbeddedNNUE get_embedded(EmbeddedNNUEType type) { + if (type == EmbeddedNNUEType::BIG) + return EmbeddedNNUE(gEmbeddedNNUEBigData, gEmbeddedNNUEBigEnd, gEmbeddedNNUEBigSize); + else + return EmbeddedNNUE(gEmbeddedNNUESmallData, gEmbeddedNNUESmallEnd, gEmbeddedNNUESmallSize); +} + } namespace Stockfish::Eval::NNUE { -const EmbeddedNNUE embeddedNNUEBig(gEmbeddedNNUEBigData, gEmbeddedNNUEBigEnd, gEmbeddedNNUEBigSize); -const EmbeddedNNUE - embeddedNNUESmall(gEmbeddedNNUESmallData, gEmbeddedNNUESmallEnd, gEmbeddedNNUESmallSize); - namespace Detail { @@ -302,6 +320,8 @@ void Network::load_internal() { } }; + const auto embedded = get_embedded(embeddedType); + MemoryBuffer buffer(const_cast(reinterpret_cast(embedded.data)), size_t(embedded.size)); diff --git a/src/nnue/network.h b/src/nnue/network.h index c1ed7717914..21e1c622205 100644 --- a/src/nnue/network.h +++ b/src/nnue/network.h @@ -34,27 +34,19 @@ namespace Stockfish::Eval::NNUE { -struct EmbeddedNNUE { - EmbeddedNNUE(const unsigned char* embeddedData, - const unsigned char* embeddedEnd, - const unsigned int embeddedSize) : - data(embeddedData), - end(embeddedEnd), - size(embeddedSize) {} - const unsigned char* data; - const unsigned char* end; - const unsigned int size; + +enum class EmbeddedNNUEType { + BIG, + SMALL, }; -extern const EmbeddedNNUE embeddedNNUEBig; -extern const EmbeddedNNUE embeddedNNUESmall; template class Network { public: - Network(EvalFile file, EmbeddedNNUE embeddedEval) : + Network(EvalFile file, EmbeddedNNUEType type) : evalFile(file), - embedded(embeddedEval) {} + embeddedType(type) {} void load(const std::string& rootDirectory, std::string evalfilePath); bool save(const std::optional& filename) const; @@ -92,8 +84,8 @@ class Network { // Evaluation function AlignedPtr network[LayerStacks]; - EvalFile evalFile; - EmbeddedNNUE embedded; + EvalFile evalFile; + EmbeddedNNUEType embeddedType; // Hash value of evaluation function structure static constexpr std::uint32_t hash = Transformer::get_hash_value() ^ Arch::get_hash_value(); diff --git a/src/uci.cpp b/src/uci.cpp index fda336b0d8e..cf0e3f09cc7 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -54,8 +54,8 @@ namespace NN = Eval::NNUE; UCI::UCI(int argc, char** argv) : networks(NN::Networks( - NN::NetworkBig({EvalFileDefaultNameBig, "None", ""}, NN::embeddedNNUEBig), - NN::NetworkSmall({EvalFileDefaultNameSmall, "None", ""}, NN::embeddedNNUESmall))), + NN::NetworkBig({EvalFileDefaultNameBig, "None", ""}, NN::EmbeddedNNUEType::BIG), + NN::NetworkSmall({EvalFileDefaultNameSmall, "None", ""}, NN::EmbeddedNNUEType::SMALL))), cli(argc, argv) { options["Debug Log File"] << Option("", [](const Option& o) { start_logger(o); });