Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 2, 2024

Conversation

Lancern
Copy link
Collaborator

@Lancern Lancern commented Jun 30, 2024

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.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for improving this, please add LLVM checks.

This patch is 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.

This patch 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
Copy link
Collaborator Author

Lancern commented Jul 2, 2024

please add LLVM checks.

LLVM checks added.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bcardosolopes bcardosolopes merged commit c8891bd into llvm:main Jul 2, 2024
6 checks passed
@Lancern Lancern deleted the dynamic-cast-to-final branch July 3, 2024 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants