[clang] [CIR] Add support for exact dynamic casts (PR #164007)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 20 08:58:22 PDT 2025


andykaylor wrote:

> > > The class traversal logic looks to be pretty much identical to the non-CIR final class dynamic_cast optimization (`CGCXXABI::getExactDynamicCastInfo`) is it possible to reuse that logic? maybe extending it if additional information is needed? (also while trying to find that code again I found yet another near copy of the same logic. _sigh_ :D0
> > 
> > 
> > Unfortunately, this is a recurring problem with CIR codegen. We've tried to keep the basic code structure as close to LLVM IR codegen as is practical. There are, of course, many places where we could be sharing the actual code for things that don't involve the target substrate. We've implemented a few of these, and for others we've just left comments saying that it should be done. We're trying to find the right balance between making fast progress on the upstreaming versus accumulating technical debt and baking in fragility.
>> 
> > Where would you suggest moving this logic? `ASTRecordLayout` maybe?
> 
> Ah, do you not have access to the ABI interface in CIR? (This is just an "Oliver doesn't know where the abstraction boundaries are" question)

No, this was an "Andy doesn't know where the best place is to put shared code" question, but I think your question about having access to the ABI interface might clarify something for me about your perspective.

CIR codegen is mostly a semi-clone of LLVM IR codegen, but because we're targeting a different substrate, it ends up being "nearly" duplicated. It happens that `CGCXXABI::getDynamicCastInfo()` is sort of an outlier here in that it doesn't contain anything that is IR-specific. Most of the functions in `CGCXXABI` can't be shared because they're things like:
```
  virtual llvm::Value *emitDynamicCastToVoid(CodeGenFunction &CGF,
                                             Address Value,
                                             QualType SrcRecordTy) = 0;
```
CIR has its own alternative to `CodeGenFunction` (`CIRGenFunction`), its own `Address` (which uses the same name), and needs an `mlir::Value` here rather than `llvm::Value *`, so we can't just use `CGCXXABI`. Instead, we have our own `CIRGenCXXABI`. We've strayed a bit further than usual from the structure of the LLVM IR codegen code here, so it's not as obvious as usual that we're operating on a completely parallel code path, but that's the general intention.

If you weren't previously aware of this, you may be sitting there thinking "These guys are complete bozos" right about now, but I promise you we really have thought through this, and this is the best approach we have been able to come up with. Keeping the code structure parallel lets us reference the LLVM IR codegen implementation to figure out things that we're missing or doing wrong, but the differences in the interfaces make actual code sharing impractical at this time.

I'm not sure there is a good place to share `CGCXXABI::getDynamicCastInfo()` since it's ABI-specific. Maybe the best we could do is refactor the CIR implementation to get it back in sync with the LLVM IR codegen.

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


More information about the cfe-commits mailing list