Skip to content

Commit 9a31975

Browse files
[SYCL][ESIMD][EMU] Replacing assert() in PI_API with return (#6275)
* [SYCL][ESIMD][EMU] Replacing assert() in PI_API with return - Returning PI_* error code
1 parent 0fdae34 commit 9a31975

File tree

1 file changed

+64
-69
lines changed

1 file changed

+64
-69
lines changed

sycl/plugins/esimd_emulator/pi_esimd_emulator.cpp

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ class ReturnHelper {
112112
// Controls PI level tracing prints.
113113
static bool PrintPiTrace = false;
114114

115+
static void PiTrace(std::string TraceString) {
116+
if (PrintPiTrace) {
117+
std::cout << TraceString << std::endl;
118+
}
119+
}
120+
115121
// Global variables used in PI_esimd_emulator
116122
// Note we only create a simple pointer variables such that C++ RT won't
117123
// deallocate them automatically at the end of the main program.
@@ -394,8 +400,8 @@ extern "C" {
394400
pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
395401
pi_uint32 *NumPlatforms) {
396402
static bool PiPlatformCachePopulated = false;
397-
static const char *PiTrace = std::getenv("SYCL_PI_TRACE");
398-
static const int PiTraceValue = PiTrace ? std::stoi(PiTrace) : 0;
403+
static const char *PiTraceEnv = std::getenv("SYCL_PI_TRACE");
404+
static const int PiTraceValue = PiTraceEnv ? std::stoi(PiTraceEnv) : 0;
399405

400406
if (PiTraceValue == -1) { // Means print all PI traces
401407
PrintPiTrace = true;
@@ -408,11 +414,9 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
408414
if (NumEntries == 0) {
409415
/// Runtime queries number of Platforms
410416
if (Platforms != nullptr) {
411-
if (PrintPiTrace) {
412-
std::cerr << "Invalid Arguments for piPlatformsGet of esimd_emultor "
413-
"(Platforms!=nullptr) while querying number of platforms"
414-
<< std::endl;
415-
}
417+
PiTrace("Invalid Arguments for piPlatformsGet of "
418+
"esimd_emulator (Platforms!=nullptr) "
419+
"while querying number of platforms");
416420
return PI_ERROR_INVALID_VALUE;
417421
}
418422
return PI_SUCCESS;
@@ -498,11 +502,8 @@ pi_result piDevicesGet(pi_platform Platform, pi_device_type DeviceType,
498502
if (NumEntries == 0) {
499503
/// Runtime queries number of devices
500504
if (Devices != nullptr) {
501-
if (PrintPiTrace) {
502-
std::cerr << "Invalid Arguments for piDevicesGet of esimd_emultor "
503-
"(Devices!=nullptr) while querying number of platforms"
504-
<< std::endl;
505-
}
505+
PiTrace("Invalid Arguments for piDevicesGet of esimd_emultor "
506+
"(Devices!=nullptr) while querying number of platforms");
506507
return PI_ERROR_INVALID_VALUE;
507508
}
508509
return PI_SUCCESS;
@@ -551,10 +552,9 @@ pi_result _pi_platform::populateDeviceCacheIfNeeded() {
551552
// e.g. CM version 7.3 => Device version = 703
552553

553554
if (((Version / 10) % 10) != 0) {
554-
if (PrintPiTrace) {
555-
std::cerr << "CM_EMU Device version info is incorrect : " << Version
556-
<< std::endl;
557-
}
555+
PiTrace("Invalid Arguments for piPlatformsGet of "
556+
"esimd_emulator (Platforms!=nullptr) "
557+
"while querying number of platforms");
558558
return PI_ERROR_INVALID_DEVICE;
559559
}
560560

@@ -906,21 +906,14 @@ pi_result piContextRelease(pi_context Context) {
906906
bool _pi_context::checkSurfaceArgument(pi_mem_flags Flags, void *HostPtr) {
907907
if (Flags & (PI_MEM_FLAGS_HOST_PTR_USE | PI_MEM_FLAGS_HOST_PTR_COPY)) {
908908
if (HostPtr == nullptr) {
909-
if (PrintPiTrace) {
910-
std::cerr << "HostPtr argument is required for "
911-
"PI_MEM_FLAGS_HOST_PTR_USE/COPY"
912-
<< std::endl;
913-
}
909+
PiTrace("HostPtr argument is required for "
910+
"PI_MEM_FLAGS_HOST_PTR_USE/COPY");
914911
return false;
915912
}
916913
// COPY and USE are mutually exclusive
917914
if ((Flags & (PI_MEM_FLAGS_HOST_PTR_USE | PI_MEM_FLAGS_HOST_PTR_COPY)) ==
918915
(PI_MEM_FLAGS_HOST_PTR_USE | PI_MEM_FLAGS_HOST_PTR_COPY)) {
919-
if (PrintPiTrace) {
920-
std::cerr
921-
<< "PI_MEM_FLAGS_HOST_PTR_USE and _COPY cannot be used together"
922-
<< std::endl;
923-
}
916+
PiTrace("PI_MEM_FLAGS_HOST_PTR_USE and _COPY cannot be used together");
924917
return false;
925918
}
926919
}
@@ -1010,10 +1003,7 @@ pi_result piMemBufferCreate(pi_context Context, pi_mem_flags Flags, size_t Size,
10101003
ARG_UNUSED(properties);
10111004

10121005
if ((Flags & PI_MEM_FLAGS_ACCESS_RW) == 0) {
1013-
if (PrintPiTrace) {
1014-
std::cerr << "Invalid memory attribute for piMemBufferCreate"
1015-
<< std::endl;
1016-
}
1006+
PiTrace("Invalid memory attribute for piMemBufferCreate");
10171007
return PI_ERROR_INVALID_OPERATION;
10181008
}
10191009

@@ -1069,9 +1059,11 @@ pi_result piMemBufferCreate(pi_context Context, pi_mem_flags Flags, size_t Size,
10691059
}
10701060

10711061
std::lock_guard<std::mutex> Lock{*PiESimdSurfaceMapLock};
1072-
assert(PiESimdSurfaceMap->find((*RetMem)->SurfaceIndex) ==
1073-
PiESimdSurfaceMap->end() &&
1074-
"Failure from CM-managed buffer creation");
1062+
if (PiESimdSurfaceMap->find((*RetMem)->SurfaceIndex) !=
1063+
PiESimdSurfaceMap->end()) {
1064+
PiTrace("Failure from CM-managed buffer creation");
1065+
return PI_ERROR_INVALID_MEM_OBJECT;
1066+
}
10751067

10761068
(*PiESimdSurfaceMap)[(*RetMem)->SurfaceIndex] = *RetMem;
10771069

@@ -1099,8 +1091,10 @@ pi_result piMemRelease(pi_mem Mem) {
10991091
// Removing Surface-map entry
11001092
std::lock_guard<std::mutex> Lock{*PiESimdSurfaceMapLock};
11011093
auto MapEntryIt = PiESimdSurfaceMap->find(Mem->SurfaceIndex);
1102-
assert(MapEntryIt != PiESimdSurfaceMap->end() &&
1103-
"Failure from Buffer/Image deletion");
1094+
if (MapEntryIt == PiESimdSurfaceMap->end()) {
1095+
PiTrace("Failure from Buffer/Image deletion");
1096+
return PI_ERROR_INVALID_MEM_OBJECT;
1097+
}
11041098
PiESimdSurfaceMap->erase(MapEntryIt);
11051099
delete Mem;
11061100
}
@@ -1122,8 +1116,8 @@ _pi_mem::~_pi_mem() {
11221116
Status = CmDevice->DestroySurface(SurfacePtr.RegularImgPtr);
11231117
}
11241118

1125-
assert(Status == cm_support::CM_SUCCESS &&
1126-
"Surface Deletion Failure from CM_EMU");
1119+
cl::sycl::detail::pi::assertion(Status == cm_support::CM_SUCCESS &&
1120+
"Surface Deletion Failure from CM_EMU");
11271121

11281122
for (auto mapit = Mappings.begin(); mapit != Mappings.end();) {
11291123
mapit = Mappings.erase(mapit);
@@ -1160,9 +1154,7 @@ pi_result piMemImageCreate(pi_context Context, pi_mem_flags Flags,
11601154
const pi_image_desc *ImageDesc, void *HostPtr,
11611155
pi_mem *RetImage) {
11621156
if ((Flags & PI_MEM_FLAGS_ACCESS_RW) == 0) {
1163-
if (PrintPiTrace) {
1164-
std::cerr << "Invalid memory attribute for piMemImageCreate" << std::endl;
1165-
}
1157+
PiTrace("Invalid memory attribute for piMemImageCreate");
11661158
return PI_ERROR_INVALID_OPERATION;
11671159
}
11681160

@@ -1265,9 +1257,11 @@ pi_result piMemImageCreate(pi_context Context, pi_mem_flags Flags,
12651257
}
12661258

12671259
std::lock_guard<std::mutex> Lock{*PiESimdSurfaceMapLock};
1268-
assert(PiESimdSurfaceMap->find((*RetImage)->SurfaceIndex) ==
1269-
PiESimdSurfaceMap->end() &&
1270-
"Failure from CM-managed image creation");
1260+
if (PiESimdSurfaceMap->find((*RetImage)->SurfaceIndex) !=
1261+
PiESimdSurfaceMap->end()) {
1262+
PiTrace("Failure from CM-managed image creation");
1263+
return PI_ERROR_INVALID_VALUE;
1264+
}
12711265

12721266
(*PiESimdSurfaceMap)[(*RetImage)->SurfaceIndex] = *RetImage;
12731267

@@ -1397,10 +1391,7 @@ pi_result piEventGetProfilingInfo(pi_event Event, pi_profiling_info ParamName,
13971391
ARG_UNUSED(ParamValue);
13981392
ARG_UNUSED(ParamValueSizeRet);
13991393

1400-
if (PrintPiTrace) {
1401-
std::cerr << "Warning : Profiling Not supported under PI_ESIMD_EMULATOR"
1402-
<< std::endl;
1403-
}
1394+
PiTrace("Warning : Profiling Not supported under PI_ESIMD_EMULATOR");
14041395
return PI_SUCCESS;
14051396
}
14061397

@@ -1503,12 +1494,15 @@ pi_result piEnqueueMemBufferRead(pi_queue Queue, pi_mem Src,
15031494

15041495
/// TODO : Support Blocked read, 'Queue' handling
15051496
if (BlockingRead) {
1506-
assert(false &&
1507-
"ESIMD_EMULATOR support for blocking piEnqueueMemBufferRead is NYI");
1497+
PiTrace(
1498+
"ESIMD_EMULATOR support for blocking piEnqueueMemBufferRead is NYI");
1499+
return PI_ERROR_INVALID_OPERATION;
15081500
}
15091501

1510-
assert(Offset == 0 &&
1511-
"ESIMD_EMULATOR does not support buffer reading with offsets");
1502+
if (Offset != 0) {
1503+
PiTrace("ESIMD_EMULATOR does not support buffer reading with offsets");
1504+
return PI_ERROR_INVALID_ARG_VALUE;
1505+
}
15121506

15131507
if (NumEventsInWaitList != 0) {
15141508
return PI_ERROR_INVALID_EVENT_WAIT_LIST;
@@ -1527,7 +1521,9 @@ pi_result piEnqueueMemBufferRead(pi_queue Queue, pi_mem Src,
15271521
// Surface. memcpy is used for BufferRead PI_API call.
15281522
memcpy(Dst, buf->MapHostPtr, Size);
15291523
} else {
1530-
assert(buf->SurfacePtr.tag == cm_surface_ptr_t::TypeRegularBuffer);
1524+
if (buf->SurfacePtr.tag != cm_surface_ptr_t::TypeRegularBuffer) {
1525+
return PI_ERROR_INVALID_MEM_OBJECT;
1526+
}
15311527
int Status = buf->SurfacePtr.RegularBufPtr->ReadSurface(
15321528
reinterpret_cast<unsigned char *>(Dst),
15331529
nullptr, // event
@@ -1620,10 +1616,7 @@ pi_result piEnqueueMemBufferMap(pi_queue Queue, pi_mem MemObj,
16201616
// because mapping already exists.
16211617
if (!Res.second) {
16221618
ret = PI_ERROR_INVALID_VALUE;
1623-
if (PrintPiTrace) {
1624-
std::cerr << "piEnqueueMemBufferMap: duplicate mapping detected"
1625-
<< std::endl;
1626-
}
1619+
PiTrace("piEnqueueMemBufferMap: duplicate mapping detected");
16271620
}
16281621
}
16291622

@@ -1657,9 +1650,7 @@ pi_result piEnqueueMemUnmap(pi_queue Queue, pi_mem MemObj, void *MappedPtr,
16571650
auto It = MemObj->Mappings.find(MappedPtr);
16581651
if (It == MemObj->Mappings.end()) {
16591652
ret = PI_ERROR_INVALID_VALUE;
1660-
if (PrintPiTrace) {
1661-
std::cerr << "piEnqueueMemUnmap: unknown memory mapping" << std::endl;
1662-
}
1653+
PiTrace("piEnqueueMemUnmap: unknown memory mapping");
16631654
}
16641655
MemObj->Mappings.erase(It);
16651656
}
@@ -1688,16 +1679,22 @@ pi_result piEnqueueMemImageRead(pi_queue CommandQueue, pi_mem Image,
16881679

16891680
/// TODO : Support Blocked read, 'Queue' handling
16901681
if (BlockingRead) {
1691-
assert(false && "ESIMD_EMULATOR does not support Blocking Read");
1682+
PiTrace("ESIMD_EMULATOR support for blocking piEnqueueMemImageRead is NYI");
1683+
return PI_ERROR_INVALID_OPERATION;
16921684
}
16931685

16941686
// SlicePitch is for 3D image while ESIMD_EMULATOR does not
16951687
// support. For 2D surfaces, SlicePitch must be 0.
1696-
assert((SlicePitch == 0) && "ESIMD_EMULATOR does not support 3D-image");
1688+
if (SlicePitch != 0) {
1689+
PiTrace("ESIMD_EMULATOR does not support 3D-image");
1690+
return PI_ERROR_INVALID_ARG_VALUE;
1691+
}
16971692

16981693
// CM_EMU does not support ReadSurface with offset
1699-
assert(Origin->x == 0 && Origin->y == 0 && Origin->z == 0 &&
1700-
"ESIMD_EMULATOR does not support 2D-image reading with offsets");
1694+
if (Origin->x != 0 || Origin->y != 0 || Origin->z != 0) {
1695+
PiTrace("ESIMD_EMULATOR does not support 2D-image reading with offsets");
1696+
return PI_ERROR_INVALID_ARG_VALUE;
1697+
}
17011698

17021699
_pi_image *PiImg = static_cast<_pi_image *>(Image);
17031700

@@ -1714,7 +1711,9 @@ pi_result piEnqueueMemImageRead(pi_queue CommandQueue, pi_mem Image,
17141711
// Surface. memcpy is used for ImageRead PI_API call.
17151712
memcpy(Ptr, PiImg->MapHostPtr, Size);
17161713
} else {
1717-
assert(PiImg->SurfacePtr.tag == cm_surface_ptr_t::TypeRegularImage);
1714+
if (PiImg->SurfacePtr.tag != cm_surface_ptr_t::TypeRegularImage) {
1715+
return PI_ERROR_INVALID_MEM_OBJECT;
1716+
}
17181717
int Status = PiImg->SurfacePtr.RegularImgPtr->ReadSurface(
17191718
reinterpret_cast<unsigned char *>(Ptr),
17201719
nullptr, // event
@@ -1949,11 +1948,7 @@ pi_result piextDeviceSelectBinary(pi_device, pi_device_binary *,
19491948
/// TODO : Support multiple images and enable selection algorithm
19501949
/// for the images
19511950
if (RawImgSize != 1) {
1952-
if (PrintPiTrace) {
1953-
std::cerr
1954-
<< "Only single device binary image is supported in ESIMD_EMULATOR"
1955-
<< std::endl;
1956-
}
1951+
PiTrace("Only single device binary image is supported in ESIMD_EMULATOR");
19571952
return PI_ERROR_INVALID_VALUE;
19581953
}
19591954
*ImgInd = 0;

0 commit comments

Comments
 (0)