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] Add cir.dyn_cast operation #483

Merged
merged 2 commits into from
Apr 19, 2024
Merged

Conversation

Lancern
Copy link
Member

@Lancern Lancern commented Feb 25, 2024

This PR adds the cir.dyn_cast operation for representing dynamic_cast in C++. It contains the following contents:

  • A new cir.dyn_cast operation.
  • Two new attributes that will be attached to cir.dyn_cast operations:
    • #cir.dyn_cast_info attributes, which gives general information about a dynamic cast (e.g. the source RTTI pointer, the dest RTTI pointer, etc.)
    • #cir.downcast_info attribute, which gives even more detailed information about a dynamic cast that is a down-cast. These information will be used when rewriting the cir.dyn_cast operation with more fundamental CIR operations.
  • CIRGen support for the new operation and attributes.
  • Rewrite the new operation with more fundamental CIR operations in LoweringPrepare.

This is a draft PR. Now I only added the new operation / attributes, and updated the CIRGen part. The LoweringPrepare for the new operation is not implemented. Hopefully the draft can get some initial feedbacks from the community and make sure it is on the right direction so we don't waste time on wrong things.

Related issue: #470 .

@lanza lanza force-pushed the main branch 2 times, most recently from ed3955a to 8b7417c Compare March 23, 2024 05:07
@Lancern Lancern marked this pull request as ready for review April 7, 2024 14:21
@Lancern
Copy link
Member Author

Lancern commented Apr 7, 2024

This PR is finally complete and I believe it's ready for some reviews. Here is a brief of what is included in this PR (copied from the commit message).

This PR adds the cir.dyn_cast operation to model the C++ dynamic_cast operator. CIRGen code for the new operation is also included. During LLVMIR lowering prepare, the operation will be replaced by more basic CIR operations.

The new operation is attached an attribute that contains ABI information about the cast. During CIRGen, CIRGenCXXABI will generate and populate the attribute. During LLVMIR lowering prepare, the attribute will be used to generate code for the operation. The attribute's type is ABI-specific. This PR includes a #cir.itanium_dyn_cast_info attribute that provides Itanium C++ ABI specific information.

Since the code generated by the new operation is also highly ABI-specific, this PR adds a new class LoweringPrepareCXXABI that provides ABI-specific behaviors for lowering prepare. Similar to CIRGenCXXABI, the class is an abstract base class that concrete ABIs can inherit and implement. This PR includes a LoweringPrepareItaniumCXXABI class that provides Itanium C++ ABI behaviors for lowering prepare.

@Lancern Lancern changed the title [WIP][CIR] Add cir.dynamic_cast operation [WIP][CIR] Add cir.dyn_cast operation Apr 7, 2024
@Lancern Lancern changed the title [WIP][CIR] Add cir.dyn_cast operation [CIR] Add cir.dyn_cast operation Apr 7, 2024
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.

Awesome, thank you for updating the PR.

Overall approach looks great, it's getting close! Few comments inline:

  • I don't think we need LoweringPrepareItaniumCXXABI abstration during LoweringPrepare. It's fine if the dynamic_cast operation already contains everything it needs w.r.t to the Itanium ABI, whenever support is added for other ABIs, the operation can be changed to support that, or a new operation could also be created that makes more sense. Let's not over design to cover a use case we don't fully understand right now.
    • Note that you are already lowering offsetHint early enough, and you should also do more of that in case there's anything blocking removal of said LoweringPrepareItaniumCXXABI.
  • ItaniumDynamicCastInfoAttr seems nice, you could add more information about Itanium ABI in the comments, but I rather we name it more regularly for now (same rationale as above): DynCastInfoAttr + #cir.dyncast_info sounds good enough.
  • Since you are adding verification, please add a dyncast.cir test to exercise the verifiers.

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp Show resolved Hide resolved
clang/test/CIR/CodeGen/dynamic-cast.cpp Outdated Show resolved Hide resolved
@Lancern
Copy link
Member Author

Lancern commented Apr 17, 2024

Rebased onto the latest main.

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.

This looks great, some remaining minor issues and should be good to go

clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp Show resolved Hide resolved
clang/lib/CIR/Dialect/IR/MissingFeatures.h Outdated Show resolved Hide resolved

auto dynCastFuncRef = castInfo.getRuntimeFunc();
mlir::Value dynCastFuncArgs[4] = {srcPtr, srcRtti, destRtti, offsetHint};
// TODO(cir): set the calling convention for __dynamic_cast.
Copy link
Member

Choose a reason for hiding this comment

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

@sitio-couto FYI. How is this going to work with your CC work?

