Source-linked AI summary

Learning to Represent Programs with Graphs

Miltiadis Allamanis, Marc Brockschmidt, Mahmoud Khademi

arXiv:1711.00740v3cs.LGcs.AIcs.PLcs.SE

TL;DR

Existing code-learning methods often underuse source code’s rich syntax and long-range semantic dependencies. The paper represents code as multi-relational graphs, trains scalable Gated Graph Neural Networks, and evaluates variable naming and misuse prediction, with stronger performance than simpler representations and practical bugs found in mature projects.

  • Problem

    Existing deep learning models capture shallow textual or syntactic structure and miss rich source-code semantics, including long-range dependencies between variable uses.

  • Method

    The paper constructs graphs combining syntax, data flow, types, and other program relationships, then applies Gated Graph Neural Networks to VarNaming and VarMisuse.

  • Results

    The best model achieves 32.9% accuracy on VarNaming and 85.5% accuracy on VarMisuse, beating simpler baselines; VarMisuse also identified bugs in mature open-source projects.

  • Takeaways & Limitations

    Modeling known program structure improves source-code learning and supports meaningful variable-name inference and variable-misuse detection.

  • Takeaways & Limitations

    Applying a trained model to an unknown project is constrained by differences in type hierarchies and vocabularies across domains.

Abstract

from arXiv · show

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.

1 INTRODUCTION

The paper argues that source-code models should exploit rich program semantics and introduces graph-based tasks for predicting variable names and detecting variable misuse.

  • Existing deep learning models often represent code as token sequences, parse trees, or flat variable-dependency networks, missing richer source-code semantics.
  • VarNaming predicts a variable’s name from its usage, requiring reasoning about code located far apart in a source file.
  • VarMisuse predicts which in-scope variable should occupy a program location, targeting semantic errors that existing static analysis may miss.
  • High accuracy requires representations of variable roles and, for VarMisuse, the usage semantics expected at each location.
  • 2.9 million lines of real-world source code yielded 32.9% accuracy on VarNaming and 85.5% accuracy on VarMisuse for the best model.

2 RELATED WORK

Prior code-learning work models textual, syntactic, or variable relationships, while this paper emphasizes explicit data flow and statistical completion without specifications.

  • Earlier source-code models represent code as token sequences or syntax trees, and identifier prediction remains a major challenge.
  • Closest prior work learns variable representations from usages but does not use data-flow information explicitly.
  • Conditional random-field approaches model relationships among variables, AST elements, and types without explicitly modeling data flow.
  • Unlike program synthesis and code transplantation methods requiring specifications, this approach statistically completes gaps from common variable-usage patterns.
  • Graph neural networks adapt deep learning methods to graph-structured inputs and have been applied to tasks including link prediction and semantic role labeling.

3 THE VARMISUSE TASK

VarMisuse frames code understanding as selecting the correct type-correct variable for a masked program location, while acknowledging that multiple assignments can be semantically equivalent.

  • Variable-misuse detection requires reasoning about program semantics, including the roles and functions of program elements and their relationships.
  • For each slot, the task selects the ground-truth variable from all type-correct variables in scope using the surrounding code context.
  • Training and evaluation match the ground-truth variable, although several assignments may be correct when variables refer to the same memory value.

4 MODEL: PROGRAMS AS GRAPHS

The model represents source code as multi-relational graphs and applies Gated Graph Neural Networks to propagate syntactic, data-flow, type, and usage information for naming and misuse prediction.

  • Program graphs encode source text together with semantic information extracted by standard compiler tools.
  • GGNNs initialize node states from labels, pass edge-type-specific messages, aggregate them, and update states with a GRU over repeated time steps.
  • The graph backbone is an AST augmented with Child and NextToken edges, while additional edges capture variable data flow and other program relationships.
  • Backward edges accelerate information propagation and make the GGNN more expressive, while GCNs generalized less well in the experiments.
  • Type embeddings include known types and their supertypes, pooled by element-wise maximum to represent type-hierarchy information.
  • For VarNaming, variable names are replaced by <SLOT> tokens and their propagated representations initialize a GRU that predicts the name.
  • For VarMisuse, a hole node represents the target context while candidate-variable representations are scored against it after GGNN propagation.
  • Large graph batches are represented as sparse disconnected components, enabling efficient GPU processing at 55 graphs per second.

5 EVALUATION

