Skip to content

[CIR][CodeGen] Introduce CIR CXXSpecialMember attribute #1711

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

bruteforceboy
Copy link
Contributor

I think this one is self-explanatory, so I will not write much πŸ™‚β€

Adding this attribute helps in optimizations like #1653, and using the attribute it's easy to create operations like cir.std.vector.ctor/cir.std.vector.dtor by just modifying IdiomRecognizer a bit. I believe it will also be useful for future optimizations. Finally, I updated quite a number of tests so they now reflect this attribute.

Please, let me know if you see any issues.

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 adding more of the building blocks here. In the future it'd be nice to have a representation for struct/class more closer to C++ and it will probably be easier to extract this type of information, but to prevent over engineering early, it feels right to incrementally add pieces that can enable us to better analyze C++ code and make transformations easier to write.

Can you look into changing LifetimeChecker.cpp to use this attribute instead of the current AST approach?

@bcardosolopes
Copy link
Member

(@andykaylor @erichkeane @dkolsen-pgi in case you have any extra thoughts here)

Copy link
Collaborator

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

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

Do we also want to set this attribute on cir.func definitions/declarations?

Copy link

github-actions bot commented Jun 25, 2025

βœ… With the latest revision this PR passed the C/C++ code formatter.

@bruteforceboy
Copy link
Contributor Author

A few updates:

  1. The attribute is now attached to the FuncOp (like ctor<type>) instead of the call
  2. The attribute now stores Type instead of a string
  3. I updated LifetimeChecker.cpp to make use of this attribute instead of the AST approach
  4. I added a couple of traits for the ctor attribute, and I think we can extend as necessary in the future
  5. CIR_CXXCtorDtor is now CIR_CXXSpecialMember
  6. Updated a couple of tests to reflect the ctor/dtor attribute.

cc: @bcardosolopes, @erichkeane, @andykaylor

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

1 comment, else seems reasonable, I'm happy when the others are.

@bruteforceboy bruteforceboy changed the title [CIR][CodeGen] Introduce CIR CXXCtorDtor attribute [CIR][CodeGen] Introduce CIR CXXSpecialMember attribute Jun 25, 2025
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 the update, one more round of suggestions

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 the updates, one more round!

@@ -3661,7 +3661,8 @@ def FuncOp : CIR_Op<"func", [
OptionalAttr<GlobalCtorAttr>:$global_ctor,
OptionalAttr<GlobalDtorAttr>:$global_dtor,
OptionalAttr<ArrayAttr>:$annotations,
OptionalAttr<AnyASTFunctionDeclAttr>:$ast
OptionalAttr<AnyASTFunctionDeclAttr>:$ast,
OptionalAttr<CIR_CXXSpecialMemberAttr>:$cxx_special_member
Copy link
Member

Choose a reason for hiding this comment

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

Please move this before the ast one.

@@ -2618,6 +2619,55 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {
state.addAttribute(annotationsNameAttr, annotations);
}

// Add CXXSpecialMember attributes.
if (mlir::succeeded(parser.parseOptionalKeyword("ctor"))) {
Copy link
Member

Choose a reason for hiding this comment

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

Both ctor and dtor already have defined printer/parsers, you shouldn't need to add the logic again here. Have you found any problems while using parseOptionalAttribute?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had problems using parseOptionalAttribute because of the custom print format I was going for: ctor<Type>. I have updated both the parsing and printing a bit now, so all these don't need to be repeated. Now the attribute looks like cxx_special_member<#cir.cxx_ctor<Type>>, and parsing is much neater now.

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.

4 participants