Multi-level Semantic-Structural Code Graphs
- Multi-level Semantic-Structural Code Graphs (SSCGs) are hierarchical, heterogeneous graph representations that capture both syntactic and semantic properties across various code granularities.
- They are constructed through static and dynamic analysis pipelines that integrate components like ASTs, CFGs, DFGs, and runtime traces to reflect code structure and dependencies.
- SSCGs enhance program analysis applications such as code completion, retrieval, clone detection, and architectural inference, delivering measurable performance improvements.
Multi-level Semantic-Structural Code Graphs (SSCGs) are heterogeneous, hierarchical graph representations designed to capture both the syntactic and semantic properties of source code across multiple granularities—typically from statements and control/data flow within functions, up through file/module structure, and ultimately to repository/project-wide dependencies. SSCGs unify traditional static analysis artifacts (such as abstract syntax trees, control- and data-flow graphs, and import/call hierarchies) into a multi-relational, multi-level graph, enabling fine-grained, compositional, and semantically robust program analysis and machine learning over code corpora. Recent frameworks instantiate SSCGs both as explicit graph-structured inputs to neural models and as latent attention masks or static/dynamic knowledge graphs for code understanding, retrieval, and generation tasks (Wang et al., 7 Sep 2025, Zhao et al., 4 Dec 2025, Zhang et al., 10 Nov 2025, Mathai et al., 2024, Iyer et al., 2020, Hu et al., 2022).
1. Formal Definitions and Structural Taxonomy
The formal structure of an SSCG is that of a directed, typed, possibly multi-relational graph , with nodes annotated by a type function and edges labeled by edge-type . SSCGs organize and into distinct abstraction or granularity levels, each reflecting a different scale of code semantics:
- Level 1 (Repository/File): Nodes correspond to files, folders, and cross-file dependencies (import, containment). Edges express hierarchical inclusion, import, or reference relations (Wang et al., 7 Sep 2025, Zhao et al., 4 Dec 2025).
- Level 2 (Module/Class/Function): Nodes represent classes, functions/methods, type-definitions; edges encode call relations, inheritance hierarchies, or type use (Wang et al., 7 Sep 2025).
- Level 3 (Function/Statement/Graph View): Nodes are AST statements, control-flow vertices (CFG), or data-flow definitions/uses (DFG); edges represent AST parent-child, CFG/DFG paths, or refinement links (Mathai et al., 2024, Iyer et al., 2020, Hu et al., 2022).
The cross-level ("hier") edges link entities across abstraction layers, for example, connecting a function node in Level 2 to its corresponding AST in Level 3, or a file node to the functions/classes it declares. This layered structure supports both compositionality (tracing dependencies across levels) and abstraction (collapsing detail when appropriate) (Iyer et al., 2020, Wang et al., 7 Sep 2025).
The adjacency can be encoded as tensors (e.g., ), and feature matrices/tensors collect node or code-level features, enabling GNN-based representation learning (Wang et al., 7 Sep 2025).
2. Construction Methodologies and Extraction Pipelines
SSCG construction is typically realized via static and/or dynamic analysis, synthesizing information from multiple code views:
- Static program structure extraction: Parsing code to ASTs (using parsers such as tree-sitter), constructing CFGs/DFGs via data/control-flow analysis, and harvesting import/call/use relations at file/module/project scope (Mathai et al., 2024, Zhao et al., 4 Dec 2025).
- Multi-level graph assembly: Nodes and edges from AST, CFG, and DFGs are merged, with auxiliary attributes/edges reflecting variable uses, function boundaries, class hierarchies, and cross-file imports (Iyer et al., 2020, Wang et al., 7 Sep 2025).
- Dynamic refinement: Runtime traces (function calls, object instantiations, concrete argument flows) are incorporated via dynamic knowledge graphs, then reconciled against static structures to form unified, execution-aware SSCGs (Zhang et al., 10 Nov 2025).
- Learned abstraction: In advanced frameworks, concept hierarchies or new edge/node types may be acquired by mining large codebases and training R-GCNs or link prediction models to propose higher-order abstractions beyond hand-tuned rules (Iyer et al., 2020).
A representative static pipeline is exemplified by CoCo, which extracts function-level CFGs, file-level symbol-dependency graphs, and project-level import graphs; these are merged into a heterogeneous SSCG, and ranked via node centrality to select context for downstream tasks (Zhao et al., 4 Dec 2025). Dynamic integration, as implemented in SemanticForge, reconciles static and runtime (test-traced) edges, yielding a canonical, versioned, and incrementally maintainable SSCG that supports O() updates (Zhang et al., 10 Nov 2025).
3. Graph Encoding, Embedding, and Neural Integration
SSCG-based frameworks employ multiple approaches for embedding graph structure and code semantics:
- Initial node and edge features are comprised of code embeddings (e.g., CodeT5p, CodeBERT), type encodings, and Laplacian positional embeddings derived from the global adjacency (Wang et al., 7 Sep 2025, Mathai et al., 2024).
- Graph neural networks (GNNs): GCN, GAT, or DeepWalk are commonly utilized. For example, in GRACE, graph-level and node embeddings are computed by propagating messages over intra- and inter-level connections, producing a global code representation for code retrieval or fusion (Wang et al., 7 Sep 2025).
- Self-attention mask infusion: CodeSAM adapts the transformer architecture by converting statement-level SSCGs into binary attention masks (constructed from multi-view graphs: AST, CFG, DFG), thereby constraining token-wise attention to reflect structural dependencies (Mathai et al., 2024).
- Cross-modal and hybrid fusion: Some systems, such as CSSAM, jointly process token-sequence encodings and graph embeddings (e.g., via GAT over AST+DFG), then merge with natural language query features through attention or CRESS modules to achieve high-fidelity code–NL alignment (Hu et al., 2022).
Advanced SSCG usage includes cross-level fusion (as in GRACE and CoCo), where structure-aware context from the SSCG is converted to natural language prompts or explicit graph serializations for LLM-based code completion; graph-based re-ranking (structure+semantic concordance) further refines retrieval and context selection (Zhao et al., 4 Dec 2025, Wang et al., 7 Sep 2025).
4. Applications in Repository-Level Code Intelligence
SSCGs have become foundational in several code intelligence scenarios:
- Repository-level code completion: GRACE and CoCo use SSCGs to retrieve and fuse context for filling code holes at any granularity, from statements to multi-module usage, surpassing RAG baselines in EM, ES, and identifier metrics (Wang et al., 7 Sep 2025, Zhao et al., 4 Dec 2025).
- Constraint-aware code generation: SemanticForge leverages static-dynamic SSCGs as the explicit troposphere for SMT-constrained beam search, pruning ill-typed or semantically invalid code paths during generation, and dynamically adapting to repository edits with sublinear update complexity (Zhang et al., 10 Nov 2025).
- Semantic code search and clone detection: Hybrid SSCGs (e.g., AST+DFG in CodeSAM, CSRG in CSSAM) enable improved matching accuracy and robustness to superficial syntactic variance, yielding significant gains in MRR and F1 over AST- or DFG-only representations (Mathai et al., 2024, Hu et al., 2022).
- Cross-granularity context selection: Node importance (e.g., via Personalized PageRank) within the SSCG guides the extraction of the most relevant code context for prompting or retrieval (Zhao et al., 4 Dec 2025).
Quantitatively, frameworks report double-digit relative improvements in EM (up to 20.2%) and substantial gains in edit similarity and identifier scores, with manageable inference overhead (<5%) (Zhao et al., 4 Dec 2025, Wang et al., 7 Sep 2025). Ensemble and structure-enriched transformer models further report systematic gains over state-of-the-art graph-augmented baselines (Mathai et al., 2024, Hu et al., 2022).
5. Comparative Analysis with Prior Representations
SSCGs generalize and subsume several traditional and recent code representations by:
| Representation | Granularity/Levels | Cross-level Abstraction | Semantics Embedded |
|---|---|---|---|
| AST | Single (syntax) | No | Syntactic hierarchy |
| CFG/DFG | Single (function) | No | Control/data flow |
| IR (LLVM-IR) | Single (SSA) | No | Def-use, arithmetic |
| SPT (Aroma) | Single | No | Canonicalized syntax |
| PSG (Iyer et al., 2020) | Multi | Yes | Hierarchical concepts |
| CSRG (Hu et al., 2022) | Dual | No (AST+DFG only) | Syntax+data flow |
| SSCG (CoCo, GRACE) | Multi | Yes | Full codebase context |
| SemanticForge | Multi (static/dyn) | Yes | Compile+runtime states |
While ASTs, CFGs, and DFGs provide local or single-mode abstraction, SSCGs unify these in a multi-relational, hierarchical structure. PSGs and SemanticForge further introduce learnability or dynamic overlay, with a systematic layering of concept abstraction and runtime semantics (Iyer et al., 2020, Zhang et al., 10 Nov 2025). SSCGs support expressiveness required for both fine-grained variable tracking and high-level paradigm/operator abstraction (e.g., "fold", "graph traversal")—capabilities not present in tree-only and SSA-based IRs (Wang et al., 7 Sep 2025, Iyer et al., 2020).
6. Scalability, Maintenance, and Experimental Insights
Empirical evaluation demonstrates that abstraction-consolidation in SSCGs causes the graph to shrink with increasing level—higher-level concept nodes subsume many lower-level operations () (Iyer et al., 2020). Incremental update algorithms achieve maintenance under repository evolution by localizing impact and deferring lazy resolution for cross-file references (Zhang et al., 10 Nov 2025).
Experimental comparison of semantic-structural similarity (over PSGs vs. SPTs) shows that PSGs provide ≈6 percentage points greater overlap across C++ code variants, indicating increased robustness to syntactic change (Iyer et al., 2020). In neural settings, structure-aware token masking and GNN fusion yield consistent improvements on code search (MRR up to 0.710), clone detection (F1 up to 0.952), and program classification (macro-F1 up to 0.9851) (Mathai et al., 2024).
Practical frameworks report inference overheads below 5%, and resource-efficient architectures (e.g., CodeSAM on CodeBERT) allow structurally rich models to operate in constrained compute environments (Mathai et al., 2024, Zhao et al., 4 Dec 2025). Structure-aware reranking consistently improves retrieval alignment with code shape and meaning, as measured by edit similarity, graph-edit distance, and node coverage (Zhao et al., 4 Dec 2025).
7. Future Directions and Generalization
Current research identifies several frontiers: learned concept hierarchies (beyond static PSLs), dynamic refinement integration, and graph-aware pretraining objectives (e.g., next-statement prediction along CFG) (Iyer et al., 2020, Mathai et al., 2024). Generalization of SSCG processing includes query planning via neural policies, program analysis and repair, vulnerability detection, architectural pattern inference, and code optimization across multiple languages and paradigms (Zhang et al., 10 Nov 2025, Mathai et al., 2024).
A plausible implication is that as SSCG extraction and embedding pipelines mature (with wider runtime coverage, cross-language support, and efficient graph storage), their adoption in code-centric AI systems will become increasingly routine for both comprehension and generative tasks, making SSCGs core artifacts for program synthesis, analysis, and LLM-based software engineering.