在LLVM中编写pass的详细教程(2)》

LLVM是一个自由软件项目,它是一种编译器基础设施,以C++写成。它的发展起源于2000年伊利诺伊大学厄巴纳-香槟分校(UIUC)的维克拉姆·艾夫(Vikram Adve)与其第一博士生克里斯·拉特纳(Chris Lattner)的研究,彼时他们想要为所有静态及动态语言创造出动态的编译技术。


LLVM的命名最早源自于底层虚拟机(Low Level Virtual Machine)的首字母缩写,但现在这个项目的范围早已大大超越其最初的意思。当前,LLVM已经发展成为被用于开发从编译器前端到后端的“一套模块及可重用的编译器及工具链技术的集合”("collection of modular and reusable compiler and toolchain technologies")。

2005年,苹果电脑雇用了克里斯·拉特纳及他的团队为苹果电脑开发应用程序系统,LLVM为现今Mac OS X及iOS开发工具的一部分,Xcode开发环境的内核使用的即是LLVM。因LLVM对产业的贡献,ACM于2012年将ACM软件系统奖授与Adve、Lattner及Evan Cheng。

----------------------------------------------------------------------------------------------------------------------------------------

在前面的文章中,我们已经介绍了通过编写pass对函数中的Basic Blocks进行遍历的方法,本文将介绍利用pass对程序中的操作码(Operation Code, OPCode)进行计数的方法。


跟之前一样,假设你的LLVM之安装目录为 ... .../llvm,那么你首先在路径 ... .../llvm/lib/Transforms 中创建一个子文件夹,例如名字叫做OpcodeCounter。然后在此文件夹下创建如下三个文件:CMakeLists.txt、OpcodeCounter.exports、OpcodeCounter.cpp。因为LLVM中作为pass的一个示例,提供了另外一个现成的pass,即在Transforms中的Hello文件夹,你可以从该文件夹下面把三个名为CMakeLists.txt、Hello.exports、Hello.cpp的文件拷贝到OpcodeCounter文件夹中并修改相应的文件名。


然后修改上层目录(即Transforms)中的CMakeLists.txt,在其末尾添加:add_subdirectory(OpcodeCounter)。然后修改... .../llvm/lib/Transforms/OpcodeCounter 中的CMakeLists.txt。注意这个文件是你从... .../llvm/lib/Transforms/Hello 中拷贝过来的。You must set up a build script that will compile the source code for the new pass,具体来说,你需要把其中出现了三次的Hello,都修改成OpcodeCounter,修改后该文件的内容如下:

# If we don't need RTTI or EH, there's no reason to export anything
# from the hello plugin.
if( NOT LLVM_REQUIRES_RTTI )
  if( NOT LLVM_REQUIRES_EH )
    set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/OpcodeCounter.exports)
  endif()
endif()

if(WIN32 OR CYGWIN)
  set(LLVM_LINK_COMPONENTS Core Support)
endif()

add_llvm_loadable_module( LLVMOpcodeCounter
  OpcodeCounter.cpp

  DEPENDS
  intrinsics_gen
  PLUGIN_TOOL
  opt
  )
然后,需要编写pass文件的具体内容。这个pass文件其实就是一个.cpp文件,就当前这个例子而言,我们要做的就是编辑OpcodeCounter.cpp的内容。该文件内容如下:
#include "llvm/IR/Function.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

namespace {
  
  struct OpcodeCounter : public FunctionPass {
    static char ID; // Pass identification, replacement for typeid
    OpcodeCounter() : FunctionPass(ID) {}

    bool runOnFunction(Function &F) override {
        std::map<std::string, int> opcode_map;
    	errs() << "Function name: ";
    	errs() << F.getName() << '\n';
    	for(Function::iterator bb = F.begin(), e = F.end(); bb!=e; bb++)
    	{
    		for(BasicBlock::iterator i = bb->begin(), i2 = bb->end(); i!=i2; i++)
    		{
    			if(opcode_map.find(i->getOpcodeName()) == opcode_map.end())
                    opcode_map[i->getOpcodeName()] = 1;
                else
                    opcode_map[i->getOpcodeName()] += 1;
    		}
    	}

        std::map<std::string, int> :: iterator p_start = opcode_map.begin();
        std::map<std::string, int> :: iterator p_final = opcode_map.end();
        while(p_start != p_final)
        {
            outs()<< p_start->first << " ::: " << p_start->second <<"\n";
            p_start++;
        }

        opcode_map.clear();
    	return false;
    }
  };
}

char OpcodeCounter::ID = 0;
static RegisterPass<OpcodeCounter> X("OpcodeCounter", "Count the number of opcode for every type");
接下来要做的就是重新bulid LLVM。进入... .../llvm/build文件夹下面,直接使用make。整个过程大概需要几分钟的样子。然后来试用一下上面编写的Pass,为此你需要在你期望的位置(例如Desktop)上建立一个新的测试文件,例如使用上一篇文章《在LLVM中编写pass的详细教程(2)》 中的名为test.c的文件。在使用Clang命令编译生成.ll文件之后,再使用下面的命令来执行上面编写的pass:

opt -load /Users/fzuo/llvm/build/lib/LLVMOpcodeCounter.dylib -OpcodeCounter test.ll

然后,你便会得到如下输出:

WARNING: You're attempting to print out a bitcode file.
This is inadvisable as it may cause display problems. If
you REALLY want to taste LLVM bitcode first-hand, you
can force output with the `-f' option.

Function name: add
add ::: 1
alloca ::: 2
load ::: 2
ret ::: 1
store ::: 2
Function name: main
alloca ::: 2
br ::: 3
call ::: 4
icmp ::: 1
load ::: 2
ret ::: 1
store ::: 1
分析上面的输出,对比之前文章中给出的IR文件,便可以知道我们的pass对每个函数进行了分析,然后统计了其中各个操作码出现的次数。


【本系列文章目录】

  • 在LLVM中编写Pass的详细教程(1):一个Hello World的Pass
  • 在LLVM中编写Pass的详细教程(2):遍历一个函数中的Basic Blocks
  • 在LLVM中编写Pass的详细教程(3):对程序中的OpCode进行计数
  • 在LLVM中编写Pass的详细教程(4):def use 与 use def
  • 在LLVM中编写Backend Pass的详细教程(1)
  • 在LLVM中编写Backend Pass的详细教程(2)


(本文完)
发布了358 篇原创文章 · 获赞 4280 · 访问量 417万+

猜你喜欢

转载自blog.csdn.net/baimafujinji/article/details/78823320