Skip to content

Commit ac42923

Browse files
committed
Reapply "[KeyInstr][Clang] For range stmt atoms" (#142630)
This reverts commit e6529dc with crash fixed. Original PR #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 4d6e44d commit ac42923

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 17 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);
@@ -1508,6 +1515,9 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
15081515
EmitStmt(S.getLoopVarStmt());
15091516
EmitStmt(S.getBody());
15101517
}
1518+
// The last block in the loop's body (which unconditionally branches to the
1519+
// `inc` block if there is one).
1520+
auto *FinalBodyBB = Builder.GetInsertBlock();
15111521

15121522
EmitStopPoint(&S);
15131523
// If there is an increment, emit it next.
@@ -1532,6 +1542,12 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
15321542

15331543
if (CGM.shouldEmitConvergenceTokens())
15341544
ConvergenceTokenStack.pop_back();
1545+
1546+
if (FinalBodyBB) {
1547+
// We want the for closing brace to be step-able on to match existing
1548+
// behaviour.
1549+
addInstToNewSourceAtom(FinalBodyBB->getTerminator(), nullptr);
1550+
}
15351551
}
15361552

15371553
void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
// - Check an immediate loop exit (`break` in this case) gets an atom group and
71+
// - doesn't cause a crash.
72+
void c() {
73+
for (int i: r)
74+
// CHECK: entry:
75+
// CHECK: for.body:
76+
// CHECK: br label %for.end, !dbg [[c_br:!.*]]
77+
break;
78+
}
79+
80+
//.
81+
// CHECK: [[DBG14]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
82+
// CHECK: [[DBG15]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
83+
// CHECK: [[DBG16]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
84+
// CHECK: [[DBG17]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
85+
// CHECK: [[DBG18]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
86+
// CHECK: [[DBG19]] = !DILocation({{.*}})
87+
// CHECK: [[DBG20]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
88+
// CHECK: [[DBG21]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 1)
89+
// CHECK: [[DBG22]] = !DILocation({{.*}}, atomGroup: 6, atomRank: 2)
90+
// CHECK: [[DBG23]] = !DILocation({{.*}}, atomGroup: 6, atomRank: 1)
91+
// CHECK: [[DBG24]] = !DILocation({{.*}} atomGroup: 8, atomRank: 1)
92+
// CHECK: [[DBG25]] = !DILocation({{.*}}, atomGroup: 7, atomRank: 2)
93+
// CHECK: [[DBG26]] = !DILocation({{.*}}, atomGroup: 7, atomRank: 1)
94+
// CHECK: [[DBG30]] = !DILocation({{.*}})
95+
//
96+
// CHECK: [[b_br]] = !DILocation({{.*}}, atomGroup: [[#]], atomRank: [[#]])
97+
//
98+
// CHECK: [[c_br]] = !DILocation({{.*}}, atomGroup: [[#]], atomRank: [[#]])
99+
//.

0 commit comments

Comments
 (0)