Skip to content

Conversation

bruteforceboy
Copy link
Contributor

@bruteforceboy bruteforceboy commented May 27, 2025

Currently, the following code snippet crashes during flattening, before lowering to llvm:

struct S {
  int a, b;
};

void foo() {
  try {
    S s{1, 2};
  } catch (...) {
  }
}

Command to reproduce:

clang tmp.cpp -Xclang -fclangir -Xclang -emit-cir-flat -S -o -

The crash happens when flattening a TryOp with an empty catch region and building catchers. Something like:

"cir.try"() ({
}, {
}) : () -> ()

the crash happens at tryOp.isCatchAllOnly() to be specific, because the catch types attribute list is empty.

The fix is simple - adding a check for an empty/non-existent catch region before building the catch clauses.

This PR adds this fix and one test.

Side-note: This enables push_back for std::vector to be lowered to llvm, for example:

#include <vector>

void foo() {
  std::vector<int> v;
  v.push_back(1);
}

@bcardosolopes bcardosolopes merged commit 16718e6 into llvm:main May 27, 2025
10 checks passed
terapines-osc-cir pushed a commit to Terapines/clangir that referenced this pull request Sep 2, 2025
…lvm#1646)

Currently, the following code snippet crashes during flattening, before
lowering to llvm:
```
struct S {
  int a, b;
};

void foo() {
  try {
    S s{1, 2};
  } catch (...) {
  }
}
```
Command to reproduce:
```
clang tmp.cpp -Xclang -fclangir -Xclang -emit-cir-flat -S -o -
```
The crash happens when flattening a TryOp with an empty catch region and
[building
catchers](https://github.com/llvm/clangir/blob/791c327da623e4cb1c193422f4b7a555f572b70a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp#L423).
Something like:
```
"cir.try"() ({
}, {
}) : () -> ()
```
the crash happens at
[`tryOp.isCatchAllOnly()`](https://github.com/llvm/clangir/blob/791c327da623e4cb1c193422f4b7a555f572b70a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp#L441C39-L441C61)
to be specific, because the catch types attribute list is empty.

The fix is simple - adding a check for an empty/non-existent catch
region before building the catch clauses.

This PR adds this fix and one test.

**Side-note:** This enables `push_back` for `std::vector` to be lowered
to llvm, for example:
```
#include <vector>

void foo() {
  std::vector<int> v;
  v.push_back(1);
}
```
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