Skip to content

Commit b7e916d

Browse files
AmrDeveloperlanza
authored andcommitted
[CIR] Early return in emitCXXTryStmt if try block is empty (#1944)
Currently, in the case of an empty try block, we emit tryOp in scope and revisit the catchers, and in the lowering pass, we have a check to delete empty scopes, but we end up with scopes that contains `cir.yeild` or an unconditioned jump to another scope which will not be deleted, also for catchers we emits globals for type info. But we can already ignore the try-catch statement if the try block is empty, because that means also all catchers are dead code, which will lead to not emitting any scopes that we know will be removed later and also not emitting any unnecessary type info Example of current emitted IR: https://godbolt.org/z/5d3jEe5K8
1 parent 1e51fc9 commit b7e916d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenException.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ mlir::Block *CIRGenFunction::getEHResumeBlock(bool isCleanup,
302302
}
303303

304304
mlir::LogicalResult CIRGenFunction::emitCXXTryStmt(const CXXTryStmt &S) {
305+
if (S.getTryBlock()->body_empty())
306+
return mlir::LogicalResult::success();
307+
305308
auto loc = getLoc(S.getSourceRange());
306309
mlir::OpBuilder::InsertPoint scopeIP;
307310

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
5+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
7+
8+
void empty_try_block_with_catch_all() {
9+
try {} catch (...) {}
10+
}
11+
12+
// CIR: cir.func{{.*}} @_Z30empty_try_block_with_catch_allv()
13+
// CIR: cir.return
14+
15+
// LLVM: define{{.*}} void @_Z30empty_try_block_with_catch_allv()
16+
// LLVM: ret void
17+
18+
// OGCG: define{{.*}} void @_Z30empty_try_block_with_catch_allv()
19+
// OGCG: ret void
20+
21+
void empty_try_block_with_catch_with_int_exception() {
22+
try {} catch (int e) {}
23+
}
24+
25+
// CIR: cir.func{{.*}} @_Z45empty_try_block_with_catch_with_int_exceptionv()
26+
// CIR: cir.return
27+
28+
// LLVM: define{{.*}} void @_Z45empty_try_block_with_catch_with_int_exceptionv()
29+
// LLVM: ret void
30+
31+
// OGCG: define{{.*}} void @_Z45empty_try_block_with_catch_with_int_exceptionv()
32+
// OGCG: ret void
33+

0 commit comments

Comments
 (0)