Skip to content

Commit 2484a49

Browse files
committed
[KeyInstr][Clang] For range stmt atoms
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 375ca9f commit 2484a49

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,14 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
14771477
if (!Weights && CGM.getCodeGenOpts().OptimizationLevel)
14781478
BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
14791479
BoolCondVal, Stmt::getLikelihood(S.getBody()));
1480-
Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights);
1480+
auto *I = Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights);
1481+
// Key Instructions: Emit the condition and branch as separate atoms to
1482+
// match existing loop stepping behaviour. FIXME: We could have the branch as
1483+
// the backup location for the condition, which would probably be a better
1484+
// experience.
1485+
if (auto *I = dyn_cast<llvm::Instruction>(BoolCondVal))
1486+
addInstToNewSourceAtom(I, nullptr);
1487+
addInstToNewSourceAtom(I, nullptr);
14811488

14821489
if (ExitBlock != LoopExit.getBlock()) {
14831490
EmitBlock(ExitBlock);
@@ -1526,6 +1533,10 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
15261533

15271534
if (CGM.shouldEmitConvergenceTokens())
15281535
ConvergenceTokenStack.pop_back();
1536+
1537+
// We want the for closing brace to be step-able on to match existing
1538+
// behaviour.
1539+
addInstToNewSourceAtom(ForBody->getTerminator(), nullptr);
15291540
}
15301541

15311542
void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang -gkey-instructions %s -gmlt -S -emit-llvm -o - \
3+
// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank
4+
5+
// Perennial quesiton: should the inc be its own source atom or not
6+
// (currently it is).
7+
8+
// FIXME: See do.c and while.c regarding cmp and cond br groups.
9+
10+
// The stores in the setup (stores to __RANGE1, __BEGIN1, __END1) are all
11+
// marked as Key. Unclear whether that's desirable. Keep for now as that's
12+
// least risky.
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: [[DBG14]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
59+
// CHECK: [[DBG15]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
60+
// CHECK: [[DBG16]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
61+
// CHECK: [[DBG17]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
62+
// CHECK: [[DBG18]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
63+
// CHECK: [[DBG19]] = !DILocation({{.*}})
64+
// CHECK: [[DBG20]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
65+
// CHECK: [[DBG21]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 1)
66+
// CHECK: [[DBG22]] = !DILocation({{.*}}, atomGroup: 6, atomRank: 2)
67+
// CHECK: [[DBG23]] = !DILocation({{.*}}, atomGroup: 6, atomRank: 1)
68+
// CHECK: [[DBG24]] = !DILocation({{.*}} atomGroup: 8, atomRank: 1)
69+
// CHECK: [[DBG25]] = !DILocation({{.*}}, atomGroup: 7, atomRank: 2)
70+
// CHECK: [[DBG26]] = !DILocation({{.*}}, atomGroup: 7, atomRank: 1)
71+
// CHECK: [[DBG30]] = !DILocation({{.*}})
72+
//.

0 commit comments

Comments
 (0)