Skip to content

[CIRGen] Convert trivial copy constructor and assignment operator calls to memcpy #1616

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

Closed
wants to merge 0 commits into from

Conversation

Arthur-Chang016
Copy link
Contributor

No description provided.

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 your first PR.

Some things that need improvements here:

  • Add a testcase (see others in clang/test/CIR for examples).
  • Follow the coding guidelines: https://llvm.github.io/clangir/GettingStarted/coding-guideline.html
  • Volatile: you need to detect whether it's volatile and add a llvm_unrecheable("NYI") (Not Yet Implemented) for it, so later on we can incrementally add volatile support and add the appropriated testcase.

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 pending few minor nits

@Arthur-Chang016
Copy link
Contributor Author

Arthur-Chang016 commented May 28, 2025

Rebase to the latest version and resolved conflicts.

The latest 6 changes are to resolve tests I break.

Thank you for taking a look—please let me know if you spot anything that still needs adjustment!

@Arthur-Chang016
Copy link
Contributor Author

Arthur-Chang016 commented May 28, 2025

I’ve resolved all test failures arising from my recent CIR CodeGen changes. After a thorough review, I’m confident there are no logic errors in the generator itself—rather, the existing tests needed to be updated to reflect the intended behavior.

0a96fdb

  • Since cir.copy now replaces the default copy constructor which is optimized out, I removed the copy ctor test.
  • Added a minimal test that forces the trivial constructor to be invoked, ensuring coverage.

b4991ff
a004b07

  • Updated tests to reflect that cir.call is correctly replaced by cir.copy.
  • Clarified in the tests that emitAggregateCopyCtor() will not emit cir.copy when the record type is empty.

054f719
c79da29
b581ea8

  • simple case : adjusted these test cases so they generate the expected cir.copy instruction.

@bcardosolopes
Copy link
Member

bcardosolopes commented May 28, 2025

Thanks for updating the PR.

CIR currently keeps trivial copy constructor and assignment operator as calls because later passes might want to recognize the underlying semantic (which we lose after it becomes a copy), i.e. a ctor call - this can be done in CIR by looking at the attached AST node (which one day we might decide to model with their proper operations, a potential line of future work if you are interested). Note this is not the case for emitMemberInitializer, which did a similar approach to your PR.

However, prior to LLVM lowering time we want to transform those into copies (which you did, but just a bit earlier). Ideally, instead of changing CIRGen, you can move this logic to LoweringPrepare (see some examples there and also in clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp on how to reuse AST). Have you explored that path?

Let me know if you have extra questions.

@Arthur-Chang016
Copy link
Contributor Author

Hi,

Totally understand that this optimization would be too early to apply at the CodeGen level.
I have a couple of questions:

  1. How can CIR carry Clang AST node information, especially when CIR is dumped as plain text?
  2. If the plain CIR doesn't retain the AST node, should we consider adding a pass to detect whether a function is a trivial copy?

I've only skimmed through the paper on LifetimeChecker and briefly looked at how pointer ownership want to handled in C++. But I'm happy to dive deeper into the implementation if needed!

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