• LLVM系列第十九章:写一个简单的Module 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创建一个简单的Module Pass的过程,以备查阅。

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

    Module Pass是针对IR代码中的每个模块 (Module)执行的。它可以对模块内的所有函数、全局变量等进行分析处理时。注意在进行处理时,Pass从模块得到函数是无序的。

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

    一、项目结构

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

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

    二、项目细节

    1. 程序模块

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

    1. SimpleModulePass,一个简单的Module Pass模块

    SimpleModulePass将会对每一个模块进行处理,即把其模块中的所有全局变量、函数等信息打印出来。

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

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

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

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

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

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

    3. Simple Module Pass

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

    // SimpleModulePass.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 SimpleModulePass : public PassInfoMixin<SimpleModulePass>
        {
        public:
    
            PreservedAnalyses run(Module& module, ModuleAnalysisManager& analysisManager)
            {
                cout << "Module: " << module.getName().str() << endl;
                cout << "    ID: " << module.getModuleIdentifier() << endl;
                cout << "    Source File Name: " << module.getSourceFileName() << endl;
                cout << "    Instruction Count: " << module.getInstructionCount() << endl;
    
                // Print out all the global variables in this module
                cout << endl << "    Gloabl Variable Count: " << module.getGlobalList().size() << endl;
                for (const auto& globalVariable : module.getGlobalList())
                {
                    cout << "        Global Variable: " << globalVariable.getName().str() << endl;
                }
    
                // Print out all the functions in this module
                cout << endl << "    Function Count: " << module.getFunctionList().size() << endl;
                for (const auto& function : module)
                {
                    cout << "        Function: " << function.getName().str() << endl;
                }
    
                // Assuming we did not change anything of the IR code
                return PreservedAnalyses::all();
            }
        };
    }
    
    // This is the new way of registering our pass
    extern "C" PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo()
    {
        return
        {
            LLVM_PLUGIN_API_VERSION,
            "SimpleModulePass",
            "v0.1",
            [](PassBuilder& passBuilder) {
                passBuilder.registerPipelineParsingCallback(
                    [](StringRef name, ModulePassManager& passManager, ArrayRef<PassBuilder::PipelineElement>) {
                        if(name == "simple-module-pass")
                        {
                            passManager.addPass(SimpleModulePass());
                            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

    三、编译

    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完成之后,再编译此项目,则仅仅需要编译SimpleModulePass项目即可。当然,这是ninja自动就能识别出来的,即所谓的增量编译技术。输出log如下(示例):

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

    3. 运行

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

    // Test.c
    
    int globalInt = 0;
    short globalShort = 1;
    const char* globalString = "This is a global string";
    
    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
    • 28
    • 29
    • 30
    • 31

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

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

    生成IR代码大体如下(示例):

    ; ModuleID = '../llvm/lib/Transforms/SimpleModulePass/Test.c'
    source_filename = "../llvm/lib/Transforms/SimpleModulePass/Test.c"
    target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
    target triple = "x86_64-apple-macosx11.0.0"
    
    @globalInt = dso_local global i32 0, align 4
    @globalShort = dso_local global i16 1, align 2
    @.str = private unnamed_addr constant [24 x i8] c"This is a global string\00", align 1
    @globalString = dso_local global i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i32 0, i32 0), align 8
    
    ; Function Attrs: noinline nounwind optnone ssp uwtable
    define dso_local i32 @Foo(i32 %a) #0 {
    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
    }
    
    ; Function Attrs: noinline nounwind optnone ssp uwtable
    define dso_local i32 @Bar(i32 %a, i32 %b) #0 {
    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
    }
    
    ; Function Attrs: noinline nounwind optnone ssp uwtable
    define dso_local i32 @Bead(i32 %a, i32 %b) #0 {
    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
    }
    
    attributes #0 = { noinline nounwind optnone ssp uwtable "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+cx8,+fxsr,+mmx,+sahf,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
    
    !llvm.module.flags = !{!0, !1}
    !llvm.ident = !{!2}
    
    !0 = !{i32 1, !"wchar_size", i32 4}
    !1 = !{i32 7, !"PIC Level", i32 2}
    !2 = !{!"clang version 12.0.1 (https://github.com/llvm/llvm-project fed41342a82f5a3a9201819a82bf7a48313e296b)"}
    
    • 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

    运行SimpleModulePass(示例):

    ./bin/opt -load-pass-plugin=lib/SimpleModulePass.dylib -passes="simple-module-pass" -disable-output ../llvm/lib/Transforms/SimpleModulePass/Test.ll
    
    • 1

    输出结果如下(示例):

    Module: ../llvm/lib/Transforms/SimpleModulePass/Test.ll
        ID: ../llvm/lib/Transforms/SimpleModulePass/Test.ll
        Source File Name: ../llvm/lib/Transforms/SimpleModulePass/Test.c
        Instruction Count: 28
    
        Gloabl Variable Count: 4
            Global Variable: globalInt
            Global Variable: globalShort
            Global Variable: .str
            Global Variable: globalString
    
        Function Count: 3
            Function: Foo
            Function: Bar
            Function: Bead
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    四、总结

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

  • 相关阅读:
    SQL连接(内连接,外连接,交叉连接)
    单片机之瑞萨RL78 串口通信的例子
    Linux笔记 - - gcc/g++和make/Makefile的使用
    web前端课程设计 HTML+CSS+JavaScript旅游风景云南城市网页设计与实现 web前端课程设计代码 web课程设计 HTML网页制作代码
    java实现课程表 II
    C++~类和对象总结(万字大总结,值得收藏)
    Spring注入Bean的几种方法
    AD - 将修改后的 PCB 封装更新到当前 PCB 中
    Log4j发布2.17.0,解决2.16.0存在的DOS攻击风险
    python爬虫实战之逆向分析酷狗音乐
  • 原文地址:https://blog.csdn.net/Zhanglin_Wu/article/details/125466341