MIOpen icon indicating copy to clipboard operation
MIOpen copied to clipboard

Mark public API with "C" linkage as "noexcept"

Open averinevg opened this issue 3 years ago • 9 comments

I suggest to mark exported functions with C linkage as noexcept for better c++ support. Most of them wrapped in miopen::try_ internally, so it is possible to add noexcept to them.

averinevg avatar Feb 08 '22 12:02 averinevg

@averinevg Can you please explain here why this is needed (or provide a link if explanation exists somewhere)?

Please set appropriate category labels (bug, enhancement, quality...), urgency label and set milestone, if it is applicable (let's discuss if something is unclear). Thanks!

atamazov avatar Feb 08 '22 23:02 atamazov

Consider, for example, miopenCreate() function, which is declared as:

extern "C" miopenStatus_t miopenCreate(miopenHandle_t* handle);

The only information we have about this function is that it has a C linkage and some return value indicating the execution status. Functions with C linkage called from C++ are not implicitly noexcept, so the compiler will have to treat them as if they may throw. However we don't have any information about exceptions in the description provided in the header. I's very uncomfortable to get some unknown exception.

On the other side, library can be linked with C code. What will be if given function throws exception?

To avoid unpleasant consequences, I suggest marking exported functions as noexcept.

averinevg avatar Feb 09 '22 15:02 averinevg

@pfultz2 Can you please share your opinion on this?

atamazov avatar Feb 09 '22 16:02 atamazov

To avoid unpleasant consequences, I suggest marking exported functions as noexcept.

noexcept wont prevent us from throwing an exception. The application will just abort instead. Using noexcept is just useful to callers to detect if the function they are calling will throw an exceptions(ie noexcept(miopenCreate(&handle)) will return true instead of false).

We may want to have some kind of custom linting to make sure we are always catching the exceptions in our api calls.

pfultz2 avatar Feb 09 '22 23:02 pfultz2

Most of our API are always catch exceptions, they are wrapped in miopen::try_. We can add noexcept to them at first stage. And then prepare remaining functions for proper exception handling.

averinevg avatar Feb 10 '22 11:02 averinevg

We may want to have some kind of custom linting to make sure we are always catching the exceptions in our api calls.

Maybe something like this:

extern "C" miopenStatus_t miopenSomeFunction() noexcept
{
    static_assert(noexcept(miopenSomeFunctionInternal()));
    return miopenSomeFunctionInternal();
}

averinevg avatar Feb 10 '22 11:02 averinevg

@averinevg This looks good to me, please go ahead with PR ;)

atamazov avatar Feb 23 '22 22:02 atamazov

@averinevg Has this been resolved? If so, please close ticket. Thanks!

ppanchad-amd avatar Apr 16 '24 14:04 ppanchad-amd

@ppanchad-amd Not yet

averinevg avatar Apr 17 '24 16:04 averinevg