ML compiler eng, your job is NP-hard

(ianbarber.blog)

33 points | by gmays 4 days ago ago

9 comments

  • saagarjha a day ago ago

    > in this case it moved one local_load 6 ops earlier, which caused ptxas to drop from 255 registers to 40. I don’t know why!

    This is usually because ptxas runs a very simple pass to try to calculate register usage and it has a number of heuristics that it tries to apply to decide whether to keep things in the same registers or to grab new ones. Usually I find that for very high performance code they are not useful and produce very surprising behavior that makes it hard to predict performance :(

  • mathisfun123 a day ago ago

    This isn't special - almost every compiler pass is a heuristic approximation of an NP-hard problem (hello register allocation, instruction scheduling).

    • ebiederm 20 hours ago ago

      A small nit. The problem of assigning registers to values (in SSA form) has a single pass optimal solution.

      Adding other complexities such as when to spill, and the possibility of reordering computation to reduce the number of registers makes the problem more difficult.

      All of which makes me wonder how many of the "NP-hard problems" are in fact easier problems that a covering general case is hard

      • fooker 15 hours ago ago

        There's no guarantee that what's optimal when assigning registers in SSA form remains optimal when you lower out of SSA.

        That's why it's a simpler problem with a polynomial time solution, not a magical bypass for np-completeness.

      • 19 hours ago ago
        [deleted]
      • mathisfun123 18 hours ago ago

        > The problem of assigning registers to values (in SSA form) has a single pass optimal solution

        I knew someone would comment this because hn is always "well akshually...". But the parenthetical is doing all the work in this "nit". Yes coloring chordal graphs is polynomial but real compilers don't do register allocation on SSA form. Spilling and spitting break chordal form.

        • hayley-patton 17 hours ago ago

          The HotSpot C1 compiler allocates on SSA form [0], so does libfirm [1], I think the Android Runtime does [2], Go does [3], and so does Cranelift [4]. And this compiler for Intel GPUs does too [5].

          [0] https://dl.acm.org/doi/acm/10.1145/1772954.1772979 however they do note that splitting breaks SSA form, and fall back to slower algorithms for values which get split.

          [1] https://libfirm.github.io/Features.html

          [2] https://arxiv.org/abs/2011.05608v1

          [3] https://developers.redhat.com/articles/2024/09/24/go-compile...

          [4] https://github.com/bytecodealliance/regalloc2/

          [5] https://www.youtube.com/watch?v=-UFjec3YctM

        • ebiederm 4 hours ago ago

          The linear scan register allocator is the optimal solution and it is used widely even on non-SSA form repreststions of code. My memory says pretty much every JIT compiler and LLVM. People use it because it is good, fast, and easy to adapt to the challenges of the task that don't show up in theory.

          I tried to understand the NP-completeness proof at one point. I learned the NP-completeness did not come from graph coloring but from program transformations like spilling and exactly how the problem had been framed.

          It is not the case that spilling and splitting necessarily break SSA-form. It is the case that any program transformation messes up an explicit register interference graph. Which makes Chatain's register allocation by graph coloring stupidly expensive in the case of code transformations.

          Having used that algorithm I really dislike it in practice. It makes everything harder all so an approximate solution can be used for part of the problem that has a cheap exact solution.

          But hey it looks simple.

          To address your original point. Most compiler algorithms are heuristics because even an O(N^2) algorithm is typically too expensive in practice.

          In general when writing a compiler the difference between O(N^3) time and exponential time of a NP-complete algorithm is a don't care. They are just too expensive.

          • mathisfun123 4 hours ago ago

            > LLVM

            LLVM does not use linear scan