一句话:提出一种可按需选择 CPS 的新编译器 IR,在直接风格与完全 CPS 间灵活优化续延。
函数式语言编译中长期争论 CPS:建模跳转需显式续延,但续延应作为第二类值并与普通函数分离,以保证代码生成效率。PLDI 2017 提出带显式 join point 的直接风格 IR,join point 即不返回、不逃逸的局部续延;该设计在 GHC 中有效,但优化时续延处理仍有改进空间。本文提出新 IR:用类似 Felleisen C 的控制算子消除冗余重写;分开处理续延的“不返回”与“不逃逸”性质,以高效编译良好行为函数;并定义选择性 CPS 翻译,在保持程序含义与类型的同时擦除控制算子。由此可在直接风格、完全 CPS 及中间风格中优化,把“是否 CPS”变为“何时需要多少 CPS”。
What makes a good compiler IR? In the context of functional languages, there has been an extensive debate on the advantages and disadvantages of continuation-passing-style (CPS). The consensus seems to be that some form of explicit continuations is necessary to model jumps in a functional style, but that they should have a 2nd-class status, separate from regular functions, to ensure efficient code generation. Building on this observation, a recent study from PLDI 2017 proposed a direct-style IR with explicit join points, which essentially represent local continuations, i.e., functions that do not return or escape. While this IR can work well in practice, as evidenced by the implementation of join points in the Glasgow Haskell Compiler (GHC), there still seems to be room for improvement, especially with regard to the way continuations are handled in the course of optimization. In this paper, we contribute to the CPS debate by developing a novel IR with the following features. First, we integrate a control operator that resembles Felleisen’s C , eliminating certain redundant rewrites observed in the previous study. Second, we treat the non-returning and non-escaping aspects of continuations separately, allowing efficient compilation of well-behaved functions defined by the user. Third, we define a selective CPS translation of our IR, which erases control operators while preserving the meaning and typing of programs. These features enable optimizations in both direct style and full CPS, as well as in any intermediate style with selectively exposed continuations. Thus, we change the spectrum of available options from “CPS yes or no” to “as much or as little CPS as you want, when you want it”.
@article{paperbot605,
title = {Compiling with continuations, or without? whatever.},
author = {Youyou Cong and Leo Osvald and Grégory M. Essertel and Tiark Rompf},
journal = {Proceedings of the ACM on Programming Languages},
volume = {3},
number = {ICFP},
year = {2019},
doi = {10.1145/3341643}
}