Skip to content

[KeyInstr][Clang] Do stmt atom #134644

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 3 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1242,9 +1242,18 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S,
// As long as the condition is true, iterate the loop.
if (EmitBoolCondBranch) {
uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount;
Builder.CreateCondBr(
auto *I = Builder.CreateCondBr(
BoolCondVal, LoopBody, LoopExit.getBlock(),
createProfileWeightsForLoop(S.getCond(), BackedgeCount));

// Key Instructions: Emit the condition and branch as separate source
// location atoms otherwise we may omit a step onto the loop condition in
// favour of the closing brace.
// FIXME: We could have the branch as the backup location for the condition,
// which would probably be a better experience (no jumping to the brace).
if (auto *CondI = dyn_cast<llvm::Instruction>(BoolCondVal))
addInstToNewSourceAtom(CondI, nullptr);
addInstToNewSourceAtom(I, nullptr);
}

LoopStack.pop();
Expand Down
33 changes: 33 additions & 0 deletions clang/test/DebugInfo/KeyInstructions/do.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %clang_cc1 -gkey-instructions -x c++ -std=c++17 %s -debug-info-kind=line-tables-only -emit-llvm -o - \
// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank

// RUN: %clang_cc1 -gkey-instructions -x c %s -debug-info-kind=line-tables-only -emit-llvm -o - \
// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank

// Perennial question: should the `dec` be in its own source atom or not
// (currently it is).

// Another question - we've made the cmp and br separate source atoms for
// now, to match existing behaviour in this case:
// 1. do {
// 2. something();
// 3. }
// 4. while (--A);
// Non key instruction behaviour is: 2, 4[, 3, 2, 4]+
// The cond br is associated with the brace on line 3 and the cmp is line 4;
// if they were in the same atom group we'd step just: 2, 3[, 2, 3]+
// FIXME: We could arguably improve the behaviour by making them the same
// group but having the cmp higher precedence, resulting in: 2, 4[, 2, 4]+.
Comment on lines +19 to +20
Copy link
Member

Choose a reason for hiding this comment

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

This sounds like the kind of FIXME that we should have a ticket for so that we don't forget it -- having enough information to decide stepping behaviours like this is great, and we should plan on improvements (once KIs have landed).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, in our internal issue tracker


void a(int A) {
// CHECK: %dec = add nsw i32 %0, -1, !dbg [[G1R2:!.*]]
// CHECK: store i32 %dec, ptr %A.addr{{.*}}, !dbg [[G1R1:!.*]]
// CHECK: %tobool = icmp ne i32 %dec, 0, !dbg [[G2R1:!.*]]
// CHECK: br i1 %tobool, label %do.body, label %do.end, !dbg [[G3R1:!.*]], !llvm.loop
do { } while (--A);
}

// CHECK: [[G1R2]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 2)
// CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
// CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
// CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)