[llvm-dev] LLVMGetBitcodeModuleInContext2 problem

Toshiyasu Morita via llvm-dev llvm-dev at lists.llvm.org
Wed Mar 8 17:51:06 PST 2017


LLVMMCLinkInMCJIT() is not enough?
The current code is:

void llvm_load_IR_library(char *path)
{
        char *error;
        LLVMExecutionEngineRef engine;
        object0_t* (*func)(void), *output;
        LLVMContextRef global_context;
        LLVMMemoryBufferRef module;
        LLVMModuleRef ir_lib_module;
        bool flag;

        printf("loading IR library from path: %s\n", path);

        LLVMCreateMemoryBufferWithContentsOfFile(path, &module, &error);

        global_context = LLVMGetGlobalContext();

        flag = LLVMGetBitcodeModuleInContext2(global_context, module,
&ir_lib_module);

        printf("LLVMGetBitcodeModuleInContext2() returned %d\n", flag);

        LLVMVerifyModule(ir_lib_module, LLVMAbortProcessAction, &error);
        LLVMDumpModule(ir_lib_module);

        LLVMLinkInMCJIT();
        LLVMInitializeNativeTarget();
        LLVMInitializeNativeAsmPrinter();
        LLVMInitializeNativeAsmParser();

        if (LLVMCreateExecutionEngineForModule(&engine, ir_lib_module,
&error) != 0) {
                fprintf(stderr, "failed to create execution engine\n");
                abort();
        }

        // Call the function
        func = (object0_t * (*)(void))LLVMGetFunctionAddress(engine,
"test");
        output = (*func)();
}


On Wed, Mar 8, 2017 at 5:45 PM, Friedman, Eli <efriedma at codeaurora.org>
wrote:

> You're going to have to read the C++ headers for a lot of things to make
> sense; the C headers don't really have much documentation.
>
> In this case:
>
>   /// getFunctionAddress - Return the address of the specified function.
>   /// This may involve code generation.
>   virtual uint64_t getFunctionAddress(const std::string &Name) {
>     // Default implementation for the interpreter.  MCJIT will override
> this.
>     // Interpreter clients should use getPointerToFunction instead.
>     return 0;
>   }
>
> I would suggest using MCJIT (LLVMCreateMCJITCompilerForModule).
>
> -Eli
>
>
> On 3/8/2017 5:25 PM, Toshiyasu Morita wrote:
>
> Oops, missed initializing some stuff. Added:
>
>         LLVMLinkInMCJIT();
>         LLVMInitializeNativeTarget();
>         LLVMInitializeNativeAsmPrinter();
>         LLVMInitializeNativeAsmParser();
>
> Now it crashes in LLVMGetFunctionAddress().
>
> Hmm.
>
>
> On Wed, Mar 8, 2017 at 5:14 PM, Toshiyasu Morita <toshi at tensyr.com> wrote:
>
>> Made it a bit further. Here's the current code:
>>
>> void llvm_load_IR_library(char *path)
>> {
>>         char *error;
>>         LLVMExecutionEngineRef engine;
>>         object0_t* (*func)(void), *output;
>>         LLVMContextRef global_context;
>>         LLVMMemoryBufferRef module;
>>         LLVMModuleRef ir_lib_module;
>>         bool flag;
>>
>>         printf("loading IR library from path: %s\n", path);
>>
>>         LLVMCreateMemoryBufferWithContentsOfFile(path, &module, &error);
>>
>>         global_context = LLVMGetGlobalContext();
>>
>>         flag = LLVMGetBitcodeModuleInContext2(global_context, module,
>> &ir_lib_module);
>>
>>         printf("LLVMGetBitcodeModuleInContext2() returned %d\n", flag);
>>
>>         LLVMVerifyModule(ir_lib_module, LLVMAbortProcessAction, &error);
>>         LLVMDumpModule(ir_lib_module);
>>
>>         if (LLVMCreateExecutionEngineForModule(&engine, ir_lib_module,
>> &error) != 0) {
>>                 fprintf(stderr, "failed to create execution engine\n");
>>                 abort();
>>         }
>>
>>         // Call the function
>>         func = (object0_t * (*)(void))LLVMGetFunctionAddress(engine,
>> "test");
>>         output = (*func)();
>> }
>>
>> when this code is executed, i see:
>>
>> ...
>> LLVMGetBitcodeModuleInContext2() returned 0
>> ; ModuleID = '/home/toshi/tensyr/debug_build/test.bc'
>> source_filename = "test.c"
>> target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
>> target triple = "x86_64-unknown-linux-gnu"
>>
>> @str = private unnamed_addr constant [16 x i8] c"test successful\00"
>>
>> ; Materializable
>> ; Function Attrs: nounwind uwtable
>> define void @test() local_unnamed_addr #0 {}
>>
>> ; Function Attrs: nounwind
>> declare i32 @puts(i8* nocapture readonly) #1
>>
>> attributes #0 = { nounwind uwtable "disable-tail-calls"="false"
>> "less-precise-fpmad"="false" "no-frame-pointer-elim"="false"
>> "no-infs-fp-math"="false" "no-jump-tables"="false"
>> "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false"
>> "stack-protector-buffer-size"="8" "target-cpu"="x86-64"
>> "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false"
>> "use-soft-float"="false" }
>> attributes #1 = { nounwind }
>>
>> !llvm.ident = !{!0}
>>
>> !0 = !{!"clang version 3.9.1 (tags/RELEASE_391/final)"}
>> failed to create execution engine
>> Aborted (core dumped)
>>
>> Not sure why LLVMCreateExecutionEngineForModule is failing.
>> Any help appreciated.
>>
>> Toshi
>>
>>
>> On Wed, Mar 8, 2017 at 3:56 PM, Toshiyasu Morita <toshi at tensyr.com>
>> wrote:
>>
>>> Or do you mean I need to load the module into memory before calling
>>> LLVMGetBitcodeModuleInContext2?
>>>
>>>
>>> > Yes, you need to load the module into memory first.
>>> > LLVMCreateMemoryBufferWithContentsOfFile will do that for you.
>>>
>>> Thanks!
>>>
>>>
>>> On Wed, Mar 8, 2017 at 3:48 PM, Friedman, Eli <efriedma at codeaurora.org>
>>> wrote:
>>>
>>>> On 3/8/2017 3:44 PM, Toshiyasu Morita wrote:
>>>>
>>>>
>>>>>         module_path = LLVMCreateMemoryBufferWithMemoryRange(path,
>>>>> strlen(path), "path", 1);
>>>>>
>>>>
>>>> LLVMCreateMemoryBufferWithContentsOfFile takes a path.
>>>>
>>>> Erm...no...the code is calling LLVMCreateMemoryBufferWithMemoryRange,
>>>> not LLVMCreateMemoryBufferWithContentsOfFile...
>>>>
>>>> Or do you mean I need to load the module into memory before calling
>>>> LLVMGetBitcodeModuleInContext2?
>>>>
>>>>
>>>> Yes, you need to load the module into memory first.
>>>> LLVMCreateMemoryBufferWithContentsOfFile will do that for you.
>>>>
>>>> -Eli
>>>>
>>>> --
>>>> Employee of Qualcomm Innovation Center, Inc.
>>>> Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
>>>>
>>>>
>>>
>>
>
>
> --
> Employee of Qualcomm Innovation Center, Inc.
> Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170308/683aa46c/attachment-0001.html>


More information about the llvm-dev mailing list