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 @@ -12,8 +12,6 @@ #include "llvm/ExecutionEngine/JITLink/JITLink.h" #include "llvm/Support/Process.h" -#include - using namespace llvm::jitlink; namespace llvm { @@ -34,7 +32,8 @@ std::swap(AI.Segments, Segs); std::swap(AI.Actions, G.allocActions()); - Parent.Mapper->initialize(AI, [&](Expected Result) { + Parent.Mapper->initialize(AI, [OnFinalize = std::move(OnFinalize)]( + Expected Result) mutable { if (!Result) { OnFinalize(Result.takeError()); return; diff --git a/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp b/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp --- a/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp +++ b/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp @@ -11,6 +11,8 @@ #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h" #include "llvm/Support/WindowsError.h" +#include + #if defined(LLVM_ON_UNIX) && !defined(__ANDROID__) #include #include @@ -300,9 +302,10 @@ void SharedMemoryMapper::initialize(MemoryMapper::AllocInfo &AI, OnInitializedFunction OnInitialized) { - auto Reservation = Reservations.find(AI.MappingBase); - assert(Reservation != Reservations.end() && - "Attempt to initialize unreserved range"); + auto Reservation = std::lower_bound( + Reservations.rbegin(), Reservations.rend(), AI.MappingBase, + [](const auto &A, const auto &B) { return A.first > B; }); + assert(Reservation != Reservations.rend() && "Attempt to initialize unreserved range"); tpctypes::SharedMemoryFinalizeRequest FR; @@ -336,7 +339,7 @@ OnInitialized(std::move(Result)); }, - SAs.Instance, AI.MappingBase, std::move(FR)); + SAs.Instance, Reservation->first, std::move(FR)); } void SharedMemoryMapper::deinitialize( @@ -407,23 +410,19 @@ } SharedMemoryMapper::~SharedMemoryMapper() { - std::vector ReservationAddrs; - if (!Reservations.empty()) { - std::lock_guard Lock(Mutex); - { - ReservationAddrs.reserve(Reservations.size()); - for (const auto &R : Reservations) { - ReservationAddrs.push_back(R.first); - } - } - } + for (const auto R : Reservations) { - std::promise P; - auto F = P.get_future(); - release(ReservationAddrs, [&](Error Err) { P.set_value(std::move(Err)); }); - // FIXME: Release can actually fail. The error should be propagated. - // Meanwhile, a better option is to explicitly call release(). - cantFail(F.get()); +#if defined(LLVM_ON_UNIX) && !defined(__ANDROID__) + + munmap(R.second.LocalAddr, R.second.Size); + +#elif defined(_WIN32) + + UnmapViewOfFile(R.second.LocalAddr); + +#endif + + } } } // namespace orc diff --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/ExecutorSharedMemoryMapperService.cpp b/llvm/lib/ExecutionEngine/Orc/TargetProcess/ExecutorSharedMemoryMapperService.cpp --- a/llvm/lib/ExecutionEngine/Orc/TargetProcess/ExecutorSharedMemoryMapperService.cpp +++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/ExecutorSharedMemoryMapperService.cpp @@ -198,6 +198,17 @@ AllErr = joinErrors(std::move(AllErr), std::move(Err)); } + // Remove the allocation from the allocation list of its reservation + for (auto &Reservation : Reservations) { + auto AllocationIt = + std::find(Reservation.second.Allocations.begin(), + Reservation.second.Allocations.end(), Base); + if (AllocationIt != Reservation.second.Allocations.end()) { + Reservation.second.Allocations.erase(AllocationIt); + break; + } + } + Allocations.erase(Base); } } @@ -265,18 +276,13 @@ Error ExecutorSharedMemoryMapperService::shutdown() { std::vector ReservationAddrs; - { - std::lock_guard Lock(Mutex); - - if (Reservations.empty()) - return Error::success(); - ReservationAddrs.reserve(Reservations.size()); - for (const auto &R : Reservations) { - ReservationAddrs.push_back(ExecutorAddr::fromPtr(R.getFirst())); - } + if (Reservations.empty()) + return Error::success(); - Reservations.clear(); + ReservationAddrs.reserve(Reservations.size()); + for (const auto &R : Reservations) { + ReservationAddrs.push_back(ExecutorAddr::fromPtr(R.getFirst())); } return release(ReservationAddrs);