Skip to content

Commit 730f305

Browse files
author
Hugh Delaney
committed
Make all calls to new safe in HIP
Make all calls to `new` safe in HIP adapter.
1 parent e05621a commit 730f305

File tree

6 files changed

+59
-42
lines changed

6 files changed

+59
-42
lines changed

source/adapters/hip/context.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(
5050
*phContext = ContextPtr.release();
5151
} catch (ur_result_t Err) {
5252
RetErr = Err;
53+
} catch (std::bad_alloc &) {
54+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
5355
} catch (...) {
54-
RetErr = UR_RESULT_ERROR_OUT_OF_RESOURCES;
56+
return UR_RESULT_ERROR_UNKNOWN;
5557
}
5658
return RetErr;
5759
}

source/adapters/hip/kernel.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ urKernelCreate(ur_program_handle_t hProgram, const char *pKernelName,
4646
hProgram, hProgram->getContext()});
4747
} catch (ur_result_t Err) {
4848
Result = Err;
49+
} catch (std::bad_alloc &) {
50+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
4951
} catch (...) {
50-
Result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
52+
return UR_RESULT_ERROR_UNKNOWN;
5153
}
5254

5355
*phKernel = RetKernel.release();

source/adapters/hip/memory.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
150150
RetMemObj = URMemObj.release();
151151
} catch (ur_result_t Err) {
152152
Result = Err;
153-
} catch (std::bad_alloc &Err) {
153+
} catch (std::bad_alloc &) {
154154
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
155155
} catch (...) {
156156
Result = UR_RESULT_ERROR_OUT_OF_RESOURCES;
@@ -213,9 +213,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferPartition(
213213
} catch (ur_result_t Err) {
214214
*phMem = nullptr;
215215
return Err;
216-
} catch (...) {
216+
} catch (std::bad_alloc &) {
217217
*phMem = nullptr;
218218
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
219+
} catch (...) {
220+
*phMem = nullptr;
221+
return UR_RESULT_ERROR_UNKNOWN;
219222
}
220223

221224
ReleaseGuard.dismiss();
@@ -391,7 +394,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreate(
391394
*phMem = URMemObj.release();
392395
} catch (ur_result_t Err) {
393396
return Err;
394-
} catch (std::bad_alloc &Err) {
397+
} catch (std::bad_alloc &) {
395398
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
396399
} catch (...) {
397400
return UR_RESULT_ERROR_UNKNOWN;

source/adapters/hip/program.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -467,35 +467,37 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
467467
hDevice) != hContext->getDevices().end(),
468468
UR_RESULT_ERROR_INVALID_CONTEXT);
469469

