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

Precise Learning of Source Code Contextual Semantics via Hierarchical Dependence Structure and Graph Attention Networks

Zhehao Zhao, Bo Yang, Ge Li, Huai Liu, Zhi Jin · DOI

Abstract

Deep learning is being used extensively in a variety of software engineering tasks, e.g., program classification and defect prediction. Although the technique eliminates the required process of feature engineering, the construction of source code model significantly affects the performance on those tasks. Most recent works was mainly focused on complementing AST-based source code models by introducing contextual dependencies extracted from CFG. However, all of them pay little attention to the representation of basic blocks, which are the basis of contextual dependencies. In this paper, we integrated AST and CFG and proposed a novel source code model embedded with hierarchical dependencies. Based on that, we also designed a neural network that depends on the graph attention mechanism. Specifically, we introduced the syntactic structural of the basic block, i.e., its corresponding AST, in source code model to provide sufficient information and fill the gap. We have evaluated this model on three practical software engineering tasks and compared it with other state-of-the-art methods. The results show that our model can significantly improve the performance. For example, compared to the best performing baseline, our model reduces the scale of parameters by 50% and achieves 4% improvement on accuracy on program classification task.

Introduction

Deep learning has revolutionized software engineering tasks like defect prediction and code summarization. However, most models struggle with a fundamental trade-off between local syntax and global context. Abstract Syntax Tree (AST)-based models are effective at capturing local syntactic structure but often fail to grasp the "big picture" of how data and control flow through a program. Conversely, Control Flow Graph (CFG)-based models excel at capturing execution paths but frequently treat "basic blocks" as simple text or line numbers, losing the rich semantic information contained within them.

Figure 5: Defective vs. fixed code in Lucene-2.2/2.4 illustrating subtle semantic differences.

As shown in the Lucene example above, subtle differences—such as returning a null versus a specialized empty object (NO_FIELDS)—can be the difference between a system crash and a functioning feature. Traditional models often miss these nuances because they do not prioritize the internal structure of basic blocks or the specific types of dependencies between them.

Methodology: The MFGNN Architecture

To address these limitations, we propose MFGNN (Multi-Flow Graph Neural Network), a hybrid model that bridges the gap between local syntax and global program context using a two-level hierarchical approach.

1. The Source Code Model

Instead of a single flat graph, we construct an Extended Control Flow Graph (ECFG) as the backbone. The outer level represents inter-procedural flow, including sequential execution, conditional branches, call flow, and dataflow (tracking variable definitions and uses). Crucially, the inner level represents each node (basic block) by its corresponding AST subtree, enriched with variable data types and constant values.

Figure 6: Hierarchical dependence structure representing a faulty code version.

2. Neural Network Design

The MFGNN architecture processes this hierarchy in three stages:

  • Local Feature Embedding: A Tree-based Convolutional Neural Network (TBCNN) extracts features from the AST subtrees within each block.
  • Contextual Feature Embedding: A modified Graph Attention Network, AGN4D (Attention-based Graph Network for Directed Graph), processes the ECFG. It uses a self-attention mechanism to dynamically weight the importance of different flow types (e.g., prioritizing dataflow over control flow for specific tasks).
  • Fusion Layer: This layer combines local and contextual features into a final program vector for classification.

Evaluation and Key Results

We evaluated MFGNN on several benchmarks, including CodeChef, Codeforces, PROMISE (for defect prediction), and OJClone (for clone detection).

  • Program Classification: MFGNN achieved a 4% improvement in accuracy over the best baseline (TreeLSTM) while reducing the parameter scale by 50%.
  • Defect Prediction: In Cross-Project Defect Prediction (CPDP), MFGNN outperformed the best metric-based methods by 6.3% in F1 score. Compared to the state-of-the-art BugContext, it improved F1 and AUC by up to 27.6% and 15.8%, respectively.
  • Clone Detection: On the OJClone dataset, it reached a 96.5% F1 score, surpassing specialized models like ASTNN and FCDetect.

Conclusion

The primary insight of this work is that the representation of the basic block is the foundation of contextual dependency. By nesting ASTs within graph nodes, MFGNN distinguishes between keywords and identifiers that look similar but behave differently. Furthermore, by restricting the order of feature extraction (local then contextual), the model is remarkably efficient, outperforming path-based models like code2seq while using 30x fewer parameters. Future work will focus on evaluating robustness on large-scale industrial codebases and refining the graph attention mechanism for even broader application.

Summary updated

BibTeX
@article{zhao2021precise,
  author = {Zhehao Zhao and Bo Yang and Ge Li and Huai Liu and Zhi Jin},
  title = {{Precise Learning of Source Code Contextual Semantics via Hierarchical Dependence Structure and Graph Attention Networks}},
  journal = {J. Syst. Softw.},
  year = {2021},
  doi = {10.1016/j.jss.2021.111108},
}
Figure