LLVM-9.0 学习者教程
项目介绍
LLVM-9.0-Learner-Tutorial 是一个面向 LLVM(v9.0.0 或 v11.0.0)初学者的详细教程。该项目通过逐步讲解和详细的文档注释,记录了作者学习 LLVM 并最终完成一个完整项目的过程。教程的目标是帮助初学者理解和掌握 LLVM,特别是如何使用 LLVM 进行 FPGA 高层次综合(High-Level Synthesis, HLS)。
项目快速启动
1. 下载项目
首先,克隆项目到本地:
git clone https://github.com/zslwyuan/LLVM-9.0-Learner-Tutorial.git
2. 安装 LLVM-9.0
如果安装 LLVM 时遇到问题,建议使用项目提供的安装脚本:
cd LLVM-9.0-Learner-Tutorial
./install_llvm.sh
3. 构建项目
进入项目目录并使用 CMake 构建项目:
mkdir build
cd build
cmake ..
make
4. 运行测试
构建完成后,可以运行测试来验证安装和构建是否成功:
./TestBuiltFiles.sh
应用案例和最佳实践
案例1:分析程序中的函数名称
通过编写一个分析 pass,可以获取程序中所有函数的名称。以下是一个简单的示例代码:
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace {
struct FuncNamePrinter : public FunctionPass {
static char ID;
FuncNamePrinter() : FunctionPass(ID) {}
bool runOnFunction(Function &F) override {
errs() << "Function Name: " << F.getName() << "\n";
return false;
}
};
}
char FuncNamePrinter::ID = 0;
static RegisterPass<FuncNamePrinter> X("funcname", "Function Name Printer", false, false);
案例2:获取函数中的依赖图
通过编写一个分析 pass,可以获取函数中的依赖图并绘制出来。以下是一个简单的示例代码:
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Analysis/DependenceAnalysis.h"
using namespace llvm;
namespace {
struct DependenceGraphPrinter : public FunctionPass {
static char ID;
DependenceGraphPrinter() : FunctionPass(ID) {}
bool runOnFunction(Function &F) override {
DependenceInfo DI(&F);
DI.print(errs());
return false;
}
};
}
char DependenceGraphPrinter::ID = 0;
static RegisterPass<DependenceGraphPrinter> Y("depgraph", "Dependence Graph Printer", false, false);
典型生态项目
1. Light-HLS
Light-HLS 是一个基于 LLVM 的项目,专注于 FPGA 高层次综合。它通过分析和优化 LLVM IR,生成适用于 FPGA 的优化代码。
2. Polly
Polly 是 LLVM 的一个子项目,专注于多面体模型(Polyhedral Model)的优化。它通过分析和优化循环结构,提高代码的并行性和性能。
3. Clang
Clang 是 LLVM 的 C/C++ 前端,提供了丰富的 API 和工具,用于源代码的分析和转换。Clang 与 LLVM 紧密集成,是 LLVM 生态系统中的重要组成部分。