Skip to content

Commit 1002436

Browse files
authored
[KeyInstr][Clang] For range stmt atoms (#134647)
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 7797824 commit 1002436

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,14 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
14831483
if (!Weights && CGM.getCodeGenOpts().OptimizationLevel)
14841484
BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
14851485
BoolCondVal, Stmt::getLikelihood(S.getBody()));
1486-
Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights);
1486+
auto *I = Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights);
1487+
// Key Instructions: Emit the condition and branch as separate atoms to
1488+
// match existing loop stepping behaviour. FIXME: We could have the branch as
1489+
// the backup location for the condition, which would probably be a better
1490+
// experience.
1491+
if (auto *CondI = dyn_cast<llvm::Instruction>(BoolCondVal))
1492+
addInstToNewSourceAtom(CondI, nullptr);
1493+
addInstToNewSourceAtom(I, nullptr);
14871494

14881495
if (ExitBlock != LoopExit.getBlock()) {
14891496
EmitBlock(ExitBlock);
@@ -1509,6 +1516,10 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
15091516
EmitStmt(S.getBody());
15101517
}
15111518

1519+
// The last block in the loop's body (which unconditionally branches to the
1520+
// `inc` block if there is one).
1521+
auto *FinalBodyBB = Builder.GetInsertBlock();
1522+
15121523
EmitStopPoint(&S);
15131524
// If there is an increment, emit it next.
15141525
EmitBlock(Continue.getBlock());
@@ -1532,6 +1543,10 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
15321543

15331544
if (CGM.shouldEmitConvergenceTokens())
15341545
ConvergenceTokenStack.pop_back();
1546+
1547+
// We want the for closing brace to be step-able on to match existing
1548+
// behaviour.
1549+
addInstToNewSourceAtom(FinalBodyBB->getTerminator(), nullptr);
15351550
}
15361551

