Skip to content

Commit a643bc9

Browse files
authored
hip: api: generalize error handling (#9170)
* hip: api: generalize error handling Generalize error handling for XRT hip external APIs. This modification doesn't change the return error values or how it captures errors. Instead, it provide template function to handle error and return error, which reduces code duplication and makes it easier for future error capturing change and error code update. Signed-off-by: Wendy Liang <wendy.liang@amd.com> * hip: api: device: fix unreachable code warning on windows Due to hip_get_device_properties() and hip_device_get_attribute() always throw error due to not implemented, MSVC compiler will throw code unreachable warning: return handle_hip_func_error(__func__, hipErrorUnknown, [&] { throw_invalid_value_if(!props, "arg passed is nullptr"); *props = xrt::core::hip::hip_get_device_properties(device); }); unreachable warning code: `*props = xrt::core::hip::hip_get_device_properties(device);` And thus change it to pass the props to the hip_get_device_properties() to avoid this warning. Or we will need to remove hip_get_device_properties() and just always returns unsupported error from top wrapper function. The same for hip_device_get_attribute(). Signed-off-by: Wendy Liang <wendy.liang@amd.com> --------- Signed-off-by: Wendy Liang <wendy.liang@amd.com>
1 parent 0f682bd commit a643bc9

File tree

7 files changed

+182
-406
lines changed

7 files changed

+182
-406
lines changed

src/runtime_src/hip/api/hip_context.cpp

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -129,109 +129,55 @@ hip_device_primary_ctx_retain(device_handle dev)
129129
hipError_t
130130
hipCtxCreate(hipCtx_t* ctx, unsigned int flags, hipDevice_t device)
131131
{
132-
try {
132+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
133133
throw_invalid_value_if(!ctx, "ctx passed is nullptr");
134134

135135
auto handle = xrt::core::hip::hip_ctx_create(flags, device);
136136
*ctx = reinterpret_cast<hipCtx_t>(handle);
137-
return hipSuccess;
138-
}
139-
catch (const xrt_core::system_error& ex) {
140-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
141-
return static_cast<hipError_t>(ex.value());
142-
}
143-
catch (const std::exception& ex) {
144-
xrt_core::send_exception_message(ex.what());
145-
}
146-
return hipErrorUnknown;
137+
});
147138
}
148139

149140
hipError_t
150141
hipCtxDestroy(hipCtx_t ctx)
151142
{
152-
try {
143+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
153144
xrt::core::hip::hip_ctx_destroy(ctx);
154-
return hipSuccess;
155-
}
156-
catch (const xrt_core::system_error& ex) {
157-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
158-
return static_cast<hipError_t>(ex.value());
159-
}
160-
catch (const std::exception& ex) {
161-
xrt_core::send_exception_message(ex.what());
162-
}
163-
return hipErrorUnknown;
145+
});
164146
}
165147

166148
hipError_t
167149
hipCtxGetDevice(hipDevice_t* device)
168150
{
169-
try {
151+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
170152
throw_invalid_value_if(!device, "device passed is nullptr");
171153

172154
*device = static_cast<int>(xrt::core::hip::hip_ctx_get_device());
173-
return hipSuccess;
174-
}
175-
catch (const xrt_core::system_error& ex) {
176-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
177-
return static_cast<hipError_t>(ex.value());
178-
}
179-
catch (const std::exception& ex) {
180-
xrt_core::send_exception_message(ex.what());
181-
}
182-
return hipErrorUnknown;
155+
});
183156
}
184157

185158
hipError_t
186159
hipCtxSetCurrent(hipCtx_t ctx)
187160
{
188-
try {
161+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
189162
xrt::core::hip::hip_ctx_set_current(ctx);
190-
return hipSuccess;
191-
}
192-
catch (const xrt_core::system_error& ex) {
193-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
194-
return static_cast<hipError_t>(ex.value());
195-
}
196-
catch (const std::exception& ex) {
197-
xrt_core::send_exception_message(ex.what());
198-
}
199-
return hipErrorUnknown;
163+
});
200164
}
201165

202166
hipError_t
203167
hipDevicePrimaryCtxRetain(hipCtx_t* pctx, hipDevice_t dev)
204168
{
205-
try {
169+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
206170
throw_invalid_value_if(!pctx, "nullptr passed");
207171

208172
auto handle = xrt::core::hip::hip_device_primary_ctx_retain(dev);
209173
*pctx = reinterpret_cast<hipCtx_t>(handle);
210-
return hipSuccess;
211-
}
212-
catch (const xrt_core::system_error& ex) {
213-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
214-
return static_cast<hipError_t>(ex.value());
215-
}
216-
catch (const std::exception& ex) {
217-
xrt_core::send_exception_message(ex.what());
218-
}
219-
return hipErrorUnknown;
174+
});
220175
}
221176

