LLVM系列第二十二章:写一个简单的编译时函数调用统计器(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系列第二十一章:写一个简单的Loop Pass
LLVM系列第二十二章:写一个简单的编译时函数调用统计器(Pass)
LLVM系列第二十三章:写一个简单的运行时函数调用统计器(Pass)
LLVM系列第二十四章:用Xcode编译调试LLVM源码
LLVM系列第二十五章:简单统计一下LLVM源码行数
LLVM系列第二十六章:理解LLVMContext
LLVM系列第二十七章:理解IRBuilder
LLVM系列第二十八章:写一个JIT Hello World
LLVM系列第二十九章:写一个简单的常量加法“消除”工具(Pass)

flex&bison系列



前言

在此记录下基于LLVM写一个简单的函数调用统计器(Pass)的过程,以备查阅。

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

假设我们要简单地统计一下,代码中每个函数的调用次数。我们可以写一个简单的Pass来做这件事。在这里,我们只考虑编译时的函数调用情况,不考虑运行时的情况。注意它们的区别,在编译时做统计,我们面对的问题是“静态的”;而在运行时做统计,我们面对的问题是“动态的”。

我们可以尝试遍历代码中的每一条指令(Instruction),判断它是否为函数调用指令并进行统计。若要遍历每一条指令,则需先找到其所在代码块(Basic Block);若要找到代码块,则需先找到其所在的函数(Function);若要找到函数,则需找到其所在的模块(Module)。所以,我们要做的是,遍历模块中的每一个函数、函数中的每一个代码块、代码块中的每一条指令。

本章我们就来写一个简单的Pass,用来统计每个函数在编译时的调用次数。

一、项目结构

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

llvm-project/llvm
├── ...
├── lib
│   └── Transforms
│       ├── CMakeLists.txt
│       └── CompileTimeFunctionCallCounter
│           ├── CMakeLists.txt
│           └── CompileTimeFunctionCallCounter.cpp
└── ...

二、项目细节

1. 程序模块

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

  1. CompileTimeFunctionCallCounter,一个简单的Pass模块,用来统计每个函数在编译时的调用次数。

如上所述,CompileTimeFunctionCallCounter将会遍历模块中的每一个函数、函数中的每一个代码块、代码块中的每一条指令,统计每个函数的调用次数并打印出来。

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

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

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

# CMakeLists.txt

add_llvm_library(CompileTimeFunctionCallCounter MODULE BUILDTREE_ONLY
    CompileTimeFunctionCallCounter.cpp

    PLUGIN_TOOL
    opt
)

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

...
add_subdirectory(CompileTimeFunctionCallCounter)
...

2. Compile-Time Function Call Counter

CompileTimeFunctionCallCounter的实现在文件lib/Transforms/CompileTimeFunctionCallCounter/CompileTimeFunctionCallCounter.cpp中(示例):

// CompileTimeFunctionCallCounter.cpp

#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"

#include <iostream>
#include <map>

using namespace llvm;
using FunctionCalls = std::map<const Function*, size_t>;

// A LLVM Pass to analyse the function calls
class CompileTimeFunctionCallCounter : public AnalysisInfoMixin<CompileTimeFunctionCallCounter>
{
public:

    using Result = FunctionCalls;

    Result run(Module& module, ModuleAnalysisManager&);

private:

    friend struct AnalysisInfoMixin<CompileTimeFunctionCallCounter>;

    // A special key to identify a particular analysis pass type
    static AnalysisKey Key;
};

// A LLVM Pass to print the function call statistics
class CompileTimeFunctionCallPrinter : public PassInfoMixin<CompileTimeFunctionCallPrinter>
{
public:

    PreservedAnalyses run(Module& module, ModuleAnalysisManager& analysisManager);
};

AnalysisKey CompileTimeFunctionCallCounter::Key;

CompileTimeFunctionCallCounter::Result CompileTimeFunctionCallCounter::run(Module& module, ModuleAnalysisManager&)
{
    Result functionCalls;

    for (const Function& function : module)
    {
        for (const BasicBlock& basicBlock : function)
        {
            for (const Instruction& instruction : basicBlock)
            {
                const CallBase* functionCallInstruction = dyn_cast<CallBase>(&instruction);
                if (nullptr == functionCallInstruction)
                {
                    // Ignore instructions that are not function calls
                    continue;
                }

                // We can get the called function if this is a direct function call
                const Function* calledFunction = functionCallInstruction->getCalledFunction();
                if (nullptr == calledFunction)
                {
                    continue;
                }

                auto functionCallIterator = functionCalls.find(calledFunction);
                if (functionCalls.end() == functionCallIterator)
                {
                    functionCallIterator = functionCalls.insert(std::make_pair(calledFunction, 0)).first;
                }

                ++functionCallIterator->second;
            }
        }
    }

    return functionCalls;
}

PreservedAnalyses CompileTimeFunctionCallPrinter::run(Module& module, ModuleAnalysisManager& analysisManager)
{
    auto functionCalls = analysisManager.getResult<CompileTimeFunctionCallCounter>(module);
    for (auto& functionCall : functionCalls)
    {
        std::cout << "Function: " << functionCall.first->getName().str() << ", "
                  << "called " << functionCall.second << " times" << std::endl;
    }

    // Assuming we did not change anything of the IR code
    return PreservedAnalyses::all();
}

// Pass registration
extern "C" LLVM_ATTRIBUTE_WEAK ::PassPluginLibraryInfo llvmGetPassPluginInfo()
{
    return {
        LLVM_PLUGIN_API_VERSION, "CompileTimeFunctionCallPrinter", LLVM_VERSION_STRING, [](PassBuilder& passBuilder) {
            // 1. Registration for "opt -passes="compile-time-function-call-counter""
            passBuilder.registerPipelineParsingCallback(
                [&](StringRef name, ModulePassManager& passManager, ArrayRef<PassBuilder::PipelineElement>) {
                    if (name == "compile-time-function-call-counter")
                    {
                        passManager.addPass(CompileTimeFunctionCallPrinter());
                        return true;
                    }

                    return false;
                });

            // 2. Registration for "analysisManager.getResult<CompileTimeFunctionCallCounter>(module)"
            passBuilder.registerAnalysisRegistrationCallback([](ModuleAnalysisManager& analysisManager) {
                analysisManager.registerPass([&]() {
                    return CompileTimeFunctionCallCounter();
                });
            });
        }};
}

三、编译

1. 生成项目文件

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

cd /path/to/llvm-project
mkdir build
cd build

cmake -G Ninja -DLLVM_ENABLE_PROJECTS=clang ../llvm

输出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

2. 编译

用ninja进行编译(示例):

ninja

如果我们是在参考第一章的步骤,编译了LLVM源码之后,再编译此项目,则只需编译CompileTimeFunctionCallCounter项目即可。当然,这是ninja自动就能识别出来的,即所谓的增量编译技术。输出log如下(示例):

[4/4] Linking CXX shared module lib/CompileTimeFunctionCallCounter.dylib

3. 运行

为了简单起见,我们就用以下Test.c文件中C代码来测试一下(示例):

// Test.c

void Foo()
{
}

void Bar()
{
    Foo();
}

void Fez()
{
    Bar();
}

int main()
{
    Foo();
    Bar();
    Fez();

    for (int i = 0; i < 5; i++)
    {
        Foo();
    }

    return 0;
}

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

mv ../llvm/lib/Transforms/CompileTimeFunctionCallCounter/Tests/Test.c.txt ../llvm/lib/Transforms/CompileTimeFunctionCallCounter/Tests/Test.c

clang -S -emit-llvm ../llvm/lib/Transforms/CompileTimeFunctionCallCounter/Tests/Test.c -o ../llvm/lib/Transforms/CompileTimeFunctionCallCounter/Tests/Test.ll

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

; ModuleID = '../llvm/lib/Transforms/CompileTimeFunctionCallCounter/Tests/Test.c'
source_filename = "../llvm/lib/Transforms/CompileTimeFunctionCallCounter/Tests/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"

; Function Attrs: noinline nounwind optnone ssp uwtable
define dso_local void @Foo() #0 {
entry:
  ret void
}

