Skip to content

Commit 46e356a

Browse files
authored
[KeyInstr][Clang] If stmt atom (#134642)
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 54eca71 commit 46e356a

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,8 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
21182118

21192119
llvm::Instruction *BrInst = Builder.CreateCondBr(CondV, TrueBlock, FalseBlock,
21202120
Weights, Unpredictable);
2121+
addInstToNewSourceAtom(BrInst, CondV);
2122+
21212123
switch (HLSLControlFlowAttr) {
21222124
case HLSLControlFlowHintAttr::Microsoft_branch:
21232125
case HLSLControlFlowHintAttr::Microsoft_flatten: {
@@ -3338,3 +3340,11 @@ void CodeGenFunction::addInstToSpecificSourceAtom(
33383340
if (CGDebugInfo *DI = getDebugInfo())
33393341
DI->addInstToSpecificSourceAtom(KeyInstruction, Backup, Atom);
33403342
}
3343+
3344+
void CodeGenFunction::addInstToNewSourceAtom(llvm::Instruction *KeyInstruction,
3345+
llvm::Value *Backup) {
3346+
if (CGDebugInfo *DI = getDebugInfo()) {
3347+
ApplyAtomGroup Grp(getDebugInfo());
3348+
DI->addInstToCurrentSourceAtom(KeyInstruction, Backup);
3349+
}
3350+
}

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,11 @@ class CodeGenFunction : public CodeGenTypeCache {
17621762
void addInstToSpecificSourceAtom(llvm::Instruction *KeyInstruction,
17631763
llvm::Value *Backup, uint64_t Atom);
17641764

1765+
/// Add \p KeyInstruction and an optional \p Backup instruction to a new atom
1766+
/// group (See ApplyAtomGroup for more info).
1767+
void addInstToNewSourceAtom(llvm::Instruction *KeyInstruction,
1768+
llvm::Value *Backup);
1769+
17651770
private:
17661771
/// SwitchInsn - This is nearest current switch instruction. It is null if
17671772
/// current context is not in a switch.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 --check-prefixes=CHECK,CHECK-CXX
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+
int g;
8+
void a(int A) {
9+
// The branch is a key instruction, with the condition being its backup.
10+
// CHECK: entry:
11+
// CHECK: %tobool = icmp ne i32 %0, 0{{.*}}, !dbg [[G1R2:!.*]]
12+
// CHECK: br i1 %tobool, label %if.then, label %if.end{{.*}}, !dbg [[G1R1:!.*]]
13+
if (A)
14+
;
15+
16+
// The assignment in the if gets a distinct source atom group.
17+
// CHECK: if.end:
18+
// CHECK: %1 = load i32, ptr %A.addr{{.*}}, !dbg [[G2R2:!.*]]
19+
// CHECK: store i32 %1, ptr @g{{.*}}, !dbg [[G2R1:!.*]]
20+
// CHECK: %tobool1 = icmp ne i32 %1, 0{{.*}}, !dbg [[G3R2:!.*]]
21+
// CHECK: br i1 %tobool1, label %if.then2, label %if.end3{{.*}}, !dbg [[G3R1:!.*]]
22+
if ((g = A))
23+
;
24+
25+
#ifdef __cplusplus
26+
// The assignment in the if gets a distinct source atom group.
27+
// CHECK-CXX: if.end3:
28+
// CHECK-CXX: %2 = load i32, ptr %A.addr{{.*}}, !dbg [[G4R2:!.*]]
29+
// CHECK-CXX: store i32 %2, ptr %B{{.*}}, !dbg [[G4R1:!.*]]
30+
// CHECK-CXX: %tobool4 = icmp ne i32 %3, 0{{.*}}, !dbg [[G5R2:!.*]]
31+
// CHECK-CXX: br i1 %tobool4, label %if.then5, label %if.end6{{.*}}, !dbg [[G5R1:!.*]]
32+
if (int B = A; B)
33+
;
34+
#endif
35+
}
36+
37+
// CHECK: [[G1R2]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 2)
38+
// CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
39+
// CHECK: [[G2R2]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
40+
// CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
41+
// CHECK: [[G3R2]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
42+
// CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
43+
// CHECK-CXX: [[G4R2]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 2)
44+
// CHECK-CXX: [[G4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
45+
// CHECK-CXX: [[G5R2]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 2)
46+
// CHECK-CXX: [[G5R1]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 1)

0 commit comments

Comments
 (0)