Skip to content

Commit 03777ba

Browse files
committed
[KeyInstr][Clang] Switch 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 2fb58a6 commit 03777ba

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,6 +2276,8 @@ void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
22762276
// failure.
22772277
llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
22782278
SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
2279+
addInstToNewSourceAtom(SwitchInsn, CondV);
2280+
22792281
if (HLSLControlFlowAttr != HLSLControlFlowHintAttr::SpellingNotCalculated) {
22802282
llvm::MDBuilder MDHelper(CGM.getLLVMContext());
22812283
llvm::ConstantInt *BranchHintConstant =
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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, int B) {
9+
// CHECK: entry:
10+
// The load gets associated with the branch rather than the store.
11+
// FIXME: Is that the best thing to do?
12+
// CHECK: %0 = load i32, ptr %A.addr{{.*}}, !dbg [[G2R2:!.*]]
13+
// CHECK: store i32 %0, ptr @g{{.*}}, !dbg [[G1R1:!.*]]
14+
// CHECK: switch i32 %0, label %{{.*}} [
15+
// CHECK: i32 0, label %sw.bb
16+
// CHECK: i32 1, label %sw.bb1
17+
// CHECK: ], !dbg [[G2R1:!.*]]
18+
switch ((g = A)) {
19+
case 0: break;
20+
case 1: {
21+
// CHECK: sw.bb1:
22+
// CHECK: %1 = load i32, ptr %B.addr{{.*}}, !dbg [[G3R2:!.*]]
23+
// CHECK: switch i32 %1, label %{{.*}} [
24+
// CHECK: i32 0, label %sw.bb2
25+
// CHECK: ], !dbg [[G3R1:!.*]]
26+
switch ((B)) {
27+
case 0: {
28+
// Test that assignments in constant-folded switches don't go missing.
29+
// CHECK-CXX: sw.bb2:
30+
// CHECK-CXX: store i32 1, ptr %C{{.*}}, !dbg [[G4R1:!.*]]
31+
#ifdef __cplusplus
32+
switch (const int C = 1; C) {
33+
case 0: break;
34+
case 1: break;
35+
default: break;
36+
}
37+
#endif
38+
} break;
39+
default: break;
40+
}
41+
} break;
42+
default: break;
43+
}
44+
}
45+
46+
// CHECK: [[G2R2]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
47+
// CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
48+
// CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
49+
// CHECK: [[G3R2]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
50+
// CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
51+
// CHECK-CXX: [[G4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)

0 commit comments

Comments
 (0)