首发于LLVM每日谈

LLVM每日谈之四十七 LLVM后端文档解析

去年在HelloLLVM杭州站的活动中,我做了一个分享:LLVM后端简介 。主要是从LLVM后端的几大环节上展开,简单介绍了LLVM后端。在新的芯片和指令集大火的今天,为LLVM添加一个新的后端,也成了大家比较关注的热点。

LLVM官方提供了两个关于后端的官方文档:

1、 The LLVM Target-Independent Code Generator

2、Writing an LLVM Backend

阅读2之前要求阅读1。将这两个文档参照起来读,基本上可以获取新建后端所需要的各类信息。这两个文档从三个层面解析了后端的架构,将后端从代码组织、执行环节和建立步骤都讲的很清楚。我们在这里就一起看看这三个层面:

1、后端的六大部分

  • Target description classes: abstract target description interfaces (代码地址:include/llvm/Target/)
  • Marchine code description classes: classes used to repesent the code being generated for a target (代码地址:include/llvm/CodeGen/)
  • The "MC" Layer: use to represent and process code at the raw machine code level(代码地址:lib/MC include/llvm/MC)
  • Target-independent code generation algorithms (代码地址:lib/CodeGen)
  • Implementations of the abstract description interfaces for particular targets (代码地址: lib/Target)
  • The target-independent JIT components (代码地址:lib/ExecutionEngine/JIT)

2、后端的七大环节

  • Instruction Selection
  • Scheduling and Formation
  • SSA-based Machine Code Optimizations
  • Register Allocation
  • Prolog/Epilog Code Insertion
  • Late Machine Code Optimizations
  • Code Emission

3、建立新后端的七大步骤

  • Create a subclass of the TargetMachine class that describes characteristics of your target machine.
  • Describe the register set of the target.
  • Describe the instruction set of the target.
  • Describe the selection and conversion of the LLVM IR from a Directed Acyclic Graph (DAG) representation of instructions to native target-specific instructions.
  • Write code for an assembly printer that converts LLVM IR to a GAS format for your target machine.
  • Optionally, add support for subtargets.
  • Optionally, add JIT support and create a machine code emitter (subclass of TargetJITInfo) that is used to emit binary code directly into memory.

这三个层面是从不同的维度去审视LLVM的后端,每个维度自成体系,不同维度之间还有着对应的关系。后续也会写文章分析其中的关系。

编辑于 2019-05-16

文章被以下专栏收录