Skip to content

Commit 375ca9f

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 38f3eec commit 375ca9f

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
@@ -1324,6 +1324,7 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
13241324
Continue = getJumpDestInCurrentScope("for.inc");
13251325
BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
13261326

1327+
llvm::BasicBlock *ForBody = nullptr;
13271328
if (S.getCond()) {
13281329
// If the for statement has a condition scope, emit the local variable
13291330
// declaration.
@@ -1348,7 +1349,7 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
13481349
ExitBlock = createBasicBlock("for.cond.cleanup");
13491350

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

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

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

13671375
if (ExitBlock != LoopExit.getBlock()) {
13681376
EmitBlock(ExitBlock);
@@ -1416,6 +1424,12 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
14161424

14171425
if (CGM.shouldEmitConvergenceTokens())
14181426
ConvergenceTokenStack.pop_back();
1427+
1428+
if (ForBody) {
1429+
// Key Instructions: We want the for closing brace to be step-able on to
1430+
// match existing behaviour.
1431+
addInstToNewSourceAtom(ForBody->getTerminator(), nullptr);
1432+
}
14191433
}
14201434

14211435
void

clang/test/KeyInstructions/for.c

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)