Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions orc-rt/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(ORC_RT_HEADERS
orc-rt/ResourceManager.h
orc-rt/RTTI.h
orc-rt/ScopeExit.h
orc-rt/SimpleNativeMemoryMap.h
orc-rt/SimplePackedSerialization.h
orc-rt/SPSAllocAction.h
orc-rt/SPSMemoryFlags.h
Expand Down
3 changes: 3 additions & 0 deletions orc-rt/include/orc-rt/SPSWrapperFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
#ifndef ORC_RT_SPSWRAPPERFUNCTION_H
#define ORC_RT_SPSWRAPPERFUNCTION_H

#include "orc-rt/Compiler.h"
#include "orc-rt/SimplePackedSerialization.h"
#include "orc-rt/WrapperFunction.h"

#define ORC_RT_SPS_INTERFACE ORC_RT_INTERFACE

namespace orc_rt {
namespace detail {

Expand Down
123 changes: 123 additions & 0 deletions orc-rt/include/orc-rt/SimpleNativeMemoryMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//===- SimpleNativeMemoryMap.h -- Mem via standard host OS APIs -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// SimpleNativeMemoryMap and related APIs.
//
//===----------------------------------------------------------------------===//

#ifndef ORC_RT_SIMPLENATIVEMEMORYMAP_H
#define ORC_RT_SIMPLENATIVEMEMORYMAP_H

#include "orc-rt/AllocAction.h"
#include "orc-rt/Error.h"
#include "orc-rt/MemoryFlags.h"
#include "orc-rt/ResourceManager.h"
#include "orc-rt/SPSWrapperFunction.h"
#include "orc-rt/move_only_function.h"

#include <map>
#include <mutex>
#include <unordered_map>
#include <vector>

namespace orc_rt {

/// JIT'd memory management backend.
///
/// Intances can:
/// 1. Reserve address space.
/// 2. Finalize memory regions within reserved memory (copying content,
/// applying permissions, running finalize actions, and recording
/// deallocate actions).
/// 3. Deallocate memory regions within reserved memory (running
/// deallocate actions and making memory available for future
/// finalize calls (if the system permits this).
/// 4. Release address space, deallocating any not-yet-deallocated finalized
/// regions, and returning the address space to the system for reuse (if
/// the system permits).
class SimpleNativeMemoryMap : public ResourceManager {
public:
/// Reserves a slab of contiguous address space for allocation.
///
/// Returns the base address of the allocated memory.
using OnReserveCompleteFn = move_only_function<void(Expected<void *>)>;
void reserve(OnReserveCompleteFn &&OnComplete, size_t Size);

/// Release a slab of contiguous address space back to the system.
using OnReleaseCompleteFn = move_only_function<void(Error)>;
void release(OnReleaseCompleteFn &&OnComplete, void *Addr);

struct FinalizeRequest {
struct Segment {
enum class ContentType : uint8_t { Uninitialized, ZeroFill, Regular };

Segment() = default;
Segment(void *Address, size_t Size, AllocGroup G, ContentType C)
: Address(Address), Size(Size), G(G), C(C) {}

void *Address = nullptr;
size_t Size = 0;
AllocGroup G;
ContentType C = ContentType::Uninitialized;
char *data() { return reinterpret_cast<char *>(Address); }
size_t size() const { return Size; }
};

std::vector<Segment> Segments;
std::vector<AllocActionPair> AAPs;
};

/// Writes content into the requested ranges, applies permissions, and
/// performs allocation actions.
using OnFinalizeCompleteFn = move_only_function<void(Expected<void *>)>;
void finalize(OnFinalizeCompleteFn &&OnComplete, FinalizeRequest FR);

/// Runs deallocation actions and resets memory permissions for the requested
/// memory.
using OnDeallocateCompleteFn = move_only_function<void(Error)>;
void deallocate(OnDeallocateCompleteFn &&OnComplete, void *Base);

void detach(ResourceManager::OnCompleteFn OnComplete) override;
void shutdown(ResourceManager::OnCompleteFn OnComplete) override;

private:
struct SlabInfo {
SlabInfo(size_t Size) : Size(Size) {}
size_t Size;
std::unordered_map<void *, std::vector<AllocAction>> DeallocActions;
};

void shutdownNext(OnCompleteFn OnComplete, std::vector<void *> Bases);
Error makeBadSlabError(void *Base, const char *Op);
SlabInfo *findSlabInfoFor(void *Base);
Error recordDeallocActions(void *Base,
std::vector<AllocAction> DeallocActions);

std::mutex M;
std::map<void *, SlabInfo> Slabs;
};

} // namespace orc_rt

ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_reserve_sps_wrapper(
orc_rt_SessionRef Session, void *CallCtx,
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);

ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_release_sps_wrapper(
orc_rt_SessionRef Session, void *CallCtx,
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);

ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_finalize_sps_wrapper(
orc_rt_SessionRef Session, void *CallCtx,
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);

ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_deallocate_sps_wrapper(
orc_rt_SessionRef Session, void *CallCtx,
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);

#endif // ORC_RT_SIMPLENATIVEMEMORYMAP_H
1 change: 1 addition & 0 deletions orc-rt/lib/executor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(files
AllocAction.cpp
ResourceManager.cpp
RTTI.cpp
SimpleNativeMemoryMap.cpp
)

add_library(orc-rt-executor STATIC ${files})
Expand Down
Loading
Loading