The evaluation tests GGNNs on variable misuse and naming across seen and unseen projects, compares graph models with less structured baselines, and examines qualitative predictions, ablations, and discovered bugs.

  • Evaluation setup: The dataset covers 29 compilable open-source C# projects, with variable-misuse examples requiring at least one type-compatible replacement variable.Projects span diverse domains, including compilers and databases.
  • Evaluation setup: The SEENPROJTEST split uses 23 projects divided into train, validation, and test sets by files, while UNSEENPROJTEST contains three projects absent from training.Two projects form the development set, and the remaining projects are split 60-10-30.
  • Quantitative evaluation: GGNNs outperform LOC, AVGLBL, and AVGBIRNN, with the gap especially large on VARMISUSE because code structure and semantics matter more there.LOC captures little information, whereas the other baselines aggregate usage contexts without explicitly encoding the problem’s rich structure.
  • Generalization to new projects: Performance remains good on unseen projects but is slightly lower than on seen projects, consistent with the mostly unknown type lattice in UNSEENPROJTEST.The authors identify unknown type hierarchies and differing project vocabularies as dominant challenges for applying trained models to new projects.
  • Ablation study: Ablations show that removing syntactic edges substantially harms both tasks, while removing semantic edges mainly affects VARMISUSE.ComputedFrom, FormalArgName, and ReturnsTo edges provide small VARMISUSE gains but substantially improve VARNAMING.
  • Qualitative evaluation: On a ServiceStack snippet, the GGNN selects the correct variable in 11 of 13 slots and learns similar representations for semantically similar usages.The model also identifies variable-misuse bugs in mature projects, including memory-consumption and misleading-error-message issues, and three additional bugs reported to Roslyn were fixed.

6 DISCUSSION & CONCLUSIONS

Source code offers rich, well-defined local semantics and extractable program-analysis information, but integrating that structure into deep learning remains challenging. VARMISUSE serves as a proxy for learning source-code meaning by requiring probabilistic refinement of type-system information.

  • Source code provides richer, more structured opportunities for deep learning than textual or perceptual data.Its local semantics are well-defined, and efficient program analyses can extract additional information.
  • Integrating syntactic and semantic program information into deep learning is an open challenge.
  • VARMISUSE goes beyond code completion by testing whether models can learn the meaning of source code.The task requires probabilistically refining information ordinarily included in type systems.

A PERFORMANCE CURVES

The GGNN model’s performance remains substantial when evaluated through ROC and precision-recall curves. At a 10% false-positive rate, it reaches true-positive rates of 73% on SEENPROJTEST and 69% on unseen projects.

  • 10% false positive rate yields a 73% true positive rate for SEENPROJTEST.
  • 10% false positive rate yields a 69% true positive rate for the unseen test.
  • ROC and precision-recall curves characterize GGNN performance across operating points.

B VARMISUSE PREDICTION SAMPLES

The qualitative VARMISUSE samples show that the model often selects correct variables and can exploit semantic or API-related cues, while failures arise from aliases, interprocedural dependencies, intent, and difficult conditionals.

  • The model correctly predicts all variables in the loop.
  • 66% accuracy for Payload and 44% for payload_ illustrate differing predictions in one sample.
  • The model is commonly confused by aliases, although either choice can yield identical behavior.
  • Interprocedural understanding across a class file is required for the model to reason about the final slot.
  • Formal parameter names help the model select correct string parameters.
  • Conditionals with rare constants remain difficult for the model to reason about.

C NEAREST NEIGHBOR OF GGNN USAGE REPRESENTATIONS

Nearest-neighbor examples indicate that GGNN usage representations organize code locations by shared semantic or protocol-related behavior. Similar representations appear for compatible API protocols, collection additions, and null checks.

  • The examples compare nearest neighbors using cosine similarity of learned usage representations.
  • Slots checked for nullness have similar representations.
  • Slots following similar API protocols receive similar representations.
  • Adding elements to collection-like objects yields similar representations.

D DATASET

The dataset comprises about 2.9MLOC across listed C# projects, with specified development and test-only subsets. A large portion was released, excluding GPL-licensed projects.

  • The dataset contains about 2.9MLOC of C# code across the listed projects.
  • Projects marked Dev formed the development set, while projects marked † belonged to the test-only dataset.
  • The remaining projects were split into train, validation, and test sets.
  • A large portion of the data was released, excluding projects with GPL licenses.
  • Results on the published dataset were averaged over three runs.
Loading 1711.00740v3…