TL;DR
This paper proposes replacing the industry-standard Static Single Assignment (SSA) form in compilers with a restricted version of Logic Programming (LP) based on Horn Clauses. By treating a program as a set of logical relations rather than a sequence of instructions, the authors show that complex program analyses—like checking for errors or optimizing code—become significantly simpler, more powerful, and naturally "path-aware."
What Problem Does This Paper Address?
Most modern compilers (like LLVM or GCC) use an Intermediate Representation (IR) called SSA. In SSA, every variable is assigned exactly once. While SSA revolutionized compiler design, it has several "warts":
- Path Obliviousness: It’s hard for the compiler to remember the conditions (like
if x > 0) that led to a specific block of code without jumping through hoops. - Forward Bias: SSA is great for analyzing code from start to finish, but a nightmare for backward analysis (e.g., "What values of
xcould have caused this crash at line 50?"). - The $\phi$-node Headache: To handle merging paths (like the end of an if-else), SSA uses "fake" functions called $\phi$-nodes. These are mathematically awkward and require special treatment during analysis.
- Input/Output Asymmetry: It’s easy to see a function's inputs, but finding all possible return values often requires scanning the entire function body.
Methods
The authors introduce LP Form, a restricted logic programming language. Unlike standard Prolog, which can be non-deterministic and "search" for answers, LP Form is:
- Deterministic: Only one "clause" (path) will ever succeed for a given set of inputs.
- Single-moded: Arguments are strictly divided into inputs and outputs.
- Relational: It replaces jumps and loops with procedure calls and recursion.
The paper provides a formal transformation to turn standard "three-address code" (the stuff most compilers start with) into this LP Form. Instead of $\phi$-nodes, LP Form uses simple parameter passing. Instead of complex branching logic, it uses guards (logical conditions that must be true for a path to be taken).
Datasets
Not discussed in the paper. As a theoretical and systems-oriented paper, it focuses on the formal definition of the IR and the logic of the transformations rather than benchmarking against a specific dataset of programs. The authors mention a preliminary implementation called LPVM.
Key Insights and Ideas
- "SSA is Functional Programming, but LP is Better": Previous researchers noted that SSA is essentially a functional language. The authors argue that a relational (logic) view is even better because it doesn't care about the direction of analysis.
- Path Awareness for Free: Because guards are part of the logical definition of a clause, a forward analysis naturally "knows" the constraints on variables without extra bookkeeping.
- Uniformity: In LP Form, a loop is just a recursive call, and a branch is just a choice between clauses. This means the compiler can use the exact same logic to analyze a single block of code or an entire multi-function program.
- Multiple Outputs: Unlike many IRs that struggle with functions returning multiple values (often forcing them into "tuples" or "structs"), LP Form handles multiple outputs natively, mirroring how actual CPUs (like x86) often produce multiple results (e.g., a quotient and a remainder) from one instruction.
Key Results
The paper demonstrates the power of LP Form through two primary examples:
- Interval Analysis: They show how LP Form easily proves that a division-by-zero error is impossible in a tricky branching program where standard SSA-based analysis would likely fail or require much more complex "Gated SSA" extensions.
- Neededness Analysis: They describe a two-phase interprocedural analysis that combines liveness analysis and dead-code elimination. Because of the LP structure, this analysis naturally scales from individual blocks to the entire program.
- Machine Code Proximity: Despite being "high-level" logic, the authors show that LP Form is surprisingly close to machine code, making final code generation straightforward.
Limitations and Future Work
- Code Duplication: In its rawest form, LP Form can duplicate the "prefix" of a function across multiple clauses. The authors address this by representing the program as a tree to factor out common code.
- Implementation Maturity: At the time of writing, the "LPVM" compiler was in early development. The paper focuses on the potential of the IR rather than a head-to-head performance benchmark against LLVM.
- Future Direction: The authors plan to use LPVM as the backend for a new language that blends declarative and imperative programming, eventually translating the optimized LP Form into LLVM IR for final hardware-specific tuning.
Summary updated