Skip to main content
QUICK REVIEW

[Paper Review] Learning to Represent Programs with Graphs

Miltiadis Allamanis, Marc Brockschmidt|arXiv (Cornell University)|Nov 1, 2017
Software Engineering ResearchComputer Science47 citations
TL;DR

This paper proposes representing source code as structured graphs incorporating syntax, data flow, and type hierarchies to improve program understanding. Using Gated Graph Neural Networks (GGNNs), the method learns semantic representations that outperform sequence-based models on VarNaming (32.9% accuracy) and VarMisuse (85.5% accuracy), detecting real bugs in open-source projects like RavenDB and Roslyn.

ABSTRACT

Learning tasks on source code (i.e., formal languages) have been considered recently, but most work has tried to transfer natural language methods and does not capitalize on the unique opportunities offered by code's known syntax. For example, long-range dependencies induced by using the same variable or function in distant locations are often not considered. We propose to use graphs to represent both the syntactic and semantic structure of code and use graph-based deep learning methods to learn to reason over program structures. In this work, we present how to construct graphs from source code and how to scale Gated Graph Neural Networks training to such large graphs. We evaluate our method on two tasks: VarNaming, in which a network attempts to predict the name of a variable given its usage, and VarMisuse, in which the network learns to reason about selecting the correct variable that should be used at a given program location. Our comparison to methods that use less structured program representations shows the advantages of modeling known structure, and suggests that our models learn to infer meaningful names and to solve the VarMisuse task in many cases. Additionally, our testing showed that VarMisuse identifies a number of bugs in mature open-source projects.

Motivation & Objective

  • To address the limitations of existing code representation methods that ignore deep semantic structures like data flow and type hierarchies.
  • To introduce a new machine learning task—variable misuse prediction (VarMisuse)—that requires reasoning about program semantics.
  • To demonstrate that graph-based models can learn meaningful program representations that generalize across diverse codebases.
  • To validate the practical utility of the approach by detecting real bugs in mature open-source projects such as RavenDB and Roslyn.

Proposed method

  • Constructing program graphs by encoding syntactic relationships (e.g., token order) and semantic relationships (e.g., data flow, parameter binding).
  • Integrating data flow and type hierarchy information into the graph structure to enrich semantic signals.
  • Training Gated Graph Neural Networks (GGNNs) on these large-scale program graphs to learn distributed representations of code elements.
  • Using the learned representations to predict variable names (VarNaming) and select correct variables in code slots (VarMisuse).
  • Scaling GGNN training to handle large graphs derived from 2.9 million lines of real-world source code.
  • Applying cosine similarity on learned representations to analyze semantic similarity across code snippets.

Experimental results

Research questions

  • RQ1Can graph-based neural networks effectively model both syntactic and semantic structures in source code to improve program representation?
  • RQ2How well can a model trained on program graphs predict variable names given usage context (VarNaming)?
  • RQ3Can a model trained on program graphs detect variable misuse errors that are semantically incorrect but syntactically valid?
  • RQ4To what extent do learned representations capture meaningful semantic similarities between code fragments?
  • RQ5Can the VarMisuse model identify real, previously undetected bugs in production-grade software?

Key findings

  • The proposed graph-based model achieved 32.9% accuracy on the VarNaming task, significantly outperforming baselines that rely on less structured representations.
  • The model reached 85.5% accuracy on the VarMisuse task, demonstrating strong capability in reasoning about variable semantics across code.
  • The model successfully detected 3 previously unknown bugs in RavenDB and 3 in Roslyn, which were privately reported and fixed by developers.
  • Visualizations show that the model learns semantically meaningful representations, grouping similar variable usages (e.g., null checks) together across different code segments.
  • The model identified a memory inefficiency in RavenDB where a buffer was unnecessarily sized to 'length' instead of 'size', a subtle but measurable issue.
  • The approach complements traditional static analysis by identifying bugs that are not caught by compilers or syntactic checks, such as incorrect error messages or copy-paste errors.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.