Dave Pitsbawn
2015-Mar-26 02:41 UTC
[LLVMdev] MCJIT finalizeObject output to use in external process
No, I was asking how to extract the code from MCJIT, and you said it use a custom memory manager. When you say that I must treat each section as a block, do you mean that there is inter-block relative offsets need to be maintained? Or that when I get a section, I must copy it to target process memory as a one-shot contiguous block. If it's second, I think we're ok. On Wed, Mar 25, 2015 at 5:39 PM, Kaylor, Andrew <andrew.kaylor at intel.com> wrote:> Are you asking about the actual mechanism for transferring the bits into > the remote process or how you locate the generated code in memory? > > > > The mechanism for transferring the bits is outside the scope of MCJIT. > The easiest way to locate the generate code is to use a custom memory > manager as lli does. MCJIT will call the memory manager to allocate memory > on a section-by-section basis. You should treat each section as a > monolithic block when copying to the remote system because the generated > code may depend on relative offsets staying fixed. > > > > -Andy > > > > *From:* Dave Pitsbawn [mailto:dpitsbawn at gmail.com] > *Sent:* Wednesday, March 25, 2015 5:00 PM > *To:* Kaylor, Andrew > *Cc:* LLVM Developers Mailing List > *Subject:* Re: [LLVMdev] MCJIT finalizeObject output to use in external > process > > > > Aha. Thanks. > > > > Seems like I need to call mapSectionAddress with the target address. But > how I copy the code? What function would I call? > > > > On Wed, Mar 25, 2015 at 4:32 PM, Kaylor, Andrew <andrew.kaylor at intel.com> > wrote: > > Yes, that is one of the intended use models for MCJIT. > > > > If you look at the source for ‘lli’ you’ll find an option called > “remote-mcjit” which does exactly this (for testing purposes). > > > > The key function (which in the lli case is called from lli’s > RemoteMemoryManager::notifyObjectLoaded method) is > ExecutionEngine::mapSectionAddress. This function tells MCJIT to reapply > relocations to the loaded object as if it were loaded at a different > address in memory than it actually is. The client is responsible for the > particulars of allocating memory in the remote process, determining the > address of the memory in the remote process’ address space and (after > calling mapSectionAddress) copying the JIT’d object to the remote process > memory and setting the memory attributes as needed for execution. > > > > -Andy > > > > > > *From:* llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] *On > Behalf Of *Dave Pitsbawn > *Sent:* Wednesday, March 25, 2015 4:07 PM > *To:* LLVM Developers Mailing List > *Subject:* [LLVMdev] MCJIT finalizeObject output to use in external > process > > > > A need has arisen to generate code using MCJIT but not in the target > process instead in a different process (and possibly even different machine > though not in the scope). > > > > Reading through the tutorials and MCJIT design document, it seems like > this is possible or was kept in mind during design of MCJIT. > > > > How do I achieve this? Are there examples? > > > > Dave > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150325/fbf88698/attachment.html>
Kaylor, Andrew
2015-Mar-26 19:52 UTC
[LLVMdev] MCJIT finalizeObject output to use in external process
You don’t need to put all the sections in the same memory block or maintain the same offsets that had on the host. What I meant was that each section must be in a contiguous block of memory. Depending on the memory model you specify when compiling the code you made need to guarantee that all sections are within 2GB of one another, but how you meet this requirement is at your discretion. Apart from that, MCJIT will take care of applying relocations to account for the relative location of each section in the remote process according to the addresses you provided in calls to mapSectionAddress. Another thing your custom memory manager may need to do that I forgot to mention is handle linking of external functions needed by the generated code. For in-process execution MCJIT will automatically link to things like standard library functions that it recognizes using the implementation of those functions in the host process. For out-of-process execution, your memory manager will need to perform this task. MCJIT will call your memory manager’s getSymbolAddress function if it encounters a call to an external function. -Andy From: Dave Pitsbawn [mailto:dpitsbawn at gmail.com] Sent: Wednesday, March 25, 2015 7:42 PM To: Kaylor, Andrew Cc: LLVM Developers Mailing List Subject: Re: [LLVMdev] MCJIT finalizeObject output to use in external process No, I was asking how to extract the code from MCJIT, and you said it use a custom memory manager. When you say that I must treat each section as a block, do you mean that there is inter-block relative offsets need to be maintained? Or that when I get a section, I must copy it to target process memory as a one-shot contiguous block. If it's second, I think we're ok. On Wed, Mar 25, 2015 at 5:39 PM, Kaylor, Andrew <andrew.kaylor at intel.com<mailto:andrew.kaylor at intel.com>> wrote: Are you asking about the actual mechanism for transferring the bits into the remote process or how you locate the generated code in memory? The mechanism for transferring the bits is outside the scope of MCJIT. The easiest way to locate the generate code is to use a custom memory manager as lli does. MCJIT will call the memory manager to allocate memory on a section-by-section basis. You should treat each section as a monolithic block when copying to the remote system because the generated code may depend on relative offsets staying fixed. -Andy From: Dave Pitsbawn [mailto:dpitsbawn at gmail.com<mailto:dpitsbawn at gmail.com>] Sent: Wednesday, March 25, 2015 5:00 PM To: Kaylor, Andrew Cc: LLVM Developers Mailing List Subject: Re: [LLVMdev] MCJIT finalizeObject output to use in external process Aha. Thanks. Seems like I need to call mapSectionAddress with the target address. But how I copy the code? What function would I call? On Wed, Mar 25, 2015 at 4:32 PM, Kaylor, Andrew <andrew.kaylor at intel.com<mailto:andrew.kaylor at intel.com>> wrote: Yes, that is one of the intended use models for MCJIT. If you look at the source for ‘lli’ you’ll find an option called “remote-mcjit” which does exactly this (for testing purposes). The key function (which in the lli case is called from lli’s RemoteMemoryManager::notifyObjectLoaded method) is ExecutionEngine::mapSectionAddress. This function tells MCJIT to reapply relocations to the loaded object as if it were loaded at a different address in memory than it actually is. The client is responsible for the particulars of allocating memory in the remote process, determining the address of the memory in the remote process’ address space and (after calling mapSectionAddress) copying the JIT’d object to the remote process memory and setting the memory attributes as needed for execution. -Andy From: llvmdev-bounces at cs.uiuc.edu<mailto:llvmdev-bounces at cs.uiuc.edu> [mailto:llvmdev-bounces at cs.uiuc.edu<mailto:llvmdev-bounces at cs.uiuc.edu>] On Behalf Of Dave Pitsbawn Sent: Wednesday, March 25, 2015 4:07 PM To: LLVM Developers Mailing List Subject: [LLVMdev] MCJIT finalizeObject output to use in external process A need has arisen to generate code using MCJIT but not in the target process instead in a different process (and possibly even different machine though not in the scope). Reading through the tutorials and MCJIT design document, it seems like this is possible or was kept in mind during design of MCJIT. How do I achieve this? Are there examples? Dave -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150326/d66c31f0/attachment.html>
Dave Pitsbawn
2015-Mar-27 03:30 UTC
[LLVMdev] MCJIT finalizeObject output to use in external process
Thanks, Andy. I'm now thinking about the case where the generated code has references to memory addresses in the IR (the only case I suppose is call remoteProcessADDR). I know this is not LLVM specific but what happens when that ADDR changes due to process restart or different machine. I know this is the clients responsibility to put the right address in the call instruction, but generally how will one achieve this? Do they iterate over the assembly (generated by MCJIT) and fixup calls after the fact outside of their LLVM/MCJIT infrastructure? Or another approach could be they store the optimized IR and just re-do it every single time for every single time they need an update. It seems like holding on the final output and then visiting all calls, and find/replacing them (:-)) with a new address seems most efficient Thoughts and ideas welcome. On Thu, Mar 26, 2015 at 12:52 PM, Kaylor, Andrew <andrew.kaylor at intel.com> wrote:> You don’t need to put all the sections in the same memory block or > maintain the same offsets that had on the host. What I meant was that each > section must be in a contiguous block of memory. > > > > Depending on the memory model you specify when compiling the code you made > need to guarantee that all sections are within 2GB of one another, but how > you meet this requirement is at your discretion. Apart from that, MCJIT > will take care of applying relocations to account for the relative location > of each section in the remote process according to the addresses you > provided in calls to mapSectionAddress. > > > > Another thing your custom memory manager may need to do that I forgot to > mention is handle linking of external functions needed by the generated > code. For in-process execution MCJIT will automatically link to things > like standard library functions that it recognizes using the implementation > of those functions in the host process. For out-of-process execution, your > memory manager will need to perform this task. MCJIT will call your memory > manager’s getSymbolAddress function if it encounters a call to an external > function. > > > > -Andy > > > > *From:* Dave Pitsbawn [mailto:dpitsbawn at gmail.com] > *Sent:* Wednesday, March 25, 2015 7:42 PM > > *To:* Kaylor, Andrew > *Cc:* LLVM Developers Mailing List > *Subject:* Re: [LLVMdev] MCJIT finalizeObject output to use in external > process > > > > No, I was asking how to extract the code from MCJIT, and you said it use a > custom memory manager. > > > > When you say that I must treat each section as a block, do you mean that > there is inter-block relative offsets need to be maintained? Or that when I > get a section, I must copy it to target process memory as a one-shot > contiguous block. If it's second, I think we're ok. > > > > > > On Wed, Mar 25, 2015 at 5:39 PM, Kaylor, Andrew <andrew.kaylor at intel.com> > wrote: > > Are you asking about the actual mechanism for transferring the bits into > the remote process or how you locate the generated code in memory? > > > > The mechanism for transferring the bits is outside the scope of MCJIT. > The easiest way to locate the generate code is to use a custom memory > manager as lli does. MCJIT will call the memory manager to allocate memory > on a section-by-section basis. You should treat each section as a > monolithic block when copying to the remote system because the generated > code may depend on relative offsets staying fixed. > > > > -Andy > > > > *From:* Dave Pitsbawn [mailto:dpitsbawn at gmail.com] > *Sent:* Wednesday, March 25, 2015 5:00 PM > *To:* Kaylor, Andrew > *Cc:* LLVM Developers Mailing List > *Subject:* Re: [LLVMdev] MCJIT finalizeObject output to use in external > process > > > > Aha. Thanks. > > > > Seems like I need to call mapSectionAddress with the target address. But > how I copy the code? What function would I call? > > > > On Wed, Mar 25, 2015 at 4:32 PM, Kaylor, Andrew <andrew.kaylor at intel.com> > wrote: > > Yes, that is one of the intended use models for MCJIT. > > > > If you look at the source for ‘lli’ you’ll find an option called > “remote-mcjit” which does exactly this (for testing purposes). > > > > The key function (which in the lli case is called from lli’s > RemoteMemoryManager::notifyObjectLoaded method) is > ExecutionEngine::mapSectionAddress. This function tells MCJIT to reapply > relocations to the loaded object as if it were loaded at a different > address in memory than it actually is. The client is responsible for the > particulars of allocating memory in the remote process, determining the > address of the memory in the remote process’ address space and (after > calling mapSectionAddress) copying the JIT’d object to the remote process > memory and setting the memory attributes as needed for execution. > > > > -Andy > > > > > > *From:* llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] *On > Behalf Of *Dave Pitsbawn > *Sent:* Wednesday, March 25, 2015 4:07 PM > *To:* LLVM Developers Mailing List > *Subject:* [LLVMdev] MCJIT finalizeObject output to use in external > process > > > > A need has arisen to generate code using MCJIT but not in the target > process instead in a different process (and possibly even different machine > though not in the scope). > > > > Reading through the tutorials and MCJIT design document, it seems like > this is possible or was kept in mind during design of MCJIT. > > > > How do I achieve this? Are there examples? > > > > Dave > > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150326/6f3b2cec/attachment.html>