• LLVM系列第二十章:写一个简单的Function Pass


    系列文章目录

    LLVM系列第一章:编译LLVM源码
    LLVM系列第二章:模块Module
    LLVM系列第三章:函数Function
    LLVM系列第四章:逻辑代码块Block
    LLVM系列第五章:全局变量Global Variable
    LLVM系列第六章:函数返回值Return
    LLVM系列第七章:函数参数Function Arguments
    LLVM系列第八章:算术运算语句Arithmetic Statement
    LLVM系列第九章:控制流语句if-else
    LLVM系列第十章:控制流语句if-else-phi
    LLVM系列第十一章:写一个Hello World
    LLVM系列第十二章:写一个简单的词法分析器Lexer
    LLVM系列第十三章:写一个简单的语法分析器Parser
    LLVM系列第十四章:写一个简单的语义分析器Semantic Analyzer
    LLVM系列第十五章:写一个简单的中间代码生成器IR Generator
    LLVM系列第十六章:写一个简单的编译器
    LLVM系列第十七章:for循环
    LLVM系列第十八章:写一个简单的IR处理流程Pass
    LLVM系列第十九章:写一个简单的Module Pass
    LLVM系列第二十章:写一个简单的Function Pass



    前言

    在此记录下用LLVM创建一个简单的Function Pass的过程,以备查阅。

    开发环境的配置请参考第一章 《LLVM系列第一章:编译LLVM源码》。

    Function Pass,顾名思义,是在程序中的每个函数(function)上执行的。Function Pass不需要以特定顺序执行,并且不会修改外部函数(external function)。具体地说,Function Pass不能添加或删除当前模块的函数或全局变量,也不能分析或修改当前正在被处理的函数之外的其它函数。

    本章我们就来写一个最简单的Function Pass。

    一、项目结构

    我们把这个简单的项目命名为SimpleFunctionPass。可以参考LLVM的源码中其它Pass流程的组织结构,来组织我们自己的代码(示例):

    llvm-project/llvm
    ├── ...
    ├── lib
    │   └── Transforms
    │       ├── CMakeLists.txt
    │       └── SimpleFunctionPass
    │           ├── CMakeLists.txt
    │           └── SimpleFunctionPass.cpp
    └── ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    二、项目细节

    1. 程序模块

    这个简单的项目只包含了一个模块:

    1. SimpleFunctionPass,一个简单的Function Pass模块

    SimpleFunctionPass将会对每一个函数进行处理,即把函数中的所有Basic Block等信息打印出来。

    注意,我们需要把SimpleFunctionPass项目加入到LLVM Transforms父项目中,即指示CMake在编译LLVM源码的同时,也要编译SimpleFunctionPass项目。

    以下是跟项目组织结构相关的部分CMake脚本。

    (1) lib/Transforms/SimpleFunctionPass/CMakeLists.txt文件(示例):

    # CMakeLists.txt
    
    add_llvm_library(SimpleFunctionPass MODULE BUILDTREE_ONLY
        SimpleFunctionPass.cpp
    
        PLUGIN_TOOL
        opt
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    (2) lib/Transforms/CMakeLists.txt文件(示例):

    ...
    add_subdirectory(SimpleFunctionPass)
    ...
    
    • 1
    • 2
    • 3

    3. Simple Function Pass

    SimpleFunctionPass的实现在文件lib/Transforms/SimpleFunctionPass/SimpleFunctionPass.cpp中:

    // SimpleFunctionPass.cpp
    
    #include "llvm/IR/PassManager.h"
    #include "llvm/Passes/PassBuilder.h"
    #include "llvm/Passes/PassPlugin.h"
    #include "llvm/Support/raw_ostream.h"
    
    #include  <iostream>
    
    using namespace llvm;
    using std::cout;
    using std::endl;
    
    namespace
    {
        class SimpleFunctionPass : public PassInfoMixin<SimpleFunctionPass>
        {
        public:
    
            PreservedAnalyses run(Function& function, FunctionAnalysisManager& analysisManager)
            {
                cout << "Function: " << function.getName().str() << endl;
                cout << "    Instruction Count: " << function.getInstructionCount() << endl;
    
                // Print out all the basic blocks in this function
                cout << endl << "    Basic Block Count: " << function.getBasicBlockList().size() << endl;
                for (const auto& basicBlock : function)
                {
                    cout << "        BasicBlock: " << basicBlock.getName().str() << endl;
                }
    
                // Print out all the arguments of this function
                cout << endl << "    Argument Count: " << function.arg_size() << endl;
                for (const auto& arg : function.args())
                {
                    cout << "        Argument: " << arg.getName().str() << endl;
                }
    
                cout << endl << "    Function Type: " << endl;
                function.getFunctionType()->print(outs());
                cout << endl << endl;
    
                // Assuming we did not change anything of the IR code
                return PreservedAnalyses::all();
            }
        };
    }
    
    // This part is the new way of registering your pass
    extern "C" PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo()
    {
        return
        {
            LLVM_PLUGIN_API_VERSION,
            "SimpleFunctionPass",
            "v0.1",
            [](PassBuilder& passBuilder) {
                passBuilder.registerPipelineParsingCallback(
                    [](StringRef name, FunctionPassManager& passManager, ArrayRef<PassBuilder::PipelineElement>) {
                        if(name == "simple-function-pass")
                        {
                            passManager.addPass(SimpleFunctionPass());
                            return true;
                        }
    
                        return false;
                    }
                );
            }
        };
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    三、编译

    1. 生成项目文件

    用CMake工具生成项目文件(示例):

    cd /path/to/llvm-project
    mkdir build
    cd build
    
    cmake -G Ninja -DLLVM_ENABLE_PROJECTS=clang ../llvm
    
    • 1
    • 2
    • 3
    • 4
    • 5

    输出log如下(示例):

    -- clang project is enabled
    -- clang-tools-extra project is disabled
    -- ...
    -- Ninja version: 1.10.2
    -- Found ld64 - /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
    -- ...
    -- LLVM host triple: x86_64-apple-darwin20.6.0
    -- LLVM default target triple: x86_64-apple-darwin20.6.0
    -- ...
    -- Configuring done
    -- Generating done
    -- Build files have been written to: .../llvm-project/build
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2. 编译

    用ninja进行编译(示例):

    ninja
    
    • 1

    如果我们是在第一章的编译LLVM完成之后,再编译此项目,则仅仅需要编译SimpleFunctionPass项目即可。当然,这是ninja自动就能识别出来的,即所谓的增量编译技术。输出log如下(示例):

    [4/4] Linking CXX shared module lib/SimpleFunctionPass.dylib
    
    • 1

    3. 运行

    为了简单起见,假设我们要对以下Test.c文件中C代码进行处理(示例):

    // Test.c
    
    int Foo(int a)
    {
        int b;
    
        if (a > 33)
        {
            b = 66;
        }
        else
        {
            b = 77;
        }
    
        return b;
    }
    
    int Bar(int a, int b)
    {
        return a + b;
    }
    
    int Bead(int a, int b)
    {
        return a * b;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    可以用clang生成IR代码,命令如下(示例):

    mv ../llvm/lib/Transforms/SimpleFunctionPass/Test.c.txt ../llvm/lib/Transforms/SimpleFunctionPass/Test.c
    
    clang -S -emit-llvm ../llvm/lib/Transforms/SimpleFunctionPass/Test.c -o ../llvm/lib/Transforms/SimpleFunctionPass/Test.ll
    
    • 1
    • 2
    • 3

    生成IR代码大体如下,当然,并不是跟clang生成的IR代码完全一样,做了一点简化(示例):

    ; ModuleID = 'Test.c'
    source_filename = "Test.c"
    
    define i32 @Foo(i32 %a) {
    entry:
      %a.addr = alloca i32, align 4
      %b = alloca i32, align 4
      store i32 %a, i32* %a.addr, align 4
      %0 = load i32, i32* %a.addr, align 4
      %cmp = icmp sgt i32 %0, 33
      br i1 %cmp, label %if.then, label %if.else
    
    if.then:                                          ; preds = %entry
      store i32 66, i32* %b, align 4
      br label %if.end
    
    if.else:                                          ; preds = %entry
      store i32 77, i32* %b, align 4
      br label %if.end
    
    if.end:                                           ; preds = %if.else, %if.then
      %1 = load i32, i32* %b, align 4
      ret i32 %1
    }
    
    define i32 @Bar(i32 %a, i32 %b) {
    entry:
      %a.addr = alloca i32, align 4
      %b.addr = alloca i32, align 4
      store i32 %a, i32* %a.addr, align 4
      store i32 %b, i32* %b.addr, align 4
      %0 = load i32, i32* %a.addr, align 4
      %1 = load i32, i32* %b.addr, align 4
      %add = add nsw i32 %0, %1
      ret i32 %add
    }
    
    define i32 @Bead(i32 %a, i32 %b) {
    entry:
      %a.addr = alloca i32, align 4
      %b.addr = alloca i32, align 4
      store i32 %a, i32* %a.addr, align 4
      store i32 %b, i32* %b.addr, align 4
      %0 = load i32, i32* %a.addr, align 4
      %1 = load i32, i32* %b.addr, align 4
      %mul = mul nsw i32 %0, %1
      ret i32 %mul
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    运行SimpleFunctionPass(示例):

    ./bin/opt -load-pass-plugin=lib/SimpleFunctionPass.dylib -passes="simple-function-pass" -disable-output Test.ll
    
    • 1

    输出结果如下(示例):

    Function: Foo
        Instruction Count: 12
    
        Basic Block Count: 4
            BasicBlock: entry
            BasicBlock: if.then
            BasicBlock: if.else
            BasicBlock: if.end
    
        Argument Count: 1
            Argument: a
    
        Function Type: 
    i32 (i32)
    
    Function: Bar
        Instruction Count: 8
    
        Basic Block Count: 1
            BasicBlock: entry
    
        Argument Count: 2
            Argument: a
            Argument: b
    
        Function Type: 
    i32 (i32, i32)
    
    Function: Bead
        Instruction Count: 8
    
        Basic Block Count: 1
            BasicBlock: entry
    
        Argument Count: 2
            Argument: a
            Argument: b
    
        Function Type: 
    i32 (i32, i32)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    四、总结

    我们用LLVM提供的C++ API,创建了一个简单的Function Pass,并且编译运行成功。完整源码示例请参看:
    https://github.com/wuzhanglin/llvm-pass-examples

  • 相关阅读:
    向量数据库
    gd32部分映射1/2,完全映射,备用功能选择等
    防火墙——计算机网络
    【c++&leetcode】1382. Balance a Binary Search Tree
    WPFComBox的操作方式获取的方式
    pychon/PIL/opencv学习过程中遇到的问题
    Vivado 2017.04版本安装教程
    c++(五)
    64线LiDAR上速度可达120Hz!一种基于图像表示的快速精确的LiDAR地面分割算法
    MySQL学习笔记(十四)索引失效有哪些情况?
  • 原文地址:https://blog.csdn.net/Zhanglin_Wu/article/details/125438188