Skip to content

Commit 98ea64f

Browse files
committed
[KeyInstr][Clang] If stmt atom
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 638b319 commit 98ea64f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,8 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
20992099

21002100
llvm::Instruction *BrInst = Builder.CreateCondBr(CondV, TrueBlock, FalseBlock,
21012101
Weights, Unpredictable);
2102+
addInstToNewSourceAtom(BrInst, CondV);
2103+
21022104
switch (HLSLControlFlowAttr) {
21032105
case HLSLControlFlowHintAttr::Microsoft_branch:
21042106
case HLSLControlFlowHintAttr::Microsoft_flatten: {

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,16 @@ class CodeGenFunction : public CodeGenTypeCache {
17681768
DI->addInstToCurrentSourceAtom(KeyInstruction, Backup);
17691769
}
17701770

1771+
/// Add \p KeyInstruction and an optional \p Backup instruction to a new atom
1772+
/// group (See ApplyAtomGroup for more info).
1773+
void addInstToNewSourceAtom(llvm::Instruction *KeyInstruction,
1774+
llvm::Value *Backup) {
1775+
if (CGDebugInfo *DI = getDebugInfo()) {
1776+
ApplyAtomGroup Grp(getDebugInfo());
1777+
DI->addInstToCurrentSourceAtom(KeyInstruction, Backup);
1778+
}
1779+
}
1780+
17711781
/// See CGDebugInfo::addRetToOverrideOrNewSourceAtom.
17721782
void addRetToOverrideOrNewSourceAtom(llvm::ReturnInst *Ret,
17731783
llvm::Value *Backup) {

clang/test/KeyInstructions/if.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 --check-prefixes=CHECK,CHECK-CXX
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+
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)