[LLVMdev] How to free memory of JIT'd function

OKUDA Naosuke okudanaosuke at gmail.com
Thu Dec 29 10:58:30 PST 2011


Hi,

I'm testing how to free memory of a JIT'd function.
I thought ExecutionEngine::freeMachineCodeForFunction() and
Function::eraseFromParent()
would work and did a test with the following sample code.
But I found that the memory usage of the process is constantly growing as
the while loop goes.
Could someone shed light on this please?

Here is the code.

int main(int argc, char **argv) {
  InitializeNativeTarget();

  LLVMContext Context;

  Module *M = new Module("test", Context);
  ExecutionEngine* EE =
llvm::EngineBuilder(M).setEngineKind(EngineKind::JIT).create();

  while (true) {
    SMDiagnostic error;
    ParseAssemblyString("define i32 @factorial(i32 %X) nounwind uwtable {\n"
                        "  %1 = alloca i32, align 4\n"
                        "  %2 = alloca i32, align 4\n"
                        "  store i32 %X, i32* %2, align 4\n"
                        "  %3 = load i32* %2, align 4\n"
                        "  %4 = icmp eq i32 %3, 0\n"
                        "  br i1 %4, label %5, label %6\n"
                        "\n"
                        "; <label>:5
; preds = %0\n"
                        "  store i32 1, i32* %1\n"
                        "  br label %12\n"
                        "\n"
                        "; <label>:6
; preds = %0\n"
                        "  %7 = load i32* %2, align 4\n"
                        "  %8 = load i32* %2, align 4\n"
                        "  %9 = sub nsw i32 %8, 1\n"
                        "  %10 = call i32 @factorial(i32 %9)\n"
                        "  %11 = mul nsw i32 %7, %10\n"
                        "  store i32 %11, i32* %1\n"
                        "  br label %12\n"
                        "\n"
                        "; <label>:12
 ; preds = %6, %5\n"
                        "  %13 = load i32* %1\n"
                        "  ret i32 %13\n"
                        "}\n",
                        M,
                        error,
                        Context);

    Function *func = M->getFunction("factorial");
    uintptr_t tmp = (uintptr_t)(EE->getPointerToFunction(func));

    EE->freeMachineCodeForFunction(func);
    func->eraseFromParent();
  }

  delete EE;

  llvm_shutdown();
}


Thank you for any help.

Naosuke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20111229/67decb88/attachment.html>


More information about the llvm-dev mailing list