Skip to content

Commit f1e4711

Browse files
zhaomaosuAllanZyne
authored andcommitted
Record usm allocated size
(cherry picked from commit 5c5e9b9)
1 parent 0aec4ce commit f1e4711

File tree

5 files changed

+76
-11
lines changed

5 files changed

+76
-11
lines changed

source/loader/layers/sanitizer/msan/msan_buffer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
9393
USMDesc.align = getAlignment();
9494
ur_usm_pool_handle_t Pool{};
9595
URes = getMsanInterceptor()->allocateMemory(
96-
Context, Device, &USMDesc, Pool, Size,
96+
Context, Device, &USMDesc, Pool, Size, AllocType::DEVICE_USM,
9797
ur_cast<void **>(&Allocation));
9898
if (URes != UR_RESULT_SUCCESS) {
9999
getContext()->logger.error(
@@ -130,8 +130,8 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
130130
ur_usm_desc_t USMDesc{};
131131
USMDesc.align = getAlignment();
132132
ur_usm_pool_handle_t Pool{};
133-
URes = getMsanInterceptor()->allocateMemory(
134-
Context, nullptr, &USMDesc, Pool, Size,
133+
URes = getContext()->urDdiTable.USM.pfnHostAlloc(
134+
Context, &USMDesc, Pool, Size,
135135
ur_cast<void **>(&HostAllocation));
136136
if (URes != UR_RESULT_SUCCESS) {
137137
getContext()->logger.error("Failed to allocate {} bytes host "

source/loader/layers/sanitizer/msan/msan_ddi.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,45 @@ ur_result_t urUSMDeviceAlloc(
9999
) {
100100
getContext()->logger.debug("==== urUSMDeviceAlloc");
101101

102-
return getMsanInterceptor()->allocateMemory(hContext, hDevice, pUSMDesc,
103-
pool, size, ppMem);
102+
return getMsanInterceptor()->allocateMemory(
103+
hContext, hDevice, pUSMDesc, pool, size, AllocType::DEVICE_USM, ppMem);
104+
}
105+
106+
///////////////////////////////////////////////////////////////////////////////
107+
/// @brief Intercept function for urUSMHostAlloc
108+
__urdlllocal ur_result_t UR_APICALL urUSMHostAlloc(
109+
ur_context_handle_t hContext, ///< [in] handle of the context object
110+
const ur_usm_desc_t
111+
*pUSMDesc, ///< [in][optional] USM memory allocation descriptor
112+
ur_usm_pool_handle_t
113+
pool, ///< [in][optional] Pointer to a pool created using urUSMPoolCreate
114+
size_t
115+
size, ///< [in] size in bytes of the USM memory object to be allocated
116+
void **ppMem ///< [out] pointer to USM host memory object
117+
) {
118+
getContext()->logger.debug("==== urUSMHostAlloc");
119+
120+
return getMsanInterceptor()->allocateMemory(
121+
hContext, nullptr, pUSMDesc, pool, size, AllocType::HOST_USM, ppMem);
122+
}
123+
124+
///////////////////////////////////////////////////////////////////////////////
125+
/// @brief Intercept function for urUSMSharedAlloc
126+
__urdlllocal ur_result_t UR_APICALL urUSMSharedAlloc(
127+
ur_context_handle_t hContext, ///< [in] handle of the context object
128+
ur_device_handle_t hDevice, ///< [in] handle of the device object
129+
const ur_usm_desc_t *
130+
pUSMDesc, ///< [in][optional] Pointer to USM memory allocation descriptor.
131+
ur_usm_pool_handle_t
132+
pool, ///< [in][optional] Pointer to a pool created using urUSMPoolCreate
133+
size_t
134+
size, ///< [in] size in bytes of the USM memory object to be allocated
135+
void **ppMem ///< [out] pointer to USM shared memory object
136+
) {
137+
getContext()->logger.debug("==== urUSMSharedAlloc");
138+
139+
return getMsanInterceptor()->allocateMemory(
140+
hContext, hDevice, pUSMDesc, pool, size, AllocType::SHARED_USM, ppMem);
104141
}
105142

106143
///////////////////////////////////////////////////////////////////////////////

source/loader/layers/sanitizer/msan/msan_interceptor.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,36 @@ ur_result_t MsanInterceptor::allocateMemory(ur_context_handle_t Context,
4646
ur_device_handle_t Device,
4747
const ur_usm_desc_t *Properties,
4848
ur_usm_pool_handle_t Pool,
49-
size_t Size, void **ResultPtr) {
49+
size_t Size, AllocType Type,
50+
void **ResultPtr) {
5051

5152
auto ContextInfo = getContextInfo(Context);
52-
std::shared_ptr<DeviceInfo> DeviceInfo = getDeviceInfo(Device);
53+
std::shared_ptr<DeviceInfo> DeviceInfo =
54+
Device ? getDeviceInfo(Device) : nullptr;
5355

5456
void *Allocated = nullptr;
5557

56-
UR_CALL(getContext()->urDdiTable.USM.pfnDeviceAlloc(
57-
Context, Device, Properties, Pool, Size, &Allocated));
58+
if (Type == AllocType::DEVICE_USM) {
59+
UR_CALL(getContext()->urDdiTable.USM.pfnDeviceAlloc(
60+
Context, Device, Properties, Pool, Size, &Allocated));
61+
} else if (Type == AllocType::HOST_USM) {
62+
UR_CALL(getContext()->urDdiTable.USM.pfnHostAlloc(
63+
Context, Properties, Pool, Size, &Allocated));
64+
} else if (Type == AllocType::SHARED_USM) {
65+
UR_CALL(getContext()->urDdiTable.USM.pfnSharedAlloc(
66+
Context, Device, Properties, Pool, Size, &Allocated));
67+
}
5868

5969
*ResultPtr = Allocated;
6070

71+
ContextInfo->MaxAllocatedSize =
72+
std::max(ContextInfo->MaxAllocatedSize, Size);
73+
74+
// For host/shared usm, we only record the alloc size.
75+
if (Type != AllocType::DEVICE_USM) {
76+
return UR_RESULT_SUCCESS;
77+
}
78+
6179
auto AI =
6280
std::make_shared<MsanAllocInfo>(MsanAllocInfo{(uptr)Allocated,
6381
Size,
@@ -432,10 +450,14 @@ ur_result_t MsanInterceptor::prepareLaunch(
432450
}
433451

434452
// Set LaunchInfo
453+
auto ContextInfo = getContextInfo(LaunchInfo.Context);
435454
LaunchInfo.Data->GlobalShadowOffset = DeviceInfo->Shadow->ShadowBegin;
436455
LaunchInfo.Data->GlobalShadowOffsetEnd = DeviceInfo->Shadow->ShadowEnd;
437456
LaunchInfo.Data->DeviceTy = DeviceInfo->Type;
438457
LaunchInfo.Data->Debug = getOptions().Debug ? 1 : 0;
458+
UR_CALL(getContext()->urDdiTable.USM.pfnDeviceAlloc(
459+
ContextInfo->Handle, DeviceInfo->Handle, nullptr, nullptr,
460+
ContextInfo->MaxAllocatedSize, &LaunchInfo.Data->CleanShadow));
439461

440462
getContext()->logger.info(
441463
"launch_info {} (GlobalShadow={}, Device={}, Debug={})",
@@ -518,6 +540,11 @@ ur_result_t USMLaunchInfo::initialize() {
518540
USMLaunchInfo::~USMLaunchInfo() {
519541
[[maybe_unused]] ur_result_t Result;
520542
if (Data) {
543+
if (Data->CleanShadow) {
544+
Result = getContext()->urDdiTable.USM.pfnFree(Context,
545+
Data->CleanShadow);
546+
assert(Result == UR_RESULT_SUCCESS);
547+
}
521548
Result = getContext()->urDdiTable.USM.pfnFree(Context, (void *)Data);
522549
assert(Result == UR_RESULT_SUCCESS);
523550
}

source/loader/layers/sanitizer/msan/msan_interceptor.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ struct ProgramInfo {
121121

122122
struct ContextInfo {
123123
ur_context_handle_t Handle;
124+
size_t MaxAllocatedSize = 1024;
124125
std::atomic<int32_t> RefCount = 1;
125126

126127
std::vector<ur_device_handle_t> DeviceList;
@@ -179,7 +180,7 @@ class MsanInterceptor {
179180
ur_device_handle_t Device,
180181
const ur_usm_desc_t *Properties,
181182
ur_usm_pool_handle_t Pool, size_t Size,
182-
void **ResultPtr);
183+
AllocType Type, void **ResultPtr);
183184
ur_result_t releaseMemory(ur_context_handle_t Context, void *Ptr);
184185

185186
ur_result_t registerProgram(ur_program_handle_t Program);

source/loader/layers/sanitizer/msan/msan_libdevice.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct MsanLaunchInfo {
5353

5454
MsanErrorReport Report;
5555

56-
uint8_t CleanShadow[128] = {};
56+
void *CleanShadow = nullptr;
5757
};
5858

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

0 commit comments

Comments
 (0)