-
Notifications
You must be signed in to change notification settings - Fork 623
Allowing an error handler from caller #12487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ Kernel* registered_kernels = reinterpret_cast<Kernel*>(registered_kernels_data); | |
size_t num_registered_kernels = 0; | ||
|
||
// Registers the kernels, but may return an error. | ||
Error register_kernels_internal(const Span<const Kernel> kernels) { | ||
Error register_kernels_internal(const Span<const Kernel> kernels, ErrorHandler errorHandler) { | ||
// Operator registration happens in static initialization time before or after | ||
// PAL init, so call it here. It is safe to call multiple times. | ||
::et_pal_init(); | ||
|
@@ -74,12 +74,19 @@ Error register_kernels_internal(const Span<const Kernel> kernels) { | |
ET_LOG(Error, "%s", kernels[i].name_); | ||
ET_LOG_KERNEL_KEY(kernels[i].kernel_key_); | ||
} | ||
|
||
if (errorHandler != nullptr) { | ||
return errorHandler(Error::RegistrationExceedingMaxKernels); | ||
} | ||
|
||
return Error::RegistrationExceedingMaxKernels; | ||
} | ||
// for debugging purpose | ||
ET_UNUSED const char* lib_name = | ||
et_pal_get_shared_library_name(kernels.data()); | ||
|
||
Error err = Error::Ok; | ||
|
||
for (const auto& kernel : kernels) { | ||
// Linear search. This is fine if the number of kernels is small. | ||
for (size_t i = 0; i < num_registered_kernels; i++) { | ||
|
@@ -88,24 +95,33 @@ Error register_kernels_internal(const Span<const Kernel> kernels) { | |
kernel.kernel_key_ == k.kernel_key_) { | ||
ET_LOG(Error, "Re-registering %s, from %s", k.name_, lib_name); | ||
ET_LOG_KERNEL_KEY(k.kernel_key_); | ||
return Error::RegistrationAlreadyRegistered; | ||
err = Error::RegistrationAlreadyRegistered; | ||
continue; | ||
} | ||
} | ||
|
||
registered_kernels[num_registered_kernels++] = kernel; | ||
} | ||
ET_LOG( | ||
Debug, | ||
"Successfully registered all kernels from shared library: %s", | ||
lib_name); | ||
|
||
return Error::Ok; | ||
if (errorHandler != nullptr) { | ||
err = errorHandler(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes me a bit nervous. Why do we want to consume an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not against an error handler, just not sure what are we doing with the returned error here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A pattern I would definitely against is to convert |
||
} | ||
|
||
if (err == Error::Ok) { | ||
ET_LOG( | ||
Debug, | ||
"Successfully registered all kernels from shared library: %s", | ||
lib_name); | ||
} | ||
|
||
return err; | ||
} | ||
|
||
} // namespace | ||
|
||
// Registers the kernels, but panics if an error occurs. Always returns Ok. | ||
Error register_kernels(const Span<const Kernel> kernels) { | ||
Error success = register_kernels_internal(kernels); | ||
Error register_kernels(const Span<const Kernel> kernels, ErrorHandler errorHandler) { | ||
Error success = register_kernels_internal(kernels, errorHandler); | ||
if (success == Error::RegistrationAlreadyRegistered || | ||
success == Error::RegistrationExceedingMaxKernels) { | ||
ET_CHECK_MSG( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the return value not sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess what situation exists where you can pass in the errorHandler to operate on the error generated by this function, but you couldnt just operate on it after its returned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If all errors are returned, then we can just operate on it after it is returned, but there are 2 errors (
RegistrationExceedingMaxKernels
andRegistrationAlreadyRegistered
) are not returned (see this line). If the caller wants to handle these 2 errors, the caller needs to specify theerrorHandler
to get a chance to handle these errors.