Skip to content

Conversation

OCHyams
Copy link
Contributor

@OCHyams OCHyams commented Apr 7, 2025

[KeyInstr][Clang] Ret atom

This patch is part of a stack that teaches Clang to generate Key Instructions
metadata for C and C++.

The Key Instructions project is introduced, including a "quick summary" section
at the top which adds context for this PR, here:
https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668

The feature is only functional in LLVM if LLVM is built with CMake flag
LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.

The Clang-side work is demoed here:
#130943

[KeyInstr][Clang] Update tests with ret atoms

This was referenced Apr 7, 2025
OCHyams added a commit to OCHyams/llvm-project that referenced this pull request May 27, 2025
CGDebugInfo::completeFunction was added previously but mistakenly
not called (dropped through the cracks while putting together
the patch stack). Moved out of llvm#134652 and llvm#134654.
OCHyams added a commit that referenced this pull request May 28, 2025
CGDebugInfo::completeFunction was added previously but mistakenly not
called (dropped through the cracks while putting together the patch
stack). Moved out of #134652 and #134654.

This patch is part of a stack that teaches Clang to generate Key Instructions
metadata for C and C++.

RFC:
https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668

The feature is only functional in LLVM if LLVM is built with CMake flag
LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.
Base automatically changed from users/OCHyams/ki-clang-builtins to main May 28, 2025 17:19
@OCHyams OCHyams force-pushed the users/OCHyams/ki-clang-ret branch from a13e2d5 to 93a95dd Compare June 3, 2025 10:05
Copy link
Contributor Author

@OCHyams OCHyams left a comment

Choose a reason for hiding this comment

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

Ready for review

  • Add parameter RetKeyInstructionsSourceAtom in EmitFunctionEpilog to request a ret gets a specific atomGroup number through addInstToSpecificSourceAtom

Most of the updated tests just check for any atom on the ret, but the two tests added in this PR check for specific atom numbers.

@OCHyams OCHyams marked this pull request as ready for review June 3, 2025 10:13
@OCHyams OCHyams requested review from SLTozer and jmorse June 3, 2025 10:13
Copy link
Member

@jmorse jmorse left a comment

Choose a reason for hiding this comment

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

If I understand correctly, most of the test changes are to ensure that return instructions get /an/ atom group, without being specific about which? IMO it'd be better to make that number explicit to avoid a return accidentally being part of the wrong atom group. It's necessary for it to be a distinct group, right? (A chore, but an important fact).

Copy link
Member

Choose a reason for hiding this comment

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

I think in isolation this comment will be hard to understand, particularly relating it to a ret. Perhaps "this atom" -> "the source atom leaving this scope"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code below has since been added in another patch that's already landed (without the comment) - rebased to remove from diff

Copy link
Member

Choose a reason for hiding this comment

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

Why's this memcpy getting the same atom group as the return?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The comment from test return.c covers this case:

// Check the stores to `retval` allocas and branches to `return` block are in
// the same atom group. They are both rank 1, which could in theory introduce
// an extra step in some optimized code. This low risk currently feels an
// acceptable for keeping the code a bit simpler (as opposed to adding
// scaffolding to make the store rank 2).

(But in this case the "store to retval" is a memcpy).

We want them in the same group because these are all instructions generated from the source return some_value.

Comment on lines +13 to +15
Copy link
Member

Choose a reason for hiding this comment

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

Could you remark on which test function this is, as I couldn't spot it. Presumably the folding happens in clang, rather than being something done by LLVM?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep in Clang - here

if (ReturnBlock.getBlock()->hasOneUse()) {

OCHyams added 12 commits June 3, 2025 15:04
This patch is part of a stack that teaches Clang to generate Key Instructions
metadata for C and C++.

The Key Instructions project is introduced, including a "quick summary" section
at the top which adds context for this PR, here:
https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668

The feature is only functional in LLVM if LLVM is built with CMake flag
LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.

The Clang-side work is demoed here:
#130943
Copy link
Contributor Author

@OCHyams OCHyams left a comment

Choose a reason for hiding this comment

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

If I understand correctly, most of the test changes are to ensure that return instructions get /an/ atom group, without being specific about which? IMO it'd be better to make that number explicit to avoid a return accidentally being part of the wrong atom group. It's necessary for it to be a distinct group, right? (A chore, but an important fact).

Done. Rebased (to address an inline comment and capture since-merged tests).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code below has since been added in another patch that's already landed (without the comment) - rebased to remove from diff

Comment on lines +13 to +15
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep in Clang - here

if (ReturnBlock.getBlock()->hasOneUse()) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The comment from test return.c covers this case:

// Check the stores to `retval` allocas and branches to `return` block are in
// the same atom group. They are both rank 1, which could in theory introduce
// an extra step in some optimized code. This low risk currently feels an
// acceptable for keeping the code a bit simpler (as opposed to adding
// scaffolding to make the store rank 2).

(But in this case the "store to retval" is a memcpy).

We want them in the same group because these are all instructions generated from the source return some_value.

@OCHyams OCHyams force-pushed the users/OCHyams/ki-clang-ret branch from 37c869a to 689c1b1 Compare June 3, 2025 14:39
Copy link
Member

@jmorse jmorse left a comment

Choose a reason for hiding this comment

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

LGTM

@OCHyams OCHyams merged commit 54d544b into main Jun 4, 2025
11 checks passed
@OCHyams OCHyams deleted the users/OCHyams/ki-clang-ret branch June 4, 2025 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants