Skip to content

Commit

Permalink
[CGCilk] Bring cilk_for-loop codegen in line with serial for-loop cod…
Browse files Browse the repository at this point in the history
…egen, including generation of mustprogress loop attribute.
  • Loading branch information
neboat committed Dec 6, 2023
1 parent 45956ef commit 262b7b2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
27 changes: 19 additions & 8 deletions clang/lib/CodeGen/CGCilk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,13 @@ void CodeGenFunction::EmitCilkForStmt(const CilkForStmt &S,
// C99 6.8.5p2/p4: The first substatement is executed if the expression
// compares unequal to 0. The condition must be a scalar type.
llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getInitCond());
Builder.CreateCondBr(
BoolCondVal, PForPH, ExitBlock,
createProfileWeightsForLoop(S.getInitCond(),
getProfileCount(S.getBody())));
llvm::MDNode *Weights = createProfileWeightsForLoop(
S.getInitCond(), getProfileCount(S.getBody()));
if (!Weights && CGM.getCodeGenOpts().OptimizationLevel)
BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
BoolCondVal, Stmt::getLikelihood(S.getBody()));

Builder.CreateCondBr(BoolCondVal, PForPH, ExitBlock, Weights);

if (ExitBlock != LoopExit.getBlock()) {
EmitBlock(ExitBlock);
Expand All @@ -623,11 +626,15 @@ void CodeGenFunction::EmitCilkForStmt(const CilkForStmt &S,
llvm::BasicBlock *CondBlock = Continue.getBlock();
EmitBlock(CondBlock);

Expr::EvalResult Result;
bool CondIsConstInt = S.getCond()->EvaluateAsInt(Result, getContext());

LoopStack.setSpawnStrategy(LoopAttributes::DAC);
const SourceRange &R = S.getSourceRange();
LoopStack.push(CondBlock, CGM.getContext(), CGM.getCodeGenOpts(), ForAttrs,
SourceLocToDebugLoc(R.getBegin()),
SourceLocToDebugLoc(R.getEnd()));
SourceLocToDebugLoc(R.getEnd()),
checkIfLoopMustProgress(CondIsConstInt));

const Expr *Inc = S.getInc();
assert(Inc && "_Cilk_for loop has no increment");
Expand Down Expand Up @@ -845,9 +852,13 @@ void CodeGenFunction::EmitCilkForStmt(const CilkForStmt &S,
// C99 6.8.5p2/p4: The first substatement is executed if the expression
// compares unequal to 0. The condition must be a scalar type.
llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Builder.CreateCondBr(
BoolCondVal, CondBlock, ExitBlock,
createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())));
llvm::MDNode *Weights =
createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()));
if (!Weights && CGM.getCodeGenOpts().OptimizationLevel)
BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
BoolCondVal, Stmt::getLikelihood(S.getBody()));

Builder.CreateCondBr(BoolCondVal, CondBlock, ExitBlock, Weights);

