[LLVMdev] Tail calls from OCaml

Jon Harrop jon at ffconsultancy.com
Thu Dec 18 23:23:41 PST 2008


On Sunday 14 December 2008 20:37:05 Jon Harrop wrote:
> How do you get a tail call using the OCaml's LLVM API?

To answer my own question: I believe this is currently impossible with the JIT 
(execution engine) because there is no way to set PerformTailCallOpt=true in 
llvm/Target/TargetOptions.cpp from OCaml.

As a workaround, I have simply edited the source to LLVM and reinstalled it. 
Specifically, changing part of "lib/Target/TargetMachine.cpp" to:

static cl::opt<bool, true>
EnablePerformTailCallOpt("tailcallopt",
                         cl::desc("Turn on tail call optimization."),
                         cl::location(PerformTailCallOpt),
                         cl::init(true));

I assume this is set to false by default in LLVM because tail calls are not 
generally desirable, e.g. they undermine stack traces?

However, you can get tail calls in statically-compiled code by:

. Call "set_function_call_conv CallConv.fast" on the function.

. Call "set_instruction_call_conv CallConv.fast" and "set_tail_call true" on 
the call instruction.

. Emit the LLVM bitcode with "Llvm_bitwriter.write_bitcode_file m "aout.bc"".

. Use "llc -tailcallopt" when compiling the bitcode to assembler.

HTH.

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e



More information about the llvm-dev mailing list