How could Neural Networks understand Programs?

Dinglan Peng, Shuxin Zheng, Yatao Li, Guolin Ke, Di He, Tie-Yan Liu

Most recent breakthroughs in Programming Language Processing (PLP) involve grafting Natural Language Processing (NLP) techniques—like BERT or GPT—directly onto source code. While effective, this approach ignores a fundamental truth: programming languages are not natural languages.

In natural language, meaning is often contextual and fluid. In programming, meaning is mathematically rigorous and defined by operational semantics: how a program updates its "environment" (memory addresses and values) through specific operations. High-level source code is a human-centric abstraction that can hide these underlying mechanics. Furthermore, the same logic can look wildly different depending on a compiler's optimization settings.

To truly "understand" a program, a model must learn the relationship between instructions and the resulting state transitions. Enter OSCAR (Operational Semantics for Code Abstract Representation), a new pre-training paradigm that moves beyond treating code as just another type of text. By training on intermediate representations and abstract environment transitions, OSCAR captures the formal logic of programs, leading to state-of-the-art performance in downstream tasks.

Bridging Syntax and Semantics with OSCAR

OSCAR introduces a hierarchical Transformer-based model designed to capture what code does, rather than just how it is written.

1. Intermediate Representation and Abstract Environments

Instead of raw source code, OSCAR uses LLVM Intermediate Representation (IR). IR sits between high-level code and machine code, where each instruction represents exactly one fundamental operation. To represent the "environment," the authors use static analysis to extract abstract information (like memory reads/writes and range constraints) rather than concrete execution traces, which would be too computationally expensive to generate at scale.

An illustration of the model architecture of OSCAR.

2. Hierarchical Architecture

OSCAR uses a two-level encoder system:

  • Lower Level: Separate token-level encoders process the IR and the abstract environment information.
  • Upper Level: An instruction-level encoder aggregates information across groups of instructions, allowing the model to handle much longer code sequences than standard Transformers.

3. Positional Condition Encoding (PCE)

Standard positional encodings tell a model where a token is in a sequence, but they don't capture the loops and jumps of a program. OSCAR introduces PCE, which encodes the Control Flow Graph (CFG) directly into the attention mechanism. It assigns three learnable vectors to each instruction: its current position, its target if a condition is true, and its target if a condition is false.

4. Contrastive Learning with Compilers

A key innovation is using compiler optimizations (O0, O1, O2, O3) as a form of data augmentation. The model is trained to recognize that two syntactically different IR snippets are semantically identical if they were compiled from the same source code with different optimization flags. This provides a "natural" self-supervision signal that is far more meaningful for code than the random token-swapping often used in NLP.

Key Results

OSCAR was pre-trained on a massive corpus of real-world programs collected from GitHub and evaluated on several difficult tasks, setting new benchmarks.

Binary Diffing: In the most challenging scenario—comparing code compiled at O0 (no optimization) vs. O3 (high optimization)—OSCAR significantly outperformed specialized tools like BinDiff and state-of-the-art models like BinaryAI. For example, on the BusyBox dataset, OSCAR achieved an average Recall@1 of 0.8838, compared to BinaryAI's 0.8737 and BinDiff's 0.6066.

Binary code similarity detection results across different optimization levels.

Algorithm Classification: On the POJ-104 test set (containing 104 different algorithm problems with ~500 student-submitted solutions each), OSCAR achieved a record-low error rate of 1.92%, nearly halving the error of the previous best model, ProGraML (3.38%).

Zero-Shot Transfer: Even without fine-tuning, OSCAR showed a remarkable ability to detect code similarity, suggesting it learns a highly robust, general representation of program logic.

Limitations and Future Work

While OSCAR represents a significant leap forward, the authors identify two main hurdles for future research:

  1. Compilability Requirement: Currently, OSCAR requires code to be compilable to generate the IR and static analysis data. This makes it difficult to use for real-time "as-you-type" suggestions in an IDE. Interactive interpreters (like Cling for C++) might offer a future solution.
  2. Loss of Variable Names: Translating to IR often normalizes variable names (e.g., changing num_batches to %1). In some cases, the human-chosen name carries vital semantic clues that the IR-based model currently misses. Future versions might benefit from keeping original tokens alongside the IR.

Summary updated

BibTeX
@inproceedings{peng2021how,
  author = {Dinglan Peng and Shuxin Zheng and Yatao Li and Guolin Ke and Di He and Tie-Yan Liu},
  title = {{How could Neural Networks understand Programs?}},
  booktitle = {International Conference on Machine Learning},
  year = {2021},
  url = {https://openreview.net/forum?id=i8JFehDFR_},
}
Figure