https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126385
--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Philipp Tomsich <[email protected]>: https://gcc.gnu.org/g:c741d285510c07b909f0c4f07e763a989b6d181f commit r17-6736-gc741d285510c07b909f0c4f07e763a989b6d181f Author: Konstantinos Eleftheriou <[email protected]> Date: Fri Nov 13 16:04:56 2026 +0100 aarch64: Cost dot-product reduction latency per core [PR126385] aarch64_in_loop_reduction_latency costs every integer vector reduction as int_stmt_cost. Reductions that the expanders implement as dot products are therefore unrolled too little on cores where the dot product has a long accumulator latency. Add a per-core reduc_dotprod_cost to simd_vec_cost, and let aarch64_vect_stmt_cost return it as the accumulation latency of DOT_PROD_EXPR and of the SAD_EXPR and WIDEN_SUM_EXPR forms expanded as dot products. count_ops records this latency separately from reduction_latency, and only determine_suggested_unroll_factor uses it, as on x86. reduction_latency also compares vector modes, and the higher latency there would make short loops lose their dot products. determine_suggested_unroll_factor ignores the dot-product latency for loops known to run fewer than aarch64-loop-vect-issue-rate-niters vector iterations, which adjust_body_cost already costs by latency alone, since unrolling them into more accumulators made them slower. The ampere1 table, which ampere1a shares, and the ampere1b table set the field to 3, following their LLVM scheduling models. All other tables use 0, which leaves their unroll factors unchanged. With -mcpu=ampere1 these loops now use four accumulators, and long loops run 1.9x to 2.7x faster on AArch64 hardware at -O3 (2.12x for the PR's kernel). This is less than unroll-and-jam of the outer loop achieves, because the jammed iterations also share their loads. 706.stockfish_r from SPEC CPU 2026 gains 5.4% with this patch. Known limitations: short loops can be much slower. For dot products and SAD, loops of 96 to 240 bytes take up to 70% longer per call, and so do loops of 16 to 80 bytes when their trip count is unknown, since the dot-product latency is ignored only for loops known to run fewer than six vector iterations. The loss comes mostly from the epilogue, which is limited to 8-byte vectors (PR110474). Between 256 and about 500 bytes the change depends on the length, from 20% faster to 14% slower. Loops that feed one accumulator through two lane-reducing operations, such as two dot products, are unrolled by two rather than four, because count_ops records the larger of their latencies rather than the sum. Long loops of this kind still run about twice as fast as without the patch. Bootstrapped/regtested on AArch64 and x86-64. gcc/ChangeLog: PR tree-optimization/126385 * config/aarch64/aarch64-json-schema.h: Add reduc_dotprod_cost to the advsimd and sve vector costs. * config/aarch64/aarch64-json-tunings-parser-generated.inc (parse_vec_costs_advsimd, parse_vec_costs_sve): Regenerate. * config/aarch64/aarch64-json-tunings-printer-generated.inc (serialize_vec_costs_advsimd, serialize_vec_costs_sve): Regenerate. * config/aarch64/aarch64-protos.h (simd_vec_cost::reduc_dotprod_cost): New member variable. * config/aarch64/aarch64.cc (aarch64_vec_op_count::dot_prod_latency): New member variable. (aarch64_vec_op_count::dump): Dump it. (enum aarch64_vect_cost_kind): New enum. (aarch64_vect_stmt_cost): Add a kind parameter. Return reduc_dotprod_cost as the latency of statements expanded as dot products. (aarch64_vector_costs::count_ops): Record dot_prod_latency. (aarch64_vector_costs::low_iteration_count_p): New function, split out from... (aarch64_vector_costs::adjust_body_cost): ...here. (aarch64_vector_costs::determine_suggested_unroll_factor): Take dot_prod_latency into account unless low_iteration_count_p. * config/aarch64/tuning_models/a64fx.h: Set reduc_dotprod_cost to 0. * config/aarch64/tuning_models/ampere1.h: Set reduc_dotprod_cost to 3. * config/aarch64/tuning_models/ampere1b.h: Likewise. * config/aarch64/tuning_models/cortexa320.h: Set reduc_dotprod_cost to 0. * config/aarch64/tuning_models/cortexa57.h: Likewise. * config/aarch64/tuning_models/cortexx925.h: Likewise. * config/aarch64/tuning_models/exynosm1.h: Likewise. * config/aarch64/tuning_models/generic.h: Likewise. * config/aarch64/tuning_models/generic_armv8_a.h: Likewise. * config/aarch64/tuning_models/generic_armv9_a.h: Likewise. * config/aarch64/tuning_models/hip12.h: Likewise. * config/aarch64/tuning_models/neoverse512tvb.h: Likewise. * config/aarch64/tuning_models/neoversen2.h: Likewise. * config/aarch64/tuning_models/neoversen3.h: Likewise. * config/aarch64/tuning_models/neoversev1.h: Likewise. * config/aarch64/tuning_models/neoversev2.h: Likewise. * config/aarch64/tuning_models/neoversev3.h: Likewise. * config/aarch64/tuning_models/neoversev3ae.h: Likewise. * config/aarch64/tuning_models/olympus.h: Likewise. * config/aarch64/tuning_models/qdf24xx.h: Likewise. * config/aarch64/tuning_models/thunderx.h: Likewise. * config/aarch64/tuning_models/thunderx2t99.h: Likewise. * config/aarch64/tuning_models/thunderx3t110.h: Likewise. * config/aarch64/tuning_models/tsv110.h: Likewise. * config/aarch64/tuning_models/xgene1.h: Likewise. * doc/params.texi (aarch64-loop-vect-issue-rate-niters): Mention the dot-product unrolling. gcc/testsuite/ChangeLog: PR tree-optimization/126385 * gcc.target/aarch64/ampere1-reduction-unroll-2-run.c: New test. * gcc.target/aarch64/ampere1-reduction-unroll-2.c: New test. * gcc.target/aarch64/ampere1-reduction-unroll-3.c: New test. * gcc.target/aarch64/pr126385.c: New test.
