Dear All
I'm making an instrumentation pass. The pass is supposed to modify the given IR in a specefic way. One of the required modifications is to insert a call to a function at a specific location.
This is the signature of the called function:
void myclass::foo(Function *f, BasicBlock* b)
This function's prototype is in an foofile.h file in include/llvm
And the function definition is in foofile.cpp file in the MCJIT folder.
and running "make" at this folder works fine and the foofile.cpp is compiled with MCJIT.cpp and another function in the same file works just fine as expected.
Not back to the instrumentation pass. How can I insert a callinst to the foo function in the given IR?
Here is the snippet that inserts the call:
Type* retTy = Type::getInt32Ty(C);
FunctionType* FuncTy = FunctionType::get(retTy, false);
PointerType* PtrToFuncTy = PointerType::get(FuncTy, 0);
Constant *fun = M->getOrInsertFunction("foo", Type::getVoidTy(C), PtrToFuncTy, Type::getLabelTy(C), nullptr);
Function *dofoo = cast<Function>(fun);
Instruction* dofooCall = CallInst::Create(fun, Args2, "", bb);
Note: Args2 is an arraylist containing 2 value pointers to a function and a basicblock, bb is the basicblock to insert the call in.
When I run the pass using op on a given IR, it produces a declaration and a call correctly like this:
declaration: declare void @foo(i32 ()*, label)
call: call void @foo(i32 ()* @main, label %for.cond)
But when I try to run the resulting .ll file using lli, everything explodes! This the first 2 lines before the stacktrace:
Can't get register for value!
UNREACHABLE executed at /home/marwayusuf/llvm-env/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:1158!
I concluded that the problem is it can't find the foo function. If this is the problem, how can I create the callinst correctly?
I tried to remove parameters from the called function "foo". So I got a different error.
LLVM ERROR: Program used external function 'foo' which could not be resolved!
Any help?
_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
The problem here is that a "Function *" isn't simply a function
pointer, or even a function pointer at all. It's a compile-time
representation of some function. Similar comments apply to BasicBlock.
I'm quite surprised it even reached SelectionDAGBuilder before blowing
up.
That information is not normally available at runtime, though in
principle you could get one in there if you were JITing. If you know
where the Function lives when you're generating the IR (and can
guarantee it won't move) you could aim for something like:
call void @foo(i8* inttoptr(i64 0x123450 to i8*), inttoptr(i64 ...))
Something like:
Function *Main = ...;
Value *Arg = Constant::CreateIntToPtr(reinterpret_cast<uint64_t>(Main));
I.e. instead of using Main as a Value in the call expression,
completely hide the fact that it's an LLVM object from the compiler
and pass it in as an opaque integer. Also, don't forget the "this"
pointer if "foo" is not a static method.
Cheers.
Tim.
Thanks a million!
I've finally went to the object file containing foo function and using objdump tool I found the exact used name in its symbol table. Using this mangled name in my pass, solved the resolve issue and my foo function is finally called correctly.
Regards,
Marwa Yusuf
Teaching Assistant - Computer Engineering Department
Faculty of Engineering - Benha University
E-JUST PhD Student
Computer Science & Engineering Dept.
________________________________________
From: Tim Northover <t.p.no...@gmail.com>
Sent: Wednesday, September 2, 2015 4:37 PM
To: marwa...@feng.bu.edu.eg
Cc: llvm...@lists.llvm.org
Subject: Re: [llvm-dev] creating a callinst to an external function
On 19 August 2015 at 23:42, marwa...@feng.bu.edu.eg via llvm-dev
Regards,
Marwa Yusuf
Teaching Assistant - Computer Engineering Department
Faculty of Engineering - Benha University
E-JUST PhD Student
Computer Science & Engineering Dept.
________________________________________
From: Tim Northover <t.p.no...@gmail.com>
Sent: Wednesday, September 2, 2015 4:37 PM
To: marwa...@feng.bu.edu.eg
Cc: llvm...@lists.llvm.org
Subject: Re: [llvm-dev] creating a callinst to an external function
On 19 August 2015 at 23:42, marwa...@feng.bu.edu.eg via llvm-dev