diff options
author | Sergio Afonso <safonsof@amd.com> | 2025-02-24 16:18:01 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-24 16:18:01 +0000 |
commit | cebb8f72b7937548bd17c7972297f2efafa1e958 (patch) | |
tree | cbd277e311dbcbac97ee13c59672f4661153cb22 /llvm/lib | |
parent | 538b898a836ac6efc3b0ec12cf27b511608d2e64 (diff) | |
download | llvm-cebb8f72b7937548bd17c7972297f2efafa1e958.zip llvm-cebb8f72b7937548bd17c7972297f2efafa1e958.tar.gz llvm-cebb8f72b7937548bd17c7972297f2efafa1e958.tar.bz2 |
[OpenMPIRBuilder] Add support for distribute constructs (#127816)
This patch adds the `OpenMPIRBuilder::createDistribute()` function and
updates `OpenMPIRBuilder::applyStaticWorkshareLoop()` in preparation for
adding `distribute` support to flang.
Co-authored-by: Dominik Adamski <dominik.adamski@amd.com>
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 59 |
1 files changed, 51 insertions, 8 deletions
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp index 18bc82f..1e6a195 100644 --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -2295,7 +2295,8 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createSections( return LoopInfo.takeError(); InsertPointOrErrorTy WsloopIP = - applyStaticWorkshareLoop(Loc.DL, *LoopInfo, AllocaIP, !IsNowait); + applyStaticWorkshareLoop(Loc.DL, *LoopInfo, AllocaIP, + WorksharingLoopType::ForStaticLoop, !IsNowait); if (!WsloopIP) return WsloopIP.takeError(); InsertPointTy AfterIP = *WsloopIP; @@ -4145,10 +4146,9 @@ static FunctionCallee getKmpcForStaticInitForType(Type *Ty, Module &M, llvm_unreachable("unknown OpenMP loop iterator bitwidth"); } -OpenMPIRBuilder::InsertPointOrErrorTy -OpenMPIRBuilder::applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI, - InsertPointTy AllocaIP, - bool NeedsBarrier) { +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::applyStaticWorkshareLoop( + DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP, + WorksharingLoopType LoopType, bool NeedsBarrier) { assert(CLI->isValid() && "Requires a valid canonical loop"); assert(!isConflictIP(AllocaIP, CLI->getPreheaderIP()) && "Require dedicated allocate IP"); @@ -4191,8 +4191,12 @@ OpenMPIRBuilder::applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI, Value *ThreadNum = getOrCreateThreadID(SrcLoc); - Constant *SchedulingType = ConstantInt::get( - I32Type, static_cast<int>(OMPScheduleType::UnorderedStatic)); + OMPScheduleType SchedType = + (LoopType == WorksharingLoopType::DistributeStaticLoop) + ? OMPScheduleType::OrderedDistribute + : OMPScheduleType::UnorderedStatic; + Constant *SchedulingType = + ConstantInt::get(I32Type, static_cast<int>(SchedType)); // Call the "init" function and update the trip count of the loop with the // value it produced. @@ -4452,6 +4456,7 @@ static void createTargetLoopWorkshareCall( RealArgs.push_back(TripCount); if (LoopType == WorksharingLoopType::DistributeStaticLoop) { RealArgs.push_back(ConstantInt::get(TripCountTy, 0)); + Builder.restoreIP({InsertBlock, std::prev(InsertBlock->end())}); Builder.CreateCall(RTLFn, RealArgs); return; } @@ -4645,7 +4650,7 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::applyWorkshareLoop( return applyDynamicWorkshareLoop(DL, CLI, AllocaIP, EffectiveScheduleType, NeedsBarrier, ChunkSize); // FIXME: Monotonicity ignored? - return applyStaticWorkshareLoop(DL, CLI, AllocaIP, NeedsBarrier); + return applyStaticWorkshareLoop(DL, CLI, AllocaIP, LoopType, NeedsBarrier); case OMPScheduleType::BaseStaticChunked: if (IsOrdered) @@ -9275,6 +9280,44 @@ OpenMPIRBuilder::createTeams(const LocationDescription &Loc, return Builder.saveIP(); } +OpenMPIRBuilder::InsertPointOrErrorTy +OpenMPIRBuilder::createDistribute(const LocationDescription &Loc, + InsertPointTy OuterAllocaIP, + BodyGenCallbackTy BodyGenCB) { + if (!updateToLocation(Loc)) + return InsertPointTy(); + + BasicBlock *OuterAllocaBB = OuterAllocaIP.getBlock(); + + if (OuterAllocaBB == Builder.GetInsertBlock()) { + BasicBlock *BodyBB = + splitBB(Builder, /*CreateBranch=*/true, "distribute.entry"); + Builder.SetInsertPoint(BodyBB, BodyBB->begin()); + } + BasicBlock *ExitBB = + splitBB(Builder, /*CreateBranch=*/true, "distribute.exit"); + BasicBlock *BodyBB = + splitBB(Builder, /*CreateBranch=*/true, "distribute.body"); + BasicBlock *AllocaBB = + splitBB(Builder, /*CreateBranch=*/true, "distribute.alloca"); + + // Generate the body of distribute clause + InsertPointTy AllocaIP(AllocaBB, AllocaBB->begin()); + InsertPointTy CodeGenIP(BodyBB, BodyBB->begin()); + if (Error Err = BodyGenCB(AllocaIP, CodeGenIP)) + return Err; + + OutlineInfo OI; + OI.OuterAllocaBB = OuterAllocaIP.getBlock(); + OI.EntryBB = AllocaBB; + OI.ExitBB = ExitBB; + + addOutlineInfo(std::move(OI)); + Builder.SetInsertPoint(ExitBB, ExitBB->begin()); + + return Builder.saveIP(); +} + GlobalVariable * OpenMPIRBuilder::createOffloadMapnames(SmallVectorImpl<llvm::Constant *> &Names, std::string VarName) { |