LLVM

269 阅读1分钟

一般编译过程分为三个过程

front end - common optimizer - back end

common optimizer好处是 前端可以有很多种语言输入 但是编译优化是通用的, 所以后端 target 也可以有很多

front end 语法解析 生成 AST 在LLVM 这里 生成 LLVM IR (intermediate representation)

LLVM IR 是自己完备的(Complete Code Representation) 可以表示为 text //in memory // bitcode in disk

这里就有 link time optimizer

一般链接时都链接.c文件, 但是因为 llvm 可以把生成的bitcode 转成 .o文件 (-flto or -O4) 然后把一些相关的都link起来 再做优化

inline across file boundaries.

截屏2021-04-25 下午1.15.29.png

The library-based design of the LLVM optimizer

.a 库 由 .o文件bulid成

.o 文件 由相互独立的 pass 类 (( stand on their own, or explicitly declare their dependencies among other passes))编译成

When given a series of passes to run, the LLVM PassManager uses the explicit dependency information to satisfy these dependencies and optimize the execution of passes.

language-specific passes:

The library-based design of the LLVM optimizer allows our implementer to pick and choose both the order in which passes execute, and which ones make sense for the image processing domain. PassManager itself doesn't know anything about the internals of the passes, the implementer is free to implement their own language-specific passes

implementer 也可以选择不同的 pass 或者重写 以及顺序 组装成 .o 文件, 然后生成.a 库

并且.a 库里面link的时候只link 被调用的.o 文件

截屏2021-04-24 下午6.10.32.png

code generator:

custom target-specific passes

截屏2021-04-24 下午6.20.53.png

截屏2021-04-24 下午6.18.58.png

www.aosabook.org/en/llvm.htm…