Tail Call Optimization


  I started off trying to test HLVM
(http://www.ffconsultancy.com/ocaml/hlvm/) in a freshly compiled
and found a few errors have crept into LLVM between revisions 2.7 and 2.8.
System info:
2.6.31-23-generic #75-Ubuntu SMP x86_64 GNU/Linux, oprofile version

HLVM uses the following code to set "guaranteed" tail call optimization:
#include "llvm/ADT/StringRef.h" <!-- had to be added, since
TargetOptions references it - as noted already in the list. -->

#include "llvm/Target/TargetOptions.h"
#include <iostream>

extern "C" {
  using namespace std;

  void enable_tail_call_opt() {
    cout << "Enabling TCO" << endl;
    //llvm::PerformTailCallOpt = true; <-- pre-2.7 name
    llvm::GuaranteedTailCallOpt = true;


Compiling 3.0 (but not 2.8 or earlier) I also had to run:
touch llvm-3.0.src/bindings/ocaml/llvm/Release/META.llvm
to passify make install, since it tried to install metadata, but
didn't have any.

  Trouble was that just about all test cases would cause "Stack
overflow" during jit execution -
making me suspect a problem with tail call optimization in LLVM.
Stack overflows didn't happen immediately in 3.0, but happened almost
immediately in 2.8.
Sure enough, reverting to LLVM2.7 fixed the issue and no overflow was observed.

  A more informed failure case for tail call optimization
was posted a year ago, but never answered. I think correctness of the
3.0 and current
implementations of "GuaranteedTailCallOpt" requires re-examination.

~ Dr. David M. Rogers
  Nanobiology Department
  Sandia National Labs

I found the following patch made my life easier...

diff -r d9c523bb68e7 -r 4052755f0c5b bindings/ocaml/llvm/Makefile
--- a/bindings/ocaml/llvm/Makefile Wed Mar 07 15:25:01 2012 -0800
+++ b/bindings/ocaml/llvm/Makefile Wed Mar 07 21:38:56 2012 -0800
@@ -27,7 +27,7 @@
# Easy way of generating META in the objdir
copy-meta: $(OcamlDir)/META.llvm

-$(OcamlDir)/META.llvm: META.llvm
+$(ObjDir)/META.llvm: META.llvm
        $(Verb) $(CP) -f $< $@

install-meta:: $(ObjDir)/META.llvm