去年在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的後端,每個維度自成體系,不同維度之間還有著對應的關係。後續也會寫文章分析其中的關係。


推薦閱讀:
相关文章