Every OpGroupNonUniformBallot now goes through SpirvModule::AddSubgroupBallot.
A repeated ballot of the same predicate in the same block becomes an
OpCopyObject of the first ballot. Within one block the active invocations do
not change, so the result is identical. The cache is cleared at every label,
function boundary, function call and demote, and helper functions keep their
own cache.
When SPIRV-Tools is enabled, block merging and specialization can still put
two ballots of one predicate into the same block after emission, so
ValidateAndOptimizeSpirv collapses repeated block ballots the same way on
every module it returns, optimized or not.
NVIDIA 580's SPIR-V compiler segfaulted (libnvidia-gpucomp+0xe3facc) on GTA V
compute shaders that ballot one predicate many times in a block. One such
shader had 163 repeated ballots across three blocks. With them collapsed, both
crashing captures (compute-fe506552cec99bfc and compute-c02e5e3f35010fa5)
compile.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>