Skip to content

[KeyInstr][Clang] For stmt atom #134646

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 9 commits into from
Jun 3, 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
19 changes: 18 additions & 1 deletion clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,14 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
BoolCondVal, Stmt::getLikelihood(S.getBody()));

Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights);
auto *I = Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights);
// Key Instructions: Emit the condition and branch as separate atoms to
// match existing loop stepping behaviour. 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);

if (ExitBlock != LoopExit.getBlock()) {
EmitBlock(ExitBlock);
Expand All @@ -1389,6 +1396,10 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
EmitStmt(S.getBody());
}

// The last block in the loop's body (which unconditionally branches to the
// `inc` block if there is one).
auto *FinalBodyBB = Builder.GetInsertBlock();

// If there is an increment, emit it next.
if (S.getInc()) {
EmitBlock(Continue.getBlock());
Expand Down Expand Up @@ -1418,6 +1429,12 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,

if (CGM.shouldEmitConvergenceTokens())
ConvergenceTokenStack.pop_back();

if (FinalBodyBB) {
// Key Instructions: We want the for closing brace to be step-able on to
// match existing behaviour.
Comment on lines +1434 to +1435
Copy link
Member

Choose a reason for hiding this comment

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

IMO the comment should indicate the intention of what FinalBodyBB -- presumably the last block of the body where the scope ends. Does this mean that for brace-less for loops, we could theoretically step on the same last-line twice if there's a simple assignment and then a branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this mean that for brace-less for loops, we could theoretically step on the same last-line twice if there's a simple assignment and then a branch?

There could be two is_stmts there for the same line, yep (I think we discussed before that interpreting that is up to the debugger).

Added to test for additional coverage.

Updated comment.

addInstToNewSourceAtom(FinalBodyBB->getTerminator(), nullptr);
}
}

void
Expand Down
145 changes: 145 additions & 0 deletions clang/test/DebugInfo/KeyInstructions/for.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// RUN: %clang_cc1 -triple x86_64-linux-gnu -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

// RUN: %clang_cc1 -triple x86_64-linux-gnu -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 inc be its own source atom or not
// (currently it is).

// TODO: See do.c and while.c regarding cmp and cond br groups.

void a(int A) {
// CHECK: entry:
// CHECK: store i32 0, ptr %i{{.*}}, !dbg [[G1R1:!.*]]
// CHECK: for.cond:
// CHECK: %cmp = icmp slt i32 %0, %1, !dbg [[G2R1:!.*]]
// CHECK: br i1 %cmp, label %for.body, label %for.end, !dbg [[G3R1:!.*]]

// TODO: The unconditional br is given an atom group here which is useful for
// O0. Since we're no longer targeting O0 we should reevaluate whether this
// adds any value.
// CHECK: for.body:
// CHECK-NEXT: br label %for.inc, !dbg [[G5R1:!.*]]

// CHECK: for.inc:
// CHECK: %inc = add{{.*}}, !dbg [[G4R2:!.*]]
// CHECK: store i32 %inc, ptr %i{{.*}}, !dbg [[G4R1:!.*]]
for (int i = 0; i < A; ++i) {
}
}

void b(int A) {
// CHECK: entry:
// CHECK: store i32 0, ptr %i{{.*}}, !dbg [[bG1R1:!.*]]
// CHECK: for.cond:
// CHECK: %cmp = icmp slt i32 %0, %1, !dbg [[bG2R1:!.*]]
// CHECK: br i1 %cmp, label %for.body, label %for.end, !dbg [[bG3R1:!.*]]

// CHECK: for.body:
// CHECK-NEXT: %2 = load i32, ptr %A.addr
// - If stmt atom:
// CHECK-NEXT: %cmp1 = icmp sgt i32 %2, 1, !dbg [[bG4R2:!.*]]
// CHECK-NEXT: br i1 %cmp1, label %if.then, label %if.end, !dbg [[bG4R1:!.*]]
// CHECK: if.then:
// CHECK-NEXT: br label %if.end

// - For closing brace.
// CHECK: if.end:
// CHECK-NEXT: br label %for.inc, !dbg [[bG6R1:!.*]]

// CHECK: for.inc:
// CHECK: %inc = add{{.*}}, !dbg [[bG5R2:!.*]]
// CHECK: store i32 %inc, ptr %i{{.*}}, !dbg [[bG5R1:!.*]]
for (int i = 0; i < A; ++i) {
if (A > 1)
;
}
}

