Skip to content

[KeyInstr][Clang] While stmt atom #134645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: users/OCHyams/ki-clang-do
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,14 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
if (!Weights && CGM.getCodeGenOpts().OptimizationLevel)
BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
BoolCondVal, Stmt::getLikelihood(S.getBody()));
Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock, Weights);
auto *I = Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock, Weights);
// Key Instructions: Emit the condition and branch as separate atoms to
// match existing loop stepping behaviour. FIXME: We could have the branch
// as the backup location for the condition, which would probably be a
// better experience. Explore this later.
if (auto *I = dyn_cast<llvm::Instruction>(BoolCondVal))
addInstToNewSourceAtom(I, nullptr);
addInstToNewSourceAtom(I, nullptr);

if (ExitBlock != LoopExit.getBlock()) {
EmitBlock(ExitBlock);
Expand Down
34 changes: 34 additions & 0 deletions clang/test/KeyInstructions/while.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %clang -gkey-instructions -x c++ -std=c++17 %s -gmlt -S -emit-llvm -o - \
// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank

// RUN: %clang -gkey-instructions -x c %s -gmlt -S -emit-llvm -o - \
// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank

// Perennial quesiton: should the `dec` be in its own source atom or not
// (currently it is).

// We've made the cmp and br separate source atoms for now, to match existing
// behaviour in this case:
// 1. while (
// 2. int i = --End
// 3. ) {
// 4. useValue(i);
// 5. }
// Without Key Instructions we go: 2, 1[, 4, 2, 1]+
// Without separating cmp and br with Key Instructions we'd get:
// 1[, 4, 1]+. If we made the cmp higher precedence than the
// br and had them in the same group, we could get:
// 2, [4, 2]+ which might be nicer. FIXME: do that later.

void a(int A) {
// CHECK: %dec = add nsw i32 %0, -1, !dbg [[G1R2:!.*]]
// CHECK: store i32 %dec, ptr %A.addr{{.*}}, !dbg [[G1R1:!.*]]
// CHECK: %tobool = icmp ne i32 %dec, 0, !dbg [[G2R1:!.*]]
// CHECK: br i1 %tobool, label %while.body, label %while.end, !dbg [[G3R1:!.*]]
while (--A) { };
}

// CHECK: [[G1R2]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 2)
// CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
// CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
// CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
Loading