From 2947f3887d1ea6907f96b68ca6528023b1a25b31 Mon Sep 17 00:00:00 2001 From: "Chibuoyim (Wilson) Ogbonna" Date: Tue, 25 Feb 2025 01:25:23 +0300 Subject: [PATCH] [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? --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 3 ++- clang/test/CIR/CodeGen/try-catch.cpp | 21 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index dc85225d0d5f..31d69d582c08 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -684,7 +684,8 @@ def StoreOp : CIR_Op<"store", [ def ReturnOp : CIR_Op<"return", [ParentOneOf<["FuncOp", "ScopeOp", "IfOp", "SwitchOp", "DoWhileOp", - "WhileOp", "ForOp", "CaseOp"]>, + "WhileOp", "ForOp", "CaseOp", + "TryOp"]>, Terminator]> { let summary = "Return from function"; let description = [{ diff --git a/clang/test/CIR/CodeGen/try-catch.cpp b/clang/test/CIR/CodeGen/try-catch.cpp index 0bcca60549b9..570017f83553 100644 --- a/clang/test/CIR/CodeGen/try-catch.cpp +++ b/clang/test/CIR/CodeGen/try-catch.cpp @@ -128,3 +128,24 @@ void tc5() { // CHECK: cir.call exception @_Z3tc5v() : () -> () // CHECK: cir.yield // CHECK: }] + +// CHECK: cir.func @_Z3tc6v() +void tc6() { + int r = 1; + try { + return; + ++r; + } catch (...) { + } +} + +// CHECK: cir.scope { +// CHECK: cir.try { +// CHECK: cir.return +// CHECK: ^bb1: // no predecessors +// CHECK: %[[V2:.*]] = cir.load {{.*}} : !cir.ptr, !s32i +// CHECK: %[[V3:.*]] = cir.unary(inc, %[[V2]]) : !s32i, !s32i +// CHECK: cir.store %[[V3]], {{.*}} : !s32i, !cir.ptr +// CHECK: cir.yield +// CHECK: } +// CHECK: }