[clang] [CIR] Upstream CIR Dialect TryOp with Catch Attrs (PR #162897)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 11 06:30:03 PDT 2025
================
@@ -4296,6 +4296,81 @@ def CIR_AllocExceptionOp : CIR_Op<"alloc.exception"> {
}];
}
+//===----------------------------------------------------------------------===//
+// TryOp
+//===----------------------------------------------------------------------===//
+
+def CIR_TryOp : CIR_Op<"try",[
+ DeclareOpInterfaceMethods<RegionBranchOpInterface>,
+ RecursivelySpeculatable, AutomaticAllocationScope, NoRegionArguments
+]> {
+ let summary = "C++ try block";
+ let description = [{
+ Holds the lexical scope of `try {}`. Note that resources used on catch
+ clauses are usually allocated in the same parent as `cir.try`.
+
+ `synthetic`: use `cir.try` to represent try/catches not originally
+ present in the source code (e.g. `g = new Class` under `-fexceptions`).
+
+ `cleanup`: signal to targets (LLVM for now) that this try/catch, needs
+ to specially tag their landing pads as needing "cleanup".
+
+ Example:
+
+ ```mlir
+ %0 = cir.alloc.exception 16 -> !cir.ptr<!some_record>
+ %1 = cir.get_global @d2 : !cir.ptr<!some_record>
+ cir.try synthetic cleanup {
+ cir.call exception @_ZN7test2_DC1ERKS_(%0, %1)
+ : (!cir.ptr<!some_record>, !cir.ptr<!some_record>) -> () cleanup {
+ %2 = cir.cast bitcast %0 : !cir.ptr<!some_record> -> !cir.ptr<!void>
+ cir.free.exception %2
+ cir.yield
+ }
+ ...
+ }
+ ```
+ }];
+
+ let arguments = (ins UnitAttr:$synthetic, UnitAttr:$cleanup,
+ OptionalAttr<ArrayAttr>:$catch_types);
+ let regions = (region AnyRegion:$try_region,
+ VariadicRegion<AnyRegion>:$catch_regions);
----------------
AmrDeveloper wrote:
That makes sense, the problem is that we can't add it after the Variadic regions. If we added it before, it works, but when TableGen generate `getCatchRegions`, it will be `regions.drop_front(2)` because it expects that always the second region will be unwinded 🤔.
I was thinking of changing the name of `catch_regions` to be, for example, ' handlers` and adding two functions, ' Option<Region> GetUnwindRegion` and `List<Region> GetCatchRegions`, what do you think?
Not sure if there is a workaround in tablegen @xlauko 🤔
https://github.com/llvm/llvm-project/pull/162897
More information about the cfe-commits
mailing list