title: LLVM JIT segfaults. What am I doing wrong?
body: <p>It is probably something basic because I am just starting to learn LLVM..</p>
  
  <p>The following creates a factorial function and tries to git and execute it (I know the generated func is correct because I was able to static compile and execute it). 
  But I get segmentation fault upon execution of the function (in EE->runFunction(TheF, Args))</p>
  
  <pre><code>#include "llvm/Module.h"
  #include "llvm/Function.h"
  #include "llvm/PassManager.h"
  #include "llvm/CallingConv.h"
  #include "llvm/Analysis/Verifier.h"
  #include "llvm/Assembly/PrintModulePass.h"
  #include "llvm/Support/IRBuilder.h"
  #include "llvm/Support/raw_ostream.h"
  #include "llvm/ExecutionEngine/JIT.h"
  #include "llvm/ExecutionEngine/GenericValue.h"
  
  
  using namespace llvm;
  
  
  Module* makeLLVMModule() {
    // Module Construction
    LLVMContext&amp; ctx = getGlobalContext();
    Module* mod = new Module("test", ctx);
    Constant* c = mod-&gt;getOrInsertFunction("fact64",
    /*ret type*/                           IntegerType::get(ctx,64),
                                           IntegerType::get(ctx,64),
    /*varargs terminated with null*/       NULL);
  
    Function* fact64 = cast&lt;Function&gt;(c);
    fact64-&gt;setCallingConv(CallingConv::C);
    /* Arg names */
    Function::arg_iterator args = fact64-&gt;arg_begin();
    Value* x = args++;
    x-&gt;setName("x");
  
  
    /* Body */
    BasicBlock* block = BasicBlock::Create(ctx, "entry", fact64);
    BasicBlock* xLessThan2Block= BasicBlock::Create(ctx, "xlst2_block", fact64);
    BasicBlock* elseBlock = BasicBlock::Create(ctx, "else_block", fact64);
    IRBuilder&lt;&gt; builder(block);
  
    Value *One = ConstantInt::get(Type::getInt64Ty(ctx), 1);
    Value *Two = ConstantInt::get(Type::getInt64Ty(ctx), 2);
  
    Value* xLessThan2 = builder.CreateICmpULT(x, Two, "tmp");
   //builder.CreateCondBr(xLessThan2, xLessThan2Block, cond_false_2);
    builder.CreateCondBr(xLessThan2, xLessThan2Block, elseBlock);
  
  
    /* Recursion */
    builder.SetInsertPoint(elseBlock);
    Value* xMinus1 = builder.CreateSub(x, One, "tmp");
    std::vector&lt;Value*&gt; args1;
    args1.push_back(xMinus1);
    Value* recur_1 = builder.CreateCall(fact64, args1.begin(), args1.end(), "tmp");
    Value* retVal = builder.CreateBinOp(Instruction::Mul, x, recur_1, "tmp");
    builder.CreateRet(retVal);
  
    /* x&lt;2 */
    builder.SetInsertPoint(xLessThan2Block);
    builder.CreateRet(One);
    return mod;
  }
  
  int main(int argc, char**argv) {
    long long x;
    if(argc &gt; 1)
      x = atol(argv[1]);
    else
      x = 4;
  
    Module* Mod = makeLLVMModule();
  
    verifyModule(*Mod, PrintMessageAction);
    PassManager PM;
    PM.add(createPrintModulePass(&amp;outs()));
    PM.run(*Mod);
  
    // Now we going to create JIT
    ExecutionEngine *EE = EngineBuilder(Mod).create();
    // Call the  function with argument x:
    std::vector&lt;GenericValue&gt; Args(1);
    Args[0].IntVal =  APInt(64, x);  
    Function* TheF = cast&lt;Function&gt;(Mod-&gt;getFunction("fact64"))  ;
  
    /* The following CRASHES.. */
    GenericValue GV = EE-&gt;runFunction(TheF, Args);
    outs() &lt;&lt; "Result: " &lt;&lt; GV.IntVal &lt;&lt; "\n";
    delete Mod;
    return 0;
  }
  </code></pre>
  
  <p><strong>Edit:</strong>
  The correct way to enable JIT (see the accepted answer below): </p>
  
  <pre><code>1.#include "llvm/ExecutionEngine/Jit.h"`
  
  2.InitializeNativeTarget();
  </code></pre>