[clang] [CIR] Upstream Exception ThrowOp with subexpr (PR #161818)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 6 13:47:23 PDT 2025


================
@@ -4166,6 +4166,40 @@ def CIR_ThrowOp : CIR_Op<"throw"> {
   let hasVerifier = 1;
 }
 
+//===----------------------------------------------------------------------===//
+// AllocExceptionOp
+//===----------------------------------------------------------------------===//
+
+def CIR_AllocExceptionOp : CIR_Op<"alloc.exception"> {
+  let summary = "Allocates an exception according to Itanium ABI";
+  let description = [{
+    Implements a slightly higher level __cxa_allocate_exception:
+
+    `void *__cxa_allocate_exception(size_t thrown_size);`
+
+    If operation fails, program terminates, not throw.
+
+    Example:
+
+    ```mlir
+    // if (b == 0) {
+    //   ...
+    //   throw "...";
+    cir.if %10 {
+        %11 = cir.alloc_exception 8 -> !cir.ptr<!void>
+        ... // store exception content into %11
+        cir.throw %11 : !cir.ptr<!cir.ptr<!u8i>>, ...
+    ```
+  }];
+
+  let arguments = (ins I64Attr:$size);
+  let results = (outs Res<CIR_PointerType, "", [MemAlloc<DefaultResource>]>:$addr);
----------------
andykaylor wrote:

I see now. We're allocating an exception for a throw expression, and since we know the throw type, we allocate the exception accordingly. OGCG is just emitting a call to __cxa_allocate_exception, which it "casts" to the correct type, but since OGCG uses opaque types the cast is never visible.

https://github.com/llvm/llvm-project/pull/161818


More information about the cfe-commits mailing list