Against Recursion
I keep thinking about the future of computing. Something about the sabbatical, at NVIDIA, always thinking about these tensor cores, accelerators, vector units. It swirls around inside my head unceasingly. Just a week ago Andrew, who is working on CSS selectors, had some bad auto-vectorization… There has to be a way of programming these machines. It is what the silicon demands!
At NVIDIA I am working on the Tile IR team, which is one of these "tile-based" programming languages, which are all the rage. But I am also a big fan of e-graphs, which are connected to solvers and databases, like via relational e-matching, and at PLDI I had the good luck of chairing the databases session, where there was a talk about joins, and also seeing some great talks on sparse tensors, which are very much database-flavored, a dot product on a sparse tensor, after all, is just an equijoin. The relational model is so rich and powerful! J. Brandon is visiting NVIDIA and M. Arntzenius is writing about relational languages. Last year I rewrote Herbie in a database-inspired way, which greatly improved performance and was quite interesting. Part of that, by the way, was moving to an SSA model, which I've come to appreciate more and more. It has a natural relationship to e-graphs as well. That makes me think of Milind Kulkarni's work, by the way one of my top papers ever, which is a kind of polyhedral model for recursive data structures. Which of course brings me back to scheduling languages, AI compilers, and so on. Scheduling, on a GPU, isn't just about time, there's a spacial "vector" dimension to it as well. And on an LPU there are multiple spatial dimensions…
Well, time to synthesize a little bit. What I've come down to is the following slogan: I am against recursion.
Recursion is, of course, a great and necessary specification of a program, but it's not a great operationalization of it. Pure, functional, recursive programs are a joy to write and reason about, but their compilers are slow.
That's because of recursion. It locks you in to a scalar execution model, which is slow. It also locks you in to heap allocation, which is slow. And hardware is not evolving in the direction of making recursion better. There are plenty of optimizations exist here but as M. Pharr says,1 [1 Who credits T. Foley.] auto-vectorization is not a programming model. The question is how we programs—where recursion is obviously helpful—but in a way that natively exposes dependencies and schedulability to the compiler.
For arrays, consider Numpy. People write complex and unstructured programs that operate uniformly on tensors and those operations can be executed in vectorized and tiled forms automatically. And modern ML compilers (like Tile or XLA) take functional code written in this style and schedule it, fusing, tiling, and allocating efficiently. But what is the analog for non-array programs? Like, for instance, for inductive data types?
I think a the unifying conceptual here draws from my earlier batches post. A recursive function, and an inductive data type, and both defined by fixed-points. Fixed-points are a unified conceptual model but they do not need to be a unified operational model. Instead, the compiler should solve for how each μ type and Y function should be represented. It could be pointers and a stack, but it could also be a flat buffer, or some kind of flattened tree traversal, or all sorts of other things.
Finite sets
I think batches is one answer. An inductive data type value created in some particular place is actually created in a specific arena. So you don't just have a Tree type, you have a Tree<A> type, where A is an arena; this is related to ownership and region typing, but with a more physical interpretation of what a "region" is. And while the set of all trees is an infinite set, any particular arena is a finite domain. Recursive functions likewise don't just recurse freely; they recurse over that finite domain, which makes them a finite relation, opening up the whole universe of query planning.
So that's the first part of this. The programming model is functional, but there's an extra, region type parameter. The program has to region-type-check, and by adding more region types to the program the programmer gives the compiler more freedom, for example by allowing the compiler to allocate different objects in different regions.
A high-level semantics here is that a region type is a type of pointer-like values. The compiler picks a representation of those values. There's always an escape hatch—heap pointers to tagged values, much like the runtime value representation of any dynamic language—but the compiler can come up with better options. For example, if every value in the region is the same type, you don't need the tags on values. If they're all created in a short burst, maybe an arena makes sense. And so on. Then every type denotes to a finite set and every function relates to a finite relation, and the compiler can query-plan to schedule those operations.
Layouts
Finite sets are unordered, but actual hardware has some kind of privileged order, like accessing and operating on nearby elements. But you can't just schedule whatever you want on whatever hardware you want, because there are inherent dependencies between computations. For example, when applying an elementwise operation on a tensor, you can vectorize things easily, because each element is independent, but when doing a reduction it's not so easy because there are linear dependencies.
So the other part of this is to imagine the iteration space of the program—the set of all function invocations that the program ever performs—and its dependency graph. And the arenas you allocate in have their own ordering. For example, if you want to compute map(f, l) in a functional-but-in-place style, you need to know that the input won't be reused. So the memory ordering (two values placed in the same memory) must correspond to a logical dependency (the values having no overlapping uses). Functional programming gives you logical dependencies, while the region types let the programmer control memory ordering.
Then the compiler's job is not only to pick not just a representation for each region, but also a representation whose internal ordering aligns with the dependency order.
In fact, we probably want something even stronger than simple region types. This can be ordering types, where we guarantee that as we recurse we recurse to a strictly larger index. Or block-decomposable types, where we can write down matrix multiply as a block matrix multiply. These naturally allow for optimizations like in-place mutation and vectorization. Again, the compiler is then tasked with finding ways to store each region compatible with the decompositions the programmer allows, while maximizing performance.
The synthesis
The synthesis is a language where every type is an ordered, finite domain and every function is a finite relation. Where the layout of that domain is free for the compiler to choose, and the order of function applications up to the planner to optimize, up to the constraint that the dependency order and domain relations are compatible. Where pointers and stack frames aren't a language primitive or an implementation detail, but a structure synthesized by the compiler.
I can't sketch out a language and a high-performance compiler here, but I think I at least have a way to get there. First, a language with strong region typing but not in a purely stack-based way, following a literature that exists for Rust-like non-linear lifetimes. A compiler that determines when regions can be co-located in the same bit of memory using that information. An extension to decomposable other otherwise-structured regions, and a compiler that can use that to vectorize and parallelize. A compiler that can pack and linearize recursive types and functions, to the point that an inductive list type becomes an array in memory. We'll see if any of it is possible.
