Killing what can't be handled – putting a spaghetti demon to eternal rest themai...

@best

Killing what can't be handled – putting a spaghetti demon to eternal rest themaister.net · 13 points · 3 comments · HN thread

TL;DR Hans-Kristian Bjørgen (themaister) describes rewriting the control flow graph structurizer in dxil-spirv, which converts DXIL shaders to SPIR-V, after the old 9000-line implementation kept breaking on game shaders. The new version drops most debuggability in favour of correctness, borrows Mesa's goto_if approach for unstructured control flow, uses PHI-to-variable lowering, and was validated with a random CFG fuzzer that found several Mesa bugs. The author reports all of a 6-year DXIL shader corpus now passes SPIR-V validation with good GPU performance.

  • The old dxil-spirv structurizer was a roughly 9000-line implementation that was fragile, hard to reason about, and had a bus factor of one.
  • The author first tested Mesa's unstructured control flow path via a fake SPIR-V extension; RADV performance was at parity, while NVIDIA proprietary performance was unknown at the time.
  • A random reducible CFG fuzzer generated CFGs, ran them on the GPU and compared bit-exact feedback, finding a number of Mesa bugs that were reported.
  • The new algorithm uses dominance frontiers and recursive level stacks for forward control flow, with skip analysis, and switch blocks are lowered into conditional branches.
  • PHI nodes are lowered to OpVariables and stores, relying on the driver's mem2reg, instead of being rewritten in SSA form.
  • Loops are handled by splitting blocks into inside and outside the loop, redirecting breaks through merge blocks, and tracking loop scopes for multi-level breaks and continues.
  • After fuzzing and the DXIL corpus test, the author considers the implementation good enough and reports good GPU performance.

Discussion The thread is very short, with three comments. Readers mostly responded to the writing style, with a commenter laughing at the prompt-injection joke in the article and another saying they will look into using the work. The one substantive exchange questions whether GPU shaders can contain infinite loops, which would be a concern for the fuzzer's CPU-side simulation. The reply says SPIR-V does not enforce termination at compile time and points to the structured control flow rules in the spec. The mood is appreciative and light.

  • Praise for the article's writing and humour (some): A commenter found the prompt-injection line very funny and thanked the author for the write-up.
  • Intends to try the work (a few): One commenter said they will look into using the implementation.
  • Infinite loops in shaders are a concern (a few): One commenter argues shaders should be finite and fast, so unbounded loops from the fuzzer's risk of GPU hangs seem unexpected.
  • SPIR-V does not forbid infinite loops (a few): A reply notes that the compiler, not SPIR-V, is supposed to enforce termination, and that SPIR-V allows describing infinite loops, with the spec's structured control flow rules linked.

Links from the comments

#shaders #SPIRV #Mesa #GPU #compilers #DXIL