Lancern added 2 commits April 18, 2024 22:27
This patch adds the `cir.dyn_cast` operation to model the C++ `dynamic_cast`
operator. CIRGen code for the new operation is also included. During LLVMIR
lowering prepare, the operation will be replaced by more basic CIR operations.

The new operation is attached an attribute named `#cir.dyn_cast_info` that
contains ABI information about the cast. During CIRGen, `CIRGenCXXABI` will
generate and populate the attribute. During LLVMIR lowering prepare, the
attribute will be used to generate code for the operation.

Since the code generated by the new operation is also highly ABI-specific, this
patch adds a new class `LoweringPrepareCXXABI` that provides ABI-specific
behaviors for lowering prepare. Similar to `CIRGenCXXABI`, the class is an
abstract base class that concrete ABIs can inherit and implement. This patch
includes a `LoweringPrepareItaniumCXXABI` class that provides Itanium C++ ABI
behaviors for lowering prepare.
@Lancern
Copy link
Member Author

Lancern commented Apr 18, 2024

Rebased onto the latest main.

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 8a322b7 into llvm:main Apr 19, 2024
6 checks passed
@Lancern Lancern deleted the dynamic-cast-op branch April 19, 2024 04:07
lanza pushed a commit that referenced this pull request Apr 29, 2024
This PR adds the `cir.dyn_cast` operation for representing
`dynamic_cast` in C++. It contains the following contents:

- [x] A new `cir.dyn_cast` operation.
- [x] ~Two new attributes that will be attached to `cir.dyn_cast`
operations:~
- [x] ~`#cir.dyn_cast_info` attributes, which gives general information
about a dynamic cast (e.g. the source RTTI pointer, the dest RTTI
pointer, etc.)~
- [x] ~`#cir.downcast_info` attribute, which gives even more detailed
information about a dynamic cast that is a down-cast. These information
will be used when rewriting the `cir.dyn_cast` operation with more
fundamental CIR operations.~
- [x] CIRGen support for the new operation and attributes.
- [x] Rewrite the new operation with more fundamental CIR operations in
LoweringPrepare.

~This is a draft PR. Now I only added the new operation / attributes,
and updated the CIRGen part. The LoweringPrepare for the new operation
is not implemented. Hopefully the draft can get some initial feedbacks
from the community and make sure it is on the right direction so we
don't waste time on wrong things.~

Related issue: #470 .
lanza pushed a commit that referenced this pull request Apr 29, 2024
This PR adds the `cir.dyn_cast` operation for representing
`dynamic_cast` in C++. It contains the following contents:

- [x] A new `cir.dyn_cast` operation.
- [x] ~Two new attributes that will be attached to `cir.dyn_cast`
operations:~
- [x] ~`#cir.dyn_cast_info` attributes, which gives general information
about a dynamic cast (e.g. the source RTTI pointer, the dest RTTI
pointer, etc.)~
- [x] ~`#cir.downcast_info` attribute, which gives even more detailed
information about a dynamic cast that is a down-cast. These information
will be used when rewriting the `cir.dyn_cast` operation with more
fundamental CIR operations.~
- [x] CIRGen support for the new operation and attributes.
- [x] Rewrite the new operation with more fundamental CIR operations in
LoweringPrepare.

~This is a draft PR. Now I only added the new operation / attributes,
and updated the CIRGen part. The LoweringPrepare for the new operation
is not implemented. Hopefully the draft can get some initial feedbacks
from the community and make sure it is on the right direction so we
don't waste time on wrong things.~

Related issue: #470 .
lanza pushed a commit that referenced this pull request Apr 29, 2024
This PR adds the `cir.dyn_cast` operation for representing
`dynamic_cast` in C++. It contains the following contents:

- [x] A new `cir.dyn_cast` operation.
- [x] ~Two new attributes that will be attached to `cir.dyn_cast`
operations:~
- [x] ~`#cir.dyn_cast_info` attributes, which gives general information
about a dynamic cast (e.g. the source RTTI pointer, the dest RTTI
pointer, etc.)~
- [x] ~`#cir.downcast_info` attribute, which gives even more detailed
information about a dynamic cast that is a down-cast. These information
will be used when rewriting the `cir.dyn_cast` operation with more
fundamental CIR operations.~
- [x] CIRGen support for the new operation and attributes.
- [x] Rewrite the new operation with more fundamental CIR operations in
LoweringPrepare.

~This is a draft PR. Now I only added the new operation / attributes,
and updated the CIRGen part. The LoweringPrepare for the new operation
is not implemented. Hopefully the draft can get some initial feedbacks
from the community and make sure it is on the right direction so we
don't waste time on wrong things.~

