diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td --- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td +++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td @@ -136,6 +136,18 @@ let assemblyFormat = "attr-dict"; } +def OMP_SCHEDULE_MOD_None : StrEnumAttrCase<"none", 0>; +def OMP_SCHEDULE_MOD_Monotonic : StrEnumAttrCase<"monotonic", 1>; +def OMP_SCHEDULE_MOD_Nonmonotonic : StrEnumAttrCase<"nonmonotonic", 2>; + +def ScheduleModifier : StrEnumAttr<"ScheduleModifier", "OpenMP Schedule Modifier", + [OMP_SCHEDULE_MOD_None, + OMP_SCHEDULE_MOD_Monotonic, + OMP_SCHEDULE_MOD_Nonmonotonic]> +{ + let cppNamespace = "::mlir::omp"; +} + //===----------------------------------------------------------------------===// // 2.9.2 Workshare Loop Construct //===----------------------------------------------------------------------===// @@ -215,6 +227,7 @@ "array of symbol references">>:$reductions, OptionalAttr:$schedule_val, Optional:$schedule_chunk_var, + OptionalAttr:$schedule_modifier, Confined, [IntMinValue<0>]>:$collapse_val, UnitAttr:$nowait, Confined, [IntMinValue<0>]>:$ordered_val, diff --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp --- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp +++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp @@ -413,6 +413,7 @@ /// sched-wo-chunk ::= `auto` | `runtime` static ParseResult parseScheduleClause(OpAsmParser &parser, SmallString<8> &schedule, + SmallVectorImpl> &modifiers, Optional &chunkSize) { if (parser.parseLParen()) return failure(); @@ -436,6 +437,14 @@ return parser.emitError(parser.getNameLoc()) << " expected schedule kind"; } + // If there is a comma, we have one or more modifiers.. + if (succeeded(parser.parseOptionalComma())) { + StringRef mod; + if (parser.parseKeyword(&mod)) + return failure(); + modifiers.push_back(mod); + } + if (parser.parseRParen()) return failure(); @@ -531,6 +540,7 @@ SmallVector reductionVars; SmallVector reductionVarTypes; SmallString<8> schedule; + SmallVector> modifiers; Optional scheduleChunkSize; const StringRef opName = result.name.getStringRef(); @@ -582,7 +592,7 @@ } else if (keyword == "schedule") { if (!schedule.empty()) return allowedOnce(parser, "schedule", opName); - if (parseScheduleClause(parser, schedule, scheduleChunkSize)) + if (parseScheduleClause(parser, schedule, modifiers, scheduleChunkSize)) return failure(); if (scheduleChunkSize) { segments[scheduleClausePos] = 1; @@ -669,6 +679,10 @@ schedule[0] = llvm::toUpper(schedule[0]); auto attr = parser.getBuilder().getStringAttr(schedule); result.addAttribute("schedule_val", attr); + if (modifiers.size() > 0) { + auto mod = parser.getBuilder().getStringAttr(modifiers[0]); + result.addAttribute("schedule_modifier", mod); + } if (scheduleChunkSize) { auto chunkSizeType = parser.getBuilder().getI32Type(); parser.resolveOperand(*scheduleChunkSize, chunkSizeType, result.operands); @@ -727,6 +741,9 @@ if (auto chunk = op.schedule_chunk_var()) { p << " = " << chunk; } + auto modifier = op.schedule_modifier(); + if (modifier && modifier.getValue() != "none") + p << ", " << modifier; p << ")"; } 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 @@ -635,8 +635,23 @@ break; } - ompBuilder->applyDynamicWorkshareLoop(ompLoc.DL, loopInfo, allocaIP, - schedType, !loop.nowait(), chunk); + if (loop.schedule_modifier().hasValue()) { + omp::ScheduleModifier modifier = + *omp::symbolizeScheduleModifier(loop.schedule_modifier().getValue()); + switch (modifier) { + case omp::ScheduleModifier::monotonic: + schedType |= llvm::omp::OMPScheduleType::ModifierMonotonic; + break; + case omp::ScheduleModifier::nonmonotonic: + schedType |= llvm::omp::OMPScheduleType::ModifierNonmonotonic; + break; + default: + // Nothing to do here. + break; + } + } + afterIP = ompBuilder->applyDynamicWorkshareLoop( + ompLoc.DL, loopInfo, allocaIP, schedType, !loop.nowait(), chunk); } // Continue building IR after the loop. Note that the LoopInfo returned by diff --git a/mlir/test/Dialect/OpenMP/ops.mlir b/mlir/test/Dialect/OpenMP/ops.mlir --- a/mlir/test/Dialect/OpenMP/ops.mlir +++ b/mlir/test/Dialect/OpenMP/ops.mlir @@ -177,7 +177,7 @@ } // CHECK: omp.wsloop (%{{.*}}) : index = (%{{.*}}) to (%{{.*}}) step (%{{.*}}) linear(%{{.*}} = %{{.*}} : memref) schedule(static) - omp.wsloop (%iv) : index = (%lb) to (%ub) step (%step) schedule(static) lastprivate(%data_var : memref) linear(%data_var = %linear_var : memref) { + omp.wsloop (%iv) : index = (%lb) to (%ub) step (%step) schedule(static, none) lastprivate(%data_var : memref) linear(%data_var = %linear_var : memref) { omp.yield } @@ -188,6 +188,20 @@ omp.yield } + // CHECK: omp.wsloop (%{{.*}}) : index = (%{{.*}}) to (%{{.*}}) step (%{{.*}}) private(%{{.*}} : memref) firstprivate(%{{.*}} : memref) lastprivate(%{{.*}} : memref) linear(%{{.*}} = %{{.*}} : memref) schedule(dynamic = %{{.*}}, nonmonotonic) collapse(3) ordered(2) + omp.wsloop (%iv) : index = (%lb) to (%ub) step (%step) ordered(2) private(%data_var : memref) + firstprivate(%data_var : memref) lastprivate(%data_var : memref) linear(%data_var = %linear_var : memref) + schedule(dynamic = %chunk_var, nonmonotonic) collapse(3) { + omp.yield + } + + // CHECK: omp.wsloop (%{{.*}}) : index = (%{{.*}}) to (%{{.*}}) step (%{{.*}}) private(%{{.*}} : memref) firstprivate(%{{.*}} : memref) lastprivate(%{{.*}} : memref) linear(%{{.*}} = %{{.*}} : memref) schedule(dynamic = %{{.*}}, monotonic) collapse(3) ordered(2) + omp.wsloop (%iv) : index = (%lb) to (%ub) step (%step) ordered(2) private(%data_var : memref) + firstprivate(%data_var : memref) lastprivate(%data_var : memref) linear(%data_var = %linear_var : memref) + schedule(dynamic = %chunk_var, monotonic) collapse(3) { + omp.yield + } + // CHECK: omp.wsloop (%{{.*}}) : index = (%{{.*}}) to (%{{.*}}) step (%{{.*}}) private({{.*}} : memref) omp.wsloop (%iv) : index = (%lb) to (%ub) step (%step) private(%data_var : memref) { omp.yield diff --git a/mlir/test/Target/LLVMIR/openmp-llvm.mlir b/mlir/test/Target/LLVMIR/openmp-llvm.mlir --- a/mlir/test/Target/LLVMIR/openmp-llvm.mlir +++ b/mlir/test/Target/LLVMIR/openmp-llvm.mlir @@ -467,6 +467,30 @@ llvm.return } +llvm.func @test_omp_wsloop_dynamic_nonmonotonic(%lb : i64, %ub : i64, %step : i64) -> () { + omp.wsloop (%iv) : i64 = (%lb) to (%ub) step (%step) schedule(dynamic, nonmonotonic) { + // CHECK: call void @__kmpc_dispatch_init_8u(%struct.ident_t* @{{.*}}, i32 %{{.*}}, i32 1073741859 + // CHECK: %[[continue:.*]] = call i32 @__kmpc_dispatch_next_8u + // CHECK: %[[cond:.*]] = icmp ne i32 %[[continue]], 0 + // CHECK br i1 %[[cond]], label %omp_loop.header{{.*}}, label %omp_loop.exit{{.*}} + llvm.call @body(%iv) : (i64) -> () + omp.yield + } + llvm.return +} + +llvm.func @test_omp_wsloop_dynamic_monotonic(%lb : i64, %ub : i64, %step : i64) -> () { + omp.wsloop (%iv) : i64 = (%lb) to (%ub) step (%step) schedule(dynamic, monotonic) { + // CHECK: call void @__kmpc_dispatch_init_8u(%struct.ident_t* @{{.*}}, i32 %{{.*}}, i32 536870947 + // CHECK: %[[continue:.*]] = call i32 @__kmpc_dispatch_next_8u + // CHECK: %[[cond:.*]] = icmp ne i32 %[[continue]], 0 + // CHECK br i1 %[[cond]], label %omp_loop.header{{.*}}, label %omp_loop.exit{{.*}} + llvm.call @body(%iv) : (i64) -> () + omp.yield + } + llvm.return +} + // ----- omp.critical.declare @mutex