[clang] [CIR] Add structured CatchParamOp (PR #165110)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 6 03:43:59 PST 2025
https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/165110
>From c5ce5c9985c401961ec518665d8fbc146c086993 Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Fri, 5 Dec 2025 21:27:34 +0100
Subject: [PATCH 1/4] [CIR] Add structured CatchParamOp
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 27 +++++++++++++++++
clang/test/CIR/IR/catch-param.cir | 32 ++++++++++++++++++++
2 files changed, 59 insertions(+)
create mode 100644 clang/test/CIR/IR/catch-param.cir
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index caa047a51b689..f9a70166625a0 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -5183,6 +5183,33 @@ def CIR_TryOp : CIR_Op<"try",[
let hasLLVMLowering = false;
}
+//===----------------------------------------------------------------------===//
+// CatchParamOp
+//===----------------------------------------------------------------------===//
+
+def CIR_CatchParamOp : CIR_Op<"catch_param", [HasParent<"cir::TryOp">]> {
+ let summary = "Represents catch clause formal parameter";
+ let description = [{
+ The `cir.catch_param` operate in the catch regions of `cir.try`.
+
+ This operation is used only before the CFG flatterning pass.
+
+ Example:
+
+ ```mlir
+ %exception = cir.catch_param -> !cir.ptr<!void>
+ ```
+ }];
+
+ let results = (outs Optional<CIR_AnyType>:$param);
+ let assemblyFormat = [{
+ (`:` qualified(type($param))^)?
+ attr-dict
+ }];
+
+ let hasLLVMLowering = false;
+}
+
//===----------------------------------------------------------------------===//
// Exception related: EhInflightOp
//===----------------------------------------------------------------------===//
diff --git a/clang/test/CIR/IR/catch-param.cir b/clang/test/CIR/IR/catch-param.cir
new file mode 100644
index 0000000000000..a16e8ec43c91b
--- /dev/null
+++ b/clang/test/CIR/IR/catch-param.cir
@@ -0,0 +1,32 @@
+// RUN: cir-opt %s --verify-roundtrip | FileCheck %s
+
+!s32i = !cir.int<s, 32>
+!void = !cir.void
+
+module {
+
+cir.func @catch_param_inside_catch() {
+ cir.scope {
+ cir.try {
+ cir.yield
+ } catch all {
+ cir.catch_param : !cir.ptr<!void>
+ cir.yield
+ }
+ }
+ cir.return
+}
+
+// CHECK: cir.func @catch_param_inside_catch() {
+// CHECK: cir.scope {
+// CHECK: cir.try {
+// CHECK: cir.yield
+// CHECK: } catch all {
+// CHECK: cir.catch_param : !cir.ptr<!void>
+// CHECK: cir.yield
+// CHECK: }
+// CHECK: }
+// CHECK: cir.return
+// CHECK: }
+
+}
>From d5add3e20d97dd60c140a36f332b6f1912bd6ba9 Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Fri, 5 Dec 2025 21:32:42 +0100
Subject: [PATCH 2/4] Update op example to match the current format
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index f9a70166625a0..cd0248d60eec7 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -5197,7 +5197,7 @@ def CIR_CatchParamOp : CIR_Op<"catch_param", [HasParent<"cir::TryOp">]> {
Example:
```mlir
- %exception = cir.catch_param -> !cir.ptr<!void>
+ %exception = cir.catch_param : !cir.ptr<!void>
```
}];
>From 3013ead0a1754da3d7be207338bb1f9641bdb186 Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Fri, 5 Dec 2025 21:52:01 +0100
Subject: [PATCH 3/4] Update Op description
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index cd0248d60eec7..0ee0fe4dacfcf 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -5188,9 +5188,10 @@ def CIR_TryOp : CIR_Op<"try",[
//===----------------------------------------------------------------------===//
def CIR_CatchParamOp : CIR_Op<"catch_param", [HasParent<"cir::TryOp">]> {
- let summary = "Represents catch clause formal parameter";
+ let summary = "Represents the catch clause formal parameter";
let description = [{
- The `cir.catch_param` operate in the catch regions of `cir.try`.
+ The `cir.catch_param` used to retrieves the exception object inside
+ the handler regions of `cir.try`.
This operation is used only before the CFG flatterning pass.
>From df40edbc37ce872480148f93fb07828e263e7646 Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Sat, 6 Dec 2025 12:28:12 +0100
Subject: [PATCH 4/4] Address code review comments
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 2 +-
clang/test/CIR/IR/catch-param.cir | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 0ee0fe4dacfcf..fcc7585cf81a5 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -5190,7 +5190,7 @@ def CIR_TryOp : CIR_Op<"try",[
def CIR_CatchParamOp : CIR_Op<"catch_param", [HasParent<"cir::TryOp">]> {
let summary = "Represents the catch clause formal parameter";
let description = [{
- The `cir.catch_param` used to retrieves the exception object inside
+ The `cir.catch_param` is used to retrieves the exception object inside
the handler regions of `cir.try`.
This operation is used only before the CFG flatterning pass.
diff --git a/clang/test/CIR/IR/catch-param.cir b/clang/test/CIR/IR/catch-param.cir
index a16e8ec43c91b..7b0f884fff11d 100644
--- a/clang/test/CIR/IR/catch-param.cir
+++ b/clang/test/CIR/IR/catch-param.cir
@@ -10,7 +10,7 @@ cir.func @catch_param_inside_catch() {
cir.try {
cir.yield
} catch all {
- cir.catch_param : !cir.ptr<!void>
+ %exception = cir.catch_param : !cir.ptr<!void>
cir.yield
}
}
@@ -22,7 +22,7 @@ cir.func @catch_param_inside_catch() {
// CHECK: cir.try {
// CHECK: cir.yield
// CHECK: } catch all {
-// CHECK: cir.catch_param : !cir.ptr<!void>
+// CHECK: %[[EXCEPTION:.*]] = cir.catch_param : !cir.ptr<!void>
// CHECK: cir.yield
// CHECK: }
// CHECK: }
More information about the cfe-commits
mailing list