[llvm-dev] [Exception Handling] Could we mark __cxa_end_catch as nounwind conditionally? (original) (raw)
Fāng-ruì Sòng via llvm-dev llvm-dev at lists.llvm.org
Sun Aug 29 23:59:11 PDT 2021
- Previous message: [llvm-dev] [Exception Handling] Could we mark __cxa_end_catch as nounwind conditionally?
- Next message: [llvm-dev] [Exception Handling] Could we mark __cxa_end_catch as nounwind conditionally?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2021-08-30, chuanqi.xcq wrote:
Hi all,
Let me introduce about the background: I find that the compiler couldn't mark
foo
asnounwind
in the following example:_ _void bar();_ _void foo() {_ _try {_ _bar();_ _} catch(...) {}_ _}_ _
From my perspective, it is clear that foo wouldn't throw any exception. So it is natural to me that the compiler could mark foo as nounwind as an optimization. But it didn't. This pattern occurs frequently in C++20 coroutine. So I tried to handle coroutine specially before in: https://reviews.llvm.org/D108277. But the all the reviewers strongly suggested that we should handle this case generally for all of functions instead of coroutines only. _Then when I looked into the details in IR, I found the reason is that cxaendcatch isn't nounwind. Here is the IR generated:_ _; Function Attrs: mustprogress uwtable_ _define dsolocal void @Z3foov() localunnamedaddr #0 personality i8* bitcast (i32 (...)* @_gxxpersonalityv0 to i8*) {_ _invoke void @Z3barv()_ _to label %5 unwind label %1_ _1: ; preds = %0_ _%2 = landingpad { i8*, i32 }_ _catch i8* null_ _%3 = extractvalue { i8*, i32 } %2, 0_ _%4 = tail call i8* @_cxabegincatch(i8* %3) #2 ; nounwind_ _tail call void @_cxaendcatch()_ _br label %5_ _5: ; preds = %0, %1_ _ret void_ _}_ _
_I found that if I marked the call to _cxaendcatch() asnounwind
, the foo could be marked asnounwind
. So I start to survey why cxaendcatch() isn't 'nounwind'. _First is the comment on cxaendcatch() in libcxxabi:_ _Upon exit for any reason, a handler must call:_ _void _cxaendcatch ();_ _This routine can be called for either a native or foreign exception._ _For a native exception:_ _* Locates the most recently caught exception and decrements its handler count._ _* Removes the exception from the caught exception stack, if the handler count goes to zero._ _* If the handler count goes down to zero, and the exception was not re-thrown_ _by throw, it locates the primary exception (which may be the same as the one_ _it's handling) and decrements its reference count. If that reference count_ _goes to zero, the function destroys the exception. In any case, if the current_ _exception is a dependent exception, it destroys that._ _For a foreign exception:_ _* If it has been rethrown, there is nothing to do._ _* Otherwise delete the exception and pop the catch stack to empty._ _
_I am not familiar with exception handling. But from the comment above, it looks like that cxaendcatch wouldn't throw. Then in clang::ItaniumCXXABI, I found this:_ _A cleanup to call _cxaendcatch. In many cases, the caught_ _exception type lets us state definitively that the thrown exception_ _type does not have a destructor. In particular:_ _- Catch-alls tell us nothing, so we have to conservatively_ _assume that the thrown exception might have a destructor._ _- Catches by reference behave according to their base types._ _- Catches of non-record types will only trigger for exceptions_ _of non-record types, which never have destructors._ _- Catches of record types can trigger for arbitrary subclasses_ _of the caught type, so we have to assume the actual thrown_ _exception type might have a throwing destructor, even if the_ _caught type's destructor is trivial or nothrow._ _
_It looks like that cxaendcatch would throw only if the exception caught has an destructor which may throw.
Yes...
But I think the situation is rare. First as the comment says, an exception type doesn't have a destructor usually. Then if it has a destructor, it is also rare that it may throw. Finally, it is a bad practice to throw from destructor which occurs in catch block. So I want to provide an option to tell the compiler whether the exceptions in current project has a may-throw destructor. In this way, we could optimize the example in the beginning.
From GCC produced .gcc_except_table, it seems that GCC unconditionally assumes that __cxa_begin_catch/__cxa_end_catch do not throw. GCC does not emit call site records for the region with __cxa_end_catch.
So I think we should unconditionally assume that __cxa_begin_catch/__cxa_end_catch don't throw as well. Sent https://reviews.llvm.org/D108905
- Previous message: [llvm-dev] [Exception Handling] Could we mark __cxa_end_catch as nounwind conditionally?
- Next message: [llvm-dev] [Exception Handling] Could we mark __cxa_end_catch as nounwind conditionally?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]