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

Modeling Functional Similarity in Source Code With Graph-Based Siamese Networks

Nikita Mehrotra, Navdha Agarwal, Piyush Gupta, Saket Anand, David Lo, Rahul Purandare · DOI

Abstract

Detecting semantic code clones—code fragments that are functionally identical but syntactically distinct—remains a significant challenge in software engineering. Traditional methods relying on Abstract Syntax Trees (ASTs) often fail to capture the underlying logic when control structures vary. This paper presents HOLMES, a novel approach that leverages Program Dependency Graphs (PDGs) and a Siamese Graph Neural Network architecture. By focusing on data and control flow rather than lexical structure, HOLMES achieves state-of-the-art performance in detecting complex Type 4 clones, significantly outperforming existing AST-based models.

Introduction

Code clone detection is vital for software maintenance, bug propagation analysis, and license compliance. While Type 1 (exact) and Type 2 (renamed) clones are easily identified, Type 4 clones—functionally similar code with entirely different syntax—pose a major hurdle. Existing deep learning tools often utilize ASTs, which represent the hierarchical structure of code. However, ASTs are sensitive to syntactic variations; for instance, a for loop and a while loop performing the same task result in vastly different tree structures, leading to false negatives.

The HOLMES Architecture

To address these limitations, HOLMES represents programs as Program Dependency Graphs (PDGs). In a PDG, nodes represent statements, while edges represent data dependencies (how variables are used across statements) and control dependencies (the execution order).

The core of HOLMES is a Siamese Graph Neural Network (GNN) architecture. As shown in Figure 1, the model consists of two identical GNN branches with shared weights. Each branch processes a PDG to generate a vector representation (embedding).

The Siamese Graph Neural Network architecture used by HOLMES to compare two Program Dependency Graphs (PDGs).

The architecture incorporates several advanced features:

  1. Attention-Based GNNs: Using Graph Attention Networks (GAT), the model learns to prioritize semantically significant paths in the graph, effectively ignoring boilerplate code.
  2. LSTM Integration: To capture long-range dependencies across multiple "hops" in the graph, HOLMES integrates LSTM layers, providing a broader receptive field than standard GNNs.
  3. Edge-Attributed Learning: The model distinguishes between control-flow and data-flow edges, learning specialized features for each type of dependency.

Why PDGs Outperform ASTs

The fundamental advantage of HOLMES lies in its use of PDGs over ASTs. ASTs capture how code is written, whereas PDGs capture what the code does. Figure 2 illustrates this distinction: while the ASTs for two different implementations of the same logic look significantly different, the underlying flow of data and control remains more consistent in a graph-based representation.

A comparison between Abstract Syntax Trees (ASTs) and Program Dependency Graphs (PDGs) for functionally similar code snippets.

By utilizing Jumping Knowledge (JK) Networks, HOLMES concatenates features from various rounds of graph propagation. This ensures the model retains local statement-level details while building a global understanding of the program's logic.

Experimental Evaluation

The researchers evaluated HOLMES against several benchmarks, including Google Code Jam (GCJ) and BigCloneBench (BCB). GCJ is particularly useful for testing Type 4 clone detection, as it contains diverse solutions to identical programming problems.

HOLMES demonstrated superior performance compared to TBCCD, the previous state-of-the-art AST-based tool:

  • BigCloneBench: HOLMES achieved an F1-score of 0.98 (vs. 0.96 for TBCCD).
  • Google Code Jam: On this more challenging semantic benchmark, HOLMES reached an F1-score of 0.92, a significant improvement over TBCCD's 0.82.
  • Generalizability: Testing on the SeSaMe dataset (real-world Java projects) showed that HOLMES maintains high precision and recall even on unseen software, indicating it learns general semantic patterns rather than specific coding styles.

Conclusion and Future Work

HOLMES represents a significant step forward in semantic clone detection by shifting the focus from syntax to program dependencies. While the requirement for compilable code and the computational overhead of GNNs are current limitations, the model's ability to identify functionally identical code across different implementations is a major breakthrough. Future research will explore cross-language clone detection and the application of transfer learning to further enhance the model's utility in large-scale software repositories.

Summary updated

BibTeX
@article{mehrotra2020modeling,
  author = {Nikita Mehrotra and Navdha Agarwal and Piyush Gupta and Saket Anand and David Lo and Rahul Purandare},
  title = {{Modeling Functional Similarity in Source Code With Graph-Based Siamese Networks}},
  journal = {IEEE Trans. Software Eng.},
  year = {2020},
  doi = {10.1109/tse.2021.3105556},
}
Figure