[clang] [CIR][EH] Preserved exception pointer address spaces in throw calls (PR #227595)

Kunal Dubey via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 00:40:43 PST 2026


================
@@ -941,45 +952,19 @@ mlir::LogicalResult ItaniumEHLowering::lowerTryThrow(cir::TryThrowOp op) {
     return mlir::success();
   }
 
-  ensureCxaThrowDecl(loc);
+  auto castToVoidPtr = [&](mlir::Value value,
+                           cir::PointerType targetType) -> mlir::Value {
+    if (value.getType() == targetType)
+      return value;
+    return cir::CastOp::create(builder, loc, targetType, cir::CastKind::bitcast,
+                               value);
+  };
 
-  // Bitcast the exception pointer to void* if necessary.
-  mlir::Value exnPtr = op.getExceptionPtr();
-  if (exnPtr.getType() != voidPtrType)
-    exnPtr = cir::CastOp::create(builder, loc, voidPtrType,
-                                 cir::CastKind::bitcast, exnPtr);
-
-  // Materialize the type_info pointer, looking up the typed symbol in the
-  // module so we get the correct pointer type for cir.get_global, then
-  // bitcasting to void* to match the runtime signature.
-  mlir::FlatSymbolRefAttr typeInfoAttr = op.getTypeInfoAttr();
-  auto typeInfoGlobal = mod.lookupSymbol<cir::GlobalOp>(typeInfoAttr);
-  if (!typeInfoGlobal)
-    return op.emitError("type_info symbol not found in module");
-  auto typeInfoPtrTy = cir::PointerType::get(typeInfoGlobal.getSymType());
-  mlir::Value typeInfo = cir::GetGlobalOp::create(builder, loc, typeInfoPtrTy,
-                                                  typeInfoAttr.getValue());
-  if (typeInfo.getType() != voidPtrType)
-    typeInfo = cir::CastOp::create(builder, loc, voidPtrType,
-                                   cir::CastKind::bitcast, typeInfo);
-
-  // Materialize the dtor pointer (or null if no dtor).
-  mlir::Value dtor;
-  if (mlir::FlatSymbolRefAttr dtorAttr = op.getDtorAttr()) {
-    auto dtorFunc = mod.lookupSymbol<cir::FuncOp>(dtorAttr);
-    if (!dtorFunc)
-      return op.emitError("dtor symbol not found in module");
-    auto dtorPtrTy = cir::PointerType::get(dtorFunc.getFunctionType());
-    dtor =
-        cir::GetGlobalOp::create(builder, loc, dtorPtrTy, dtorAttr.getValue());
-    if (dtor.getType() != voidPtrType)
-      dtor = cir::CastOp::create(builder, loc, voidPtrType,
-                                 cir::CastKind::bitcast, dtor);
-  } else {
-    dtor = cir::ConstantOp::create(
-        builder, loc,
-        cir::ConstPtrAttr::get(voidPtrType, builder.getI64IntegerAttr(0)));
-  }
+  mlir::Value exnPtr = castToVoidPtr(op.getExceptionPtr(), cxaThrowVoidPtrType);
+  mlir::Value typeInfo = castToVoidPtr(op.getTypeInfo(), globalVoidPtrType);
+  mlir::Value dtor = castToVoidPtr(op.getDtor(), cxaThrowVoidPtrType);
+
+  ensureCxaThrowDecl(loc);
----------------
xakep8 wrote:

oh right I had to move it to some other place but I forgot about this part, I'll add to the verifier.

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


More information about the cfe-commits mailing list