Skip to main content
QUICK REVIEW

[Paper Review] Neural Code Comprehension: A Learnable Representation of Code Semantics

Tal Ben‐Nun, Alice Shoshana Jakobovits|arXiv (Cornell University)|Jun 19, 2018
Software Engineering ResearchComputer Science89 citations
TL;DR

This paper introduces inst2vec, a learnable embedding space for LLVM IR statements using contextual flow graphs (XFGs) that combine data- and control-flow, enabling strong code semantics understanding across languages and tasks without heavy fine-tuning.

ABSTRACT

With the recent success of embeddings in natural language processing, research has been conducted into applying similar methods to code analysis. Most works attempt to process the code directly or use a syntactic tree representation, treating it like sentences written in a natural language. However, none of the existing methods are sufficient to comprehend program semantics robustly, due to structural features such as function calls, branching, and interchangeable order of statements. In this paper, we propose a novel processing technique to learn code semantics, and apply it to a variety of program analysis tasks. In particular, we stipulate that a robust distributional hypothesis of code applies to both human- and machine-generated programs. Following this hypothesis, we define an embedding space, inst2vec, based on an Intermediate Representation (IR) of the code that is independent of the source programming language. We provide a novel definition of contextual flow for this IR, leveraging both the underlying data- and control-flow of the program. We then analyze the embeddings qualitatively using analogies and clustering, and evaluate the learned representation on three different high-level tasks. We show that even without fine-tuning, a single RNN architecture and fixed inst2vec embeddings outperform specialized approaches for performance prediction (compute device mapping, optimal thread coarsening); and algorithm classification from raw code (104 classes), where we set a new state-of-the-art.

Motivation & Objective

  • Propose a language-agnostic, semantics-focused representation of code statements using LLVM IR.
  • Define contextual flow graphs (XFGs) that fuse data- and control-flow information.
  • Create and train inst2vec embeddings for LLVM IR statements.
  • Evaluate the embeddings on code-analytic tasks including algorithm classification and device/threads tuning predictions.

Proposed method

  • Convert source code from multiple languages to LLVM IR in SSA form to obtain a language-agnostic representation.
  • Construct Contextual Flow Graphs (XFGs) that encode both data dependencies and execution dependencies across blocks, branches, and functions.
  • Preprocess LLVM IR statements and train 200-dimensional inst2vec embeddings via skip-gram on statement-context pairs derived from XFGs.
  • Use a fixed pre-trained inst2vec embedding matrix as input to a two-layer LSTM network for downstream tasks.
  • Evaluate embeddings on three high-level tasks: algorithm classification, heterogeneous compute device mapping, and optimal thread coarsening factor prediction.

Experimental results

Research questions

  • RQ1Can a language-agnostic IR-based embedding space capture code semantics across languages?
  • RQ2Do contextual flow graphs improve the quality of statement embeddings beyond data-flow or control-flow alone?
  • RQ3How well do pre-trained inst2vec embeddings perform on high-level code understanding tasks without fine-tuning?

Key findings

  • inst2vec embeddings trained on XFG contexts produce meaningful clustering and analogies, outperforming CFG/DFG-only representations.
  • A context size of 2 statements yields the best embedding quality for semantic and syntactic analogies and distance tests.
  • An RNN with fixed inst2vec embeddings matches or surpasses specialized models on three tasks: POJ-104 algorithm classification, heterogeneous device mapping, and thread coarsening prediction.
  • For algorithm classification on POJ-104, inst2vec achieves 94.83% test accuracy, surpassing prior methods including TBCNN and surface-feature baselines.
  • On device mapping and thread coarsening, inst2vec-imm (with immediate values) often yields the best or competitive performance, approaching or exceeding state-of-the-art baselines.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.