diff --git a/llvm/include/llvm/ExecutionEngine/Orc/MapperJITLinkMemoryManager.h b/llvm/include/llvm/ExecutionEngine/Orc/MapperJITLinkMemoryManager.h --- a/llvm/include/llvm/ExecutionEngine/Orc/MapperJITLinkMemoryManager.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/MapperJITLinkMemoryManager.h @@ -49,6 +49,10 @@ private: class InFlightAlloc; + void completeAllocation(jitlink::LinkGraph &G, jitlink::BasicLayout BL, + OnAllocatedFunction OnAllocated, + Expected Result); + std::mutex Mutex; // We reserve multiples of this from the executor address space diff --git a/llvm/lib/ExecutionEngine/Orc/MapperJITLinkMemoryManager.cpp b/llvm/lib/ExecutionEngine/Orc/MapperJITLinkMemoryManager.cpp --- a/llvm/lib/ExecutionEngine/Orc/MapperJITLinkMemoryManager.cpp +++ b/llvm/lib/ExecutionEngine/Orc/MapperJITLinkMemoryManager.cpp @@ -72,54 +72,11 @@ auto TotalSize = SegsSizes->total(); - auto CompleteAllocation = [this, &G, BL = std::move(BL), - OnAllocated = std::move(OnAllocated)]( - Expected Result) mutable { - if (!Result) { - Mutex.unlock(); - return OnAllocated(Result.takeError()); - } - - auto NextSegAddr = Result->Start; - - std::vector SegInfos; - - for (auto &KV : BL.segments()) { - auto &AG = KV.first; - auto &Seg = KV.second; - - auto TotalSize = Seg.ContentSize + Seg.ZeroFillSize; - - Seg.Addr = NextSegAddr; - Seg.WorkingMem = Mapper->prepare(NextSegAddr, TotalSize); - - NextSegAddr += alignTo(TotalSize, Mapper->getPageSize()); - - MemoryMapper::AllocInfo::SegInfo SI; - SI.Offset = Seg.Addr - Result->Start; - SI.ContentSize = Seg.ContentSize; - SI.ZeroFillSize = Seg.ZeroFillSize; - SI.AG = AG; - SI.WorkingMem = Seg.WorkingMem; - - SegInfos.push_back(SI); - } - - UsedMemory.insert({Result->Start, NextSegAddr - Result->Start}); - - if (NextSegAddr < Result->End) { - // Save the remaining memory for reuse in next allocation(s) - AvailableMemory.insert(NextSegAddr, Result->End - 1, true); - } - Mutex.unlock(); - - if (auto Err = BL.apply()) { - OnAllocated(std::move(Err)); - return; - } - - OnAllocated(std::make_unique(*this, G, Result->Start, - std::move(SegInfos))); + auto CompleteAlloc = [this, &G, BL = std::move(BL), + OnAllocated = std::move(OnAllocated)]( + Expected Result) mutable { + completeAllocation(G, std::move(BL), std::move(OnAllocated), + std::move(Result)); }; Mutex.lock(); @@ -138,9 +95,9 @@ if (SelectedRange.empty()) { // no already reserved range was found auto TotalAllocation = alignTo(TotalSize, ReservationUnits); - Mapper->reserve(TotalAllocation, std::move(CompleteAllocation)); + Mapper->reserve(TotalAllocation, std::move(CompleteAlloc)); } else { - CompleteAllocation(SelectedRange); + CompleteAlloc(SelectedRange); } } @@ -185,5 +142,55 @@ }); } +void MapperJITLinkMemoryManager::completeAllocation( + jitlink::LinkGraph &G, jitlink::BasicLayout BL, + OnAllocatedFunction OnAllocated, Expected Result) { + if (!Result) { + Mutex.unlock(); + return OnAllocated(Result.takeError()); + } + + auto NextSegAddr = Result->Start; + + std::vector SegInfos; + + for (auto &KV : BL.segments()) { + auto &AG = KV.first; + auto &Seg = KV.second; + + auto TotalSize = Seg.ContentSize + Seg.ZeroFillSize; + + Seg.Addr = NextSegAddr; + Seg.WorkingMem = Mapper->prepare(NextSegAddr, TotalSize); + + NextSegAddr += alignTo(TotalSize, Mapper->getPageSize()); + + MemoryMapper::AllocInfo::SegInfo SI; + SI.Offset = Seg.Addr - Result->Start; + SI.ContentSize = Seg.ContentSize; + SI.ZeroFillSize = Seg.ZeroFillSize; + SI.AG = AG; + SI.WorkingMem = Seg.WorkingMem; + + SegInfos.push_back(SI); + } + + UsedMemory.insert({Result->Start, NextSegAddr - Result->Start}); + + if (NextSegAddr < Result->End) { + // Save the remaining memory for reuse in next allocation(s) + AvailableMemory.insert(NextSegAddr, Result->End - 1, true); + } + Mutex.unlock(); + + if (auto Err = BL.apply()) { + OnAllocated(std::move(Err)); + return; + } + + OnAllocated(std::make_unique(*this, G, Result->Start, + std::move(SegInfos))); +} + } // end namespace orc } // end namespace llvm