NewsCrawler.org

SSA from bytecode: converting stack code into SSA form in ZJIT

The author describes how the function add_iseq_to_hir in ZJIT[1], a part of CRuby, converts stack-based bytecode for the YARV[2] virtual machine into a high-level SSA[3] IR[4] for analysis and optimization. The function is about 2000 lines long because it handles many specific opcodes, but its core is small. Each opcode has a fixed number of implicit stack inputs and outputs, which can be counted from the bytecode alone.

Instead of tracking the actual values on the stack, the conversion tracks where each value came from. Each instruction is given a name and refers to its output by that name. For the example push 3, push 4, add, the result is v0 = const 3, v1 = const 4, and v2 = add v0, v1. The author notes that v0 and v2 share the same stack slot, which is why SSA values are only "ish" one per stack slot, and describes SSA as pushing time through the IR.

The author shows a short Ruby program with a concrete interpreter, which prints 7, and an abstract interpreter, which produces the SSA code. Handling control flow requires a mechanism such as phi nodes or basic block parameters, which the author leaves for a future post. The title is a tongue-in-cheek reference to "functional pearl" papers and to a paper by Andrew Appel.

Read the original bernsteinbear.comDiscussion

Sources

Published: First seen: Читать по-русски

Open as a page