Related issue: #470 .
bruteforceboy pushed a commit to bruteforceboy/clangir that referenced this pull request Oct 2, 2024
This PR adds the `cir.dyn_cast` operation for representing
`dynamic_cast` in C++. It contains the following contents:

- [x] A new `cir.dyn_cast` operation.
- [x] ~Two new attributes that will be attached to `cir.dyn_cast`
operations:~
- [x] ~`#cir.dyn_cast_info` attributes, which gives general information
about a dynamic cast (e.g. the source RTTI pointer, the dest RTTI
pointer, etc.)~
- [x] ~`#cir.downcast_info` attribute, which gives even more detailed
information about a dynamic cast that is a down-cast. These information
will be used when rewriting the `cir.dyn_cast` operation with more
fundamental CIR operations.~
- [x] CIRGen support for the new operation and attributes.
- [x] Rewrite the new operation with more fundamental CIR operations in
LoweringPrepare.

~This is a draft PR. Now I only added the new operation / attributes,
and updated the CIRGen part. The LoweringPrepare for the new operation
is not implemented. Hopefully the draft can get some initial feedbacks
from the community and make sure it is on the right direction so we
don't waste time on wrong things.~

Related issue: llvm#470 .
Hugobros3 pushed a commit to shady-gang/clangir that referenced this pull request Oct 2, 2024
This PR adds the `cir.dyn_cast` operation for representing
`dynamic_cast` in C++. It contains the following contents:

- [x] A new `cir.dyn_cast` operation.
- [x] ~Two new attributes that will be attached to `cir.dyn_cast`
operations:~
- [x] ~`#cir.dyn_cast_info` attributes, which gives general information
about a dynamic cast (e.g. the source RTTI pointer, the dest RTTI
pointer, etc.)~
- [x] ~`#cir.downcast_info` attribute, which gives even more detailed
information about a dynamic cast that is a down-cast. These information
will be used when rewriting the `cir.dyn_cast` operation with more
fundamental CIR operations.~
- [x] CIRGen support for the new operation and attributes.
- [x] Rewrite the new operation with more fundamental CIR operations in
LoweringPrepare.

~This is a draft PR. Now I only added the new operation / attributes,
and updated the CIRGen part. The LoweringPrepare for the new operation
is not implemented. Hopefully the draft can get some initial feedbacks
from the community and make sure it is on the right direction so we
don't waste time on wrong things.~

Related issue: llvm#470 .
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
This PR adds the `cir.dyn_cast` operation for representing
`dynamic_cast` in C++. It contains the following contents:

- [x] A new `cir.dyn_cast` operation.
- [x] ~Two new attributes that will be attached to `cir.dyn_cast`
operations:~
- [x] ~`#cir.dyn_cast_info` attributes, which gives general information
about a dynamic cast (e.g. the source RTTI pointer, the dest RTTI
pointer, etc.)~
- [x] ~`#cir.downcast_info` attribute, which gives even more detailed
information about a dynamic cast that is a down-cast. These information
will be used when rewriting the `cir.dyn_cast` operation with more
fundamental CIR operations.~
- [x] CIRGen support for the new operation and attributes.
- [x] Rewrite the new operation with more fundamental CIR operations in
LoweringPrepare.

~This is a draft PR. Now I only added the new operation / attributes,
and updated the CIRGen part. The LoweringPrepare for the new operation
is not implemented. Hopefully the draft can get some initial feedbacks
from the community and make sure it is on the right direction so we
don't waste time on wrong things.~

Related issue: llvm#470 .
lanza pushed a commit that referenced this pull request Nov 5, 2024
This PR adds the `cir.dyn_cast` operation for representing
`dynamic_cast` in C++. It contains the following contents:

- [x] A new `cir.dyn_cast` operation.
- [x] ~Two new attributes that will be attached to `cir.dyn_cast`
operations:~
- [x] ~`#cir.dyn_cast_info` attributes, which gives general information
about a dynamic cast (e.g. the source RTTI pointer, the dest RTTI
pointer, etc.)~
- [x] ~`#cir.downcast_info` attribute, which gives even more detailed
information about a dynamic cast that is a down-cast. These information
will be used when rewriting the `cir.dyn_cast` operation with more
fundamental CIR operations.~
- [x] CIRGen support for the new operation and attributes.
- [x] Rewrite the new operation with more fundamental CIR operations in
LoweringPrepare.

~This is a draft PR. Now I only added the new operation / attributes,
and updated the CIRGen part. The LoweringPrepare for the new operation
is not implemented. Hopefully the draft can get some initial feedbacks
from the community and make sure it is on the right direction so we
don't waste time on wrong things.~

Related issue: #470 .
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.

2 participants