从字节码生成 SSA:命令式瑰宝
SSA from Bytecode: Imperative Pearl

原始链接: https://bernsteinbear.com/blog/ssa-bytecode/

作者解释了 ZJIT 如何将 CRuby/YARV 栈式字节码转换为 SSA IR。虽然真正的 `add_iseq_to_hir` 函数由于操作码的边界情况而有约 1900 行,但其核心思想很简单:在编译时静态模拟操作数栈。由于每个操作码的栈输入和栈输出都是已知的,编译器不需要实际的运行时值;它只需跟踪每个栈条目是由哪条先前指令产生的。因此 `push 3; push 4; add` 会变成 `v0 = const 3; v1 = const 4; v2 = add v0, v1`。 作者将操作真实值的具体字节码解释器,与操作 SSA 名称并发出 IR 指令的小型抽象解释器进行了比较。可以利用不断增长的 code 数组生成唯一名称。结果展示了抽象解释的本质,以及“将时间沿 IR 向前推进”。处理跨基本块的控制流需要 phi 节点或块参数。

黑客新闻 最新 | 往期 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 从字节码到 SSA:命令式瑰宝 (bernsteinbear.com) 7 分,来自 surprisetalk,1 小时前 | 往期 | 收藏 | 讨论 帮助 | 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Ufuk made a comment that he enjoyed my description of SSA from bytecode in Aaron’s livestream with me. While filming the livestream—an intro to ZJIT internals—I got a little nervous that people would shy away from our 1900 line add_iseq_to_hir function. This function is responsible for converting CRuby’s stack based bytecode for the YARV virtual machine into a high-level SSA IR for ZJIT to analyze and optimize.

It takes bytecode that looks roughly like this:

into SSA form, giving an SSA value to each stack slot, ish:

v0 = const 3
v1 = const 4
v2 = add v0, v1

While our function is 2000 lines long because it handles a lot of specific opcodes and weird facts about those opcodes, the core of it is small. Here is that core.

What if a stack, but virtual

The bytecode assumes that when it is run, it will be operating on a stack. This means that every opcode has implicit stack inputs and implicit stack outputs.

push 3, for example, has empty stack inputs and one stack output. By contrast, add has two stack inputs and one stack output. Some instructions such as nop are empty on both counts. As far as I can tell, all instructions have at most one output.

These counts are known when looking at the bytecode: we can reason statically about how many inputs and outputs each opcode has. Some are variadic, such as send (call), but they come bearing an argument count in a nearby field.

Let’s model how the first example runs on the VM by adding a view of the stack after each instruction has run. The stack will be displayed as a list left-to-right, where the left is the bottom and the right is the top.

       ; []
push 3 ; [3]
push 4 ; [3, 4]
add    ; [7]

Because the input counts and output counts are static, when ZJIT reads through the bytecode, we can run the stack parts of it at compile-time.

We don’t really need to know what the values on the stack are; we only need to keep track of where they came from. And when I say “where they came from”, I mean that we name each instruction and refer to its output by the instruction’s name. I like how the paper Design of the Java HotSpot™ Client Compiler for Java 6 (PDF) puts it:

An instruction that loads or computes a value represents both the operation and its result, so that operands can be represented as pointers to previous instructions.

Here is what that looks like for the above example. In addition to the run-time stack, I will also write the SSA instruction to indicate that we are generating new code:

       ; []
push 3 ; v0 = const 3
       ; [v0]
push 4 ; v1 = const 4
       ; [v0, v1]
add    ; v2 = add v0, v1
       ; [v2]

This is neat! We have transformed stack based bytecode to a form where instructions can directly reference their inputs. You may notice, though, that v0 and v2 both end up in the same stack slot (the first element). This is why I said earlier “giving an SSA value to each stack slot, ish”.

The difference between the two is time. v0 was in the operand stack at one point but now v2 is. This is kind of why I describe SSA as “pushing time through the IR”.

Alright, let’s implement it.

Implementation

We’ll start with the real (“concrete”) interpreter of the bytecode, which operates on a value stack. This gives you a simplified version of how Ruby internals work:

def interpret(bytecode)
  stack = []
  bytecode.each do |op|
    case op
    in [:push, c]
      stack << c
    in [:add]
      right = stack.pop
      left = stack.pop
      stack << left + right
    else
      raise "Unknown op #{op}"
    end
  end
  stack.pop
end

bytecode = [
  [:push, 3],
  [:push, 4],
  [:add],
]

puts interpret(bytecode)  # 7

Now let’s look at the abstract intepreter, which by contrast operates on an SSA stack. You can see that they are quite similar looking:

def to_ssa(bytecode)
  code = []
  stack = []
  bytecode.each do |op|
    case op
    in [:push, c]
      name = :"v#{code.length}"
      code << "#{name} = const #{c}"
      stack << name
    in [:add]
      name = :"v#{code.length}"
      right = stack.pop
      left = stack.pop
      code << "#{name} = add #{left}, #{right}"
      stack << name
    else
      raise "Unknown op #{op}"
    end
  end
  code
end

bytecode = [
  [:push, 3],
  [:push, 4],
  [:add],
]

puts to_ssa(bytecode)
# v0 = const 3
# v1 = const 4
# v2 = add v0, v1

The names could be anything as long as they are unique. For simplicity and ease of reading, I picked the length of the code array, which grows monotonically. This is an array/vector-based analogue to the real-pointer approach in the HotSpot paper from above.

Ta da! You have built a small abstract interpreter.

Now to make it global (to handle control flow) you need some kind of inter-basic-block data flow mechanism such as phi nodes or basic block parameters. Perhaps more on that in another post, but for now, you may enjoy my collection of other people’s writing about that.

Conclusion

I originally wanted to title this blog post “you could have invented abstract interpretation” or similar, but I decided that I find those titles obnoxious because they assume the reader is or thinks of themself as unintelligent. Perhaps we could have invented it.

The title is a bit of a tongue-in-cheek nod to the “functional pearl” papers that I always felt should be simpler than they are but leave me confused. And also kind of a nicer nod to SSA is Functional Programming (PDF) by Andrew Appel.

References

联系我们 contact @ memedata.com