Skip to content

Commit d489432

Browse files
committed
[KeyInstr][Clang] Do stmt atom
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++. 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
1 parent 97617b6 commit d489432

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,9 +1242,17 @@ 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 atoms to
1250+
// match existing loop stepping behaviour. FIXME: We could have the branch
1251+
// as the backup location for the condition, which would probably be a
1252+
// better experience (no jumping to the brace).
1253+
if (auto *I = dyn_cast<llvm::Instruction>(BoolCondVal))
1254+
addInstToNewSourceAtom(I, nullptr);
1255+
addInstToNewSourceAtom(I, nullptr);
12481256
}
12491257

12501258
LoopStack.pop();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %clang -gkey-instructions -x c++ -std=c++17 %s -gmlt -S -emit-llvm -o - \
2+
// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank
3+
4+
// RUN: %clang -gkey-instructions -x c %s -gmlt -S -emit-llvm -o - \
5+
// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank
6+
7+
// Perennial quesiton: 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)