Class-Dictionary Specialization with Rank-2 Polymorphic Functions
Rank-2 polymorphism, when combined with type-class-constrained arguments, enables powerful abstractions and code reuse by allowing functions to accept arguments that are themselves ad-hoc polymorphic.
Optimizing compilers like the Glasgow Haskell Compiler (GHC) use techniques like class-dictionary specialization and inlining for optimization.
However, these techniques can falter when the rank-2 polymorphic functions are recursive.
Specializing the polymorphic arguments of recursive rank-2 polymorphic function applications requires inlining the applied function to expose type and dictionary applications to the arguments, but the compiler's heuristic-driven inliner is reluctant to inline recursive functions, risking non-termination during compilation.
This stalemate causes recursive rank-2 polymorphic functions to remain un-optimized and be left with a runtime penalty.
Within the Haskell ecosystem, we identify this stalemate within three widely used libraries: the Scrap Your Boilerplate (SYB) and SYB With Class (SYB3) generic-programming libraries, and a fragment of the \texttt{lens} optics library.
In these libraries, the combination of rank-2 polymorphism, type-class constraints and recursion is central to their implementation.
In this paper, we present a new optimization technique that breaks this stalemate and enables class-dictionary specialization with recursive rank-2 polymorphic functions.
We introduce a partial evaluator that strategically applies the standard transformations of inlining, $\beta$-reduction and memoization to applications of rank-2 polymorphic functions that are \emph{partially static}, i.e., whose arguments have some information known statically.
This process exposes applications of its polymorphic arguments onto concrete types and dictionaries, which can be specialized by the standard compiler optimization pipeline.
Additionally, we introduce type-constant folding to evaluate run-time type-equality tests statically, further leveraging static type information gained from partial evaluation.
We implement our technique as a GHC plugin and demonstrate its effectiveness by resolving the performance bottlenecks in the aforementioned Haskell libraries.
On SYB and SYB3 traversals, our technique achieves speedups of $43\times$ on average (up to $155\times$) and $6.1\times$ on average (up to $9.5\times$), respectively, matching the performance of their hand-written counterparts. On the fragment of the \texttt{lens} library containing the identified slowdowns, our technique achieves speedups of $1.6\times$ on average (up to $2.1\times$).
Tue 6 OctDisplayed time zone: Pacific Time (US & Canada) change
15:30 - 17:00 | Compiler Optimization and Code GenerationOOPSLA at East Hall 1 Chair(s): Kirshanthan Sundararajah Virginia Tech | ||
15:30 18mTalk | Class-Dictionary Specialization with Rank-2 Polymorphic Functions OOPSLA Link to publication DOI Pre-print | ||
15:48 18mTalk | Automatic Propagation of Profile Information through the Optimization Pipeline OOPSLA Elisa Frohlich Federal University of Minas Gerais, Angelica Moreira Microsoft Research, Fernando Magno Quintão Pereira Federal University of Minas Gerais DOI | ||
16:06 18mTalk | Automatically Generating ML Compiler Backends from Tensor Accelerator ISA Descriptions OOPSLA Devansh Jain University of Illinois at Urbana-Champaign, Akash Pardeshi University of Illinois at Urbana-Champaign, Marco Frigo University of Illinois at Urbana-Champaign, Kaustubh Khulbe University of Illinois at Urbana-Champaign, Krut Patel NVIDIA, Saatvik Lochan University of Illinois at Urbana-Champaign, Jai Arora University of Illinois at Urbana-Champaign, Charith Mendis University of Illinois at Urbana-Champaign DOI Pre-print | ||
16:24 18mTalk | Symbolic Basic Block Profiling for Machine Learning Kernels OOPSLA Jingyu Qiu University of Rochester, Rongcui Dong University of Rochester, Sreepathi Pai University of Rochester DOI | ||
16:42 18mTalk | Filtr: Compiling Bioinformatics Recurrences OOPSLA Bala Vinaithirthan Stanford University, Shiv Sundram Stanford University, Sneha Goenka Princeton University, Fredrik Kjolstad Stanford University DOI | ||