Skip to content

Commit e0244f3

Browse files
bruteforceboylanza
authored andcommitted
[CIR][CodeGen] Support return in TryOp (#1398)
This PR adds support for returns inside of a TryOp, for example: ``` void foo() { int r = 1; try { return; ++r; } catch (...) { } } ``` Currently, it fails during the CodeGen with: ``` error: 'cir.return' op expects parent op to be one of 'cir.func, cir.scope, cir.if, cir.switch, cir.do, cir.while, cir.for, cir.case' ``` were TryOp's omitted on purpose?
1 parent 49ba380 commit e0244f3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,8 @@ def StoreOp : CIR_Op<"store", [
684684

685685
def ReturnOp : CIR_Op<"return", [ParentOneOf<["FuncOp", "ScopeOp", "IfOp",
686686
"SwitchOp", "DoWhileOp",
687-
"WhileOp", "ForOp", "CaseOp"]>,
687+
"WhileOp", "ForOp", "CaseOp",
688+
"TryOp"]>,
688689
Terminator]> {
689690
let summary = "Return from function";
690691
let description = [{

clang/test/CIR/CodeGen/try-catch.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,24 @@ void tc5() {
128128
// CHECK: cir.call exception @_Z3tc5v() : () -> ()
129129
// CHECK: cir.yield
130130
// CHECK: }]
131+
132+
// CHECK: cir.func @_Z3tc6v()
133+
void tc6() {
134+
int r = 1;
135+
try {
136+
return;
137+
++r;
138+
} catch (...) {
139+
}
140+
}
141+
142+
// CHECK: cir.scope {
143+
// CHECK: cir.try {
144+
// CHECK: cir.return
145+
// CHECK: ^bb1: // no predecessors
146+
// CHECK: %[[V2:.*]] = cir.load {{.*}} : !cir.ptr<!s32i>, !s32i
147+
// CHECK: %[[V3:.*]] = cir.unary(inc, %[[V2]]) : !s32i, !s32i
148+
// CHECK: cir.store %[[V3]], {{.*}} : !s32i, !cir.ptr<!s32i>
149+
// CHECK: cir.yield
150+
// CHECK: }
151+
// CHECK: }

0 commit comments

Comments
 (0)