470-
ur_result_t Result = UR_RESULT_SUCCESS;
471-
472-
std::unique_ptr<ur_program_handle_t_> RetProgram{
473-
new ur_program_handle_t_{hContext, hDevice}};
474-
475-
if (pProperties) {
476-
if (pProperties->count > 0 && pProperties->pMetadatas == nullptr) {
477-
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
478-
} else if (pProperties->count == 0 && pProperties->pMetadatas != nullptr) {
479-
return UR_RESULT_ERROR_INVALID_SIZE;
470+
try {
471+
std::unique_ptr<ur_program_handle_t_> RetProgram{
472+
new ur_program_handle_t_{hContext, hDevice}};
473+
474+
if (pProperties) {
475+
if (pProperties->count > 0 && pProperties->pMetadatas == nullptr) {
476+
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
477+
} else if (pProperties->count == 0 &&
478+
pProperties->pMetadatas != nullptr) {
479+
return UR_RESULT_ERROR_INVALID_SIZE;
480+
}
481+
UR_CHECK_ERROR(
482+
RetProgram->setMetadata(pProperties->pMetadatas, pProperties->count));
480483
}
481-
Result =
482-
RetProgram->setMetadata(pProperties->pMetadatas, pProperties->count);
483-
UR_ASSERT(Result == UR_RESULT_SUCCESS, Result);
484-
}
485484

486-
auto pBinary_string = reinterpret_cast<const char *>(pBinary);
487-
if (size == 0) {
488-
size = strlen(pBinary_string) + 1;
489-
}
490-
491-
UR_ASSERT(size, UR_RESULT_ERROR_INVALID_SIZE);
485+
auto pBinary_string = reinterpret_cast<const char *>(pBinary);
486+
if (size == 0) {
487+
size = strlen(pBinary_string) + 1;
488+
}
492489

493-
Result = RetProgram->setBinary(pBinary_string, size);
494-
UR_ASSERT(Result == UR_RESULT_SUCCESS, Result);
490+
UR_ASSERT(size, UR_RESULT_ERROR_INVALID_SIZE);
495491

496-
*phProgram = RetProgram.release();
492+
UR_CHECK_ERROR(RetProgram->setBinary(pBinary_string, size));
497493

498-
return Result;
494+
*phProgram = RetProgram.release();
495+
} catch (std::bad_alloc &) {
496+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
497+
} catch (...) {
498+
return UR_RESULT_ERROR_UNKNOWN;
499+
}
500+
return UR_RESULT_SUCCESS;
499501
}
500502

501503
// This entry point is only used for native specialization constants (SPIR-V),

source/adapters/hip/queue.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,10 @@ urQueueCreate(ur_context_handle_t hContext, ur_device_handle_t hDevice,
155155
return UR_RESULT_SUCCESS;
156156
} catch (ur_result_t Err) {
157157
return Err;
158+
} catch (std::bad_alloc &) {
159+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
158160
} catch (...) {
159-
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
161+
return UR_RESULT_ERROR_UNKNOWN;
160162
}
161163
}
162164

source/adapters/hip/sampler.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,26 @@
1414
ur_result_t urSamplerCreate(ur_context_handle_t hContext,
1515
const ur_sampler_desc_t *pDesc,
1616
ur_sampler_handle_t *phSampler) {
17-
std::unique_ptr<ur_sampler_handle_t_> RetImplSampl{
18-
new ur_sampler_handle_t_(hContext)};
17+
try {
18+
std::unique_ptr<ur_sampler_handle_t_> RetImplSampl{
19+
new ur_sampler_handle_t_(hContext)};
1920

20-
if (pDesc && pDesc->stype == UR_STRUCTURE_TYPE_SAMPLER_DESC) {
21-
RetImplSampl->Props |= pDesc->normalizedCoords;
22-
RetImplSampl->Props |= pDesc->filterMode << 1;
23-
RetImplSampl->Props |= pDesc->addressingMode << 2;
24-
} else {
25-
// Set default values
26-
RetImplSampl->Props |= true; // Normalized Coords
27-
RetImplSampl->Props |= UR_SAMPLER_ADDRESSING_MODE_CLAMP << 2;
28-
}
21+
if (pDesc && pDesc->stype == UR_STRUCTURE_TYPE_SAMPLER_DESC) {
22+
RetImplSampl->Props |= pDesc->normalizedCoords;
23+
RetImplSampl->Props |= pDesc->filterMode << 1;
24+
RetImplSampl->Props |= pDesc->addressingMode << 2;
25+
} else {
26+
// Set default values
27+
RetImplSampl->Props |= true; // Normalized Coords
28+
RetImplSampl->Props |= UR_SAMPLER_ADDRESSING_MODE_CLAMP << 2;
29+
}
2930

30-
*phSampler = RetImplSampl.release();
31+
*phSampler = RetImplSampl.release();
32+
} catch (std::bad_alloc &) {
33+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
34+
} catch (...) {
35+
return UR_RESULT_ERROR_UNKNOWN;
36+
}
3137
return UR_RESULT_SUCCESS;
3238
}
3339

0 commit comments

Comments
 (0)