Introducing Compiler Semantics into Large Language Models as Programming Language Translators: A Case Study of C to x86 Assembly

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

Introduction

Traditional compilers are monumental achievements in software engineering, often requiring decades of manual effort to support new hardware architectures. While Large Language Models (LLMs) have demonstrated impressive capabilities in high-level programming tasks, they frequently stumble when bridging the "semantic gap" between high-level languages like C and low-level machine instructions such as x86 assembly. This research identifies three critical hurdles that prevent standard LLMs from serving as reliable neural compilers:

  1. Numerical Fragility: LLMs struggle with the precise conversion of floating-point literals into the exact IEEE-754 hexadecimal representations required by assembly code.
  2. Semantic Inconsistency: Compilers like GCC can translate the same C construct in multiple ways (e.g., using jump tables vs. if-else chains for switch statements), creating a "one-to-many" mapping that confuses models during training.
  3. The Long-Tail Problem: Rare instructions or complex data structures appear infrequently in standard datasets, leading to poor performance on edge cases.

Methodology

To address these challenges, the researchers fine-tuned a CodeLlama-13B model using Low-Rank Adaptation (LoRA). The core innovation lies in introducing "compiler-aware" techniques that offload rigid logic to symbolic systems and normalize the target output.

  • Symbolic Interpretation: Instead of forcing the LLM to calculate hexadecimal values, the model is trained to output symbolic expressions (e.g., float(0.15625)). A rule-based script then handles the precise IEEE-754 conversion.
  • Switch-Case Normalization: To eliminate semantic ambiguity, the training data was normalized using specific compiler flags (-fno-jump-tables), ensuring switch statements consistently followed a predictable if-else assembly structure.
  • Refinement Augmentation Loop: The team implemented an iterative pipeline. When the model failed a test case, a secondary model (Mistral-7B) analyzed the failure to synthesize ten times more similar examples, specifically targeting difficult patterns like AVX SIMD instructions.
The proposed neural compilation pipeline, featuring data augmentation, preprocessing for switch normalization and symbolic reasoning, and an automated evaluation loop.

Results and Analysis

The specialized model, referred to as the "ALL" enhanced model, achieved a breakthrough 91.88% IO Accuracy on the full evaluation set. This metric measures whether the compiled code actually executes and produces the correct output, providing a more rigorous benchmark than simple text similarity.

Comparison of IO Accuracy across different benchmarks. The 'All' model significantly outperforms the baseline and GPT-4 Turbo, particularly on the NumericBench subset.

Key performance insights include:

  • Crushing General-Purpose Models: GPT-4 Turbo achieved only 40.85% accuracy on the same task, while other specialized models like DeepSeekCoder (71%) also trailed significantly.
  • Mathematical Mastery: On the NumericBench subset, which focuses on mathematical constants, the baseline LLM's accuracy was a mere 3.8%. With symbolic interpretation, this performance surged to over 90%.
  • Consistency Gains: Normalizing switch statements provided a measurable boost in accuracy by reducing the complexity of the translation space the model had to navigate.

Discussion and Conclusion

This study demonstrates that the future of neural compilation may not lie in simply increasing parameter counts, but in domain-aware fine-tuning. By delegating rigid, rule-based parts of compilation—like floating-point conversion—to symbolic interpreters, the LLM can focus on the structural translation logic where it excels.

While currently limited to non-optimized (-O0) code and primarily C-to-x86 translation, the researchers provided evidence that this methodology is extensible to other architectures like ARM and RISC-V. This approach could eventually automate the porting of compilers to new hardware, potentially saving thousands of person-years of manual engineering effort.

Summary updated

BibTeX
@inproceedings{zhang2024introducing,
  author = {Shuoming Zhang and Jiacheng Zhao and Chunwei Xia and Zheng Wang and Yunji Chen and Huimin Cui},
  title = {{Introducing Compiler Semantics into Large Language Models as Programming Language Translators: A Case Study of C to x86 Assembly}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.findings-emnlp.55},
}
Figure