void c(int A) {
// CHECK: entry:
// CHECK: for.cond:
// CHECK-NEXT: %0 = load i32, ptr %A.addr
// - If stmt atom:
// CHECK-NEXT: %cmp = icmp sgt i32 %0, 1, !dbg [[cG1R2:!.*]]
// CHECK-NEXT: br i1 %cmp, label %if.then, label %if.end, !dbg [[cG1R1:!.*]]
// CHECK: if.then:
// CHECK-NEXT: br label %if.end

// - For closing brace.
// CHECK: if.end:
// CHECK-NEXT: br label %for.inc, !dbg [[cG3R1:!.*]]

// CHECK: for.inc:
// CHECK-NEXT: %1 = load i32, ptr %A.addr
// CHECK-NEXT: %inc = add{{.*}}, !dbg [[cG2R2:!.*]]
// CHECK-NEXT: store i32 %inc, ptr %A.addr{{.*}}, !dbg [[cG2R1:!.*]]
for (; /*no cond*/ ; ++A) {
if (A > 1)
;
}
}

void d() {
// - Check the `for` keyword gets an atom group.
// CHECK: entry:
// CHECK-NEXT: br label %for.cond

// CHECK: for.cond:
// CHECK: br label %for.cond, !dbg [[dG1R1:!.*]], !llvm.loop
for ( ; ; )
{
}
}

int x, i;
void ee();
void e() {
// - Check we assign an atom group to `for.body`s `br`, even without braces.
// - TODO: Investigate whether this is needed.
// CHECK: entry:

// CHECK: for.cond:
// CHECK-NEXT: %0 = load i32, ptr @i
// CHECK-NEXT: %cmp = icmp slt i32 %0, 3, !dbg [[eG1R1:!.*]]
// CHECK-NEXT: br i1 %cmp, label %for.body, label %for.end, !dbg [[eG2R1:!.*]]

// CHECK: for.body:
// CHECK-NEXT: %1 = load i32, ptr @i{{.*}}, !dbg [[eG3R2:!.*]]
// CHECK-NEXT: store i32 %1, ptr @x{{.*}}, !dbg [[eG3R1:!.*]]
// CHECK-NEXT: br label %for.inc, !dbg [[eG4R1:!.*]]
for (; i < 3; ee())
x = i;
}


// CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
// CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
// CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
// CHECK: [[G5R1]] = !DILocation(line: 29,{{.*}} atomGroup: 5, atomRank: 1)
// CHECK: [[G4R2]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 2)
// CHECK: [[G4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)

// CHECK: [[bG1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
// CHECK: [[bG2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
// CHECK: [[bG3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
// CHECK: [[bG4R2]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 2)
// CHECK: [[bG4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
// CHECK: [[bG6R1]] = !DILocation(line: 57,{{.*}} atomGroup: 6, atomRank: 1)
// CHECK: [[bG5R2]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 2)
// CHECK: [[bG5R1]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 1)

// CHECK: [[cG1R2]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 2)
// CHECK: [[cG1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
// CHECK: [[cG3R1]] = !DILocation(line: 81,{{.*}} atomGroup: 3, atomRank: 1)
// CHECK: [[cG2R2]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
// CHECK: [[cG2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)

// CHECK: [[dG1R1]] = !DILocation(line: 91, column: 3, scope: ![[#]], atomGroup: 1, atomRank: 1)

// CHECK: [[eG1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
// CHECK: [[eG2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
// CHECK: [[eG3R2]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
// CHECK: [[eG3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
// CHECK: [[eG4R1]] = !DILocation(line: 113, column: 5, scope: ![[#]], atomGroup: 4, atomRank: 1)
Loading