Abstract
Large Language Models (LLMs) have demonstrated remarkable proficiency in generating human-like code snippets; however, they often struggle with the rigid syntactic and logical constraints of formal programming languages. This paper introduces COMPCODER, a three-stage pipeline designed to enhance the "compilability" of neural code generation by integrating real-time compiler feedback into the training process. By combining supervised fine-tuning, reinforcement learning, and a dedicated discriminator, COMPCODER significantly improves compilation rates across multiple benchmarks, raising Python code completion success from approximately 44% to over 89%.
Introduction: The "Perception" Gap in Code Generation
Modern LLMs like GPT-2 and CodeGPT treat programming code primarily as a sequence of text tokens. While this approach allows them to capture the stylistic nuances of human-written code, it often fails to account for the formal logic required by compilers. A single missing parenthesis or an indentation error—mistakes that are trivial for a human to rectify—can render an AI-generated snippet entirely non-functional. Previous research indicates that up to 97% of patches generated by advanced models are non-compilable, suggesting that these models lack a fundamental "perception" of compilability because they are not trained with a compiler in the loop.
The COMPCODER Framework
To bridge the gap between text generation and valid programming, the authors propose COMPCODER, a three-stage training pipeline:
1. Language Model Fine-Tuning
The process begins with a baseline model (CodeGPT) which undergoes supervised fine-tuning on a high-quality dataset of compilable code. This stage ensures the model achieves a baseline level of "fluency" and understands the general structure of the target programming language.
2. Compilability Reinforcement Learning
In the second stage, the model is optimized using Reinforcement Learning (specifically the PPO2 algorithm). The model generates code snippets and receives a binary reward based on compiler feedback: a +1.0 reward if the code compiles successfully and a -1.0 penalty if it fails. To prevent the model from "cheating"—such as generating empty strings or trivial comments that technically compile but lack utility—the authors implement a KL-divergence penalty. This keeps the model's output distribution close to the original supervised model while optimizing for validity.
3. Compilability Discrimination
The final stage involves a discriminator (a two-layer MLP) trained jointly with the generator. This discriminator learns to predict the likelihood that a candidate code snippet will compile. During the inference phase, the model uses this discriminator to rank multiple candidates produced by a beam search, selecting the snippet with the highest predicted probability of being valid.
Experimental Setup and Datasets
The researchers evaluated COMPCODER on two primary tasks using established benchmarks:
- Code Completion: 50,000 compilable Python methods were extracted from the CodeSearchNet corpus, filtered specifically for Python 3 compatibility.
- Text-to-Code Generation: The AdvTest dataset was utilized, comprising approximately 41,000 text-code pairs.
In both instances, the ground-truth data was verified using a standard Python environment (codeop module) to ensure that the training and evaluation data were strictly compilable.
Results and Analysis
The implementation of COMPCODER resulted in dramatic improvements in the Compilation Rate (CR) across both evaluated tasks:
- Code Completion: The CR improved from a baseline of 46.84% (standard CodeGPT) to 94.48%.
- Text-to-Code: The CR rose from 70.3% to 96.2%.
Beyond simple success rates, the authors observed that COMPCODER is particularly effective as code complexity increases. As the number of generated tokens grew (from 30 to 45), the performance gap between COMPCODER and standard models widened. While baseline models struggled with the increased syntactic overhead of longer snippets, COMPCODER maintained a consistently high success rate.
Furthermore, the improvement in compilation did not come at the expense of semantic accuracy. The "Edit Similarity" (the degree to which the generated code matches the human reference) remained stable, suggesting the model was generating the intended code more accurately rather than just generating random valid syntax.
Conclusion and Future Work
COMPCODER demonstrates that incorporating a compiler into the training loop is a highly effective strategy for improving the reliability of neural code generation. However, the authors acknowledge a critical distinction: compilable code is not necessarily correct code. A program may pass a compiler while still containing logic errors or failing to meet the user's functional requirements.
The next frontier for this research involves moving from "compilability feedback" to "execution feedback." Future iterations of this work will explore the use of unit tests during the reinforcement learning stage to ensure that generated code is not only syntactically valid but also semantically and functionally correct.
Summary updated