[LLVMdev] IR blocks for calling function pointers

Tim Northover t.p.northover at gmail.com
Mon Mar 23 20:49:17 PDT 2015


Hi,

On 23 March 2015 at 19:04, Avinash Chiganmi <avinash.ca at gmail.com> wrote:
>   %4 = load void (i64)** @foo_ptr, align 8, !dbg !26
>   %call = call i32 %4(i32 %3), !dbg !26
>
> What LLVM function calls should be used to generate the above IR blocks?

I think the key thing you should notice in the "correct" code is that
@foo_ptr isn't actually a function. It's a generic global with type
void(i64)* (which means, as with all other LLVM globals that the
symbol @foo_ptr has type void(i64)** and you must load it to get the
actual value of the function pointer). If you look at the module where
it's defined you'll see something like

    @foo_ptr = global void(i64)* null

instead of a function definition. So you'd get a handle to it via a call like

    auto FooFunctionPtr = M.getOrInsertGlobal("foo_ptr",
PointerType::get(FunctionType::get(IRB.getVoidTy(), FooArgs, false)));

after that (and given that FooFunctionPtr is a Value with type
"void(i64)**") there's really only one sane set of operations you can
perform on it:

    auto FooFunction = IRB.getLoad(FooFunctionPtr)
    IRB.CreateCall(FooFunction, IRB.CreateLoad(LenAlloca));

Does that help?

Cheers.

Tim.



More information about the llvm-dev mailing list