222177
hipError_t
223178
hipDevicePrimaryCtxRelease(hipDevice_t dev)
224179
{
225-
try {
180+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
226181
xrt::core::hip::hip_device_primary_ctx_release(dev);
227-
return hipSuccess;
228-
}
229-
catch (const xrt_core::system_error& ex) {
230-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
231-
return static_cast<hipError_t>(ex.value());
232-
}
233-
catch (const std::exception& ex) {
234-
xrt_core::send_exception_message(ex.what());
235-
}
236-
return hipErrorUnknown;
182+
});
237183
}

src/runtime_src/hip/api/hip_device.cpp

Lines changed: 31 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ hip_device_get_name(hipDevice_t device)
105105
return (xrt_core::device_query<xrt_core::query::rom_vbnv>((device_cache.get_or_error(device))->get_xrt_device().get_handle()));
106106
}
107107

108-
static hipDeviceProp_t
109-
hip_get_device_properties(hipDevice_t device)
108+
static void
109+
hip_get_device_properties(hipDeviceProp_t* props, hipDevice_t device)
110110
{
111+
throw_invalid_value_if(!props, "arg passed is nullptr");
111112
throw_invalid_device_if(check(device), "device requested is not available");
112113

113114
throw std::runtime_error("Not implemented");
@@ -127,9 +128,10 @@ hip_device_get_uuid(hipDevice_t device)
127128
return uid;
128129
}
129130

130-
static int
131-
hip_device_get_attribute(hipDeviceAttribute_t attr, int device)
131+
static void
132+
hip_device_get_attribute(int* pi, hipDeviceAttribute_t attr, int device)
132133
{
134+
throw_invalid_value_if(!pi, "arg passed is nullptr");
133135
throw_invalid_device_if(check(device), "device requested is not available");
134136

135137
throw std::runtime_error("Not implemented");
@@ -156,62 +158,32 @@ hip_kernel_name_ref(const hipFunction_t f)
156158
hipError_t
157159
hipInit(unsigned int flags)
158160
{
159-
try {
160-
xrt::core::hip::hip_init(flags);
161-
return hipSuccess;
162-
}
163-
catch (const xrt_core::system_error& ex) {
164-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
165-
return static_cast<hipError_t>(ex.value());
166-
}
167-
catch (const std::exception& ex) {
168-
xrt_core::send_exception_message(ex.what());
169-
}
170-
return hipErrorNotInitialized;
161+
return handle_hip_func_error(__func__, hipErrorNotInitialized, [&] {
162+
xrt::core::hip::hip_init(flags); });
171163
}
172164

173165
hipError_t
174166
hipGetDeviceCount(size_t* count)
175167
{
176-
try {
168+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
177169
throw_invalid_value_if(!count, "arg passed is nullptr");
178-
179170
*count = xrt::core::hip::hip_get_device_count();
180-
return hipSuccess;
181-
}
182-
catch (const xrt_core::system_error& ex) {
183-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
184-
return static_cast<hipError_t>(ex.value());
185-
}
186-
catch (const std::exception& ex) {
187-
xrt_core::send_exception_message(ex.what());
188-
}
189-
return hipErrorUnknown;
171+
});
190172
}
191173

192174
hipError_t
193175
hipDeviceGet(hipDevice_t* device, int ordinal)
194176
{
195-
try {
177+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
196178
throw_invalid_value_if(!device, "device is nullptr");
197-
198179
*device = xrt::core::hip::hip_device_get(ordinal);
199-
return hipSuccess;
200-
}
201-
catch (const xrt_core::system_error& ex) {
202-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
203-
return static_cast<hipError_t>(ex.value());
204-
}
205-
catch (const std::exception& ex) {
206-
xrt_core::send_exception_message(ex.what());
207-
}
208-
return hipErrorUnknown;
180+
});
209181
}
210182

