通过可变空洞以扁平/非高阶方式构造 ANF

- 通过可变空洞以扁平/非高阶方式构造 ANF 前置条件 前提 算法 实现 特性 未来工作
- 前置条件
- 算法
- 实现
通过可变空洞以扁平/非高阶方式构造 ANF
本文将描述一种算法,用于从一种类函数式的基础语言构造 ANF/类似的 IR,该语言不使用闭包,也没有相关的额外空间需求。据我所知,这个算法并不新颖,而且显然在命令式编译领域已经为人所知有一段时间了。然而,我承认我没有来源,也没有见过有人针对类函数式语言描述过它。
感谢 Ryan Brewer 和 rpjohnst 的启发。
如果你只对代码感兴趣,请跳到实现部分。
前置条件
首先,让我们描述输入语言和输出语言。我们的输入语言将如下所示:
type ml =
| Let of string * ml * ml
| Name of string
| Int of int
| Add of ml * ml
| Print of ml
这旨在呼应某种简单的函数式编程语言,并包含演示该算法所需的所有元素。
我们的输出语言将看起来像一种词法作用域的扁平 ANF 风格构造,所有 let 都在顶层。目标是将我们的输入 ml 转换为这种语言。
type base =
| Name of string
| Add of string * string
| Int of int
| Print of string
type anf =
| Let of string * base * anf
| Done of string
| Hole of anf option ref
值得注意的新增是 anf 中的 Hole 构造子,其用途现在将进行描述。
前提
想象我们正在尝试转换项
let x =
let y = 2 in
y + 1
in
x + 2
当我们到达 x 的主体时,我们遇到了一个难题。我们需要把 y 的转换放在 x 之前,以便使用 y 时作用域正确。因此,我们必须在继续之前转换 y。然而,我们如何使用这个转换呢?x 的内容需要放入 y 的 Let 的 anf 字段中,所以不清楚如何既在 x 之前转换 y,又在 y 中使用 x 的已完成转换。
解决这个问题的一种“传统”方法是通过续延,其中 y 被传入一个闭包,该闭包表示“接下来做什么”,在它转换其主体之后。然后我们可以把 x 的内容放入那个闭包,稍后调用。这有两个主要缺陷:
- 它使用额外空间。一个项的转换将分配约“所需”内存的 2 倍;它必须为 ANF 构造本身分配内存,以及为构造期间所需的闭包分配内存。
- 它更慢,由于上述闭包分配和应用。
本方法解决了这些问题。
算法
回想一下,我们在 anf 类型中留下了一个 anf option ref 类型的 Hole 字段。我将这样表示一个空洞:[·]。核心思想是,我们对任何给定 ml 片段的转换将返回:
- 片段本身。
- 一个我们可以用来引用片段中绑定内容的名称(在加法/类似带有嵌套内容的转换中,必须虚构名称。)
- 由空洞创建的引用,以便它可以被填充。
val convert : ml -> (anf * string * anf option ref)
考虑这个简化的例子:
let x =
let y = 2 in y + 2
in x
当我们转换 x 时,这信号需要转换 y。转换一个 let 有两个步骤——我们必须先转换“头部”(2),然后转换“主体”(y)。
转换头部将导致类似这样的形式
let y = 2 in
[·]
注意空洞被留下以便稍后填充。这样,y 头部的转换不需要知道其环境。我们在代码中通过 Hole 构造子表示这一点,它包含一个对片段的引用,该片段可以为空(None)或已填充(Some ...)。然后,主体的转换可以继续进行,首先生成预期的加法,将其绑定到 nm 以便稍后引用:
文章标题:通过可变洞实现的 ANF 扁平/非高阶构造
正文:
let nm = y + 2 in
[·]
现在我们需要把这个片段放到正确的词法作用域中。我们可以通过把它插入到头转换时留下的洞中来实现:
let y = 2 in
[
let nm = y + 2 in
[·]
]
然后,返回这个片段、绑定的名字(y)以及对内部洞的引用,我们就可以继续转换 x;首先,转换头部,使用转换 y 时返回的名字:
let x = nm in
[·]
然后,转换主体:
let nm2 = x in
[·]
let x = nm in
[
let nm2 = x in
[·]
]
最后把它插入到 y 返回的洞中。
let y = 2 in
[
let nm = y + 2 in
[
let x = nm in
[
let nm2 = x in
[·]
]
]
]
我们剩下最后一个洞,可以用 Done 填充,以明确表示一条控制流路径的结束。这种策略的一个缺点是它确实会生成许多不需要的名字。其中大部分可以通过谨慎地对某些 let 做特殊处理来避免,其余的可以通过一次简单的折叠 pass 来消除。
实现
下面是该算法的一个示例实现,用 OCaml 编写。
type ml =
| Let of string * ml * ml
| Name of string
| Int of int
| Add of ml * ml
| Print of ml
(* Suffix with ' to dodge OCaml annoyances. *)
type base =
| Name' of string
| Add' of string * string
| Int' of int
| Print' of string
type anf =
| Let' of string * base * anf
| Done' of string
| Hole' of anf option ref
let fresh_name =
let counter = ref 0 in
fun () ->
incr counter;
"nm" ^ string_of_int !counter
(* A helper to make an anf fragment of form
let new_name = base in
[·]
*)
let make_let_hole (base : base) : (anf * string * anf option ref) =
let name = fresh_name () in
let r = ref None in
(Let' (name, base, Hole' r), name, r)
let rec convert (ml : ml) : (anf * string * anf option ref) =
match ml with
| Name nm ->
(* Bind to a new name, and return.
A case that can be eliminated in most cases.
*)
make_let_hole (Name' nm)
| Int i ->
make_let_hole (Int' i)
| Add (a, b) ->
(* First, we convert `a` and `b` *)
let (a_anf, a_name, a_hole) = convert a in
let (b_anf, b_name, b_hole) = convert b in
(* We chose a left-to-right evaluation order, so we
want to put `a` before `b`. Hence, plug `b`'s fragment
into the hole provided by `a`.
*)
a_hole := Some b_anf;
(* Then, we need a fragment for the addition itself. *)
let (add_anf, add_name, add_hole) =
make_let_hole (Add' (a_name, b_name))
in
(* We put this in `b`'s hole. *)
b_hole := Some add_anf;
(* Finally, we return the whole expression.
Everything is contained within `a_anf`.
If the user wishes to refer to the result of this conversion,
they should use add_name.
If they wish to plug something into it,
they should use add_hole.
*)
(a_anf, add_name, add_hole)
| Print body ->
(* Print is similar. *)
let (body_anf, body_name, body_hole) = convert body in
let (print_anf, print_name, print_hole) =
make_let_hole (Print' body_name)
in
body_hole := Some print_anf;
(body_anf, print_name, print_hole)
| Let (name, head, body) ->
let (head_anf, head_name, head_hole) = convert head in
let (body_anf, body_name, body_hole) = convert body in
(* We need to bind this let before body to ensure it is
properly lexically scoped.
*)
let let_anf = Let' (name, Name' head_name, body_anf) in
(* Then fill it as expected. *)
head_hole := Some let_anf;
(head_anf, body_name, body_hole)
我建议你自己用 utop 或类似的工具来摆弄一下这个转换。在下文中,我会对输出稍作“清理”,以免它变得不可读。
如果我们使用第一个示例项:
utop # let term = Let("x", Let("y", Int 2, Add (Name "y", Int 1)), Add (Name "x", Int 3));;
val term : ml =
Let ("x", Let ("y", Int 2, Add (Name "y", Int 1)), Add (Name "x", Int 3))
utop # convert term;;
(* 清理后:*)
(let nm1 = 2 in
let y = nm1 in
let nm2 = y in
let nm3 = 1 in
let nm4 = nm2 + nm3 in
let x = nm4 in
let nm5 = x in
let nm6 = 3 in
let nm7 = nm5 + nm6 in
Hole [·]
, "nm7"
, <empty reference>
)
然后,我们可以按上述方式插入返回的 hole。通过一些检查,我们可以验证这确实正确,并且符合我们预期的类 ANF 形式。
如前所述,大量的重复名称可以通过特判 let _ = <direct base> 和折叠 pass 等策略来缓解。
性质
显然,该算法不使用闭包,也不使用任何间接跳转。它的内存分配不超过表示输出程序所需;不存在过多的闭包分配。我目前还没有好的方法来在真实世界的大输入上测试性能,但我没有理由相信它不会比等效的高阶版本表现更好。
未来工作
我相信这个方法也应该可以扩展到 CPS,尽管我目前还没有尝试过。例如《Compiling with continuations, continued》中描述的“常规”高阶 CPS 转换看起来与 ANF 转换的高阶表示非常相似,所以我很感兴趣它是否也能以类似方式改造成可变形式。