[clang] 6bd3543 - [KeyInstr][Clang] While stmt atom (#134645)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 23 06:42:31 PDT 2025
Author: Orlando Cazalet-Hyams
Date: 2025-05-23T14:42:28+01:00
New Revision: 6bd3543a4d2977cb63f7212594ea0f2ac6e8ab12
URL: https://github.com/llvm/llvm-project/commit/6bd3543a4d2977cb63f7212594ea0f2ac6e8ab12
DIFF: https://github.com/llvm/llvm-project/commit/6bd3543a4d2977cb63f7212594ea0f2ac6e8ab12.diff
LOG: [KeyInstr][Clang] While stmt atom (#134645)
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.
Added:
clang/test/DebugInfo/KeyInstructions/while.c
Modified:
clang/lib/CodeGen/CGStmt.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index ca1e6522ad564..3b701116e5e4b 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -1130,7 +1130,15 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
if (!Weights && CGM.getCodeGenOpts().OptimizationLevel)
BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
BoolCondVal, Stmt::getLikelihood(S.getBody()));
- Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock, Weights);
+ auto *I = Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock, Weights);
+ // 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 `while` keyword.
+ // FIXME: We could have the branch as the backup location for the condition,
+ // which would probably be a better experience. Explore this later.
+ if (auto *CondI = dyn_cast<llvm::Instruction>(BoolCondVal))
+ addInstToNewSourceAtom(CondI, nullptr);
+ addInstToNewSourceAtom(I, nullptr);
if (ExitBlock != LoopExit.getBlock()) {
EmitBlock(ExitBlock);
diff --git a/clang/test/DebugInfo/KeyInstructions/while.c b/clang/test/DebugInfo/KeyInstructions/while.c
new file mode 100644
index 0000000000000..fc65abf5155d5
--- /dev/null
+++ b/clang/test/DebugInfo/KeyInstructions/while.c
@@ -0,0 +1,34 @@
+// 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).
+
+// We've made the cmp and br separate source atoms for now, to match existing
+// behaviour in this case:
+// 1. while (
+// 2. int i = --End
+// 3. ) {
+// 4. useValue(i);
+// 5. }
+// Without Key Instructions we go: 2, 1[, 4, 2, 1]+
+// Without separating cmp and br with Key Instructions we'd get:
+// 1[, 4, 1]+. If we made the cmp higher precedence than the
+// br and had them in the same group, we could get:
+// 2, [4, 2]+ which might be nicer. FIXME: do that later.
+
+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 %while.body, label %while.end, !dbg [[G3R1:!.*]]
+ 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)
More information about the cfe-commits
mailing list