[all-commits] [llvm/llvm-project] e50aea: [JITLink][ORC] Major JITLinkMemoryManager refactor.

lhames via All-commits all-commits at lists.llvm.org
Mon Oct 11 19:13:12 PDT 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: e50aea58d59c8cfae807a7fee21c4227472c0678
      https://github.com/llvm/llvm-project/commit/e50aea58d59c8cfae807a7fee21c4227472c0678
  Author: Lang Hames <lhames at gmail.com>
  Date:   2021-10-11 (Mon, 11 Oct 2021)

  Changed paths:
    M llvm/examples/OrcV2Examples/LLJITWithCustomObjectLinkingLayer/LLJITWithCustomObjectLinkingLayer.cpp
    M llvm/examples/OrcV2Examples/LLJITWithObjectLinkingLayerPlugin/LLJITWithObjectLinkingLayerPlugin.cpp
    M llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
    M llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h
    A llvm/include/llvm/ExecutionEngine/JITLink/MemoryFlags.h
    M llvm/include/llvm/ExecutionEngine/Orc/EPCDebugObjectRegistrar.h
    M llvm/include/llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h
    M llvm/include/llvm/ExecutionEngine/Orc/EPCIndirectionUtils.h
    M llvm/include/llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h
    M llvm/include/llvm/Support/Memory.h
    M llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
    M llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h
    M llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
    M llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
    M llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.cpp
    M llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.h
    M llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp
    M llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
    M llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp
    M llvm/lib/ExecutionEngine/JITLink/MachO_x86_64.cpp
    A llvm/lib/ExecutionEngine/JITLink/MemoryFlags.cpp
    M llvm/lib/ExecutionEngine/Orc/DebugObjectManagerPlugin.cpp
    M llvm/lib/ExecutionEngine/Orc/ELFNixPlatform.cpp
    M llvm/lib/ExecutionEngine/Orc/EPCDebugObjectRegistrar.cpp
    M llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp
    M llvm/lib/ExecutionEngine/Orc/EPCIndirectionUtils.cpp
    M llvm/lib/ExecutionEngine/Orc/ExecutorProcessControl.cpp
    M llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
    M llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp
    M llvm/lib/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.cpp
    M llvm/tools/llvm-jitlink/llvm-jitlink.cpp
    M llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
    M llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp
    M llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp

  Log Message:
  -----------
  [JITLink][ORC] Major JITLinkMemoryManager refactor.

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.




More information about the All-commits mailing list