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

OKUDA Naosuke okudanaosuke at gmail.com
Wed Jan 4 17:41:54 PST 2012


Hi,

I put the sample code and a brief analysis using Valgrind to GitHub
in order to make my problem clear.
https://github.com/naosuke/how-to-free-memory-of-JIT-function
The Valgrind heap profiler indicates memory leaking but I don't get
what is wrong with the way to free memory.

If someone could please offer some advice/suggestion on this, I would
really appreciate it.

Best,
Naosuke

On Thu, Dec 29, 2011 at 6:58 PM, OKUDA Naosuke <okudanaosuke at gmail.com> wrote:
> 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
>




More information about the llvm-dev mailing list