if (ExitBlock != LoopExit.getBlock()) {
EmitBlock(ExitBlock);
Expand Down
38 changes: 19 additions & 19 deletions clang/test/Cilk/cilkfor-bounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,23 +965,23 @@ void down_ne_stride_flip(size_t start, size_t end, size_t stride) {
// CHECK: [[PFORCONDCLEANUP]]:
// CHECK-NEXT: sync within %[[SYNCREG]]

// CHECK: ![[LOOPMD]] = distinct !{![[LOOPMD]], ![[SPAWNSTRATEGY:.+]]}
// CHECK: ![[LOOPMD]] = distinct !{![[LOOPMD]], ![[MUSTPROGRESS:[0-9]+]], ![[SPAWNSTRATEGY:[0-9]+]]}
// CHECK: ![[SPAWNSTRATEGY]] = !{!"tapir.loop.spawn.strategy", i32 1}
// CHECK: ![[LOOPMD2]] = distinct !{![[LOOPMD2]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD3]] = distinct !{![[LOOPMD3]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD4]] = distinct !{![[LOOPMD4]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD5]] = distinct !{![[LOOPMD5]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD6]] = distinct !{![[LOOPMD6]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD7]] = distinct !{![[LOOPMD7]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD8]] = distinct !{![[LOOPMD8]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD9]] = distinct !{![[LOOPMD9]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD10]] = distinct !{![[LOOPMD10]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD11]] = distinct !{![[LOOPMD11]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD12]] = distinct !{![[LOOPMD12]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD13]] = distinct !{![[LOOPMD13]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD14]] = distinct !{![[LOOPMD14]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD15]] = distinct !{![[LOOPMD15]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD16]] = distinct !{![[LOOPMD16]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD17]] = distinct !{![[LOOPMD17]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD18]] = distinct !{![[LOOPMD18]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD19]] = distinct !{![[LOOPMD19]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD2]] = distinct !{![[LOOPMD2]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD3]] = distinct !{![[LOOPMD3]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD4]] = distinct !{![[LOOPMD4]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD5]] = distinct !{![[LOOPMD5]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD6]] = distinct !{![[LOOPMD6]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD7]] = distinct !{![[LOOPMD7]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD8]] = distinct !{![[LOOPMD8]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD9]] = distinct !{![[LOOPMD9]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD10]] = distinct !{![[LOOPMD10]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD11]] = distinct !{![[LOOPMD11]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD12]] = distinct !{![[LOOPMD12]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD13]] = distinct !{![[LOOPMD13]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD14]] = distinct !{![[LOOPMD14]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD15]] = distinct !{![[LOOPMD15]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD16]] = distinct !{![[LOOPMD16]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD17]] = distinct !{![[LOOPMD17]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD18]] = distinct !{![[LOOPMD18]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
// CHECK: ![[LOOPMD19]] = distinct !{![[LOOPMD19]], ![[MUSTPROGRESS]], ![[SPAWNSTRATEGY]]}
9 changes: 5 additions & 4 deletions clang/test/Cilk/tapirloopattrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ void parfor_unroll_vec(double *restrict y, double *restrict x, double a, int n)
// CHECK: !llvm.loop [[LOOPID2:![0-9]+]]
// CHECK: !llvm.loop [[LOOPID3:![0-9]+]]

// CHECK: [[LOOPID1]] = distinct !{[[LOOPID1]], [[TAPIR_SPAWN_STRATEGY:![0-9]+]], [[NOVEC:![0-9]+]]}
// CHECK: [[LOOPID1]] = distinct !{[[LOOPID1]], [[MUSTPROGRESS:![0-9]+]], [[TAPIR_SPAWN_STRATEGY:![0-9]+]], [[NOVEC:![0-9]+]]}
// CHECK: [[MUSTPROGRESS]] = !{!"llvm.loop.mustprogress"}
// CHECK: [[TAPIR_SPAWN_STRATEGY]] = !{!"tapir.loop.spawn.strategy", i32 1}
// CHECK: [[NOVEC]] = !{!"llvm.loop.vectorize.width", i32 1}

// CHECK: [[LOOPID2]] = distinct !{[[LOOPID2]], [[MUSTPROGRESS:![0-9]+]], [[VECATTRS:!.+]], [[VECFOLLOWALL1:![0-9]+]]}
// CHECK: [[LOOPID2]] = distinct !{[[LOOPID2]], [[MUSTPROGRESS]], [[VECATTRS:!.+]], [[VECFOLLOWALL1:![0-9]+]]}
// CHECK: [[VECFOLLOWALL1]] = !{!"llvm.loop.vectorize.followup_all", [[VECFOLLOW1:![0-9]+]]}
// CHECK: [[VECFOLLOW1]] = distinct !{[[VECFOLLOW1]], [[MUSTPROGRESS]], [[VECFOLLOWATTRS:!.+]]}

// CHECK: [[LOOPID3]] = distinct !{[[LOOPID3]], [[TAPIR_SPAWN_STRATEGY]], [[VECATTRS]], [[VECFOLLOWALL2:![0-9]+]]}
// CHECK: [[LOOPID3]] = distinct !{[[LOOPID3]], [[MUSTPROGRESS]], [[TAPIR_SPAWN_STRATEGY]], [[VECATTRS]], [[VECFOLLOWALL2:![0-9]+]]}
// CHECK: [[VECFOLLOWALL2]] = !{!"llvm.loop.vectorize.followup_all", [[VECFOLLOW2:![0-9]+]]}
// CHECK: [[VECFOLLOW2]] = distinct !{[[VECFOLLOW2]], [[TAPIR_SPAWN_STRATEGY]], [[VECFOLLOWATTRS]]}
// CHECK: [[VECFOLLOW2]] = distinct !{[[VECFOLLOW2]], [[MUSTPROGRESS]], [[TAPIR_SPAWN_STRATEGY]], [[VECFOLLOWATTRS]]}

0 comments on commit 262b7b2

Please sign in to comment.