]> git.sesse.net Git - stockfish/commit
Avoid unnecessary stores in the affine transform
authorTomasz Sobczyk <tomasz.sobczyk1997@gmail.com>
Tue, 27 Jul 2021 20:12:14 +0000 (22:12 +0200)
committerJoost VandeVondele <Joost.VandeVondele@gmail.com>
Fri, 30 Jul 2021 15:15:52 +0000 (17:15 +0200)
commit26edf9534ad571a6d26bf9db47d21776cbf45d54
treea230f4f01fe35f1afd1a84db2992dad9dea9da08
parente973eee919a8d450f095d102a0d52c196a8e7793
Avoid unnecessary stores in the affine transform

This patch improves the codegen in the AffineTransform::forward function for architectures >=SSSE3. Current code works directly on memory and the compiler cannot see that the stores through outptr do not alias the loads through weights and input32. The solution implemented is to perform the affine transform with local variables as accumulators and only store the result to memory at the end. The number of accumulators required is OutputDimensions / OutputSimdWidth, which means that for the 1024->16 affine transform it requires 4 registers with SSSE3, 2 with AVX2, 1 with AVX512. It also cuts the number of stores required by NumRegs * 256 for each node evaluated. The local accumulators are expected to be assigned to registers, but even if this cannot be done in some case due to register pressure it will help the compiler to see that there is no aliasing between the loads and stores and may still result in better codegen.

See https://godbolt.org/z/59aTKbbYc for codegen comparison.

passed STC:
LLR: 2.94 (-2.94,2.94) <-0.50,2.50>
Total: 140328 W: 10635 L: 10358 D: 119335
Ptnml(0-2): 302, 8339, 52636, 8554, 333

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

No functional change
src/nnue/layers/affine_transform.h