Thanks for the tip - getting closer: $ ./capi_test 5 6 args[0]: 5 args[1]: 6 result: 4294959200 Here's the code I changed: printf("args[0]: %d\n", (int)LLVMGenericValueToInt(args[0], 0)); printf("args[1]: %d\n", (int)LLVMGenericValueToInt(args[1], 0)); uint64_t (*func)(); func = (uint64_t (*)())LLVMGetFunctionAddress(engine, "sum"); printf("result: %lu\n", (*func)()); Anything else I should look at? Toshi On Wed, Jan 25, 2017 at 5:20 PM, Andres Freund <andres at anarazel.de> wrote:> Hi, > > On 2017-01-25 15:17:04 -0800, Toshiyasu Morita via llvm-dev wrote: > > long long x = strtoll(argv[1], NULL, 10); > > long long y = strtoll(argv[2], NULL, 10); > > > > LLVMGenericValueRef args[] > > {LLVMCreateGenericValueOfInt(LLVMInt32Type(), x, 0), > > > > LLVMCreateGenericValueOfInt(LLVMInt32Type(), y, 0)}; > > > > printf("args[0]: %d\n", (int)LLVMGenericValueToInt(args[0], 0)); > > printf("args[1]: %d\n", (int)LLVMGenericValueToInt(args[1], 0)); > > > > LLVMGenericValueRef res = LLVMRunFunction(engine, sum, 2, args); > > > > printf("result: %d\n", (int)LLVMGenericValueToInt(res, 0)); > > > It seems to fail to add, and always returns the first argument. > > Any help greatly appreciated. > > I don't think LLVMGenericValueToInt / LLVMRunFunction really work with > mcjit. I'd bet you'd see better results if you'd use > LLVMGetFunctionAddress, cast the returned pointer to the correc type, > and then call the function via that. > > Greetings, > > Andres Freund >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170125/f2bf1adf/attachment.html>
Hi, On 2017-01-25 18:16:09 -0800, Toshiyasu Morita wrote:> Thanks for the tip - getting closer: > > $ ./capi_test 5 6 > args[0]: 5 > args[1]: 6 > result: 4294959200 > > Here's the code I changed: > > printf("args[0]: %d\n", (int)LLVMGenericValueToInt(args[0], 0)); > printf("args[1]: %d\n", (int)LLVMGenericValueToInt(args[1], 0)); > > uint64_t (*func)(); > func = (uint64_t (*)())LLVMGetFunctionAddress(engine, "sum"); > printf("result: %lu\n", (*func)());You're calling the function without arguments, but it's (in IR) declared to take two. And you're miscasting the return value to be int64, instead of int32 as the function's declared. Andres
Andres Freund wrote:> On 2017-01-25 18:16:09 -0800, Toshiyasu Morita wrote: >> Thanks for the tip - getting closer: >> >> $ ./capi_test 5 6 >> args[0]: 5 >> args[1]: 6 >> result: 4294959200 >> >> Here's the code I changed: >> >> printf("args[0]: %d\n", (int)LLVMGenericValueToInt(args[0], 0)); >> printf("args[1]: %d\n", (int)LLVMGenericValueToInt(args[1], 0)); >> >> uint64_t (*func)(); >> func = (uint64_t (*)())LLVMGetFunctionAddress(engine, "sum"); >> printf("result: %lu\n", (*func)()); > >You're calling the function without arguments, but it's (in IR) declared >to take two. And you're miscasting the return value to be int64, instead >of int32 as the function's declared.Thanks again - revised code snippet: int (*func)(LLVMGenericValueRef *); func = (int (*)(LLVMGenericValueRef *))LLVMGetFunctionAddress(engine, "sum"); int res = (*func)(args); printf("result: %d\n", res); Output: $ ./capi_test 5 6 args[0]: 5 args[1]: 6 result: -7872 Do the two argument pointers need to be passed separately? Does the function return an LLVMGenericValueRef? Toshi On Wed, Jan 25, 2017 at 6:30 PM, Andres Freund <andres at anarazel.de> wrote:> Hi, > > On 2017-01-25 18:16:09 -0800, Toshiyasu Morita wrote: > > Thanks for the tip - getting closer: > > > > $ ./capi_test 5 6 > > args[0]: 5 > > args[1]: 6 > > result: 4294959200 > > > > Here's the code I changed: > > > > printf("args[0]: %d\n", (int)LLVMGenericValueToInt(args[0], 0)); > > printf("args[1]: %d\n", (int)LLVMGenericValueToInt(args[1], 0)); > > > > uint64_t (*func)(); > > func = (uint64_t (*)())LLVMGetFunctionAddress(engine, "sum"); > > printf("result: %lu\n", (*func)()); > > You're calling the function without arguments, but it's (in IR) declared > to take two. And you're miscasting the return value to be int64, instead > of int32 as the function's declared. > > Andres >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170125/ebe70d6f/attachment.html>