diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h b/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h --- a/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h +++ b/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h @@ -113,8 +113,13 @@ enum class OMPScheduleType { Static = 34, /**< static unspecialized */ DynamicChunked = 35, + GuidedChunked = 36, /**< guided unspecialized */ + Runtime = 37, + Auto = 38, /**< auto */ + ModifierNonmonotonic = (1 << 30), /**< Set if the nonmonotonic schedule modifier was present */ + LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue */ ModifierNonmonotonic) }; diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h --- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h +++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h @@ -388,6 +388,7 @@ InsertPointTy createDynamicWorkshareLoop(const LocationDescription &Loc, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP, + omp::OMPScheduleType scheduleType, bool NeedsBarrier, Value *Chunk = nullptr); diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -1252,7 +1252,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createDynamicWorkshareLoop( const LocationDescription &Loc, CanonicalLoopInfo *CLI, - InsertPointTy AllocaIP, bool NeedsBarrier, Value *Chunk) { + InsertPointTy AllocaIP, OMPScheduleType schedType, bool NeedsBarrier, + Value *Chunk) { // Set up the source location value for OpenMP runtime. Builder.SetCurrentDebugLocation(Loc.DL); @@ -1299,7 +1300,7 @@ Value *ThreadNum = getOrCreateThreadID(SrcLoc); OMPScheduleType DynamicSchedType = - OMPScheduleType::DynamicChunked | OMPScheduleType::ModifierNonmonotonic; + schedType | OMPScheduleType::ModifierNonmonotonic; Constant *SchedulingType = ConstantInt::get(I32Type, static_cast(DynamicSchedType)); diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp --- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp +++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp @@ -149,6 +149,10 @@ DebugLoc DL; }; +class OpenMPIRBuilderTestWithParams + : public OpenMPIRBuilderTest, + public ::testing::WithParamInterface {}; + // Returns the value stored in the given allocation. Returns null if the given // value is not a result of an allocation, if no value is stored or if there is // more than one store. @@ -1708,18 +1712,34 @@ EXPECT_EQ(NumCallsInExitBlock, 3u); } -TEST_F(OpenMPIRBuilderTest, DynamicWorkShareLoop) { +TEST_P(OpenMPIRBuilderTestWithParams, DynamicWorkShareLoop) { using InsertPointTy = OpenMPIRBuilder::InsertPointTy; OpenMPIRBuilder OMPBuilder(*M); OMPBuilder.initialize(); IRBuilder<> Builder(BB); OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL}); + omp::OMPScheduleType SchedType = GetParam(); + uint32_t ChunkSize = 1; + switch (SchedType) { + case omp::OMPScheduleType::DynamicChunked: + case omp::OMPScheduleType::GuidedChunked: + ChunkSize = 7; + break; + case omp::OMPScheduleType::Auto: + case omp::OMPScheduleType::Runtime: + ChunkSize = 1; + break; + default: + assert(0 && "unknown type for this test"); + break; + } + Type *LCTy = Type::getInt32Ty(Ctx); Value *StartVal = ConstantInt::get(LCTy, 10); Value *StopVal = ConstantInt::get(LCTy, 52); Value *StepVal = ConstantInt::get(LCTy, 2); - Value *ChunkVal = ConstantInt::get(LCTy, 7); + Value *ChunkVal = ConstantInt::get(LCTy, ChunkSize); auto LoopBodyGen = [&](InsertPointTy, llvm::Value *) {}; CanonicalLoopInfo *CLI = OMPBuilder.createCanonicalLoop( @@ -1737,7 +1757,7 @@ Value *IV = CLI->getIndVar(); InsertPointTy EndIP = - OMPBuilder.createDynamicWorkshareLoop(Loc, CLI, AllocaIP, + OMPBuilder.createDynamicWorkshareLoop(Loc, CLI, AllocaIP, GetParam(), /*NeedsBarrier=*/true, ChunkVal); // The returned value should be the "after" point. ASSERT_EQ(EndIP.getBlock(), AfterIP.getBlock()); @@ -1775,7 +1795,7 @@ "__kmpc_dispatch_init_4u"); EXPECT_EQ(InitCall->getNumArgOperands(), 7U); EXPECT_EQ(InitCall->getArgOperand(6), - ConstantInt::get(Type::getInt32Ty(Ctx), 7)); + ConstantInt::get(Type::getInt32Ty(Ctx), ChunkSize)); ConstantInt *OrigLowerBound = dyn_cast(LowerBoundStore->getValueOperand()); @@ -1807,6 +1827,13 @@ EXPECT_FALSE(verifyModule(*M, &errs())); } +INSTANTIATE_TEST_CASE_P(OpenMPWSLoopSchedulingTypes, + OpenMPIRBuilderTestWithParams, + ::testing::Values(omp::OMPScheduleType::DynamicChunked, + omp::OMPScheduleType::GuidedChunked, + omp::OMPScheduleType::Auto, + omp::OMPScheduleType::Runtime)); + TEST_F(OpenMPIRBuilderTest, MasterDirective) { using InsertPointTy = OpenMPIRBuilder::InsertPointTy; OpenMPIRBuilder OMPBuilder(*M); diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp --- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp @@ -179,16 +179,19 @@ if (loop.getNumLoops() != 1) return opInst.emitOpError("collapsed loops not yet supported"); - bool isStatic = true; - + // Static is the default. + omp::ClauseScheduleKind schedule = omp::ClauseScheduleKind::Static; if (loop.schedule_val().hasValue()) { - auto schedule = - omp::symbolizeClauseScheduleKind(loop.schedule_val().getValue()); + schedule = + *omp::symbolizeClauseScheduleKind(loop.schedule_val().getValue()); if (schedule != omp::ClauseScheduleKind::Static && - schedule != omp::ClauseScheduleKind::Dynamic) - return opInst.emitOpError("only static (default) and dynamic loop " - "schedule is currently supported"); - isStatic = (schedule == omp::ClauseScheduleKind::Static); + schedule != omp::ClauseScheduleKind::Dynamic && + schedule != omp::ClauseScheduleKind::Guided && + schedule != omp::ClauseScheduleKind::Auto && + schedule != omp::ClauseScheduleKind::Runtime) + return opInst.emitOpError( + "only static (default), dynamic and guided loop " + "schedule is currently supported"); } // Find the loop configuration. @@ -249,13 +252,32 @@ insertBlock, insertBlock->getFirstInsertionPt()); llvm::OpenMPIRBuilder::InsertPointTy afterIP; llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder(); - if (isStatic) { + if (schedule == omp::ClauseScheduleKind::Static) { loopInfo = ompBuilder->createStaticWorkshareLoop(ompLoc, loopInfo, allocaIP, !loop.nowait(), chunk); afterIP = loopInfo->getAfterIP(); } else { - afterIP = ompBuilder->createDynamicWorkshareLoop(ompLoc, loopInfo, allocaIP, - !loop.nowait(), chunk); + llvm::omp::OMPScheduleType schedType; + switch (schedule) { + case omp::ClauseScheduleKind::Dynamic: + schedType = llvm::omp::OMPScheduleType::DynamicChunked; + break; + case omp::ClauseScheduleKind::Guided: + schedType = llvm::omp::OMPScheduleType::GuidedChunked; + break; + case omp::ClauseScheduleKind::Auto: + schedType = llvm::omp::OMPScheduleType::Auto; + break; + case omp::ClauseScheduleKind::Runtime: + schedType = llvm::omp::OMPScheduleType::Runtime; + break; + default: + assert(0 && "Unknown schedule value"); + break; + } + + afterIP = ompBuilder->createDynamicWorkshareLoop( + ompLoc, loopInfo, allocaIP, schedType, !loop.nowait(), chunk); } // Continue building IR after the loop.