Skip to content

Commit

Permalink
Record usm allocated size
Browse files Browse the repository at this point in the history
(cherry picked from commit 5c5e9b9)
  • Loading branch information
zhaomaosu authored and AllanZyne committed Jan 9, 2025
1 parent 0aec4ce commit f1e4711
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
6 changes: 3 additions & 3 deletions source/loader/layers/sanitizer/msan/msan_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
USMDesc.align = getAlignment();
ur_usm_pool_handle_t Pool{};
URes = getMsanInterceptor()->allocateMemory(
Context, Device, &USMDesc, Pool, Size,
Context, Device, &USMDesc, Pool, Size, AllocType::DEVICE_USM,
ur_cast<void **>(&Allocation));
if (URes != UR_RESULT_SUCCESS) {
getContext()->logger.error(
Expand Down Expand Up @@ -130,8 +130,8 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
ur_usm_desc_t USMDesc{};
USMDesc.align = getAlignment();
ur_usm_pool_handle_t Pool{};
URes = getMsanInterceptor()->allocateMemory(
Context, nullptr, &USMDesc, Pool, Size,
URes = getContext()->urDdiTable.USM.pfnHostAlloc(
Context, &USMDesc, Pool, Size,
ur_cast<void **>(&HostAllocation));
if (URes != UR_RESULT_SUCCESS) {
getContext()->logger.error("Failed to allocate {} bytes host "
Expand Down
41 changes: 39 additions & 2 deletions source/loader/layers/sanitizer/msan/msan_ddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,45 @@ ur_result_t urUSMDeviceAlloc(
) {
getContext()->logger.debug("==== urUSMDeviceAlloc");

return getMsanInterceptor()->allocateMemory(hContext, hDevice, pUSMDesc,
pool, size, ppMem);
return getMsanInterceptor()->allocateMemory(
hContext, hDevice, pUSMDesc, pool, size, AllocType::DEVICE_USM, ppMem);
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urUSMHostAlloc
__urdlllocal ur_result_t UR_APICALL urUSMHostAlloc(
ur_context_handle_t hContext, ///< [in] handle of the context object
const ur_usm_desc_t
*pUSMDesc, ///< [in][optional] USM memory allocation descriptor
ur_usm_pool_handle_t
pool, ///< [in][optional] Pointer to a pool created using urUSMPoolCreate
size_t
size, ///< [in] size in bytes of the USM memory object to be allocated
void **ppMem ///< [out] pointer to USM host memory object
) {
getContext()->logger.debug("==== urUSMHostAlloc");

return getMsanInterceptor()->allocateMemory(
hContext, nullptr, pUSMDesc, pool, size, AllocType::HOST_USM, ppMem);
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urUSMSharedAlloc
__urdlllocal ur_result_t UR_APICALL urUSMSharedAlloc(
ur_context_handle_t hContext, ///< [in] handle of the context object
ur_device_handle_t hDevice, ///< [in] handle of the device object
const ur_usm_desc_t *
pUSMDesc, ///< [in][optional] Pointer to USM memory allocation descriptor.
ur_usm_pool_handle_t
pool, ///< [in][optional] Pointer to a pool created using urUSMPoolCreate
size_t
size, ///< [in] size in bytes of the USM memory object to be allocated
void **ppMem ///< [out] pointer to USM shared memory object
) {
getContext()->logger.debug("==== urUSMSharedAlloc");

return getMsanInterceptor()->allocateMemory(
hContext, hDevice, pUSMDesc, pool, size, AllocType::SHARED_USM, ppMem);
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
35 changes: 31 additions & 4 deletions source/loader/layers/sanitizer/msan/msan_interceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,36 @@ ur_result_t MsanInterceptor::allocateMemory(ur_context_handle_t Context,
ur_device_handle_t Device,
const ur_usm_desc_t *Properties,
ur_usm_pool_handle_t Pool,
size_t Size, void **ResultPtr) {
size_t Size, AllocType Type,
void **ResultPtr) {

auto ContextInfo = getContextInfo(Context);
std::shared_ptr<DeviceInfo> DeviceInfo = getDeviceInfo(Device);
std::shared_ptr<DeviceInfo> DeviceInfo =
Device ? getDeviceInfo(Device) : nullptr;

void *Allocated = nullptr;

UR_CALL(getContext()->urDdiTable.USM.pfnDeviceAlloc(
Context, Device, Properties, Pool, Size, &Allocated));
if (Type == AllocType::DEVICE_USM) {
UR_CALL(getContext()->urDdiTable.USM.pfnDeviceAlloc(
Context, Device, Properties, Pool, Size, &Allocated));
} else if (Type == AllocType::HOST_USM) {
UR_CALL(getContext()->urDdiTable.USM.pfnHostAlloc(
Context, Properties, Pool, Size, &Allocated));
} else if (Type == AllocType::SHARED_USM) {
UR_CALL(getContext()->urDdiTable.USM.pfnSharedAlloc(
Context, Device, Properties, Pool, Size, &Allocated));
}

*ResultPtr = Allocated;

ContextInfo->MaxAllocatedSize =
std::max(ContextInfo->MaxAllocatedSize, Size);

// For host/shared usm, we only record the alloc size.
if (Type != AllocType::DEVICE_USM) {
return UR_RESULT_SUCCESS;
}

auto AI =
std::make_shared<MsanAllocInfo>(MsanAllocInfo{(uptr)Allocated,
Size,
Expand Down Expand Up @@ -432,10 +450,14 @@ ur_result_t MsanInterceptor::prepareLaunch(
}

// Set LaunchInfo
auto ContextInfo = getContextInfo(LaunchInfo.Context);
LaunchInfo.Data->GlobalShadowOffset = DeviceInfo->Shadow->ShadowBegin;
LaunchInfo.Data->GlobalShadowOffsetEnd = DeviceInfo->Shadow->ShadowEnd;
LaunchInfo.Data->DeviceTy = DeviceInfo->Type;
LaunchInfo.Data->Debug = getOptions().Debug ? 1 : 0;
UR_CALL(getContext()->urDdiTable.USM.pfnDeviceAlloc(
ContextInfo->Handle, DeviceInfo->Handle, nullptr, nullptr,
ContextInfo->MaxAllocatedSize, &LaunchInfo.Data->CleanShadow));

getContext()->logger.info(
"launch_info {} (GlobalShadow={}, Device={}, Debug={})",
Expand Down Expand Up @@ -518,6 +540,11 @@ ur_result_t USMLaunchInfo::initialize() {
USMLaunchInfo::~USMLaunchInfo() {
[[maybe_unused]] ur_result_t Result;
if (Data) {
if (Data->CleanShadow) {
Result = getContext()->urDdiTable.USM.pfnFree(Context,
Data->CleanShadow);
assert(Result == UR_RESULT_SUCCESS);
}
Result = getContext()->urDdiTable.USM.pfnFree(Context, (void *)Data);
assert(Result == UR_RESULT_SUCCESS);
}
Expand Down
3 changes: 2 additions & 1 deletion source/loader/layers/sanitizer/msan/msan_interceptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ struct ProgramInfo {

struct ContextInfo {
ur_context_handle_t Handle;
size_t MaxAllocatedSize = 1024;
std::atomic<int32_t> RefCount = 1;

std::vector<ur_device_handle_t> DeviceList;
Expand Down Expand Up @@ -179,7 +180,7 @@ class MsanInterceptor {
ur_device_handle_t Device,
const ur_usm_desc_t *Properties,
ur_usm_pool_handle_t Pool, size_t Size,
void **ResultPtr);
AllocType Type, void **ResultPtr);
ur_result_t releaseMemory(ur_context_handle_t Context, void *Ptr);

ur_result_t registerProgram(ur_program_handle_t Program);
Expand Down
2 changes: 1 addition & 1 deletion source/loader/layers/sanitizer/msan/msan_libdevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct MsanLaunchInfo {

MsanErrorReport Report;

uint8_t CleanShadow[128] = {};
void *CleanShadow = nullptr;
};

// Based on the observation, only the last 24 bits of the address of the private
Expand Down

0 comments on commit f1e4711

Please sign in to comment.