Skip to content

Commit 16fbead

Browse files
committed
[KeyInstr][Clang] For 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 510cb67 commit 16fbead

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,7 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
13261326
Continue = getJumpDestInCurrentScope("for.inc");
13271327
BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
13281328

1329+
llvm::BasicBlock *ForBody = nullptr;
13291330
if (S.getCond()) {
13301331
// If the for statement has a condition scope, emit the local variable
13311332
// declaration.
@@ -1350,7 +1351,7 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
13501351
ExitBlock = createBasicBlock("for.cond.cleanup");
13511352

13521353
// As long as the condition is true, iterate the loop.
1353-
llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1354+
ForBody = createBasicBlock("for.body");
13541355

13551356
// C99 6.8.5p2/p4: The first substatement is executed if the expression
13561357
// compares unequal to 0. The condition must be a scalar type.
@@ -1364,7 +1365,14 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
13641365
BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
13651366
BoolCondVal, Stmt::getLikelihood(S.getBody()));
13661367

1367-
Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights);
1368+
auto *I = Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights);
1369+
// Key Instructions: Emit the condition and branch as separate atoms to
1370+
// match existing loop stepping behaviour. FIXME: We could have the branch
1371+
// as the backup location for the condition, which would probably be a
1372+
// better experience (no jumping to the brace).
1373+
if (auto *I = dyn_cast<llvm::Instruction>(BoolCondVal))
1374+
addInstToNewSourceAtom(I, nullptr);
1375+
addInstToNewSourceAtom(I, nullptr);
13681376

13691377
if (ExitBlock != LoopExit.getBlock()) {
13701378
EmitBlock(ExitBlock);
@@ -1418,6 +1426,12 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
14181426

14191427
if (CGM.shouldEmitConvergenceTokens())
14201428
ConvergenceTokenStack.pop_back();
1429+
1430+
if (ForBody) {
1431+
// Key Instructions: We want the for closing brace to be step-able on to
1432+
// match existing behaviour.
1433+
addInstToNewSourceAtom(ForBody->getTerminator(), nullptr);
1434+
}
14211435
}
14221436

14231437
void
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %clang -gkey-instructions -x c++ %s -gmlt -S -emit-llvm -o - \
2+
// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank
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+
// Perennial quesiton: should the inc be its own source atom or not
8+
// (currently it is).
9+
10+
// FIXME: See do.c and while.c regarding cmp and cond br groups.
11+
12+
void a(int A) {
13+
// CHECK: entry:
14+
// CHECK: store i32 0, ptr %i{{.*}}, !dbg [[G1R1:!.*]]
15+
// CHECK: for.cond:
16+
// CHECK: %cmp = icmp slt i32 %0, %1, !dbg [[G2R1:!.*]]
17+
// CHECK: br i1 %cmp, label %for.body, label %for.end, !dbg [[G3R1:!.*]]
18+
19+
// FIXME: Added uncond br group here which is useful for O0, which we're
20+
// no longer targeting. With optimisations loop rotate puts the condition
21+
// into for.inc and simplifycfg smooshes that and for.body together, so
22+
// it's not clear whether it adds any value.
23+
// CHECK: for.body:
24+
// CHECK: br label %for.inc, !dbg [[G5R1:!.*]]
25+
26+
// CHECK: for.inc:
27+
// CHECK: %inc = add{{.*}}, !dbg [[G4R2:!.*]]
28+
// CHECK: store i32 %inc, ptr %i{{.*}}, !dbg [[G4R1:!.*]]
29+
for (int i = 0; i < A; ++i) { }
30+
}
31+
32+
// CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
33+
// CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
34+
// CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
35+
// CHECK: [[G5R1]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 1)
36+
// CHECK: [[G4R2]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 2)
37+
// CHECK: [[G4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)

0 commit comments

Comments
 (0)