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

Neural Code Comprehension: A Learnable Representation of Code Semantics

Tal Ben-Nun, Alice Shoshana Jakobovits, Torsten Hoefler

Overview

Researchers from ETH Zurich have developed inst2vec, a novel approach to representing computer code as continuous vectors (embeddings) that capture its actual semantic meaning rather than just its syntax. By representing code as Contextual Flow Graphs (XFGs)—which combine how data moves and how logic branches—they have created a language-agnostic "neural code comprehension" pipeline. This pipeline outperforms specialized models in downstream tasks like algorithm classification and performance prediction.

The Neural Code Comprehension pipeline.

The Problem with Treating Code Like Text

Most early attempts to apply artificial intelligence to code treated programs like natural language text (sequences of words) or simple syntax trees (ASTs). However, code is fundamentally different from English: a programmer can reorder independent statements, rename variables, or change a for loop to a while loop without altering what the program actually does.

Traditional NLP-based methods struggle with these semantic equivalencies. The authors identified a critical gap: the field needs a representation that is robust (ignores superficial syntactic changes), learnable (compatible with standard deep learning architectures), and language-independent (works whether the source is C++, Python, or Fortran).

Methods: From Source Code to Semantics

The authors propose a multi-step pipeline to move from raw source code to rich, semantic embeddings:

  1. LLVM IR as the Foundation: Instead of raw source code, the pipeline ingests LLVM Intermediate Representation (IR). This is a low-level, "assembly-like" format used by modern compilers. It strips away "syntactic sugar" and provides a common denominator across many programming languages.
  2. Contextual Flow Graphs (XFGs): This is the paper's core innovation. The authors argue that the "context" of a code statement isn't just the line physically above or below it. Instead, they build a graph that combines data-flow (where variables come from) and control-flow (where the program jumps next).
Translation of source code into LLVM IR, dataflow blocks, and the final Contextual Flow Graph (XFG).
  1. inst2vec (Instruction to Vector): Using these XFGs, they apply a skip-gram model similar to Word2vec. If two instructions frequently appear in the same "flow context," they end up close together in a 200-dimensional embedding space.
  2. Neural Architecture: Finally, they feed these fixed inst2vec embeddings into a standard two-layer LSTM (RNN) to solve high-level tasks like classification or performance modeling.

Datasets and Training

The authors compiled a massive, diverse training corpus of over 50 million lines of LLVM IR to train their embeddings. This corpus included large open-source projects (TensorFlow, the Linux Kernel, OpenCV), standard benchmarks (NAS, Rodinia, SHOC), and procedurally generated synthetic linear algebra code to ensure a wide variety of data types and operations.

For evaluation, they utilized:

  • POJ-104: A dataset of 104 algorithm classes (e.g., bubble sort vs. quicksort) written by 500 different programmers.
  • OpenCL Dataset: Used for predicting the optimal hardware mapping (CPU vs. GPU) and optimal thread coarsening for parallel programs.

Key Insights

  • The Distributional Hypothesis for Code: The authors successfully adapted the linguistic idea that "words in similar contexts have similar meanings" to programming. In their model, "statements in similar flows have similar semantics."
  • XFG > CFG or DFG: They demonstrated that looking at only logic branches (Control Flow) or only variable usage (Data Flow) is insufficient. The hybrid XFG is necessary to capture the full picture of program execution.
  • Semantic Analogies: Much like Word2vec can solve King - Man + Woman = Queen, inst2vec can solve code analogies. It inherently understands that an integer add is to a sub what a floating-point fadd is to an fsub.
  • Zero Fine-Tuning: Remarkably, the embeddings were kept fixed during the high-level tasks. The embeddings themselves captured enough fundamental "knowledge" about code that the downstream RNN didn't need to relearn the basics of programming.

Key Results

  • Algorithm Classification: On the POJ-104 benchmark, the inst2vec approach achieved 94.83% accuracy, setting a new state-of-the-art and outperforming specialized Tree-Based CNNs.
  • Heterogeneous Device Mapping: The model predicted whether code would run faster on a CPU or GPU with ~88% accuracy, beating "DeepTune," a model specifically designed for this exact task.
  • Performance Speedups: In optimization tasks, their "neural comprehension" approach led to better hardware utilization and speedups than manual feature engineering by human experts.

Limitations and Future Work

While highly effective, the approach has limitations. The base version of inst2vec replaces specific immediate values (like 42 or 3.14) with generic tags like <INT>. While the authors proposed an "inst2vec-imm" variant to handle this, it remains a challenge for tasks where the specific value of a constant drastically alters performance.

Furthermore, because this relies on static analysis, the model understands code by looking at its structure, not by executing it. It cannot "see" behaviors that only emerge at runtime with specific inputs. Finally, on tasks with extremely small datasets (like thread coarsening with only 17 programs), the model's performance was less consistent.

Looking forward, the authors suggest this foundational representation could lead to "mechanized code comprehension," where AI can automatically optimize code, detect vulnerabilities, or translate between different programming languages with high fidelity.

Summary updated

BibTeX
@inproceedings{bennun2018neural,
  author = {Tal Ben-Nun and Alice Shoshana Jakobovits and Torsten Hoefler},
  title = {{Neural Code Comprehension: A Learnable Representation of Code Semantics}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2018},
}
Figure