Table of Contents
Fetching ...

LEGO-Compiler: Enhancing Neural Compilation Through Translation Composability

Shuoming Zhang, Jiacheng Zhao, Chunwei Xia, Zheng Wang, Yunji Chen, Xiaobing Feng, Huimin Cui

TL;DR

LEGO-Compiler introduces a scalable neural compilation framework that uses LEGO translation to decompose large programs into composable control blocks, paired with a verifiable, stepwise workflow and a self-correction loop. The method achieves high behavioral accuracy on real-world benchmarks ($>99\%$ on ExeBench and $97.9\%$ on AnsiBench) and demonstrates near an order-of-magnitude improvements in code-size scalability, validated through formal composability proofs and extensive experiments. By reducing context length and enabling modular translation, the approach offers a practical pathway for LLMs to contribute to system-level tasks while complementing traditional compilers. Limitations include handling highly dynamic language features and very large codebases, with future work focusing on integration with external tools, broader architecture support, and optimization techniques.

Abstract

Large language models (LLMs) have the potential to revolutionize how we design and implement compilers and code translation tools. However, existing LLMs struggle to handle long and complex programs. We introduce LEGO-Compiler, a novel neural compilation system that leverages LLMs to translate high-level languages into assembly code. Our approach centers on three key innovations: LEGO translation, which decomposes the input program into manageable blocks; breaking down the complex compilation process into smaller, simpler verifiable steps by organizing it as a verifiable LLM workflow by external tests; and a feedback mechanism for self-correction. Supported by formal proofs of translation composability, LEGO-Compiler demonstrates high accuracy on multiple datasets, including over 99% on ExeBench and 97.9% on industrial-grade AnsiBench. Additionally, LEGO-Compiler has also acheived near one order-of-magnitude improvement on compilable code size scalability. This work opens new avenues for applying LLMs to system-level tasks, complementing traditional compiler technologies.

LEGO-Compiler: Enhancing Neural Compilation Through Translation Composability

TL;DR

LEGO-Compiler introduces a scalable neural compilation framework that uses LEGO translation to decompose large programs into composable control blocks, paired with a verifiable, stepwise workflow and a self-correction loop. The method achieves high behavioral accuracy on real-world benchmarks ( on ExeBench and on AnsiBench) and demonstrates near an order-of-magnitude improvements in code-size scalability, validated through formal composability proofs and extensive experiments. By reducing context length and enabling modular translation, the approach offers a practical pathway for LLMs to contribute to system-level tasks while complementing traditional compilers. Limitations include handling highly dynamic language features and very large codebases, with future work focusing on integration with external tools, broader architecture support, and optimization techniques.

Abstract

Large language models (LLMs) have the potential to revolutionize how we design and implement compilers and code translation tools. However, existing LLMs struggle to handle long and complex programs. We introduce LEGO-Compiler, a novel neural compilation system that leverages LLMs to translate high-level languages into assembly code. Our approach centers on three key innovations: LEGO translation, which decomposes the input program into manageable blocks; breaking down the complex compilation process into smaller, simpler verifiable steps by organizing it as a verifiable LLM workflow by external tests; and a feedback mechanism for self-correction. Supported by formal proofs of translation composability, LEGO-Compiler demonstrates high accuracy on multiple datasets, including over 99% on ExeBench and 97.9% on industrial-grade AnsiBench. Additionally, LEGO-Compiler has also acheived near one order-of-magnitude improvement on compilable code size scalability. This work opens new avenues for applying LLMs to system-level tasks, complementing traditional compiler technologies.

Paper Structure

This paper contains 29 sections, 6 theorems, 1 equation, 9 figures, 4 tables, 2 algorithms.

Key Result

Theorem 1

For any two basic statements $stmt_1$ and $stmt_2$ in SRC, as defined in def:basic_statement, their translation is composable: $T(stmt_1 \circ stmt_2) \equiv T(stmt_1) \cdot T(stmt_2)$

Figures (9)

  • Figure 1: a. Plain translation vs LEGO translation, by splitting the program into smaller composable control blocks(parts), translating each part becomes an easier task, and rebuilding each translated partial result will form a full translation. b. LEGO compiler, a special case for LEGO translation, to translate each part correctly, a symbol table needs to be maintained first and provided during translation.
  • Figure 2: Neural compilation workflow in LEGO-Compiler. Left figure shows the behavioral verification process with unit-tests. Right figure shows the detailed steps in the workflow, some step is residual that may be skipped as performing such step may be unnecessary for certain input program.
  • Figure 3: AnsiBench evaluation results with Claude-3.7-Sonnet. The token count only computes the input length of C code, and typically, the output assembly will be 3-6 times larger in token size.
  • Figure 4: Example workflow for LEGO-Compiler on a full ExeBench example: source code analysis triggers thoughts, including variable mapping, splitting control blocks and value collection illustrated.
  • Figure 5: Complexity breakdown of ExeBench and its hard 10% (roughly) subset, we use llvm as the analysis tool, then filter the subset with the following conditions: number of basic blocks(BB) $\geq$ 10 or max instructions in BB $\geq$ 80 or total instructions $\geq$ 200. Upper figures characterize the overall of Exebench and Lower figures characterize the hard 10% subset.
  • ...and 4 more figures

Theorems & Definitions (22)

  • Definition 1
  • Definition 2: Basic Statement
  • Definition 3: Basic Block
  • Definition 4: Control Block
  • Definition 5
  • Definition 6: Compound Control Block
  • Definition 7: Translation Function and Valid Translations
  • Definition 8: Translation Composability
  • Theorem 1: Composability of Basic Statements
  • proof
  • ...and 12 more