Dynamic Recompilation

Dynamic recompilation is a runtime technique where a system recompiles part of a running program to produce better code. Seen in emulators, virtual machines, and as the “rewrite the binary” endgame of self-optimizing software.

Kajanan introduced me to this while we were talking about Memory-Mapped Files.

Emulation use

Emulators use dynamic recompilation to convert machine code from one CPU architecture (e.g. PlayStation) to another (e.g. x86), re-emitting code optimized for the target.

From ECE459 L20

Take a segment of the running binary, feed it to a compiler on the target system, lower to IR, optimize using runtime information, emit a new binary, and swap it in while the old segment isn’t executing.

  • Upside: inline or rewrite library functions specialized for the actual use case
  • [EHS19] show recompilation paying off on a 649×649 matrix with 20000 iterations, once the compile cost is amortized
  • Downside: the target system needs a compatible compiler, which end-user machines often lack and some servers forbid