; Function Attrs: noinline nounwind optnone ssp uwtable
define dso_local void @Bar() #0 {
entry:
  call void @Foo()
  ret void
}

; Function Attrs: noinline nounwind optnone ssp uwtable
define dso_local void @Fez() #0 {
entry:
  call void @Bar()
  ret void
}

; Function Attrs: noinline nounwind optnone ssp uwtable
define dso_local i32 @main() #0 {
entry:
  %retval = alloca i32, align 4
  %i = alloca i32, align 4
  store i32 0, i32* %retval, align 4
  call void @Foo()
  call void @Bar()
  call void @Fez()
  store i32 0, i32* %i, align 4
  br label %for.cond

for.cond:                                         ; preds = %for.inc, %entry
  %0 = load i32, i32* %i, align 4
  %cmp = icmp slt i32 %0, 5
  br i1 %cmp, label %for.body, label %for.end

for.body:                                         ; preds = %for.cond
  call void @Foo()
  br label %for.inc

for.inc:                                          ; preds = %for.body
  %1 = load i32, i32* %i, align 4
  %inc = add nsw i32 %1, 1
  store i32 %inc, i32* %i, align 4
  br label %for.cond, !llvm.loop !3

for.end:                                          ; preds = %for.cond
  ret i32 0
}

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)"}
!3 = distinct !{!3, !4}
!4 = !{!"llvm.loop.mustprogress"}

运行CompileTimeFunctionCallCounter(示例):

./bin/opt -load-pass-plugin=lib/CompileTimeFunctionCallCounter.dylib -passes="compile-time-function-call-counter" -disable-output ../llvm/lib/Transforms/CompileTimeFunctionCallCounter/Tests/Test.ll

输出结果如下(示例):

Function: Foo, called 3 times
Function: Bar, called 2 times
Function: Fez, called 1 times

四、总结

我们用LLVM提供的C++ API,写了一个简单的Pass,用来统计每个函数在编译时的调用次数,并且做了测试。完整源码示例请参看:
https://github.com/wuzhanglin/llvm-pass-examples

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值