Skip to content

Commit 189d5da

Browse files
authored
[KeyInstr][Clang] Do stmt atom (#134644)
See test comment for possible future improvement. 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.
1 parent eb37abe commit 189d5da

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,9 +1242,18 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S,
12421242
// As long as the condition is true, iterate the loop.
12431243
if (EmitBoolCondBranch) {
12441244
uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount;
1245-
Builder.CreateCondBr(
1245+
auto *I = Builder.CreateCondBr(
12461246
BoolCondVal, LoopBody, LoopExit.getBlock(),
12471247
createProfileWeightsForLoop(S.getCond(), BackedgeCount));
1248+
1249+
// Key Instructions: Emit the condition and branch as separate source
1250+
// location atoms otherwise we may omit a step onto the loop condition in
1251+
// favour of the closing brace.
1252+
// FIXME: We could have the branch as the backup location for the condition,
1253+
// which would probably be a better experience (no jumping to the brace).
1254+
if (auto *CondI = dyn_cast<llvm::Instruction>(BoolCondVal))
1255+
addInstToNewSourceAtom(CondI, nullptr);
1256+
addInstToNewSourceAtom(I, nullptr);
12481257
}
12491258

12501259
LoopStack.pop();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %clang_cc1 -gkey-instructions -x c++ -std=c++17 %s -debug-info-kind=line-tables-only -emit-llvm -o - \
2+
// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank
3+
4+
// RUN: %clang_cc1 -gkey-instructions -x c %s -debug-info-kind=line-tables-only -emit-llvm -o - \
5+
// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank
6+
7+
// Perennial question: should the `dec` be in its own source atom or not
8+
// (currently it is).
9+
10+
// Another question - we've made the cmp and br separate source atoms for
11+
// now, to match existing behaviour in this case:
12+
// 1. do {
13+
// 2. something();
14+
// 3. }
15+
// 4. while (--A);
16+
// Non key instruction behaviour is: 2, 4[, 3, 2, 4]+
17+
// The cond br is associated with the brace on line 3 and the cmp is line 4;
18+
// if they were in the same atom group we'd step just: 2, 3[, 2, 3]+
19+
// FIXME: We could arguably improve the behaviour by making them the same
20+
// group but having the cmp higher precedence, resulting in: 2, 4[, 2, 4]+.
21+
22+
void a(int A) {
23+
// CHECK: %dec = add nsw i32 %0, -1, !dbg [[G1R2:!.*]]
24+
// CHECK: store i32 %dec, ptr %A.addr{{.*}}, !dbg [[G1R1:!.*]]
25+
// CHECK: %tobool = icmp ne i32 %dec, 0, !dbg [[G2R1:!.*]]
26+
// CHECK: br i1 %tobool, label %do.body, label %do.end, !dbg [[G3R1:!.*]], !llvm.loop
27+
do { } while (--A);
28+
}
29+
30+
// CHECK: [[G1R2]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 2)
31+
// CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
32+
// CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
33+
// CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)

0 commit comments

Comments
 (0)