<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/65116>65116</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[wasm][wasm-exceptions][c++] Destructors called too early when throwing call is in inline assembly
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
lambdageek
</td>
</tr>
</table>
<pre>
Repro: https://godbolt.org/z/8W8Eb9nr3
```cxx
int foo();
int bar();
static
__attribute__((noinline))
int
baz(){
int x = 0;
#if 1
__asm(
"try i32; "
"call _bar;"
"catch_all;"
" rethrow 0;"
" "
"end_try;"
"i32.const 1;"
"i32.add;"
"local.set %0;"
: "=r"(x)
);
#else
try {
x = 1 + bar ();
} catch (...) {
throw;
}
#endif
return x;
}
class X {
public:
explicit X() {}
~X() {foo();};
};
int square(int num) {
X x{}; // call to foo() happens here
return baz();
// expected the call to foo to happen after baz returns
}
```
When I compile this with `-O -fcxx-exceptions -fwasm-exceptions` I expect the destructor for `x` to be called after `baz()` returns (or when we're unwinding because it throws). Indeed that is the case if you change `#if 1` to `#if 0` and compile the C++ version of the code. On the other hand when the exception is in the inline asm code, the destructor is called before the call to `baz()`.
Am I doing something wrong? Is there some way to tell clang that the inline wasm may throw?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VUGT6ioT_TW46TIVYRLjwsV4_axydau-zZ2dRULH8B4BH5BrvIv3219BYqLOjGUlpLs5nD7dAHdOnjXilmQ7ku0XvPONsVvF21LwM-Lfi9KI2_b_eLGGsHdovL84wt4JPRB6OBtRGuUTY8-EHv4Qeih-Ff8rN9oyku5J-j4-83T4V30_WKT2UBtDaEHohrDdbC25fbEOT-e5l9UwPp2491aWncfTKUYX2kitpMYwkW4muGFQ8j8j5nrEBAAIq_VA2B7SeS3KZA2rOeh04q4NkydL-BFKvb2BZJSwXfj65K64UnAKybDd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