Russell Wallace via llvm-dev
2016-Mar-29 01:33 UTC
[llvm-dev] JIT compiler and calls to existing functions
That seems to work, thanks! The specific code I ended up with to call int64_t print(int64_t) looks like: auto f = builder.CreateIntToPtr( ConstantInt::get(builder.getInt64Ty(), uintptr_t(print)), PointerType::getUnqual(FunctionType::get( builder.getInt64Ty(), {builder.getInt64Ty()}, false))); return builder.CreateCall(f, args); On Mon, Mar 28, 2016 at 1:40 PM, Caldarale, Charles R < Chuck.Caldarale at unisys.com> wrote:> > From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] > > On Behalf Of Russell Wallace via llvm-dev > > Subject: [llvm-dev] JIT compiler and calls to existing functions > > > In the context of a JIT compiler, what's the recommended way to generate > a call to an > > existing function, that is, not one that you are generating on-the-fly > with LLVM, but > > one that's already linked into your program? For example the cosine > function (from the > > standard math library); the Kaleidoscope tutorial recommends looking it > up by name with > > dlsym("cos"), but it seems to me that it should be possible to use a > more efficient and > > portable solution that takes advantage of the fact that you already have > an actual pointer > > to cos, even if you haven't linked with debugging symbols. > > 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. > > - Chuck > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160329/80353574/attachment.html>
Philip Reames via llvm-dev
2016-Mar-29 01:59 UTC
[llvm-dev] JIT compiler and calls to existing functions
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. Philip On 03/28/2016 06:33 PM, Russell Wallace via llvm-dev wrote:> That seems to work, thanks! The specific code I ended up with to call > int64_t print(int64_t) looks like: > > auto f = builder.CreateIntToPtr( > ConstantInt::get(builder.getInt64Ty(), uintptr_t(print)), > PointerType::getUnqual(FunctionType::get( > builder.getInt64Ty(), {builder.getInt64Ty()}, false))); > return builder.CreateCall(f, args); > > > On Mon, Mar 28, 2016 at 1:40 PM, Caldarale, Charles R > <Chuck.Caldarale at unisys.com <mailto:Chuck.Caldarale at unisys.com>> wrote: > > > From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org > <mailto:llvm-dev-bounces at lists.llvm.org>] > > On Behalf Of Russell Wallace via llvm-dev > > Subject: [llvm-dev] JIT compiler and calls to existing functions > > > In the context of a JIT compiler, what's the recommended way to > generate a call to an > > existing function, that is, not one that you are generating > on-the-fly with LLVM, but > > one that's already linked into your program? For example the > cosine function (from the > > standard math library); the Kaleidoscope tutorial recommends > looking it up by name with > > dlsym("cos"), but it seems to me that it should be possible to > use a more efficient and > > portable solution that takes advantage of the fact that you > already have an actual pointer > > to cos, even if you haven't linked with debugging symbols. > > 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. > > - Chuck > > > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160328/f9051fab/attachment.html>
Russell Wallace via llvm-dev
2016-Mar-29 02:25 UTC
[llvm-dev] JIT compiler and calls to existing functions
Right, but when you say link time, the JIT compiler I'm writing works the way openJDK or v8 do, it reads a script, JIT compiles it into memory and runs the code in memory without ever writing anything to disk (an option for ahead of time compilation may come later, but that will be a while down the road), so we might be doing different things? On Tue, Mar 29, 2016 at 2:59 AM, Philip Reames <listmail at philipreames.com> wrote:> 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. > > Philip > > > On 03/28/2016 06:33 PM, Russell Wallace via llvm-dev wrote: > > That seems to work, thanks! The specific code I ended up with to call > int64_t print(int64_t) looks like: > > auto f = builder.CreateIntToPtr( > ConstantInt::get(builder.getInt64Ty(), uintptr_t(print)), > PointerType::getUnqual(FunctionType::get( > builder.getInt64Ty(), {builder.getInt64Ty()}, false))); > return builder.CreateCall(f, args); > > > On Mon, Mar 28, 2016 at 1:40 PM, Caldarale, Charles R < > <Chuck.Caldarale at unisys.com>Chuck.Caldarale at unisys.com> wrote: > >> > From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] >> > On Behalf Of Russell Wallace via llvm-dev >> > Subject: [llvm-dev] JIT compiler and calls to existing functions >> >> > In the context of a JIT compiler, what's the recommended way to >> generate a call to an >> > existing function, that is, not one that you are generating on-the-fly >> with LLVM, but >> > one that's already linked into your program? For example the cosine >> function (from the >> > standard math library); the Kaleidoscope tutorial recommends looking it >> up by name with >> > dlsym("cos"), but it seems to me that it should be possible to use a >> more efficient and >> > portable solution that takes advantage of the fact that you already >> have an actual pointer >> > to cos, even if you haven't linked with debugging symbols. >> >> 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. >> >> - Chuck >> >> > > > _______________________________________________ > LLVM Developers mailing listllvm-dev at lists.llvm.orghttp://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160329/2f53f3c7/attachment.html>