[clang] [CIR] Upstream DynamicCastOp (PR #161734)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 6 15:46:31 PDT 2025


================
@@ -601,6 +601,60 @@ def CIR_VTableAttr : CIR_Attr<"VTable", "vtable", [TypedAttrInterface]> {
   }];
 }
 
+//===----------------------------------------------------------------------===//
+// DynamicCastInfoAttr
+//===----------------------------------------------------------------------===//
+
+def CIR_DynamicCastInfoAttr : CIR_Attr<"DynamicCastInfo", "dyn_cast_info"> {
+  let summary = "ABI specific information about a dynamic cast";
+  let description = [{
+    Provide ABI specific information about a dynamic cast operation.
+
+    The `srcRtti` and the `destRtti` parameters give the RTTI of the source
+    record type and the destination record type, respectively.
+
+    The `runtimeFunc` parameter gives the `__dynamic_cast` function which is
+    provided by the runtime. The `badCastFunc` parameter gives the
+    `__cxa_bad_cast` function which is also provided by the runtime.
+
+    The `offsetHint` parameter gives the hint value that should be passed to the
+    `__dynamic_cast` runtime function.
+  }];
+
+  let parameters = (ins
+    CIR_GlobalViewAttr:$srcRtti,
+    CIR_GlobalViewAttr:$destRtti,
+    "mlir::FlatSymbolRefAttr":$runtimeFunc,
+    "mlir::FlatSymbolRefAttr":$badCastFunc,
+    CIR_IntAttr:$offsetHint
+  );
+
+  let builders = [
+    AttrBuilderWithInferredContext<(ins "GlobalViewAttr":$srcRtti,
+                                        "GlobalViewAttr":$destRtti,
+                                        "mlir::FlatSymbolRefAttr":$runtimeFunc,
+                                        "mlir::FlatSymbolRefAttr":$badCastFunc,
+                                        "IntAttr":$offsetHint), [{
+      return $_get(srcRtti.getContext(), srcRtti, destRtti, runtimeFunc,
+                   badCastFunc, offsetHint);
+    }]>,
+  ];
+
+  let genVerifyDecl = 1;
+  let assemblyFormat = [{
+    `<`
+      `srcRtti` `=` qualified($srcRtti) `,` `destRtti` `=` qualified($destRtti)
+      `,` `runtimeFunc` `=` $runtimeFunc `,` `badCastFunc` `=` $badCastFunc `,`
+      `offsetHint` `=` qualified($offsetHint)
+    `>`
----------------
andykaylor wrote:

If I use `struct(params)` this prints as

`#dyn_cast_info__ZTI4Base__ZTI7Derived = #cir.dyn_cast_info<src_rtti = <@_ZTI4Base>, dest_rtti = <@_ZTI7Derived>, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = <0>>`

However, it fails to parse with this error:

```
error: expected a trailing type
#dyn_cast_info__ZTI4Base__ZTI7Derived = #cir.dyn_cast_info<src_rtti = <@_ZTI4Base>, dest_rtti = <@_ZTI7Derived>, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = <0>>
```
Your second suggestion seems to work.

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


More information about the cfe-commits mailing list