[llvm] [Offload] Guard olMemAlloc/Free with a mutex (PR #153786)
Ross Brunton via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 15 04:03:34 PDT 2025
https://github.com/RossBrunton created https://github.com/llvm/llvm-project/pull/153786
Both these functions update an `AllocInfoMap` structure in the context,
however they did not use any locks, causing random failures in threaded
code. Now they use a mutex.
>From 31ce7b96630dcf9b4184b5b3249f2a89b78073e5 Mon Sep 17 00:00:00 2001
From: Ross Brunton <ross at codeplay.com>
Date: Fri, 15 Aug 2025 12:02:24 +0100
Subject: [PATCH] [Offload] Guard olMemAlloc/Free with a mutex
Both these functions update an `AllocInfoMap` structure in the context,
however they did not use any locks, causing random failures in threaded
code. Now they use a mutex.
---
offload/liboffload/src/OffloadImpl.cpp | 30 ++++++++++++++++----------
1 file changed, 19 insertions(+), 11 deletions(-)
diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 1c9dfc69d445a..16459108d2c4a 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -125,6 +125,7 @@ struct OffloadContext {
bool TracingEnabled = false;
bool ValidationEnabled = true;
DenseMap<void *, AllocInfo> AllocInfoMap{};
+ std::mutex AllocInfoMapMutex{};
SmallVector<ol_platform_impl_t, 4> Platforms{};
size_t RefCount;
@@ -449,26 +450,33 @@ Error olMemAlloc_impl(ol_device_handle_t Device, ol_alloc_type_t Type,
return Alloc.takeError();
*AllocationOut = *Alloc;
- OffloadContext::get().AllocInfoMap.insert_or_assign(*Alloc,
- AllocInfo{Device, Type});
+ {
+ std::lock_guard<std::mutex> Lock(OffloadContext::get().AllocInfoMapMutex);
+ OffloadContext::get().AllocInfoMap.insert_or_assign(
+ *Alloc, AllocInfo{Device, Type});
+ }
return Error::success();
}
Error olMemFree_impl(void *Address) {
- if (!OffloadContext::get().AllocInfoMap.contains(Address))
- return createOffloadError(ErrorCode::INVALID_ARGUMENT,
- "address is not a known allocation");
-
- auto AllocInfo = OffloadContext::get().AllocInfoMap.at(Address);
- auto Device = AllocInfo.Device;
- auto Type = AllocInfo.Type;
+ ol_device_handle_t Device;
+ ol_alloc_type_t Type;
+ {
+ std::lock_guard<std::mutex> Lock(OffloadContext::get().AllocInfoMapMutex);
+ if (!OffloadContext::get().AllocInfoMap.contains(Address))
+ return createOffloadError(ErrorCode::INVALID_ARGUMENT,
+ "address is not a known allocation");
+
+ auto AllocInfo = OffloadContext::get().AllocInfoMap.at(Address);
+ Device = AllocInfo.Device;
+ Type = AllocInfo.Type;
+ OffloadContext::get().AllocInfoMap.erase(Address);
+ }
if (auto Res =
Device->Device->dataDelete(Address, convertOlToPluginAllocTy(Type)))
return Res;
- OffloadContext::get().AllocInfoMap.erase(Address);
-
return Error::success();
}
More information about the llvm-commits
mailing list