[llvm-dev] creating a callinst to an external function

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 2 07:37:40 PDT 2015


On 19 August 2015 at 23:42, marwayusuf at feng.bu.edu.eg via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
>       void myclass::foo(Function *f, BasicBlock* b)
> [...]
>  call:                   call void @foo(i32 ()* @main, label %for.cond)

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.


More information about the llvm-dev mailing list