summaryrefslogtreecommitdiff
path: root/llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp
AgeCommit message (Collapse)Author
2025-03-14[ORC] Make runAllocActions and runDeallocActions asynchorous.Lang Hames
Part of ongoing work to make core ORC operations asynchronous. This only affects the runner utilities, not the AllocationActions themselves. The AllocationActions will be made asynchronous in a future patch.
2025-01-14[JITLink] Use target triple for LinkGraph pointer size and endianness.Lang Hames
Removes LinkGraph's PointerSize and Endianness members and uses the triple to find these values instead. Also removes some redundant Triple copies.
2024-12-06[JITLink] Switch to SymbolStringPtr for Symbol names (#115796)Jared Wyles
Use SymbolStringPtr for Symbol names in LinkGraph. This reduces string interning on the boundary between JITLink and ORC, and allows pointer comparisons (rather than string comparisons) between Symbol names. This should improve the performance and readability of code that bridges between JITLink and ORC (e.g. ObjectLinkingLayer and ObjectLinkingLayer::Plugins). To enable use of SymbolStringPtr a std::shared_ptr<SymbolStringPool> is added to LinkGraph and threaded through to its construction sites in LLVM and Bolt. All LinkGraphs that are to have symbol names compared by pointer equality must point to the same SymbolStringPool instance, which in ORC sessions should be the pool attached to the ExecutionSession. --------- Co-authored-by: Lang Hames <lhames@gmail.com>
2024-08-28[ORC] Generalize loadRelocatableObject to loadLinkableFile, add archive support.Lang Hames
This allows us to rewrite part of StaticLibraryDefinitionGenerator in terms of loadLinkableFile. It's also useful for clients who may not know (either from file extensions or context) whether a given path will be an object file, an archive, or a universal binary. rdar://134638070
2024-02-16[JITLink] Always unmap standard segments in ↵Min-Yih Hsu
InProcessMemoryManager::deallocate (#81943) Right now InProcessMemoryManager only releases a standard segment (via sys::Memory::releaseMappedMemory) in `deallocate` when there is a DeallocAction associated, leaving residual memory pages in the process until termination. Despite being a de facto memory leak, it won't cause a major issue if users only create a single LLJIT instance per process, which is the most common use cases. It will, however, drain virtual memory pages if we create thousands of ephemeral LLJIT instances in the same process. This patch fixes this issue by releasing every standard segments regardless of the attached DeallocAction.
2023-10-12Use llvm::endianness::{big,little,native} (NFC)Kazu Hirata
Note that llvm::support::endianness has been renamed to llvm::endianness while becoming an enum class as opposed to an enum. This patch replaces support::{big,little,native} with llvm::endianness::{big,little,native}.
2023-09-28[ORC] Rename MemLifetimePolicy to MemLifetime.Lang Hames
The *Policy suffix came from the earlier MemAllocPolicy type, where it was included to distinguish the type from a memory-allocation operation. MemLifetime is a noun already, so the *Policy suffix is just dead weight now.
2023-03-18Re-apply "[JITLink][ORC] Rename MemDeallocPolicy to MemLifetime..." with fixes.Lang Hames
This reapplies 2cc64df0bd6a802eab592dbc282463c3e4a4281c, which was reverted in 5379c46d490640bfa80283e00240b6f1006092b4 due to bot failures. The new patch contains fixes to ELFLinkGraphBuilder.h to better handle non-SHT_ALLOC sections (these were being accidentally skipped in the previous patch), and to skip SHT_NULL sections.
2023-03-17Revert "[JITLink][ORC] Rename MemDeallocPolicy to MemLifetimePolicy, add ..."Lang Hames
This reverts commit 2cc64df0bd6a802eab592dbc282463c3e4a4281c while I investigate bot failures (e.g. https://lab.llvm.org/buildbot/#/builders/3/builds/23081).
2023-03-17[JITLink][ORC] Rename MemDeallocPolicy to MemLifetimePolicy, add NoAlloc option.Lang Hames
The original MemDeallocPolicy had two options: * Standard: allocated memory lives until deallocated or abandoned. * Finalize: allocated memory lives until all finalize actions have been run, then is destroyed. This patch introduces a new 'NoAlloc' option. NoAlloc indicates that the section should be ignored by the JITLinkMemoryManager -- the memory manager should allocate neither working memory nor executor address space to blocks in NoAlloc sections. The NoAlloc option is intended to support metadata sections (e.g. debug info) that we want to keep in the graph and have fixed up if necessary, but don't want allocated or transmitted to the executor (or we want that allocation and transmission to be managed manually by plugins). Since NoAlloc blocks are ignored by the JITLinkMemoryManager they will not have working memory allocated to them by default post-allocation. Clients wishing to modify the content of a block in a NoAlloc section should call `Block::getMutableMemory(LinkGraph&)` to get writable memory allocated on the LinkGraph's allocator (this memory will exist for the lifetime of the graph). If no client requests mutable memory prior to the fixup phase then the generic link algorithm will do so when it encounters the first edge in any given block. Addresses of blocks in NoAlloc sections are initialized by the LinkGraph creator (a LinkGraphBuilder, if the graph is generated from an object file), and should not be modified by the JITLinkMemoryManager. Plugins are responsible for updating addresses if they add/remove content from these sections. The meaning of addresses in NoAlloc-sections is backend/plugin defined, but for fixup purposes they will be treated the same as addresses in Standard/Finalize sections. References from Standard/Finalize sections to NoAlloc sections are expected to be common (these represent metadata tracking executor addresses). References from NoAlloc sections to Standard/Finalize sections are expected to be rare/non-existent (they would represent JIT'd code / data tracking metadata in the controller, which would be surprising). LinkGraphBuilders and specific backends may impose additional constraints on edges between Standard/Finalize and NoAlloc sections where required for correctness. Differential Revision: https://reviews.llvm.org/D146183
2023-02-01[JITLink] Ensure that in-flight alloc is abandoned on error in post-alloc phase.Lang Hames
If an error occurs during the post-allocation phase (JITLinkerBase::linkPhase2) then we need to call JITLinkMemoryManager::InFlightAlloc::abandon so that the allocation has a chance to clean up. This was already handled for later phases, but we were skipping the abandon step when we bailed out of phase 2.
2022-10-03[ORC][JITLink] Move MemoryFlags.h (MemProt, AllocGroup,...) from JITLink to ORC.Lang Hames
Moving these types to OrcShared eliminates the need for the separate WireProtectionFlags type.
2022-09-12[llvm] Use x.empty() instead of llvm::empty(x) (NFC)Kazu Hirata
I'm planning to deprecate and eventually remove llvm::empty. I thought about replacing llvm::empty(x) with std::empty(x), but it turns out that all uses can be converted to x.empty(). That is, no use requires the ability of std::empty to accept C arrays and std::initializer_list. Differential Revision: https://reviews.llvm.org/D133677
2022-02-06[llvm] Use = default (NFC)Kazu Hirata
2022-01-10[ORC] Add helper functions for running finalize / dealloc actions.Lang Hames
runFinalizeActions takes an AllocActions vector and attempts to run its finalize actions. If any finalize action fails then all paired dealloc actions up to the failing pair are run, and the error(s) returned. If all finalize actions succeed then a vector containing the dealloc actions is returned. runDeallocActions takes a vector<WrapperFunctionCall> containing dealloc action calls and runs them all, returning any error(s). These helpers are intended to simplify the implementation of JITLinkMemoryManager::InFlightAlloc::finalize and JITLinkMemoryManager::deallocate overrides by taking care of execution (and potential roll-back) of allocation actions.
2022-01-08[ORC][JITLink] Merge JITLink AllocActionCall and ORC WrapperFunctionCall.Lang Hames
These types performed identical roles. Merging them simplifies interoperability between JITLink and ORC APIs (allowing us to address a few FIXMEs).
2022-01-06Re-apply "[JITLink] Update JITLink to use ExecutorAddr rather... " with fixes.Lang Hames
This re-applies 133f86e95492b2a00b944e070878424cfa73f87c, which was reverted in c5965a411c635106a47738b8d2e24db822b7416f while I investigated bot failures. The original failure contained an arithmetic conversion think-o (on line 419 of EHFrameSupport.cpp) that could cause failures on 32-bit platforms. The issue should be fixed in this patch.
2022-01-06Revert "[JITLink] Update JITLink to use ExecutorAddr rather than..."Lang Hames
This reverts commit 133f86e95492b2a00b944e070878424cfa73f87c while I investigate the bot failures at https://lab.llvm.org/buildbot#builders/186/builds/3370.
2022-01-06[JITLink] Update JITLink to use ExecutorAddr rather than JITTargetAddress.Lang Hames
ExecutorAddr is the preferred representation for executor process addresses now.
2022-01-03[JITLink] Move AllocActions and associated types out of JITLinkMemoryManager.Lang Hames
They're shared with LinkGraph, so having them as top-level types makes sense, and saves users from qualifying the names everywhere.
2021-11-14Reapply "[ORC] Initial MachO debugging support (via GDB JIT debug.." with fixes.Lang Hames
This reapplies e1933a0488a50eb939210808fc895d374570d891 (which was reverted in f55ba3525eb19baed7d3f23638cbbd880246a370 due to bot failures, e.g. https://lab.llvm.org/buildbot/#/builders/117/builds/2768). The bot failures were due to a missing symbol error: We use the input object's mangling to decide how to mangle the debug-info registration function name. This caused lookup of the registration function to fail when the input object mangling didn't match the host mangling. Disbaling the test on non-Darwin platforms is the easiest short-term solution. I have filed https://llvm.org/PR52503 with a proposed longer term solution.
2021-11-14Revert "[ORC] Initial MachO debugging support (via GDB JIT debug..."Lang Hames
This reverts commit e1933a0488a50eb939210808fc895d374570d891 until I can look into bot failures.
2021-11-13[ORC] Initial MachO debugging support (via GDB JIT debug registration interface)Lang Hames
This commit adds a new plugin, GDBJITDebugInfoRegistrationPlugin, that checks for objects containing debug info and registers any debug info found via the GDB JIT registration API. To enable this registration without redundantly representing non-debug sections this plugin synthesizes a new embedded object within a section of the LinkGraph. An allocation action is used to make the registration call. Currently MachO only. ELF users can still use the DebugObjectManagerPlugin. The two are likely to be merged in the near future.
2021-11-12[JITLink] Fix think-o in handwritten CWrapperFunctionResult -> Error converter.Lang Hames
We need to skip the length field when generating error strings. No test case: This hand-hacked deserializer should be removed in the near future once JITLink can use generic ORC APIs (including SPS and WrapperFunction).
2021-10-31[JITLink] Fix alloc action call signature in InProcessMemoryManager.Lang Hames
Alloc actions should return a CWrapperFunctionResult. JITLink does not have access to this type yet, due to library layering issues, so add a cut-down version with a fixme.
2021-10-11[ORC] Fix a typo in a variable name.Lang Hames
2021-10-11Re-apply e50aea58d59, "Major JITLinkMemoryManager refactor". with fixes.Lang Hames
Adds explicit narrowing casts to JITLinkMemoryManager.cpp. Honors -slab-address option in llvm-jitlink.cpp, which was accidentally dropped in the refactor. This effectively reverts commit 6641d29b70993bce6dbd7e0e0f1040753d38842f.
2021-10-11Revert "[JITLink][ORC] Major JITLinkMemoryManager refactor."Lang Hames
This reverts commit e50aea58d59c8cfae807a7fee21c4227472c0678 while I investigate bot failures.
2021-10-11[JITLink][ORC] Major JITLinkMemoryManager refactor.Lang Hames
This commit substantially refactors the JITLinkMemoryManager API to: (1) add asynchronous versions of key operations, (2) give memory manager implementations full control over link graph address layout, (3) enable more efficient tracking of allocated memory, and (4) support "allocation actions" and finalize-lifetime memory. Together these changes provide a more usable API, and enable more powerful and efficient memory manager implementations. To support these changes the JITLinkMemoryManager::Allocation inner class has been split into two new classes: InFlightAllocation, and FinalizedAllocation. The allocate method returns an InFlightAllocation that tracks memory (both working and executor memory) prior to finalization. The finalize method returns a FinalizedAllocation object, and the InFlightAllocation is discarded. Breaking Allocation into InFlightAllocation and FinalizedAllocation allows InFlightAllocation subclassses to be written more naturally, and FinalizedAlloc to be implemented and used efficiently (see (3) below). In addition to the memory manager changes this commit also introduces a new MemProt type to represent memory protections (MemProt replaces use of sys::Memory::ProtectionFlags in JITLink), and a new MemDeallocPolicy type that can be used to indicate when a section should be deallocated (see (4) below). Plugin/pass writers who were using sys::Memory::ProtectionFlags will have to switch to MemProt -- this should be straightworward. Clients with out-of-tree memory managers will need to update their implementations. Clients using in-tree memory managers should mostly be able to ignore it. Major features: (1) More asynchrony: The allocate and deallocate methods are now asynchronous by default, with synchronous convenience wrappers supplied. The asynchronous versions allow clients (including JITLink) to request and deallocate memory without blocking. (2) Improved control over graph address layout: Instead of a SegmentRequestMap, JITLinkMemoryManager::allocate now takes a reference to the LinkGraph to be allocated. The memory manager is responsible for calculating the memory requirements for the graph, and laying out the graph (setting working and executor memory addresses) within the allocated memory. This gives memory managers full control over JIT'd memory layout. For clients that don't need or want this degree of control the new "BasicLayout" utility can be used to get a segment-based view of the graph, similar to the one provided by SegmentRequestMap. Once segment addresses are assigned the BasicLayout::apply method can be used to automatically lay out the graph. (3) Efficient tracking of allocated memory. The FinalizedAlloc type is a wrapper for an ExecutorAddr and requires only 64-bits to store in the controller. The meaning of the address held by the FinalizedAlloc is left up to the memory manager implementation, but the FinalizedAlloc type enforces a requirement that deallocate be called on any non-default values prior to destruction. The deallocate method takes a vector<FinalizedAlloc>, allowing for bulk deallocation of many allocations in a single call. Memory manager implementations will typically store the address of some allocation metadata in the executor in the FinalizedAlloc, as holding this metadata in the executor is often cheaper and may allow for clean deallocation even in failure cases where the connection with the controller is lost. (4) Support for "allocation actions" and finalize-lifetime memory. Allocation actions are pairs (finalize_act, deallocate_act) of JITTargetAddress triples (fn, arg_buffer_addr, arg_buffer_size), that can be attached to a finalize request. At finalization time, after memory protections have been applied, each of the "finalize_act" elements will be called in order (skipping any elements whose fn value is zero) as ((char*(*)(const char *, size_t))fn)((const char *)arg_buffer_addr, (size_t)arg_buffer_size); At deallocation time the deallocate elements will be run in reverse order (again skipping any elements where fn is zero). The returned char * should be null to indicate success, or a non-null heap-allocated string error message to indicate failure. These actions allow finalization and deallocation to be extended to include operations like registering and deregistering eh-frames, TLS sections, initializer and deinitializers, and language metadata sections. Previously these operations required separate callWrapper invocations. Compared to callWrapper invocations, actions require no extra IPC/RPC, reducing costs and eliminating a potential source of errors. Finalize lifetime memory can be used to support finalize actions: Sections with finalize lifetime should be destroyed by memory managers immediately after finalization actions have been run. Finalize memory can be used to support finalize actions (e.g. with extra-metadata, or synthesized finalize actions) without incurring permanent memory overhead.
2021-08-11[llvm][clang][NFC] updates inline licence infoChristopher Di Bella
Some files still contained the old University of Illinois Open Source Licence header. This patch replaces that with the Apache 2 with LLVM Exception licence. Differential Revision: https://reviews.llvm.org/D107528
2021-03-09[JITLink] Assert that segment mapping does not exceed allocation size.Lang Hames
2021-03-05[JITLink] Fix Wtype-limits gcc warning (NFC)Yang Fan
GCC warning: ``` In file included from /usr/include/c++/9/cassert:44, from /home/vsts/work/1/llvm-project/llvm/include/llvm/ADT/BitVector.h:21, from /home/vsts/work/1/llvm-project/llvm/include/llvm/Support/Program.h:17, from /home/vsts/work/1/llvm-project/llvm/include/llvm/Support/Process.h:32, from /home/vsts/work/1/llvm-project/llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp:11: /home/vsts/work/1/llvm-project/llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp: In member function ‘virtual llvm::Expected<std::unique_ptr<llvm::jitlink::JITLinkMemoryManager::Allocation> > llvm::jitlink::InProcessMemoryManager::allocate(const llvm::jitlink::JITLinkDylib*, const SegmentsRequestMap&)’: /home/vsts/work/1/llvm-project/llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp:129:40: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits] 129 | assert(SlabRemaining.allocatedSize() >= 0 && "Mapping exceeds allocation"); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~ ``` The return type of `allocatedSize()` is `size_t`, thus the expression `SlabRemaining.allocatedSize() >= 0` always evaluate to `true`.
2021-03-02[Orc] Fix remaining memory size of slab allocatorStefan Gränitz
2020-12-14Re-apply 8904ee8ac7e with missing header included this time.Lang Hames
2020-12-13Revert "[JITLink] Add JITLinkDylib type, thread through JITLinkMemoryManager ↵Nico Weber
APIs." This reverts commit 8904ee8ac7ebcc50a60de0914abc6862e28b6664. Didn't `git add` llvm/ExecutionEngine/JITLink/JITLinkDylib.h and hence doesn't build anywhere.
2020-12-14[JITLink] Add JITLinkDylib type, thread through JITLinkMemoryManager APIs.Lang Hames
JITLinkDylib represents a target dylib for a JITLink link. By representing this explicitly we can: - Enable JITLinkMemoryManagers to manage allocations on a per-dylib basis (e.g by maintaining a seperate allocation pool for each JITLinkDylib). - Enable new features and diagnostics that require information about the target dylib (not implemented in this patch).
2020-03-03[JITLink] Fix a pointer-to-integer cast in jitlink::InProcessMemoryManager.Lang Hames
reinterpret_cast'ing the block base address directly to a uint64_t leaves the high bits in an implementation-defined state, but JITLink expects them to be zero. Switching to pointerToJITTargetAddress for the cast should fix this. This should fix the jitlink test failures that we have seen on some of the 32-bit testers.
2019-10-15[JITLink] Switch to slab allocation for InProcessMemoryManager, re-enable test.Lang Hames
InProcessMemoryManager used to make separate memory allocation calls for each permission level (RW, RX, RO), which could lead to target-out-of-range errors if data and code were placed too far apart (this was the source of failures in the JITLink/AArch64 testcase when it was first landed). This patch updates InProcessMemoryManager to allocate a single slab which is subdivided between text and data. This should guarantee that accesses remain in-range provided that individual object files do not exceed 1Mb in size. This patch also re-enables the JITLink/AArch64 testcase. llvm-svn: 374948
2019-10-04[JITLink] Switch from an atom-based model to a "blocks and symbols" model.Lang Hames
In the Atom model the symbols, content and relocations of a relocatable object file are represented as a graph of atoms, where each Atom represents a contiguous block of content with a single name (or no name at all if the content is anonymous), and where edges between Atoms represent relocations. If more than one symbol is associated with a contiguous block of content then the content is broken into multiple atoms and layout constraints (represented by edges) are introduced to ensure that the content remains effectively contiguous. These layout constraints must be kept in mind when examining the content associated with a symbol (it may be spread over multiple atoms) or when applying certain relocation types (e.g. MachO subtractors). This patch replaces the Atom model in JITLink with a blocks-and-symbols model. The blocks-and-symbols model represents relocatable object files as bipartite graphs, with one set of nodes representing contiguous content (Blocks) and another representing named or anonymous locations (Symbols) within a Block. Relocations are represented as edges from Blocks to Symbols. This scheme removes layout constraints (simplifying handling of MachO alt-entry symbols, and hopefully ELF sections at some point in the future) and simplifies some relocation logic. llvm-svn: 373689
2019-06-14[JITLink] Move JITLinkMemoryManager into its own header.Lang Hames
llvm-svn: 363444