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 operation for dynamic_cast #470

Closed
Lancern opened this issue Feb 18, 2024 · 3 comments
Closed

CIR operation for dynamic_cast #470

Lancern opened this issue Feb 18, 2024 · 3 comments

Comments

@Lancern
Copy link
Member

Lancern commented Feb 18, 2024

PR #426 introduces CIRGen support for the dynamic_cast operator and enables a fast path to LLVM lowering. As a follow up, we plan to invent a dedicated operation for dynamic_cast. During LLVM lowering prepare, this new operation will be replaced by what is generated currently for dynamic_cast.

There are multiple possible approaches to add this new operation, however:

The first approach is to reuse the current cir.cast operation and add new variants to represent dynamic_cast. Code below shows a possible CIRGen:

struct Base { /* ... */ };
struct Derived : Base { /* ... */ };

Derived *test(Base *b) {
  // %0 = cir.cast(dynamic, %b : !cir.ptr<!struct_Base>) : !cir.ptr<!struct_Derived>
  return dynamic_cast<Derived *>(b);
}

Derived &test(Base &b) {
  // %0 = cir.cast(dynamic, %b : !cir.ptr<!struct_Base>) : !cir.ptr<!struct_Derived>
  // %is_not_null = cir.cast(ptr_to_bool, %0)
  // %is_null = cir.unary(not, %is_null)
  // cir.if %is_null {
  //   cir.throw ...
  // }
  return dynamic_cast<Derived &>(b);
}

The names of these new variants can be decided later.

The second approach is to invent a new cir.dynamic_cast operation for it:

Derived *test(Base *b) {
  // %0 = cir.dynamic_cast(%b : !cir.ptr<!struct_Base>) : !cir.ptr<!struct_Derived>
  return dynamic_cast<Derived *>(b);
}

Derived &test(Base &b) {
  // %0 = cir.dynamic_cast(%b : !cir.ptr<!struct_Base>) : !cir.ptr<!struct_Derived>
  // %is_not_null = cir.cast(ptr_to_bool, %0)
  // %is_null = cir.unary(not, %is_null)
  // cir.if %is_null {
  //   cir.throw ...
  // }
  return dynamic_cast<Derived &>(b);
}

There may be other approaches as well.

Which one do you prefer?

@bcardosolopes
Copy link
Member

I'd say second approach so we're able to add a constraint directly in tablegen for requiring pointer input and output

@Lancern
Copy link
Member Author

Lancern commented Feb 22, 2024

Sound reasonable. Let's follow this approach.

bcardosolopes pushed a commit that referenced this issue Apr 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: #470 .
@bcardosolopes
Copy link
Member

Already implemented, closing

lanza pushed a commit that referenced this issue 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 issue 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 issue 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 issue 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 issue 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 issue 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 issue 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

No branches or pull requests

2 participants