You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[CIR][CodeGen] Emit dtor properly for objects in TernaryOp (llvm#1614)
Currently, the following code snippet fails with a crash:
```
#include <stdio.h>
struct X {
int a;
X(int a) : a(a) {}
~X() {}
};
bool foo(const X &) { return false; }
bool bar() { return foo(1) || foo(2); }
```
it fails with
```
error: 'cir.yield' op must be the last operation in the parent block
```
The crash can be traced to the [construction of the
TernaryOp](https://github.com/llvm/clangir/blob/5df50096be1a783c26b48ea437523347df8a3110/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp#L2728).
A
[yield](https://github.com/llvm/clangir/blob/5df50096be1a783c26b48ea437523347df8a3110/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp#L2776)
is created and when the LexicalScope is destroyed the destructors for
the object follow.
So, the CIR looks something like:
```
%11 = "cir.ternary"(%10) ({
%13 = "cir.const"() <{value = #cir.bool<true> : !cir.bool}> : () -> !cir.bool
"cir.yield"(%13) : (!cir.bool) -> ()
}, {
%12 = "cir.const"() <{value = #cir.bool<false> : !cir.bool}> : () -> !cir.bool
"cir.yield"(%12) : (!cir.bool) -> ()
}) : (!cir.bool) -> !cir.bool
"cir.yield"(%11) : (!cir.bool) -> ()
"cir.call"(%7) <{callee = @_ZN1XD2Ev, calling_conv = 1 : i32, extra_attrs = #cir<extra({nothrow = #cir.nothrow})>, side_effect = 1 : i32}> ({
})
```
which is wrong, since the yield should come last.
The fix here is forcing a cleanup and then the yield follows. A similar
rule also applies
[here](https://github.com/llvm/clangir/blob/5df50096be1a783c26b48ea437523347df8a3110/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp#L2657)
for the "&&" case, e.g., foo(1) && foo(2) instead in the code snippet.
One more modification I made is adding a check for when the current
lexScope is ternary, when creating dispatch blocks for cleanups. I
believe in this case, we do not want to create a dispatch block, please
correct me if I am wrong.
Finally, I add two tests to verify that everything works as intended.
0 commit comments