Abstract
Large Language Models (LLMs) have demonstrated remarkable capabilities in code generation and understanding. However, they primarily process code as linear text, often overlooking the inherent structural information such as syntax trees and logic flows. This paper introduces CGBridge, a novel "plug-and-play" framework designed to inject structural code knowledge into frozen LLMs. CGBridge bridges the gap between graph-based program structures and linear text through a three-stage training pipeline: self-supervised structural pre-training, cross-modal semantic alignment, and instruction-based task adaptation. Experimental results show that CGBridge significantly improves performance in code summarization and translation, particularly under code obfuscation, while remaining 4x faster than standard fine-tuning methods like LoRA.
Introduction
Current LLMs treat source code as a flat sequence of text tokens, creating a fundamental "blind spot." In reality, code follows a strict hierarchy (Abstract Syntax Trees) and complex logic paths (Control and Data Flow Graphs). Linearizing code into text results in the loss of this structural "DNA," making models fragile. For instance, a state-of-the-art model might correctly summarize a function, but if variable names are renamed to gibberish (obfuscation), performance often tanks because the model relies on token patterns rather than the underlying logic. Existing attempts to fix this, such as graph-augmented prompting, often result in extremely long, noisy prompts that overwhelm the model's context window.
Methodology
CGBridge employs a three-stage pipeline to teach an LLM to understand code graphs without modifying the LLM’s internal weights:
- Self-Supervised Structural Pre-training: The authors train a Code Graph Encoder (CGE)—specifically a Graph Transformer—on a massive dataset of unlabeled code graphs. The CGE learns to represent code structure by predicting missing edges and using contrastive learning to match different "views" of the same graph.
- Cross-Modal Semantic Alignment: A "Bridge" module (a set of Transformer layers) is trained to align these graph embeddings with the LLM’s text space. It uses three strategies: matching graphs to their corresponding text, contrasting them against "hard negatives," and generating text summaries grounded in the graph.
- Instruction-Based Task Adaptation: Finally, the Bridge module generates "soft prompts" (learnable vectors) that are injected into a frozen LLM. Only the Bridge is fine-tuned for specific tasks like summarization or translation, keeping the heavy LLM weights untouched.
Evaluation and Results
The researchers utilized the CodeSearchNet (Python) and XLCoST (Python-to-Java) benchmarks for evaluation. Crucially, they released a new large-scale dataset of 270,000 Code Property Graphs (CPGs), each including heterogeneous entities like ASTs, CFGs, and DFGs.
- Summarization and Translation: CGBridge achieved a 16.19% relative gain in "LLM-as-a-Judge" scores over the base Qwen2.5-Coder-1.5B model. In code translation, it saw a massive 38.87% relative gain in Execution Accuracy compared to graph-augmented prompting.
- Obfuscation Resilience: CGBridge is nearly immune to variable renaming. While LoRA-tuned models saw a 15-17% drop in performance when variable names were obfuscated, CGBridge’s performance remained stable (dropping only ~1%).
- Efficiency at Scale: Unlike LoRA, which adds computational overhead to every layer during inference, CGBridge does its graph processing upfront. This results in a 4x speedup in inference time compared to LoRA-tuned variants.
Conclusion
CGBridge demonstrates that explicitly modeling code structure is vital for robust code understanding. By using a modular bridge to connect graph encoders with frozen LLMs, the framework achieves state-of-the-art performance with high efficiency and scalability. Future work will explore broader language support and repository-level context to help LLMs understand how different modules interact across an entire codebase.
Summary updated