Loading and storing variables using an LLVM pass

I am trying to do this:

1- Insert a variable t1.

2- Insert a call to the chrono function (to get time).

3- Store chrono's return value in t1

4- Insert a call to "function" (a function I wrote) and pass t1 as its parameter. function does some calculations on t1.

In code it is:

float t1 = std::chrono::duration_cast<chrono::nanoseconds(chrono_steady_clock::now().time_since_epoch().count());
function(t1);

However I want to insert the above with an LLVM pass in a program that I am trying to modify. I am unsure on how to do it, but my idea is:

Constant *TFunc = M.getOrInsertFunction("std::chrono::duration_cast<chrono::nanoseconds(chrono_steady_clock::now().time_since_epoch().count())", Type::getFloatTy(M.getContext()),NULL);
tfunc = cast<Function>(TFunc);
for (*certain type of instructions Inst*){
    CallInst *CurrInst = dyn_cast<CallInst>(Inst);
    AllocaInst *Talloc = new AllocaInst(Type::getFloatTy((*Inst).getContext()),"t1");
    Instruction *Tcall = CallInst::Create(tfunc,"");
    StoreInst* store_t = new StoreInst(Tcall,Talloc,(Instruction*)CurrInst);
    if(storeT != NULL) {
        LoadInst* loadT = new LoadInst(storeT,"",false);
        Value* t1 = loadT;
        Instruction *newInst = CallInst::Create(hook,loadT, "");
        Inst->getInstList().insert((Instruction*)CurrInst, newInst);
    }
}

The error I am getting is:

0 libLLVM-3.4.so.1 0x40f8150f llvm::sys::PrintStackTrace(_IO_FILE*) + 47 1 libLLVM-3.4.so.1 0x40f8177f 2 libLLVM-3.4.so.1 0x40f812ec 3
0x40022400 __kernel_sigreturn + 0 4 libLLVM-3.4.so.1 0x40864ee7 llvm::LoadInst::LoadInst(llvm::Value*, char const*, bool, llvm::Instruction*) + 71 5 pass.so 0x400265b2 Stack dump: 0. Program arguments: /usr/bin/clang -cc1 -triple i386-pc-linux-gnu -emit-obj -disable-free -disable-llvm-verifier -main-file-name mtd.cc -mrelocation-model pic -pic-level 2 -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.24 -momit-leaf-frame-pointer -g -coverage-file /../mtd.o -resource-dir /usr/bin/../lib/clang/3.4 -dependency-file .deps/mtd.d -MT mtd.o -sys-header-deps -MP -include config.h -D NDEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D NDEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D NDEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /usr/lib/llvm-3.4/include -I /usr/lib/llvm-3.4/include -I /usr /lib/llvm-3.4/include -internal-isystem /usr/include//c++/4.8 -internal-isystem /usr/include//c++/4.8/i386-linux-gnu -internal-isystem /usr/include//c++/4.8/backward -internal-isystem /usr/include//i386-linux-gnu/c++/4.8 -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../../include/c++/4.8 -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../../include/c++/4.8/i686-linux-gnu -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../../include/c++/4.8/backward -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../../include/i686-linux-gnu/c++/4.8 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.4/include -internal-externc-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/include -internal-externc-isystem /usr/include/i386-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Woverloaded-virtual -Wcast-qual -W -Wall -w -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /.../ma sstree-beta-master -ferror-limit 19 -fmessage-length 80 -fvisibility-inlines-hidden -mstackrealign -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -load /../pass.so -o mtd.o -x c++ mtd.cc 1. parser at end of file 2. Per-module optimization passes 3. Running pass 'Synchronization profiler' on module 'mtd.cc'. clang: error: unable to execute command: Segmentation fault (core dumped) clang: error: clang frontend command failed due to signal (use -v to see invocation) Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4) Target: i386-pc-linux-gnu Thread model: posix clang: note: diagnostic msg: PLEASE submit a bug report to http://bugs.debian.org/ and include the crash backtrace, preprocessed source, and associated run script. clang: note: diagnostic msg:


PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang: note: diagnostic msg: /tmp/mtd-7d9a20.cpp clang: note: diagnostic msg: /tmp/mtd-7d9a20.sh clang: note: diagnostic msg:

******************** make: *** [mtd.o] Error 254

Reading documentations didn't help me much other than producing the above code. I have the following questions:

1- What is wrong with my pass's code? In other words, how to add the code I want to?

2- What does this error mean? I cannot see any meaningful message in it other than there was a segmentation fault and the command couldn't be executed (may be because I am a newbie?).

3- I know how to insert calls to function that I write in another C++ file (like "function") but not to functions but not to functions defined in C++ libraries like this chrono function which is why I wrote "std::chrono::duration_cast

Apologies if my questions are too basic. Help and guidance will be appreciated!


As Instruction inherits from Value in LLVM, after you insert your function as a call instruction you can just pass that instruction to your next function. No need for the load store operations you are trying to do. So call your time function and pass the instruction object you used to call it to "function".

链接地址: http://www.djcxy.com/p/68700.html

上一篇: 为什么这个由我的编译器生成的llvm IR是segfaulting

下一篇: 使用LLVM传递加载和存储变量