Author: Amr Hesham
Date: 2026-01-07T20:06:21+01:00
New Revision: 664fb3b61061a96e4e9c730bacd1de5b8ce3f669

URL: 
https://github.com/llvm/llvm-project/commit/664fb3b61061a96e4e9c730bacd1de5b8ce3f669
DIFF: 
https://github.com/llvm/llvm-project/commit/664fb3b61061a96e4e9c730bacd1de5b8ce3f669.diff

LOG: [CIR] Upstream Exceptions EHTypeIdOp (#172558)

Upstream the Exceptions EHTypeIdOp

Issue https://github.com/llvm/llvm-project/issues/154992

Added: 
    clang/test/CIR/IR/eh-type-id.cir
    clang/test/CIR/IR/invalid-eh-type-id.cir
    clang/test/CIR/Lowering/eh-type-id.cir

Modified: 
    clang/include/clang/CIR/Dialect/IR/CIROps.td
    clang/lib/CIR/Dialect/IR/CIRDialect.cpp
    clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td 
b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 8358b076ee7b6..4274ed25542b1 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -5519,6 +5519,30 @@ def CIR_EhInflightOp : CIR_Op<"eh.inflight_exception"> {
   }];
 }
 
+//===----------------------------------------------------------------------===//
+// Exception related: EhTypeIdOp
+//===----------------------------------------------------------------------===//
+
+def CIR_EhTypeIdOp : CIR_Op<"eh.typeid",
+  [Pure, DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
+  let summary = "Compute exception type id from its global type symbol";
+  let description = [{
+    Returns the exception type id for a given global symbol representing
+    a type.
+
+    Example:
+    ```mlir
+    %type_id = cir.eh.typeid @_ZTIi
+    ```
+  }];
+
+  let arguments = (ins FlatSymbolRefAttr:$type_sym);
+  let results = (outs CIR_UInt32:$type_id);
+  let assemblyFormat = [{
+    $type_sym attr-dict
+  }];
+}
+
 
//===----------------------------------------------------------------------===//
 // Atomic operations
 
//===----------------------------------------------------------------------===//

diff  --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 8f0dc705181e6..e1df5d5185a04 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -3508,6 +3508,19 @@ static mlir::ParseResult parseTryHandlerRegions(
   return mlir::success();
 }
 
+//===----------------------------------------------------------------------===//
+// EhTypeIdOp
+//===----------------------------------------------------------------------===//
+
+LogicalResult
+cir::EhTypeIdOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
+  Operation *op = symbolTable.lookupNearestSymbolFrom(*this, getTypeSymAttr());
+  if (!isa_and_nonnull<GlobalOp>(op))
+    return emitOpError("'")
+           << getTypeSym() << "' does not reference a valid cir.global";
+  return success();
+}
+
 
//===----------------------------------------------------------------------===//
 // TableGen'd op method definitions
 
//===----------------------------------------------------------------------===//

diff  --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp 
b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 7686add38250e..cbd4550056c51 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -3470,6 +3470,18 @@ mlir::LogicalResult 
CIRToLLVMResumeFlatOpLowering::matchAndRewrite(
   return mlir::success();
 }
 
+mlir::LogicalResult CIRToLLVMEhTypeIdOpLowering::matchAndRewrite(
+    cir::EhTypeIdOp op, OpAdaptor adaptor,
+    mlir::ConversionPatternRewriter &rewriter) const {
+  mlir::Value addrOp = mlir::LLVM::AddressOfOp::create(
+      rewriter, op.getLoc(),
+      mlir::LLVM::LLVMPointerType::get(rewriter.getContext()),
+      op.getTypeSymAttr());
+  rewriter.replaceOpWithNewOp<mlir::LLVM::EhTypeidForOp>(
+      op, rewriter.getI32Type(), addrOp);
+  return mlir::success();
+}
+
 mlir::LogicalResult CIRToLLVMTrapOpLowering::matchAndRewrite(
     cir::TrapOp op, OpAdaptor adaptor,
     mlir::ConversionPatternRewriter &rewriter) const {

diff  --git a/clang/test/CIR/IR/eh-type-id.cir 
b/clang/test/CIR/IR/eh-type-id.cir
new file mode 100644
index 0000000000000..d9acfa96b6230
--- /dev/null
+++ b/clang/test/CIR/IR/eh-type-id.cir
@@ -0,0 +1,22 @@
+// RUN: cir-opt %s --verify-roundtrip | FileCheck %s
+
+!u32i = !cir.int<u, 32>
+!u8i = !cir.int<u, 8>
+!void = !cir.void
+
+module {
+
+cir.global "private" constant external @_ZTIi : !cir.ptr<!u8i>
+
+cir.func private @exception_handling_type_id() {
+  %type_id = cir.eh.typeid @_ZTIi
+  cir.return
+}
+
+// CHECK: cir.func private @exception_handling_type_id() {
+// CHECK:   %[[TYPE_ID:.*]] = cir.eh.typeid @_ZTIi
+// CHECK:   cir.return
+// CHECK: }
+
+}
+

diff  --git a/clang/test/CIR/IR/invalid-eh-type-id.cir 
b/clang/test/CIR/IR/invalid-eh-type-id.cir
new file mode 100644
index 0000000000000..9c90af45dffef
--- /dev/null
+++ b/clang/test/CIR/IR/invalid-eh-type-id.cir
@@ -0,0 +1,16 @@
+// RUN: cir-opt %s -verify-diagnostics -split-input-file
+
+!u32i = !cir.int<u, 32>
+!u8i = !cir.int<u, 8>
+!void = !cir.void
+
+module {
+
+cir.func private @exception_handling_type_id() {
+  // expected-error@+1 {{'cir.eh.typeid' op '_ZTIi' does not reference a valid 
cir.global}}
+  %type_id = cir.eh.typeid @_ZTIi
+  cir.return
+}
+
+}
+

diff  --git a/clang/test/CIR/Lowering/eh-type-id.cir 
b/clang/test/CIR/Lowering/eh-type-id.cir
new file mode 100644
index 0000000000000..ada9f22f86a72
--- /dev/null
+++ b/clang/test/CIR/Lowering/eh-type-id.cir
@@ -0,0 +1,23 @@
+// RUN: cir-opt %s -cir-to-llvm -o %t.cir
+
+!u32i = !cir.int<u, 32>
+!u8i = !cir.int<u, 8>
+!void = !cir.void
+
+module {
+
+cir.global "private" constant external @_ZTIi : !cir.ptr<!u8i>
+
+cir.func private @exception_handling_type_id() {
+  %type_id = cir.eh.typeid @_ZTIi
+  cir.return
+}
+
+// CHECK: llvm.func @exception_handling_type_id() attributes {sym_visibility = 
"private"} {
+// CHECK:   %[[GV_ADDR_OF:.*]] = llvm.mlir.addressof @_ZTIi : !llvm.ptr
+// CHECK:   %[[TYPE_ID:.*]] = llvm.intr.eh.typeid.for %[[GV_ADDR_OF]] : 
(!llvm.ptr) -> i32
+// CHECK:   llvm.return
+// CHECK: }
+
+}
+


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to