Skip to content

Conversation

@bruteforceboy
Copy link
Contributor

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. A yield 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 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.

@bcardosolopes bcardosolopes merged commit 594543f into llvm:main May 20, 2025
9 checks passed
terapines-osc-cir pushed a commit to Terapines/clangir that referenced this pull request Sep 2, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants