Skip to content

Commit 2340481

Browse files
committed
handle pointers to the middle of SVM allocations in more cases
1 parent 1912b90 commit 2340481

1 file changed

Lines changed: 45 additions & 50 deletions

File tree

‎layers/99_svmplusplus/emulate.cpp‎

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -602,39 +602,6 @@ static cl_context getContext(
602602
return context;
603603
}
604604

605-
static inline bool isSystemPtr(
606-
cl_context context,
607-
const void* ptr)
608-
{
609-
const auto& layerContext = getLayerContext();
610-
if (layerContext.isKnownAlloc(context, ptr)) {
611-
return layerContext.getAllocInfo(context, ptr).IsSystemPointer;
612-
}
613-
return false;
614-
}
615-
616-
static inline bool isUSMPtr(
617-
cl_context context,
618-
const void* ptr)
619-
{
620-
const auto& layerContext = getLayerContext();
621-
if (layerContext.isKnownAlloc(context, ptr)) {
622-
return layerContext.getAllocInfo(context, ptr).IsUSMPointer;
623-
}
624-
return false;
625-
}
626-
627-
static inline bool isSVMPtr(
628-
cl_context context,
629-
const void* ptr)
630-
{
631-
const auto& layerContext = getLayerContext();
632-
if (layerContext.isKnownAlloc(context, ptr)) {
633-
return layerContext.getAllocInfo(context, ptr).IsSVMPointer;
634-
}
635-
return false;
636-
}
637-
638605
static void parseSVMAllocProperties(
639606
const cl_svm_alloc_properties_khr* props,
640607
cl_device_id& device,
@@ -820,18 +787,23 @@ cl_int CL_API_CALL clSVMFreeWithPropertiesKHR_EMU(
820787
}
821788

822789
cl_int errorCode = CL_SUCCESS;
823-
if (isUSMPtr(context, ptr)) {
824-
cl_platform_id platform = getPlatform(context);
825-
const auto& USMFuncs = getLayerContext().getUSMFuncs(platform);
826-
errorCode = USMFuncs.clMemBlockingFreeINTEL(
827-
context,
828-
ptr);
829-
} else if (isSVMPtr(context, ptr)) {
830-
g_pNextDispatch->clSVMFree(
831-
context,
832-
ptr);
833-
} else if (isSystemPtr(context, ptr)) {
834-
align_free(ptr);
790+
if (getLayerContext().isKnownAlloc(context, ptr)) {
791+
const auto& allocInfo = getLayerContext().getAllocInfo(context, ptr);
792+
if (allocInfo.IsUSMPointer) {
793+
cl_platform_id platform = getPlatform(context);
794+
const auto& USMFuncs = getLayerContext().getUSMFuncs(platform);
795+
errorCode = USMFuncs.clMemBlockingFreeINTEL(
796+
context,
797+
ptr);
798+
} else if (allocInfo.IsSVMPointer) {
799+
g_pNextDispatch->clSVMFree(
800+
context,
801+
ptr);
802+
} else if (allocInfo.IsSystemPointer) {
803+
align_free(ptr);
804+
} else {
805+
errorCode = CL_INVALID_VALUE;
806+
}
835807
} else {
836808
errorCode = CL_INVALID_VALUE;
837809
}
@@ -1313,9 +1285,13 @@ cl_int CL_API_CALL clSetKernelArgSVMPointer_override(
13131285
cl_uint arg_index,
13141286
const void* arg_value)
13151287
{
1288+
std::lock_guard<std::mutex> lock(SLayerContext::Mutex);
13161289
cl_context context = getContext(kernel);
1290+
const void* base = nullptr;
1291+
SAllocInfo allocInfo;
1292+
getLayerContext().findAllocInfo(context, arg_value, base, allocInfo);
13171293

1318-
if (isUSMPtr(context, arg_value)) {
1294+
if (allocInfo.IsUSMPointer) {
13191295
cl_platform_id platform = getPlatform(context);
13201296
const auto& USMFuncs = getLayerContext().getUSMFuncs(platform);
13211297
return USMFuncs.clSetKernelArgMemPointerINTEL(
@@ -1336,6 +1312,7 @@ cl_int CL_API_CALL clSetKernelExecInfo_override(
13361312
size_t param_value_size,
13371313
const void* param_value)
13381314
{
1315+
std::lock_guard<std::mutex> lock(SLayerContext::Mutex);
13391316
switch (param_name) {
13401317
case CL_KERNEL_EXEC_INFO_SVM_INDIRECT_ACCESS_KHR:
13411318
{
@@ -1372,7 +1349,10 @@ cl_int CL_API_CALL clSetKernelExecInfo_override(
13721349
std::vector<const void*> nonNullSVMPtrs;
13731350
for (size_t i = 0; i < numPtrs; ++i) {
13741351
if (svmPtrs[i] != nullptr) {
1375-
if (isUSMPtr(context, svmPtrs[i])) {
1352+
const void* base = nullptr;
1353+
SAllocInfo allocInfo;
1354+
getLayerContext().findAllocInfo(context, svmPtrs[i], base, allocInfo);
1355+
if (allocInfo.IsUSMPointer) {
13761356
nonNullUSMPtrs.push_back(svmPtrs[i]);
13771357
} else {
13781358
nonNullSVMPtrs.push_back(svmPtrs[i]);
@@ -1411,7 +1391,11 @@ void CL_API_CALL clSVMFree_override(
14111391
void* ptr)
14121392
{
14131393
std::lock_guard<std::mutex> lock(SLayerContext::Mutex);
1414-
if (isUSMPtr(context, ptr)) {
1394+
const void* base = nullptr;
1395+
SAllocInfo allocInfo;
1396+
getLayerContext().findAllocInfo(context, ptr, base, allocInfo);
1397+
1398+
if (allocInfo.IsUSMPointer) {
14151399
cl_platform_id platform = getPlatform(context);
14161400
const auto& USMFuncs = getLayerContext().getUSMFuncs(platform);
14171401
USMFuncs.clMemFreeINTEL(context, ptr);
@@ -1486,7 +1470,15 @@ cl_int CL_API_CALL clEnqueueSVMMemcpy_override(
14861470
return CL_SUCCESS;
14871471
}
14881472

1489-
if (isUSMPtr(context, dst_ptr) || isUSMPtr(context, src_ptr)) {
1473+
const void* base = nullptr;
1474+
1475+
SAllocInfo dstAllocInfo;
1476+
getLayerContext().findAllocInfo(context, dst_ptr, base, dstAllocInfo);
1477+
1478+
SAllocInfo srcAllocInfo;
1479+
getLayerContext().findAllocInfo(context, src_ptr, base, srcAllocInfo);
1480+
1481+
if (dstAllocInfo.IsUSMPointer || srcAllocInfo.IsUSMPointer) {
14901482
cl_platform_id platform = getPlatform(context);
14911483
const auto& USMFuncs = getLayerContext().getUSMFuncs(platform);
14921484
cl_int ret = USMFuncs.clEnqueueMemcpyINTEL(
@@ -1545,7 +1537,10 @@ cl_int CL_API_CALL clEnqueueSVMMemFill_override(
15451537
return CL_SUCCESS;
15461538
}
15471539

1548-
if (isUSMPtr(context, svm_ptr)) {
1540+
const void* base = nullptr;
1541+
SAllocInfo allocInfo;
1542+
getLayerContext().findAllocInfo(context, svm_ptr, base, allocInfo);
1543+
if (allocInfo.IsUSMPointer) {
15491544
cl_platform_id platform = getPlatform(context);
15501545
const auto& USMFuncs = getLayerContext().getUSMFuncs(platform);
15511546
cl_int ret = USMFuncs.clEnqueueMemFillINTEL(

0 commit comments

Comments
 (0)