llvm / clangir

A new (MLIR based) high-level IR for clang.
https://clangir.org
Other
307 stars 84 forks source link

[CIR][CIRGen] Add support for exact dynamic cast #709

Closed Lancern closed 1 day ago

Lancern commented 4 days ago

This PR implements the last piece to make CIR catch up upstream CodeGen on dynamic_cast support. It ports an upstream optimization "exact cast" to CIR.

The basic idea of exact cast is when dynamic_cast to a final class, we don't have to call into the runtime -- we could just check if the dynamic type of the source object is exactly the destination type by quickly comparing the vtable pointers. To give a concrete example of this optimization:

struct Base { virtual ~Base(); };
struct Derived final : Base {};

Derived *test(Base *src) { return dynamic_cast<Derived *>(src); }

Without the optimization, we have to call the runtime function __dynamic_cast to do the heavy and slow type check. After enabling the optimization, we could quickly carry out the runtime type check by inline checking whether the vtable ptr of src points to the vtable of Derived.

This PR also fixes a bug in existing dynamic_cast CIRGen code. The bug mistakenly removes the insertion point after emitting a call to bad_cast, causing the CIRGen of any follow up statements to crash.

Lancern commented 2 days ago

please add LLVM checks.

LLVM checks added.