[llvm] c69df92 - [Orc] Use MapperJITLinkMemoryManager with InProcessMapper in llvm-jitlink tool
Anubhab Ghosh via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 26 22:43:51 PDT 2022
Author: Anubhab Ghosh
Date: 2022-08-27T11:07:09+05:30
New Revision: c69df92b4f8b39a81cc0ba2a52b4f8a4bd655435
URL: https://github.com/llvm/llvm-project/commit/c69df92b4f8b39a81cc0ba2a52b4f8a4bd655435
DIFF: https://github.com/llvm/llvm-project/commit/c69df92b4f8b39a81cc0ba2a52b4f8a4bd655435.diff
LOG: [Orc] Use MapperJITLinkMemoryManager with InProcessMapper in llvm-jitlink tool
MapperJITLinkMemoryManager has slab allocation. Combined with
InProcessMapper, it can replace InProcessMemoryManager.
It can also replace JITLinkSlabAllocator through the InProcessDeltaMapper
that adds an offset to the executor addresses for use in tests.
Differential Revision: https://reviews.llvm.org/D132315
Added:
Modified:
llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h
llvm/tools/llvm-jitlink/llvm-jitlink.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h b/llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h
index 80dac5aee80bc..17072d2fa5b12 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h
@@ -78,7 +78,7 @@ class MemoryMapper {
virtual ~MemoryMapper();
};
-class InProcessMemoryMapper final : public MemoryMapper {
+class InProcessMemoryMapper : public MemoryMapper {
public:
InProcessMemoryMapper(size_t PageSize);
diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
index eb4b55c965877..117d02f7bff9f 100644
--- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
+++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
@@ -26,6 +26,7 @@
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
#include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
+#include "llvm/ExecutionEngine/Orc/MapperJITLinkMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
#include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
@@ -454,6 +455,78 @@ static void dumpSectionContents(raw_ostream &OS, LinkGraph &G) {
}
}
+// A memory mapper with a fake offset applied only used for -noexec testing
+class InProcessDeltaMapper final : public InProcessMemoryMapper {
+public:
+ InProcessDeltaMapper(size_t PageSize, uint64_t TargetAddr)
+ : InProcessMemoryMapper(PageSize), TargetMapAddr(TargetAddr),
+ DeltaAddr(0) {}
+
+ static Expected<std::unique_ptr<InProcessDeltaMapper>> Create() {
+ auto PageSize = sys::Process::getPageSize();
+ if (!PageSize)
+ return PageSize.takeError();
+ return std::make_unique<InProcessDeltaMapper>(*PageSize, SlabAddress);
+ }
+
+ void reserve(size_t NumBytes, OnReservedFunction OnReserved) override {
+ InProcessMemoryMapper::reserve(
+ NumBytes, [this, OnReserved = std::move(OnReserved)](
+ Expected<ExecutorAddrRange> Result) mutable {
+ if (!Result)
+ return OnReserved(Result.takeError());
+
+ assert(DeltaAddr == 0 && "Overwriting previous offset");
+ if (TargetMapAddr != ~0ULL)
+ DeltaAddr = TargetMapAddr - Result->Start.getValue();
+ auto OffsetRange = ExecutorAddrRange(Result->Start + DeltaAddr,
+ Result->End + DeltaAddr);
+
+ OnReserved(OffsetRange);
+ });
+ }
+
+ char *prepare(ExecutorAddr Addr, size_t ContentSize) override {
+ return InProcessMemoryMapper::prepare(Addr - DeltaAddr, ContentSize);
+ }
+
+ void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override {
+ auto FixedAI = AI;
+ FixedAI.MappingBase -= DeltaAddr;
+ InProcessMemoryMapper::initialize(
+ FixedAI, [this, OnInitialized = std::move(OnInitialized)](
+ Expected<ExecutorAddr> Result) mutable {
+ if (!Result)
+ return OnInitialized(Result.takeError());
+
+ OnInitialized(ExecutorAddr(Result->getValue() + DeltaAddr));
+ });
+ }
+
+ void deinitialize(ArrayRef<ExecutorAddr> Allocations,
+ OnDeinitializedFunction OnDeInitialized) override {
+ std::vector<ExecutorAddr> Addrs(Allocations.size());
+ for (const auto Base : Allocations) {
+ Addrs.push_back(Base - DeltaAddr);
+ }
+
+ InProcessMemoryMapper::deinitialize(Addrs, std::move(OnDeInitialized));
+ }
+
+ void release(ArrayRef<ExecutorAddr> Reservations,
+ OnReleasedFunction OnRelease) override {
+ std::vector<ExecutorAddr> Addrs(Reservations.size());
+ for (const auto Base : Reservations) {
+ Addrs.push_back(Base - DeltaAddr);
+ }
+ InProcessMemoryMapper::release(Addrs, std::move(OnRelease));
+ }
+
+private:
+ uint64_t TargetMapAddr;
+ uint64_t DeltaAddr;
+};
+
class JITLinkSlabAllocator final : public JITLinkMemoryManager {
private:
struct FinalizedAllocInfo {
@@ -721,12 +794,24 @@ Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
return SlabSize * Units;
}
-static std::unique_ptr<JITLinkMemoryManager> createMemoryManager() {
+static std::unique_ptr<JITLinkMemoryManager> createInProcessMemoryManager() {
if (!SlabAllocateSizeString.empty()) {
auto SlabSize = ExitOnErr(getSlabAllocSize(SlabAllocateSizeString));
- return ExitOnErr(JITLinkSlabAllocator::Create(SlabSize));
+
+ return ExitOnErr(
+ MapperJITLinkMemoryManager::CreateWithMapper<InProcessDeltaMapper>(
+ SlabSize));
}
- return ExitOnErr(InProcessMemoryManager::Create());
+
+#ifdef _WIN32
+ return ExitOnErr(
+ MapperJITLinkMemoryManager::CreateWithMapper<InProcessMemoryMapper>(
+ 1024 * 1024));
+#else
+ return ExitOnErr(
+ MapperJITLinkMemoryManager::CreateWithMapper<InProcessMemoryMapper>(
+ 1024 * 1024 * 1024));
+#endif
}
static Expected<MaterializationUnit::Interface>
@@ -1012,7 +1097,7 @@ Expected<std::unique_ptr<Session>> Session::Create(Triple TT) {
EPC = std::make_unique<SelfExecutorProcessControl>(
std::make_shared<SymbolStringPool>(),
std::make_unique<InPlaceTaskDispatcher>(), std::move(TT), *PageSize,
- createMemoryManager());
+ createInProcessMemoryManager());
}
Error Err = Error::success();
More information about the llvm-commits
mailing list