Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[KeyInstr][Clang] Ret atom #134652

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

Merged
merged 12 commits into from
Jun 4, 2025
Merged

[KeyInstr][Clang] Ret atom #134652

merged 12 commits into from
Jun 4, 2025

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 OCHyams marked this pull request as draft May 23, 2025 13:52
@OCHyams
Copy link
Contributor Author

OCHyams commented May 23, 2025

Keeping in draft till the patches below this in the stack are merged, as this touches all their tests. Unfortunately the diff is going to be messed up till then too due to various branch retargeting and rebasing.

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 jmorse and SLTozer 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).

@@ -1118,6 +1118,8 @@ void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {

// Create the branch.
llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
// This is the primary instruction for this atom, acting in place of a ret.
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

__builtin_va_start(list, z);
// CHECK: vaarg.end:
// CHECK-NEXT: %vaarg.addr = phi ptr
// CHECK-NEXT: call void @llvm.memcpy{{.*}}, !dbg [[G1R1:!.*]]
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
// Also check that in the case of a single return (no control flow) the
// return instruction inherits the atom group of the branch to the return
// block when the blocks get folded togather.
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).

@@ -1118,6 +1118,8 @@ void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {

// Create the branch.
llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
// This is the primary instruction for this atom, acting in place of a ret.
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
// Also check that in the case of a single return (no control flow) the
// return instruction inherits the atom group of the branch to the return
// block when the blocks get folded togather.
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()) {

__builtin_va_start(list, z);
// CHECK: vaarg.end:
// CHECK-NEXT: %vaarg.addr = phi ptr
// CHECK-NEXT: call void @llvm.memcpy{{.*}}, !dbg [[G1R1:!.*]]
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
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 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.

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.
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.