NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Learning to Represent Programs with Graphs

Miltiadis Allamanis, Marc Brockschmidt, Mahmoud Khademi

Most machine learning models for source code treat programs like natural language—processing them as a sequence of tokens or, at best, a hierarchical parse tree. However, code is fundamentally different from English. It possesses strict, well-defined semantics and relies heavily on long-range dependencies. For example, a variable declared on line 10 might not be used again until line 500. A standard sequence-based model, such as an LSTM, often "forgets" the context over such a vast distance.

By ignoring the known structure of code—such as how data flows between variables or how types are organized—we make the learning task unnecessarily difficult. To address this, researchers from Microsoft and Simon Fraser University propose representing source code as a graph that combines its syntax with its semantic data flow. By applying Gated Graph Neural Networks (GGNNs) to these structures, their model can reason about programs far more effectively than text-based approaches.

Constructing Program Graphs

The core contribution of this work is the construction of Program Graphs and the application of GGNNs to learn from them. The challenge is to create a representation that captures both the syntax (the grammar) and the semantics (the logic and data flow) of a program in a way that a neural network can digest.

The authors start with a standard Abstract Syntax Tree (AST) but augment it with several "semantic" edge types:

  • Data Flow Edges: LastRead and LastWrite connect a variable usage directly to its previous occurrences, bridging long gaps in the code.
  • Control Flow Edges: GuardedBy and GuardedByNegation connect variables to the expressions that check them (e.g., an if (x != null) block).
  • API/Type Edges: FormalArgName connects a passed argument to the name of the parameter in the method definition.

Unlike standard neural networks, GGNNs propagate information through this graph via "message passing." Each node starts with an embedding based on its text and type. In each step, nodes send messages to their neighbors, and their internal states are updated using a Gated Recurrent Unit (GRU) cell. This mechanism allows information about a variable's usage on line 500 to flow directly back to its declaration on line 10. To scale this computationally expensive process, the authors developed a method to batch many disconnected program graphs into one large "super-graph," efficiently utilizing GPU parallelism.

Experimental Setup and Key Results

The authors curated a massive dataset of 2.9 million lines of C# code from 29 top-starred GitHub projects, including compilers and databases. They evaluated the model on two primary tasks:

  1. VARNAMING: Predicting a variable's name based on its usage.
  2. VARMISUSE: Identifying if the wrong variable was used in a specific slot (a common source of bugs).

They compared their GGNN against bidirectional RNNs (BiRNN) and "local" models that only look at immediate context, testing on both seen projects (SEENPROJTEST) and entirely unseen projects (UNSEENPROJTEST) to ensure the model wasn't just memorizing specific coding styles.

Performance comparison of GGNN against baseline models on VarMisuse and VarNaming tasks.

The GGNN model demonstrated clear superiority over traditional methods. For the VARMISUSE task, the GGNN achieved an impressive 85.5% accuracy, compared to only 73.7% for the strongest BiRNN baseline. Even on entirely unseen projects, it maintained a high 78.2% accuracy. For VARNAMING, the model achieved an exact-match accuracy of 53.6%, significantly higher than the 36-42% range of sequence-based models.

Beyond benchmarks, the authors used the model to scan mature projects and found real bugs. For instance, in the RavenDB project, the model correctly identified a spot where a developer had accidentally used a variable named clazz when they should have used first. These bugs were reported to and fixed by the developers.

The Importance of Semantic Edges

Why does the graph approach work so well? By providing the model with explicit data-flow edges, it doesn't have to "learn" how variables work from scratch. This makes the model more efficient and accurate, even with less data. One of the most effective features for bug finding was the GuardedBy edge, which allows the model to realize when a variable has been null-checked and should be used in the subsequent block.

Ablation study showing the performance drop when semantic edges are removed.

An ablation study further proved that moving beyond basic syntax is crucial. When the model was restricted to using only syntax edges (the standard AST), performance on the VARMISUSE task plummeted from 85.5% to 55.3%. The semantic edges are what truly allow the model to "understand" the program's intent.

Limitations and Future Work

While powerful, the approach has a few limitations:

  • Interprocedural Reasoning: The current model mostly looks at context within a single file or method. It cannot yet reason about complex logic that spans across an entire large-scale system.
  • Static Analysis Dependency: The approach requires the code to be compilable (using the Roslyn C# compiler) to extract the graph. It cannot easily be applied to "broken" or partial code snippets.
  • Rare Constants: The model struggles with reasoning about rare constants or specific literal values in conditional statements.

Despite these constraints, representing programs as graphs opens a highly promising direction for machine learning on source code. The authors suggest that this representation could soon be extended to other tasks like code completion, automated code review, and even more advanced bug-finding tools.

Summary updated

BibTeX
@inproceedings{allamanis2018learning,
  author = {Miltiadis Allamanis and Marc Brockschmidt and Mahmoud Khademi},
  title = {{Learning to Represent Programs with Graphs}},
  booktitle = {International Conference on Learning Representations},
  year = {2018},
  url = {https://openreview.net/forum?id=BJOFETxR-},
}
Figure