Unofficial Hacker News companion — get notified on comment replies, and more.

So, I rewrote my game in C3 (opens external site)

159 points by lerno 5 days ago | www.madrigalgames.com | 46 comments on HN | Vote on HN (opens Hacker News) |

View full thread · View on Hacker News

Comment thread (10)

  1. The vast majority of time is spent in LLVM, though, as reported by `--time-report`.

    I did a little research and it looks like Zig puts everything into one module and feeds it to LLVM. That approach is inherently single-threaded because there's just one giant module. Clang, on the other hand, treats each translation unit as a module, so it works in parallel. Presumably c3c works the same but I'm not familiar with it.

    I really wish Zig would parallelize the object file emission in a way that would benefit any selected backend (i.e. whether LLVM or their own backend).

    Replies (2)
    1. People often generate way more IR than necessary in a naive way, and rely on LLVM to trim the fat. This simplifies the frontend, but I don't think its fair to blame LLVM for being slow in this case.
      Replies (2)
      1. This is certainly also true. From the beginning C3 tried to keep codegen similar to Clang. After all, this is what LLVM is tested on, and it would be natural that these patterns work the best.

        To get even more mileage you can do some optimizations in the frontend, before LLVM lowering (as long as it won’t break debug) to reduce IR even further.

        Replies (1)
        1. Btw, more and more languages opt for a single compilation units, especially ones like Rust (and Zig i guess) where basically all dispatch is static and there's lots of duplicate work created by generic monomorphization (which might end up bloating binaries as well).

          Considering this, I think LLVM might want to add other methods of paralallelizing computation within a single compilation unit.

          Replies (1)
          1. And then you want to have incremental compilation, how? There's a reason why separate compilation was a thing.

            Furthermore, I believe the assumptions are built hard into LLVM.

            But I mean it's not even a problem. Even if you build everything monolithic (and we can debate the downsides of that), you can still create "virtual" modules in order to parallelize codegen. It's an extremely obvious win and it's dead simple. I'd be surprised if Zig doesn't do that. It's literally the first idea that would come to mind for speeding up "single compilation unit" languages.

      2. There are also certain IR patterns that trigger what is essentially quadratic compilation time in LLVM.

        We found that out the hard way, when we compiled our relatively simple scripting language, only to be faced with minutes of compilation for scripts of ~2000 lines. Larger tweaks to the front end to avoid certain patterns worked to a degree.

        The point is, IR matters more than one might think.

        Replies (1)
        1. what patterns do you mean? I know people don't really like phi nodes, and managing locals, so they spill everything to memory by default, which is kinda bad.
          Replies (2)
          1. That's not really that much of a problem. Try loading and storing large structures instead of using llvm.memcpy. That's quite a bit worse I believe.
          2. We never fully understood what exactly triggered it, and it's been a while, but I believe it was due to too many local %foo variables. Each compiled line would generate a bunch of temporary variables that were only required within that line. That appeared to put some pressure on some pass. We then began re-using variables over lines, which did not fix the root issue, but brought compile times into acceptable ranges.
    2. Roughly speaking: In C3 each module in the language is treated as a separate LLVM module and the compiler emits the resulting .o files in parallel.