[KeyInstr][Clang] Do stmt atom#134644
Conversation
|
@llvm/pr-subscribers-clang-codegen @llvm/pr-subscribers-clang Author: Orlando Cazalet-Hyams (OCHyams) ChangesSee test comment for possible future improvement. This patch is part of a stack that teaches Clang to generate Key Instructions The Key Instructions project is introduced, including a "quick summary" section The feature is only functional in LLVM if LLVM is built with CMake flag The Clang-side work is demoed here: Full diff: https://github.com/llvm/llvm-project/pull/134644.diff 2 Files Affected:
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 7dd82d73d6b6a..e46cfb433ab89 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -1242,9 +1242,17 @@ 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 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 *I = dyn_cast<llvm::Instruction>(BoolCondVal))
+ addInstToNewSourceAtom(I, nullptr);
+ addInstToNewSourceAtom(I, nullptr);
}
LoopStack.pop();
diff --git a/clang/test/KeyInstructions/do.c b/clang/test/KeyInstructions/do.c
new file mode 100644
index 0000000000000..4b7a38cb35985
--- /dev/null
+++ b/clang/test/KeyInstructions/do.c
@@ -0,0 +1,33 @@
+// RUN: %clang -gkey-instructions -x c++ -std=c++17 %s -gmlt -S -emit-llvm -o - \
+// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank
+
+// RUN: %clang -gkey-instructions -x c %s -gmlt -S -emit-llvm -o - \
+// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank
+
+// Perennial quesiton: 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]+.
+
+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)
|
eeee08d to
97617b6
Compare
2ee4d45 to
58697b4
Compare
jmorse
left a comment
There was a problem hiding this comment.
LGTM with some nits to address.
| createProfileWeightsForLoop(S.getCond(), BackedgeCount)); | ||
|
|
||
| // Key Instructions: Emit the condition and branch as separate atoms to | ||
| // match existing loop stepping behaviour. FIXME: We could have the branch |
There was a problem hiding this comment.
IMO "existing" should be expanded into what the stepping actually is, as this comment could unexpectedly go out of date.
| if (auto *I = dyn_cast<llvm::Instruction>(BoolCondVal)) | ||
| addInstToNewSourceAtom(I, nullptr); |
There was a problem hiding this comment.
Please call the casted BoolCondVal something other than I to avoid the reader having to think about shadowed variables ._.
| // 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 quesiton: should the `dec` be in its own source atom or not |
There was a problem hiding this comment.
| // Perennial quesiton: should the `dec` be in its own source atom or not | |
| // Perennial question: should the `dec` be in its own source atom or not |
| // 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]+. |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Done, in our internal issue tracker
97617b6 to
2e3a53b
Compare
58697b4 to
766c234
Compare
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
766c234 to
965a4fd
Compare

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