Skip to main content
QUICK REVIEW

[论文解读] Neural Code Comprehension: A Learnable Representation of Code Semantics

Tal Ben‐Nun, Alice Shoshana Jakobovits|arXiv (Cornell University)|Jun 19, 2018
Software Engineering Research被引用 89
一句话总结

本论文引入 inst2vec,这是一个用于 LLVM IR 语句的可学习嵌入空间,通过将数据流和控制流结合在一起的上下文流图 (XFGs),实现跨语言和任务的强代码语义理解,无需大量微调。

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.

研究动机与目标

  • 提出一种语言无关、以语义为焦点的代码语句表示,基于 LLVM IR。
  • 定义将数据依赖和执行依赖融为一体的 Contextual Flow Graphs (XFGs)。
  • 创建并训练 LLVM IR 语句的 inst2vec 嵌入。
  • 在包括算法分类和设备/线程调优预测等代码分析任务上评估嵌入。

提出的方法

  • 将来自多种语言的源代码转换为处于 SSA 形式的 LLVM IR,以获得语言无关的表示。
  • 构建 Contextual Flow Graphs (XFGs),编码跨块、跨分支和跨函数的数据依赖和执行依赖。
  • 对 LLVM IR 语句进行预处理,并通过来自 XFG 的语句-上下文对的 skip-gram 训练出 200 维的 inst2vec 嵌入。
  • 使用固定的预训练 inst2vec 嵌入矩阵作为输入,输入到两层 LSTM 网络以进行下游任务。
  • 在三个高级任务上评估嵌入:算法分类、异构计算设备映射以及线程粗化因子预测。

实验结果

研究问题

  • RQ1一个语言无关的 IR 基嵌入空间是否能够捕捉跨语言的代码语义?
  • RQ2上下文流图是否比单独的数据流或控制流更能提升语句嵌入的质量?
  • RQ3在不进行微调的情况下,预训练的 inst2vec 嵌入在高级代码理解任务中的表现如何?

主要发现

  • 在 XFG 上下文训练的 inst2vec 嵌入能够产生有意义的聚类与类比,优于仅 CFG/DFG 表示。
  • 2 条语句的上下文规模在语义和句法类比与距离测试中能达到最佳嵌入质量。
  • 使用固定的 inst2vec 嵌入的 RNN 在三个任务上达到或超过专门模型:POJ-104 算法分类、异构设备映射与线程粗化预测。
  • 在 POJ-104 的算法分类上,inst2vec 达到 94.83% 的测试准确率,超越包括 TBCNN 和表面特征基线在内的先前方法。
  • 在设备映射和线程粗化任务上,inst2vec-imm(含即时值)通常达到最佳或具竞争力的表现,接近或超过最先进的基线。

更好的研究,从现在开始

从论文设计到论文写作,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。