Skip to content

Commit 41c8267

Browse files
Applied remarks
1 parent 5b2a971 commit 41c8267

File tree

4 files changed

+74
-82
lines changed

4 files changed

+74
-82
lines changed

llvm/include/llvm/Frontend/OpenMP/OMPConstants.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@ enum class RTLDependenceKindTy {
277277
DepOmpAllMem = 0x80,
278278
};
279279

280+
/// A type of worksharing loop construct
281+
enum class WorksharingLoopType {
282+
// Worksharing `for`-loop
283+
ForStaticLoop,
284+
// Worksharing `distrbute`-loop
285+
DistributeStaticLoop,
286+
// Worksharing `distrbute parallel for`-loop
287+
DistributeForStaticLoop
288+
};
289+
280290
} // end namespace omp
281291

282292
} // end namespace llvm

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -439,16 +439,6 @@ class OffloadEntriesInfoManager {
439439
/// Each OpenMP directive has a corresponding public generator method.
440440
class OpenMPIRBuilder {
441441
public:
442-
/// A type of worksharing loop construct
443-
enum class WorksharingLoopType {
444-
// Worksharing `for`-loop
445-
ForStaticLoop,
446-
// Worksharing `distrbute`-loop
447-
DistributeStaticLoop,
448-
// Worksharing `distrbute parallel for`-loop
449-
DistributeForStaticLoop
450-
};
451-
452442
/// Create a new OpenMPIRBuilder operating on the given module \p M. This will
453443
/// not have an effect on \p M (see initialize)
454444
OpenMPIRBuilder(Module &M)
@@ -913,8 +903,8 @@ class OpenMPIRBuilder {
913903
/// Modifies the canonical loop to be a statically-scheduled workshare loop
914904
/// which is executed on the device
915905
///
916-
/// This takes a \p LoopInfo representing a canonical loop, such as the one
917-
/// created by \p createCanonicalLoop and emits additional instructions to
906+
/// This takes a \p CLI representing a canonical loop, such as the one
907+
/// created by \see createCanonicalLoop and emits additional instructions to
918908
/// turn it into a workshare loop. In particular, it calls to an OpenMP
919909
/// runtime function in the preheader to call OpenMP device rtl function
920910
/// which handles worksharing of loop body interations.
@@ -930,7 +920,7 @@ class OpenMPIRBuilder {
930920
/// \returns Point where to insert code after the workshare construct.
931921
InsertPointTy applyWorkshareLoopTarget(DebugLoc DL, CanonicalLoopInfo *CLI,
932922
InsertPointTy AllocaIP,
933-
WorksharingLoopType LoopType);
923+
omp::WorksharingLoopType LoopType);
934924

935925
/// Modifies the canonical loop to be a statically-scheduled workshare loop.
936926
///
@@ -1055,7 +1045,8 @@ class OpenMPIRBuilder {
10551045
Value *ChunkSize = nullptr, bool HasSimdModifier = false,
10561046
bool HasMonotonicModifier = false, bool HasNonmonotonicModifier = false,
10571047
bool HasOrderedClause = false,
1058-
WorksharingLoopType LoopType = WorksharingLoopType::ForStaticLoop);
1048+
omp::WorksharingLoopType LoopType =
1049+
omp::WorksharingLoopType::ForStaticLoop);
10591050

10601051
/// Tile a loop nest.
10611052
///

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,27 +2679,27 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyStaticChunkedWorkshareLoop(
26792679
// Always interpret integers as unsigned similarly to CanonicalLoopInfo.
26802680
static FunctionCallee
26812681
getKmpcForStaticLoopForType(Type *Ty, OpenMPIRBuilder *OMPBuilder,
2682-
OpenMPIRBuilder::WorksharingLoopType LoopType) {
2682+
WorksharingLoopType LoopType) {
26832683
unsigned Bitwidth = Ty->getIntegerBitWidth();
26842684
Module &M = OMPBuilder->M;
26852685
switch (LoopType) {
2686-
case OpenMPIRBuilder::WorksharingLoopType::ForStaticLoop:
2686+
case WorksharingLoopType::ForStaticLoop:
26872687
if (Bitwidth == 32)
26882688
return OMPBuilder->getOrCreateRuntimeFunction(
26892689
M, omp::RuntimeFunction::OMPRTL___kmpc_for_static_loop_4u);
26902690
if (Bitwidth == 64)
26912691
return OMPBuilder->getOrCreateRuntimeFunction(
26922692
M, omp::RuntimeFunction::OMPRTL___kmpc_for_static_loop_8u);
26932693
break;
2694-
case OpenMPIRBuilder::WorksharingLoopType::DistributeStaticLoop:
2694+
case WorksharingLoopType::DistributeStaticLoop:
26952695
if (Bitwidth == 32)
26962696
return OMPBuilder->getOrCreateRuntimeFunction(
26972697
M, omp::RuntimeFunction::OMPRTL___kmpc_distribute_static_loop_4u);
26982698
if (Bitwidth == 64)
26992699
return OMPBuilder->getOrCreateRuntimeFunction(
27002700
M, omp::RuntimeFunction::OMPRTL___kmpc_distribute_static_loop_8u);
27012701
break;
2702-
case OpenMPIRBuilder::WorksharingLoopType::DistributeForStaticLoop:
2702+
case WorksharingLoopType::DistributeForStaticLoop:
27032703
if (Bitwidth == 32)
27042704
return OMPBuilder->getOrCreateRuntimeFunction(
27052705
M, omp::RuntimeFunction::OMPRTL___kmpc_distribute_for_static_loop_4u);
@@ -2708,15 +2708,16 @@ getKmpcForStaticLoopForType(Type *Ty, OpenMPIRBuilder *OMPBuilder,
27082708
M, omp::RuntimeFunction::OMPRTL___kmpc_distribute_for_static_loop_8u);
27092709
break;
27102710
}
2711-
if (Bitwidth != 32 && Bitwidth != 64)
2712-
llvm_unreachable("unknown OpenMP loop iterator bitwidth");
2713-
return FunctionCallee();
2711+
if (Bitwidth != 32 && Bitwidth != 64) {
2712+
llvm_unreachable("Unknown OpenMP loop iterator bitwidth");
2713+
}
2714+
llvm_unreachable("Unknown type of OpenMP worksharing loop");
27142715
}
27152716

27162717
// Inserts a call to proper OpenMP Device RTL function which handles
27172718
// loop worksharing.
27182719
static void createTargetLoopWorkshareCall(
2719-
OpenMPIRBuilder *OMPBuilder, OpenMPIRBuilder::WorksharingLoopType LoopType,
2720+
OpenMPIRBuilder *OMPBuilder, WorksharingLoopType LoopType,
27202721
BasicBlock *InsertBlock, Value *Ident, Value *LoopBodyArg,
27212722
Type *ParallelTaskPtr, Value *TripCount, Function &LoopBodyFn) {
27222723
Type *TripCountTy = TripCount->getType();
@@ -2726,16 +2727,11 @@ static void createTargetLoopWorkshareCall(
27262727
getKmpcForStaticLoopForType(TripCountTy, OMPBuilder, LoopType);
27272728
SmallVector<Value *, 8> RealArgs;
27282729
RealArgs.push_back(Ident);
2729-
/*loop body func*/
27302730
RealArgs.push_back(Builder.CreateBitCast(&LoopBodyFn, ParallelTaskPtr));
2731-
/*loop body args*/
27322731
RealArgs.push_back(LoopBodyArg);
2733-
/*num of iters*/
27342732
RealArgs.push_back(TripCount);
2735-
if (LoopType == OpenMPIRBuilder::WorksharingLoopType::DistributeStaticLoop) {
2736-
/*block chunk*/ RealArgs.push_back(TripCountTy->getIntegerBitWidth() == 32
2737-
? Builder.getInt32(0)
2738-
: Builder.getInt64(0));
2733+
if (LoopType == WorksharingLoopType::DistributeStaticLoop) {
2734+
RealArgs.push_back(ConstantInt::get(TripCountTy, 0));
27392735
Builder.CreateCall(RTLFn, RealArgs);
27402736
return;
27412737
}
@@ -2744,17 +2740,12 @@ static void createTargetLoopWorkshareCall(
27442740
Builder.restoreIP({InsertBlock, std::prev(InsertBlock->end())});
27452741
Value *NumThreads = Builder.CreateCall(RTLNumThreads, {});
27462742

2747-
/*num of threads*/ RealArgs.push_back(
2743+
RealArgs.push_back(
27482744
Builder.CreateZExtOrTrunc(NumThreads, TripCountTy, "num.threads.cast"));
2749-
if (LoopType ==
2750-
OpenMPIRBuilder::WorksharingLoopType::DistributeForStaticLoop) {
2751-
/*block chunk*/ RealArgs.push_back(TripCountTy->getIntegerBitWidth() == 32
2752-
? Builder.getInt32(0)
2753-
: Builder.getInt64(0));
2745+
RealArgs.push_back(ConstantInt::get(TripCountTy, 0));
2746+
if (LoopType == WorksharingLoopType::DistributeForStaticLoop) {
2747+
RealArgs.push_back(ConstantInt::get(TripCountTy, 0));
27542748
}
2755-
/*thread chunk */ RealArgs.push_back(TripCountTy->getIntegerBitWidth() == 32
2756-
? Builder.getInt32(1)
2757-
: Builder.getInt64(1));
27582749

27592750
Builder.CreateCall(RTLFn, RealArgs);
27602751
}
@@ -2764,7 +2755,7 @@ workshareLoopTargetCallback(OpenMPIRBuilder *OMPIRBuilder,
27642755
CanonicalLoopInfo *CLI, Value *Ident,
27652756
Function &OutlinedFn, Type *ParallelTaskPtr,
27662757
const SmallVector<Instruction *, 4> &ToBeDeleted,
2767-
OpenMPIRBuilder::WorksharingLoopType LoopType) {
2758+
WorksharingLoopType LoopType) {
27682759
IRBuilder<> &Builder = OMPIRBuilder->Builder;
27692760
BasicBlock *Preheader = CLI->getPreheader();
27702761
Value *TripCount = CLI->getTripCount();
@@ -2795,19 +2786,19 @@ workshareLoopTargetCallback(OpenMPIRBuilder *OMPIRBuilder,
27952786
// Find the instruction which corresponds to loop body argument structure
27962787
// and remove the call to loop body function instruction.
27972788
Value *LoopBodyArg;
2798-
for (auto instIt = Preheader->begin(); instIt != Preheader->end(); ++instIt) {
2799-
if (CallInst *CallInstruction = dyn_cast<CallInst>(instIt)) {
2800-
if (CallInstruction->getCalledFunction() == &OutlinedFn) {
2801-
// Check in case no argument structure has been passed.
2802-
if (CallInstruction->arg_size() > 1)
2803-
LoopBodyArg = CallInstruction->getArgOperand(1);
2804-
else
2805-
LoopBodyArg = Constant::getNullValue(Builder.getPtrTy());
2806-
CallInstruction->eraseFromParent();
2807-
break;
2808-
}
2809-
}
2810-
}
2789+
User *OutlinedFnUser = OutlinedFn.getUniqueUndroppableUser();
2790+
assert(OutlinedFnUser &&
2791+
"Expected unique undroppable user of outlined function");
2792+
CallInst *OutlinedFnCallInstruction = dyn_cast<CallInst>(OutlinedFnUser);
2793+
assert(OutlinedFnCallInstruction && "Expected outlined function call");
2794+
assert((OutlinedFnCallInstruction->getParent() == Preheader) &&
2795+
"Expected outlined function call to be located in loop preheader");
2796+
// Check in case no argument structure has been passed.
2797+
if (OutlinedFnCallInstruction->arg_size() > 1)
2798+
LoopBodyArg = OutlinedFnCallInstruction->getArgOperand(1);
2799+
else
2800+
LoopBodyArg = Constant::getNullValue(Builder.getPtrTy());
2801+
OutlinedFnCallInstruction->eraseFromParent();
28112802

28122803
createTargetLoopWorkshareCall(OMPIRBuilder, LoopType, Preheader, Ident,
28132804
LoopBodyArg, ParallelTaskPtr, TripCount,
@@ -2818,9 +2809,10 @@ workshareLoopTargetCallback(OpenMPIRBuilder *OMPIRBuilder,
28182809
CLI->invalidate();
28192810
}
28202811

2821-
OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyWorkshareLoopTarget(
2822-
DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP,
2823-
OpenMPIRBuilder::WorksharingLoopType LoopType) {
2812+
OpenMPIRBuilder::InsertPointTy
2813+
OpenMPIRBuilder::applyWorkshareLoopTarget(DebugLoc DL, CanonicalLoopInfo *CLI,
2814+
InsertPointTy AllocaIP,
2815+
WorksharingLoopType LoopType) {
28242816
uint32_t SrcLocStrSize;
28252817
Constant *SrcLocStr = getOrCreateSrcLocStr(DL, SrcLocStrSize);
28262818
Value *Ident = getOrCreateIdent(SrcLocStr, SrcLocStrSize);
@@ -2844,14 +2836,14 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyWorkshareLoopTarget(
28442836

28452837
// Insert new loop counter variable which will be used only in loop
28462838
// body.
2847-
AllocaInst *newLoopCnt = Builder.CreateAlloca(CLI->getIndVarType(), 0, "");
2848-
Instruction *newLoopCntLoad =
2849-
Builder.CreateLoad(CLI->getIndVarType(), newLoopCnt);
2839+
AllocaInst *NewLoopCnt = Builder.CreateAlloca(CLI->getIndVarType(), 0, "");
2840+
Instruction *NewLoopCntLoad =
2841+
Builder.CreateLoad(CLI->getIndVarType(), NewLoopCnt);
28502842
// New loop counter instructions are redundant in the loop preheader when
28512843
// code generation for workshare loop is finshed. That's why mark them as
28522844
// ready for deletion.
2853-
ToBeDeleted.push_back(newLoopCntLoad);
2854-
ToBeDeleted.push_back(newLoopCnt);
2845+
ToBeDeleted.push_back(NewLoopCntLoad);
2846+
ToBeDeleted.push_back(NewLoopCnt);
28552847

28562848
// Analyse loop body region. Find all input variables which are used inside
28572849
// loop body region.
@@ -2884,22 +2876,17 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyWorkshareLoopTarget(
28842876
// We need to model loop body region as the function f(cnt, loop_arg).
28852877
// That's why we replace loop induction variable by the new counter
28862878
// which will be one of loop body function argument
2887-
std::vector<User *> Users(CLI->getIndVar()->user_begin(),
2888-
CLI->getIndVar()->user_end());
2889-
for (User *use : Users) {
2890-
if (Instruction *inst = dyn_cast<Instruction>(use)) {
2891-
if (ParallelRegionBlockSet.count(inst->getParent())) {
2892-
inst->replaceUsesOfWith(CLI->getIndVar(), newLoopCntLoad);
2879+
for (auto Use = CLI->getIndVar()->user_begin();
2880+
Use != CLI->getIndVar()->user_end(); ++Use) {
2881+
if (Instruction *Inst = dyn_cast<Instruction>(*Use)) {
2882+
if (ParallelRegionBlockSet.count(Inst->getParent())) {
2883+
Inst->replaceUsesOfWith(CLI->getIndVar(), NewLoopCntLoad);
28932884
}
28942885
}
28952886
}
2896-
Extractor.findInputsOutputs(Inputs, Outputs, SinkingCands);
2897-
for (Value *Input : Inputs) {
2898-
// Make sure that loop counter variable is not merged into loop body
2899-
// function argument structure and it is passed as separate variable
2900-
if (Input == newLoopCntLoad)
2901-
OI.ExcludeArgsFromAggregate.push_back(Input);
2902-
}
2887+
// Make sure that loop counter variable is not merged into loop body
2888+
// function argument structure and it is passed as separate variable
2889+
OI.ExcludeArgsFromAggregate.push_back(NewLoopCntLoad);
29032890

29042891
// PostOutline CB is invoked when loop body function is outlined and
29052892
// loop body is replaced by call to outlined function. We need to add
@@ -2920,7 +2907,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyWorkshareLoop(
29202907
bool NeedsBarrier, omp::ScheduleKind SchedKind, Value *ChunkSize,
29212908
bool HasSimdModifier, bool HasMonotonicModifier,
29222909
bool HasNonmonotonicModifier, bool HasOrderedClause,
2923-
OpenMPIRBuilder::WorksharingLoopType LoopType) {
2910+
WorksharingLoopType LoopType) {
29242911
if (Config.isTargetDevice())
29252912
return applyWorkshareLoopTarget(DL, CLI, AllocaIP, LoopType);
29262913
OMPScheduleType EffectiveScheduleType = computeOpenMPScheduleType(

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,26 +2257,30 @@ TEST_F(OpenMPIRBuilderTest, StaticWorkshareLoopTarget) {
22572257

22582258
IRBuilder<>::InsertPoint AfterIP = OMPBuilder.applyWorkshareLoop(
22592259
DL, CLI, AllocaIP, true, OMP_SCHEDULE_Static, nullptr, false, false,
2260-
false, false, OpenMPIRBuilder::WorksharingLoopType::ForStaticLoop);
2260+
false, false, WorksharingLoopType::ForStaticLoop);
22612261
Builder.restoreIP(AfterIP);
22622262
Builder.CreateRetVoid();
22632263

22642264
OMPBuilder.finalize();
22652265
EXPECT_FALSE(verifyModule(*M, &errs()));
22662266

22672267
CallInst *WorkshareLoopRuntimeCall = nullptr;
2268+
int WorkshareLoopRuntimeCallCnt = 0;
22682269
for (auto Inst = Preheader->begin(); Inst != Preheader->end(); ++Inst) {
22692270
CallInst *Call = dyn_cast<CallInst>(Inst);
2270-
if (Call) {
2271-
if (Call->getCalledFunction()) {
2272-
if (Call->getCalledFunction()->getName() ==
2273-
"__kmpc_for_static_loop_4u") {
2274-
WorkshareLoopRuntimeCall = Call;
2275-
}
2276-
}
2271+
if (!Call)
2272+
continue;
2273+
if (!Call->getCalledFunction())
2274+
continue;
2275+
2276+
if (Call->getCalledFunction()->getName() == "__kmpc_for_static_loop_4u") {
2277+
WorkshareLoopRuntimeCall = Call;
2278+
WorkshareLoopRuntimeCallCnt++;
22772279
}
22782280
}
22792281
EXPECT_NE(WorkshareLoopRuntimeCall, nullptr);
2282+
// Verify that there is only one call to workshare loop function
2283+
EXPECT_EQ(WorkshareLoopRuntimeCallCnt, 1);
22802284
// Check that pointer to loop body function is passed as second argument
22812285
Value *LoopBodyFuncArg = WorkshareLoopRuntimeCall->getArgOperand(1);
22822286
EXPECT_EQ(Builder.getPtrTy(), LoopBodyFuncArg->getType());

0 commit comments

Comments
 (0)