Skip to content

Commit 00cc8b6

Browse files
[OpenMPIRBuilder] Add support for distribute constructs
This patch adds the `OpenMPIRBuilder::createDistribute()` function and updates `OpenMPIRBuilder::applyStaticWorkshareLoop()` in preparation for adding `distribute` support to flang. Co-authored-by: Sergio Afonso <[email protected]>
1 parent d5148f0 commit 00cc8b6

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,12 +1004,12 @@ class OpenMPIRBuilder {
10041004
/// preheader of the loop.
10051005
/// \param NeedsBarrier Indicates whether a barrier must be inserted after
10061006
/// the loop.
1007+
/// \param LoopType Type of workshare loop.
10071008
///
10081009
/// \returns Point where to insert code after the workshare construct.
1009-
InsertPointOrErrorTy applyStaticWorkshareLoop(DebugLoc DL,
1010-
CanonicalLoopInfo *CLI,
1011-
InsertPointTy AllocaIP,
1012-
bool NeedsBarrier);
1010+
InsertPointOrErrorTy applyStaticWorkshareLoop(
1011+
DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP,
1012+
omp::WorksharingLoopType LoopType, bool NeedsBarrier);
10131013

10141014
/// Modifies the canonical loop a statically-scheduled workshare loop with a
10151015
/// user-specified chunk size.
@@ -2666,6 +2666,15 @@ class OpenMPIRBuilder {
26662666
Value *NumTeamsLower = nullptr, Value *NumTeamsUpper = nullptr,
26672667
Value *ThreadLimit = nullptr, Value *IfExpr = nullptr);
26682668

2669+
/// Generator for `#omp distribute`
2670+
///
2671+
/// \param Loc The location where the distribute construct was encountered.
2672+
/// \param AllocaIP The insertion points to be used for alloca instructions.
2673+
/// \param BodyGenCB Callback that will generate the region code.
2674+
InsertPointOrErrorTy createDistribute(const LocationDescription &Loc,
2675+
InsertPointTy AllocaIP,
2676+
BodyGenCallbackTy BodyGenCB);
2677+
26692678
/// Generate conditional branch and relevant BasicBlocks through which private
26702679
/// threads copy the 'copyin' variables from Master copy to threadprivate
26712680
/// copies.

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,7 +2295,8 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createSections(
22952295
return LoopInfo.takeError();
22962296

22972297
InsertPointOrErrorTy WsloopIP =
2298-
applyStaticWorkshareLoop(Loc.DL, *LoopInfo, AllocaIP, !IsNowait);
2298+
applyStaticWorkshareLoop(Loc.DL, *LoopInfo, AllocaIP,
2299+
WorksharingLoopType::ForStaticLoop, !IsNowait);
22992300
if (!WsloopIP)
23002301
return WsloopIP.takeError();
23012302
InsertPointTy AfterIP = *WsloopIP;
@@ -4145,10 +4146,9 @@ static FunctionCallee getKmpcForStaticInitForType(Type *Ty, Module &M,
41454146
llvm_unreachable("unknown OpenMP loop iterator bitwidth");
41464147
}
41474148

4148-
OpenMPIRBuilder::InsertPointOrErrorTy
4149-
OpenMPIRBuilder::applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI,
4150-
InsertPointTy AllocaIP,
4151-
bool NeedsBarrier) {
4149+
OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::applyStaticWorkshareLoop(
4150+
DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP,
4151+
WorksharingLoopType LoopType, bool NeedsBarrier) {
41524152
assert(CLI->isValid() && "Requires a valid canonical loop");
41534153
assert(!isConflictIP(AllocaIP, CLI->getPreheaderIP()) &&
41544154
"Require dedicated allocate IP");
@@ -4191,8 +4191,12 @@ OpenMPIRBuilder::applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI,
41914191

41924192
Value *ThreadNum = getOrCreateThreadID(SrcLoc);
41934193

4194-
Constant *SchedulingType = ConstantInt::get(
4195-
I32Type, static_cast<int>(OMPScheduleType::UnorderedStatic));
4194+
OMPScheduleType SchedType =
4195+
(LoopType == WorksharingLoopType::DistributeStaticLoop)
4196+
? OMPScheduleType::OrderedDistribute
4197+
: OMPScheduleType::UnorderedStatic;
4198+
Constant *SchedulingType =
4199+
ConstantInt::get(I32Type, static_cast<int>(SchedType));
41964200

41974201
// Call the "init" function and update the trip count of the loop with the
41984202
// value it produced.
@@ -4452,6 +4456,7 @@ static void createTargetLoopWorkshareCall(
44524456
RealArgs.push_back(TripCount);
44534457
if (LoopType == WorksharingLoopType::DistributeStaticLoop) {
44544458
RealArgs.push_back(ConstantInt::get(TripCountTy, 0));
4459+
Builder.restoreIP({InsertBlock, std::prev(InsertBlock->end())});
44554460
Builder.CreateCall(RTLFn, RealArgs);
44564461
return;
44574462
}
@@ -4645,7 +4650,7 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::applyWorkshareLoop(
46454650
return applyDynamicWorkshareLoop(DL, CLI, AllocaIP, EffectiveScheduleType,
46464651
NeedsBarrier, ChunkSize);
46474652
// FIXME: Monotonicity ignored?
4648-
return applyStaticWorkshareLoop(DL, CLI, AllocaIP, NeedsBarrier);
4653+
return applyStaticWorkshareLoop(DL, CLI, AllocaIP, LoopType, NeedsBarrier);
46494654

46504655
case OMPScheduleType::BaseStaticChunked:
46514656
if (IsOrdered)
@@ -9275,6 +9280,44 @@ OpenMPIRBuilder::createTeams(const LocationDescription &Loc,
92759280
return Builder.saveIP();
92769281
}
92779282

9283+
OpenMPIRBuilder::InsertPointOrErrorTy
9284+
OpenMPIRBuilder::createDistribute(const LocationDescription &Loc,
9285+
InsertPointTy OuterAllocaIP,
9286+
BodyGenCallbackTy BodyGenCB) {
9287+
if (!updateToLocation(Loc))
9288+
return InsertPointTy();
9289+
9290+
BasicBlock *OuterAllocaBB = OuterAllocaIP.getBlock();
9291+
9292+
if (OuterAllocaBB == Builder.GetInsertBlock()) {
9293+
BasicBlock *BodyBB =
9294+
splitBB(Builder, /*CreateBranch=*/true, "distribute.entry");
9295+
Builder.SetInsertPoint(BodyBB, BodyBB->begin());
9296+
}
9297+
BasicBlock *ExitBB =
9298+
splitBB(Builder, /*CreateBranch=*/true, "distribute.exit");
9299+
BasicBlock *BodyBB =
9300+
splitBB(Builder, /*CreateBranch=*/true, "distribute.body");
9301+
BasicBlock *AllocaBB =
9302+
splitBB(Builder, /*CreateBranch=*/true, "distribute.alloca");
9303+
9304+
// Generate the body of distribute clause
9305+
InsertPointTy AllocaIP(AllocaBB, AllocaBB->begin());
9306+
InsertPointTy CodeGenIP(BodyBB, BodyBB->begin());
9307+
if (Error Err = BodyGenCB(AllocaIP, CodeGenIP))
9308+
return Err;
9309+
9310+
OutlineInfo OI;
9311+
OI.OuterAllocaBB = OuterAllocaIP.getBlock();
9312+
OI.EntryBB = AllocaBB;
9313+
OI.ExitBB = ExitBB;
9314+
9315+
addOutlineInfo(std::move(OI));
9316+
Builder.SetInsertPoint(ExitBB, ExitBB->begin());
9317+
9318+
return Builder.saveIP();
9319+
}
9320+
92789321
GlobalVariable *
92799322
OpenMPIRBuilder::createOffloadMapnames(SmallVectorImpl<llvm::Constant *> &Names,
92809323
std::string VarName) {

0 commit comments

Comments
 (0)