美文网首页LLVM
llvm学习日记十:llvm后端概览

llvm学习日记十:llvm后端概览

作者: 鸣人的大哥 | 来源:发表于2019-11-27 16:15 被阅读0次

参考书:《Getting Started with LLVM Core Libraries》

一、概览图:

整体概述:IR在翻译成目标代码时,图中白色方框Passes表示进一步优化,灰色方框表示翻译过程中的各个阶段。


image.png
  • Instruction Selection :(指令选择)该阶段将IR的三地址结构转换为有向无环图(DAG :Directed Acyclic Graph),每一个DAG表示一个 basic block 的计算,所以每个 basic block 关联着不同的DAG。
  • Instruction Scheduling : (指令调度)在Instruction Selection 我们能确定使用哪些指令去计算 basic block ,这被编码在类SelectionDAG中。但是DAG并不确定指令执行顺序,这里第一个 Instruction Scheduling 实例(也叫做 Pre-register Allocation(RA) Scheduling ) 对指令排序。
  • Register Allocation : (寄存器分配)LLVM IR的寄存器是无限的,这一步翻译成有限的特定目标的寄存器。
  • Instruction Scheduling :第二个指令调度实例, 也叫做 Post-register Allocation(RA) Scheduling
  • Code Emission : 该阶段将 MachineInstr 转换成 MCInst 实例,更适合汇编器和链接器。
    因此,指令表示有四个较清晰的层次:LLVM IR ,SelectionDAG nodes,MachineInstr, and MCInst.

二、使用LLC

llc是我们后端使用的主要工具

// 生成汇编
$ llc sum.bc -o sum.s
// 生成obj文件
$ llc sum.bc -filetype=obj -o sum.o
// 指定后端架构
$ llc -march=mips -filetype=obj sum.bc -o sum.o

三、了解后端代码结构

后端实现分散在LLVM源码树的不同的目录,主要的库在lib目录和子目录(CodeGen, MC, TableGen, and Target)

  • CodeGen:常规代码生成算法的实现:instruction selection, scheduler, register allocation, and all analyses needed for them
  • MC : 底层功能的实现,比如:the assembler (assembly parser), relaxation algorithm (disassembler), and specific object file idioms such as ELF, COFF, MachO, and so on.
  • TableGen : 包含TableGen 工具的完整实现,它用来生成C++代码,基于顶层的tb目标文件描述。
  • 如果要写一个新的后端,只需要在 Target 目录下新建一个子目录。
    下边我们看一下Target/Sparc目录的组织结构:


    image.png

四、了解后端库

llc只有一小部分非共享代码tools/llc/llc.cpp,它的大多数功能实现都是可重用的库。

  • 下面是目标无关的代码生成器的库,忽略掉了libLLVM前缀:

• AsmParser.a: This library contains code to parse assembly text and implement an assembler
• AsmPrinter.a: This library contains code to print assembly language and implement a backend that generates assembly files
• CodeGen.a: This library contains the code generation algorithms
• MC.a: This library contains the MCInst class and related ones and is used to
represent the program in the lowest level that LLVM allows
• MCDisassembler.a: This library contains the code to implement a
disassembler that reads object code and decodes bytes to MCInst objects
• MCJIT.a: This library contains the implementation for the just-in-time code
generator
• MCParser.a: This library contains the interface to the MCAsmParser class and is used to implement a component that parses assembly text and performs part of the work of an assembler
• SelectionDAG.a: This library contains SelectionDAG and related classes
• Target.a: This library contains the interfaces that allow the target-independent algorithms to solicit target-dependent functionality, although this functionality per se is implemented in other libraries
(the target-dependent ones)

  • 下面是特定目标的库:
    • <Target>AsmParser.a: This library contains the target-specific part of the AsmParser library, responsible for implementing an assembler for the target machine
    • <Target>AsmPrinter.a: This library contains the functionality to print target instructions and allow the backend to generate assembly language files
    • <Target>CodeGen.a: This library contains the majority of the target-dependent functionality of the backend, including specific register handling rules, instruction selection, and scheduling
    • <Target>Desc.a: This library contains target-machine information regarding the low-level MC infrastructure and is responsible for registering target-specific MC objects such as MCCodeEmitter
    • <Target>Disassembler.a: This library complements the MCDisassembler library with target-dependent functionality to build a system that is able to read bytes and decode them into MCInst target instructions
    • <Target>Info.a: This library is responsible for registering the
    target in the LLVM code generator system and provides façade classes that allow the target-independent code generator libraries to access target-specific functionality

相关文章

  • llvm学习日记十:llvm后端概览

    参考书:《Getting Started with LLVM Core Libraries》 一、概览图: 整体概...

  • 对LLVM和跨平台的理解

    广义 LLVM 架构包括编译前端 + 中间优化 + 编译后端; 狭隘 LLVM 指的就是中间优化 + 编译后端; ...

  • LLVM

    是模块化、可重用的编译器以及工具链技术的集合 广义的LLVM:整个LLVM架构狭义的LLVM:LLVM后端(代码优...

  • LLVM IR

    LLVM Intermediate Representation,链接 LLVM 前端和后端的桥梁,与编程语言和运...

  • oc- APP编译过程以及启动过程

    简单介绍 - LLVM编译器 苹果使用的是LLVM编译器,LLVM架构设计的非常好,主要分为前端,中间,后端 Fr...

  • iOS llvm-1

    LLVM 传统编译器 LLVM区别于传统的编译器,它前端和后端分开了 LLVM流程1.所有的代码会经过[词法分析,...

  • iOS 疯狂讲义(上)整理

    1、LLVM(低级虚拟机)的 Clang 编译器来编译 OC 程序 Clang(前端)-- LLVM(后端)Cla...

  • 【LLVM】LLVM的安装与配置

    LLVM学习入门 入门学习LLVM开始,回顾一下之前的知识。首先,LLVM由三个部分组成, 第一部分是LLVM组件...

  • LLVM

    LLVM 什么是LLVM? 官网:https://llvm.org/ The LLVM Project is a ...

  • iOS_LLVM

    LLVM 官网:https://llvm.org/[https://llvm.org/] The LLVM Pro...

网友评论

    本文标题:llvm学习日记十:llvm后端概览

    本文链接:https://www.haomeiwen.com/subject/rjlbictx.html