<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
The option we use is to have a custom memory manager, override the
getPointerToNamedFunction function, and provide the pointer to the
external function at link time. The inttoptr scheme works fairly
well, but it does make for some pretty ugly and sometimes hard to
analyze IR. I recommend leaving everything symbolic until link time
if you can.<br>
<br>
Philip<br>
<br>
<div class="moz-cite-prefix">On 03/28/2016 06:33 PM, Russell Wallace
via llvm-dev wrote:<br>
</div>
<blockquote
cite="mid:CAH+nB+w9L5Xdg9=vtHQ_FKdk1ecSqCdx4TswygB++R-LpOJ2+g@mail.gmail.com"
type="cite">
<div dir="ltr">That seems to work, thanks! The specific code I
ended up with to call int64_t print(int64_t) looks like:
<div><br>
</div>
<div>
<div> auto f = builder.CreateIntToPtr(</div>
<div> ConstantInt::get(builder.getInt64Ty(),
uintptr_t(print)),</div>
<div> PointerType::getUnqual(FunctionType::get(</div>
<div> builder.getInt64Ty(),
{builder.getInt64Ty()}, false)));</div>
<div> return builder.CreateCall(f, args);</div>
</div>
<div><br>
</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Mon, Mar 28, 2016 at 1:40 PM,
Caldarale, Charles R <span dir="ltr"><<a
moz-do-not-send="true"
href="mailto:Chuck.Caldarale@unisys.com" target="_blank"><a class="moz-txt-link-abbreviated" href="mailto:Chuck.Caldarale@unisys.com">Chuck.Caldarale@unisys.com</a></a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">> From:
llvm-dev [mailto:<a moz-do-not-send="true"
href="mailto:llvm-dev-bounces@lists.llvm.org">llvm-dev-bounces@lists.llvm.org</a>]<br>
> On Behalf Of Russell Wallace via llvm-dev<br>
> Subject: [llvm-dev] JIT compiler and calls to existing
functions<br>
<div>
<div class="h5"><br>
> In the context of a JIT compiler, what's the
recommended way to generate a call to an<br>
> existing function, that is, not one that you are
generating on-the-fly with LLVM, but<br>
> one that's already linked into your program? For
example the cosine function (from the<br>
> standard math library); the Kaleidoscope tutorial
recommends looking it up by name with<br>
> dlsym("cos"), but it seems to me that it should be
possible to use a more efficient and<br>
> portable solution that takes advantage of the fact
that you already have an actual pointer<br>
> to cos, even if you haven't linked with debugging
symbols.<br>
<br>
</div>
</div>
Perhaps not the most elegant, but we simply use the
IRBuilder.CreateIntToPtr() method to construct the Callee
argument for IRBuilder.CreateCall(). The first argument for
CreateIntToPtr() comes from ConstantInt::get(I64,
uintptr_t(ptr)), while the second is a function type pointer
defined by using PointerType::get() on the result of
FunctionType::get() with the appropriate function signature.<br>
<br>
- Chuck<br>
<br>
</blockquote>
</div>
<br>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
</blockquote>
<br>
</body>
</html>