5 votes

Compilers for free with weval

2 comments

  1. [2]
    tauon
    Link
    It sounds very interesting, especially given the numbers from that small demo. But I think I’m still struggling to understand what the “wasm soup” (as mentioned in the illustration) is composed...

    It sounds very interesting, especially given the numbers from that small demo. But I think I’m still struggling to understand what the “wasm soup” (as mentioned in the illustration) is composed of, and how it came to be exactly. How do you optimize an interpreter + program together? Is that even what’s being done here?

    1 vote
    1. skybrian
      (edited )
      Link Parent
      Yes, that’s what’s happening. It’s much the same as compiling a regular program that has a lot of constants. For example, when there is a function call with a constant argument, the compiler can...

      Yes, that’s what’s happening. It’s much the same as compiling a regular program that has a lot of constants. For example, when there is a function call with a constant argument, the compiler can inline the function and then apply any optimizations it has for expressions that contain constants.

      The main loop is unrolled and then simplified. You can think of this as making a copy of the loop for each possible value of the program counter, and then simplifying each copy, assuming the program and the program counter are constant for that invocation. So the switch ends up taking a constant and then only one branch is possible, so the others are removed you and you get the code for executing one instruction with some constants substituted in.

      These are optimizations that could be done on any program, except that it would result in massive code bloat due to all the function inlining and loop unrolling, so compilers have heuristics about when optimizations are worth it. A compiler designed for partial evaluation will have different heuristics that take some hints. For example, the program counter can be marked as a loop variable that should be unrolled aggressively.

      In practice, compilers aren’t usually written this way, because they don't need the kind of flexibility you get from being able to edit the interpreter’s source code. To apply this technique, you still need a compiler that implements lots of optimizations, except that it’s working at another level removed from the actual program.

      This compiler isn’t really “for free,” though it’s reusable for compiling multiple languages.

      3 votes