15371552
void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -gkey-instructions %s -debug-info-kind=line-tables-only -emit-llvm -o - \
2+
// RUN: | FileCheck %s
3+
4+
// Perennial question: should the inc be its own source atom or not
5+
// (currently it is).
6+
7+
// FIXME: See do.c and while.c regarding cmp and cond br groups.
8+
9+
// The stores in the setup (stores to __RANGE1, __BEGIN1, __END1) are all
10+
// marked as Key. Unclear whether that's desirable. Keep for now as that's
11+
// least risky (at worst it introduces an unnecessary step while debugging,
12+
// as opposed to potentially losing one we want).
13+
14+
// Check the conditional branch (and the condition) in FOR_COND and
15+
// unconditional branch in FOR_BODY are Key Instructions.
16+
17+
struct Range {
18+
int *begin();
19+
int *end();
20+
} r;
21+
22+
// CHECK-LABEL: define dso_local void @_Z1av(
23+
// CHECK-SAME: ) #[[ATTR0:[0-9]+]] !dbg [[DBG10:![0-9]+]] {
24+
// CHECK-NEXT: [[ENTRY:.*:]]
25+
// CHECK-NEXT: [[__RANGE1:%.*]] = alloca ptr, align 8
26+
// CHECK-NEXT: [[__BEGIN1:%.*]] = alloca ptr, align 8
27+
// CHECK-NEXT: [[__END1:%.*]] = alloca ptr, align 8
28+
// CHECK-NEXT: [[I:%.*]] = alloca i32, align 4
29+
// CHECK-NEXT: store ptr @r, ptr [[__RANGE1]], align 8, !dbg [[DBG14:![0-9]+]]
30+
// CHECK-NEXT: [[CALL:%.*]] = call noundef ptr @_ZN5Range5beginEv(ptr noundef nonnull align 1 dereferenceable(1) @r), !dbg [[DBG15:![0-9]+]]
31+
// CHECK-NEXT: store ptr [[CALL]], ptr [[__BEGIN1]], align 8, !dbg [[DBG16:![0-9]+]]
32+
// CHECK-NEXT: [[CALL1:%.*]] = call noundef ptr @_ZN5Range3endEv(ptr noundef nonnull align 1 dereferenceable(1) @r), !dbg [[DBG17:![0-9]+]]
33+
// CHECK-NEXT: store ptr [[CALL1]], ptr [[__END1]], align 8, !dbg [[DBG18:![0-9]+]]
34+
// CHECK-NEXT: br label %[[FOR_COND:.*]], !dbg [[DBG19:![0-9]+]]
35+
// CHECK: [[FOR_COND]]:
36+
// CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[__BEGIN1]], align 8, !dbg [[DBG19]]
37+
// CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr [[__END1]], align 8, !dbg [[DBG19]]
38+
// CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[TMP0]], [[TMP1]], !dbg [[DBG20:![0-9]+]]
39+
// CHECK-NEXT: br i1 [[CMP]], label %[[FOR_BODY:.*]], label %[[FOR_END:.*]], !dbg [[DBG21:![0-9]+]]
40+
// CHECK: [[FOR_BODY]]:
41+
// CHECK-NEXT: [[TMP2:%.*]] = load ptr, ptr [[__BEGIN1]], align 8, !dbg [[DBG19]]
42+
// CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4, !dbg [[DBG22:![0-9]+]]
43+
// CHECK-NEXT: store i32 [[TMP3]], ptr [[I]], align 4, !dbg [[DBG23:![0-9]+]]
44+
// CHECK-NEXT: br label %[[FOR_INC:.*]], !dbg [[DBG24:![0-9]+]]
45+
// CHECK: [[FOR_INC]]:
46+
// CHECK-NEXT: [[TMP4:%.*]] = load ptr, ptr [[__BEGIN1]], align 8, !dbg [[DBG19]]
47+
// CHECK-NEXT: [[INCDEC_PTR:%.*]] = getelementptr inbounds nuw i32, ptr [[TMP4]], i32 1, !dbg [[DBG25:![0-9]+]]
48+
// CHECK-NEXT: store ptr [[INCDEC_PTR]], ptr [[__BEGIN1]], align 8, !dbg [[DBG26:![0-9]+]]
49+
// CHECK-NEXT: br label %[[FOR_COND]], !dbg [[DBG19]], !llvm.loop
50+
// CHECK: [[FOR_END]]:
51+
// CHECK-NEXT: ret void, !dbg [[DBG30:![0-9]+]]
52+
//
53+
void a() {
54+
for (int i: r)
55+
;
56+
}
57+
58+
// - Check the branch out of the body gets an atom group (and gets it correct
59+
// if there's ctrl-flow in the body).
60+
void b() {
61+
for (int i: r) {
62+
if (i)
63+
;
64+
// CHECK: entry:
65+
// CHECK: if.end:
66+
// CHECK-NEXT: br label %for.inc, !dbg [[b_br:!.*]]
67+
}
68+
}
69+
70+
//.
71+
// CHECK: [[DBG14]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
72+
// CHECK: [[DBG15]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
73+
// CHECK: [[DBG16]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
74+
// CHECK: [[DBG17]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
75+
// CHECK: [[DBG18]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
76+
// CHECK: [[DBG19]] = !DILocation({{.*}})
77+
// CHECK: [[DBG20]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
78+
// CHECK: [[DBG21]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 1)
79+
// CHECK: [[DBG22]] = !DILocation({{.*}}, atomGroup: 6, atomRank: 2)
80+
// CHECK: [[DBG23]] = !DILocation({{.*}}, atomGroup: 6, atomRank: 1)
81+
// CHECK: [[DBG24]] = !DILocation({{.*}} atomGroup: 8, atomRank: 1)
82+
// CHECK: [[DBG25]] = !DILocation({{.*}}, atomGroup: 7, atomRank: 2)
83+
// CHECK: [[DBG26]] = !DILocation({{.*}}, atomGroup: 7, atomRank: 1)
84+
// CHECK: [[DBG30]] = !DILocation({{.*}})
85+
//
86+
// CHECK: [[b_br]] = !DILocation({{.*}}, atomGroup: [[#]], atomRank: [[#]])
87+
//.

0 commit comments

Comments
 (0)