211183
hipError_t
212184
hipDeviceGetName(char* name, int len, hipDevice_t device)
213185
{
214-
try {
186+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
215187
throw_invalid_value_if((!name || len <= 0), "invalid arg");
216188

217189
auto name_str = xrt::core::hip::hip_device_get_name(device);
@@ -220,16 +192,7 @@ hipDeviceGetName(char* name, int len, hipDevice_t device)
220192
auto cpy_size = (static_cast<size_t>(len) <= (name_str.length() + 1) ? (len - 1) : name_str.length());
221193
std::memcpy(name, name_str.c_str(), cpy_size);
222194
name[cpy_size] = '\0';
223-
return hipSuccess;
224-
}
225-
catch (const xrt_core::system_error& ex) {
226-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
227-
return static_cast<hipError_t>(ex.value());
228-
}
229-
catch (const std::exception& ex) {
230-
xrt_core::send_exception_message(ex.what());
231-
}
232-
return hipErrorUnknown;
195+
});
233196
}
234197

235198
#if HIP_VERSION >= 60000000
@@ -256,91 +219,47 @@ hipError_t
256219
hipGetDeviceProperties(hipDeviceProp_t* props, hipDevice_t device)
257220
#endif
258221
{
259-
try {
260-
throw_invalid_value_if(!props, "arg passed is nullptr");
261-
262-
*props = xrt::core::hip::hip_get_device_properties(device);
263-
return hipSuccess;
264-
}
265-
catch (const xrt_core::system_error& ex) {
266-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
267-
return static_cast<hipError_t>(ex.value());
268-
}
269-
catch (const std::exception& ex) {
270-
xrt_core::send_exception_message(ex.what());
271-
}
272-
return hipErrorUnknown;
222+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
223+
xrt::core::hip::hip_get_device_properties(props, device);
224+
});
273225
}
274226

275227
hipError_t
276228
hipDeviceGetUuid(hipUUID* uuid, hipDevice_t device)
277229
{
278-
try {
230+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
279231
throw_invalid_value_if(!uuid, "arg passed is nullptr");
280-
281232
*uuid = xrt::core::hip::hip_device_get_uuid(device);
282-
return hipSuccess;
283-
}
284-
catch (const xrt_core::system_error& ex) {
285-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
286-
return static_cast<hipError_t>(ex.value());
287-
}
288-
catch (const std::exception& ex) {
289-
xrt_core::send_exception_message(ex.what());
290-
}
291-
return hipErrorUnknown;
233+
});
292234
}
293235

294236
hipError_t
295237
hipDeviceGetAttribute(int* pi, hipDeviceAttribute_t attr, int device)
296238
{
297-
try {
298-
throw_invalid_value_if(!pi, "arg passed is nullptr");
299-
300-
*pi = xrt::core::hip::hip_device_get_attribute(attr, device);
301-
return hipSuccess;
302-
}
303-
catch (const xrt_core::system_error& ex) {
304-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
305-
return static_cast<hipError_t>(ex.value());
306-
}
307-
catch (const std::exception& ex) {
308-
xrt_core::send_exception_message(ex.what());
309-
}
310-
return hipErrorUnknown;
239+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
240+
xrt::core::hip::hip_device_get_attribute(pi, attr, device);
241+
});
311242
}
312243

313244
hipError_t
314245
hipSetDevice(int device)
315246
{
316-
try {
247+
return handle_hip_func_error(__func__, hipErrorUnknown, [&] {
317248
xrt::core::hip::hip_set_device(device);
318-
return hipSuccess;
319-
}
320-
catch (const xrt_core::system_error& ex) {
321-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
322-
return static_cast<hipError_t>(ex.value());
323-
}
324-
catch (const std::exception& ex) {
325-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
326-
}
327-
return hipErrorUnknown;
249+
});
328250
}
329251

330252
const char *
331253
hipKernelNameRef(const hipFunction_t f)
332254
{
333-
try {
255+
const char* kname_ref = nullptr;
256+
hipError_t err = handle_hip_func_error(__func__, hipErrorInvalidValue, [&] {
334257
throw_invalid_value_if(!f, "arg passed is nullptr");
258+
kname_ref = xrt::core::hip::hip_kernel_name_ref(f);
259+
});
335260

336-
return xrt::core::hip::hip_kernel_name_ref(f);
337-
}
338-
catch (const xrt_core::system_error& ex) {
339-
xrt_core::send_exception_message(std::string(__func__) + " - " + ex.what());
261+
if (err != hipSuccess)
340262
return nullptr;
341-
}
342-
catch (const std::exception& ex) {
343-
xrt_core::send_exception_message(ex.what());
344-
}
345-
return nullptr;
263+
264+
return kname_ref;
346265
}

0 commit comments

Comments
 (0)