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 @@ -120,6 +120,10 @@ Runtime = 37, Auto = 38, // auto + StaticBalancedChunked = 45, // static with chunk adjustment (e.g., simd) + GuidedSimd = 46, // guided with chunk adjustment + RuntimeSimd = 47, // runtime with chunk adjustment + ModifierMonotonic = (1 << 29), // Set if the monotonic schedule modifier was present ModifierNonmonotonic = 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 @@ -139,11 +139,13 @@ 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 OMP_SCHEDULE_MOD_SIMD : StrEnumAttrCase<"simd", 3>; def ScheduleModifier : StrEnumAttr<"ScheduleModifier", "OpenMP Schedule Modifier", [OMP_SCHEDULE_MOD_None, OMP_SCHEDULE_MOD_Monotonic, - OMP_SCHEDULE_MOD_Nonmonotonic]> + OMP_SCHEDULE_MOD_Nonmonotonic, + OMP_SCHEDULE_MOD_SIMD]> { let cppNamespace = "::mlir::omp"; } @@ -227,6 +229,7 @@ OptionalAttr:$schedule_val, Optional:$schedule_chunk_var, OptionalAttr:$schedule_modifier, + OptionalAttr:$simd_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 @@ -241,16 +241,38 @@ } } +static ParseResult +validateModifiers(OpAsmParser &parser, + SmallVectorImpl> &modifiers) { + if (modifiers.size() > 2) + return parser.emitError(parser.getNameLoc()) << " unexpected modifier(s)"; + // If we have one modifier that is "simd", then stick a "none" modiifer in + // index 0. + if (modifiers.size() == 1) { + if (modifiers[0] == "simd") { + modifiers.push_back(modifiers[0]); + modifiers[0] = "none"; + } + } else if (modifiers.size() == 2 && + (modifiers[0] == "simd" || + (modifiers[1] != "simd" && modifiers[1] != "none"))) + return parser.emitError(parser.getNameLoc()) << " incorrect modifier order"; + return success(); +} + //===----------------------------------------------------------------------===// // Parser and printer for Schedule Clause //===----------------------------------------------------------------------===// /// schedule ::= `schedule` `(` sched-list `)` -/// sched-list ::= sched-val | sched-val sched-list -/// sched-val ::= sched-with-chunk | sched-wo-chunk +/// sched-list ::= sched-val | sched-val sched-list | sched-val `,` +/// sched-modifier sched-val ::= sched-with-chunk | sched-wo-chunk /// sched-with-chunk ::= sched-with-chunk-types (`=` ssa-id-and-type)? /// sched-with-chunk-types ::= `static` | `dynamic` | `guided` /// sched-wo-chunk ::= `auto` | `runtime` +/// sched-modifier ::= sched-modifier-val | sched-modifier-val `,` +/// sched-modifier-val sched-modifier ::= `monotonic` | `nonmonotonic` | `simd` +/// | `none` static ParseResult parseScheduleClause(OpAsmParser &parser, SmallString<8> &schedule, SmallVectorImpl> &modifiers, @@ -278,13 +300,20 @@ } // If there is a comma, we have one or more modifiers.. - if (succeeded(parser.parseOptionalComma())) { + while (succeeded(parser.parseOptionalComma())) { StringRef mod; if (parser.parseKeyword(&mod)) return failure(); + if (mod != "none" && mod != "monotonic" && mod != "nonmonotonic" && + mod != "simd") + return parser.emitError(parser.getNameLoc()) + << " unknown modifier type: " << mod; modifiers.push_back(mod); } + if (validateModifiers(parser, modifiers)) + return failure(); + if (parser.parseRParen()) return failure(); @@ -294,6 +323,7 @@ /// Print schedule clause static void printScheduleClause(OpAsmPrinter &p, StringRef &sched, llvm::Optional modifier, + llvm::Optional simd, Value scheduleChunkVar) { std::string schedLower = sched.lower(); p << "(" << schedLower; @@ -301,6 +331,8 @@ p << " = " << scheduleChunkVar; if (modifier && modifier.getValue() != "none") p << ", " << modifier; + if (simd && simd.getValue() != "none") + p << ", " << simd; p << ") "; } @@ -822,6 +854,10 @@ if (modifiers.size() > 0) { auto mod = parser.getBuilder().getStringAttr(modifiers[0]); result.addAttribute("schedule_modifier", mod); + if (modifiers.size() > 1) { + mod = parser.getBuilder().getStringAttr(modifiers[1]); + result.addAttribute("simd_modifier", mod); + } } if (scheduleChunkSize) { auto chunkSizeType = parser.getBuilder().getI32Type(); @@ -952,7 +988,7 @@ if (auto sched = op.schedule_val()) { p << "schedule"; printScheduleClause(p, sched.getValue(), op.schedule_modifier(), - op.schedule_chunk_var()); + op.simd_modifier(), op.schedule_chunk_var()); } if (auto collapse = op.collapse_val()) 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 @@ -684,6 +684,13 @@ ompBuilder->collapseLoops(diLoc, loopInfos, {}); allocaIP = findAllocaInsertPoint(builder, moduleTranslation); + + bool isSimd = false; + if (auto simd = loop.simd_modifier()) { + omp::ScheduleModifier modifier = *omp::symbolizeScheduleModifier(*simd); + isSimd = (modifier == omp::ScheduleModifier::simd); + } + if (schedule == omp::ClauseScheduleKind::Static) { ompBuilder->applyStaticWorkshareLoop(ompLoc.DL, loopInfo, allocaIP, !loop.nowait(), chunk); @@ -694,13 +701,19 @@ schedType = llvm::omp::OMPScheduleType::DynamicChunked; break; case omp::ClauseScheduleKind::Guided: - schedType = llvm::omp::OMPScheduleType::GuidedChunked; + if (isSimd) + schedType = llvm::omp::OMPScheduleType::GuidedSimd; + else + 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; + if (isSimd) + schedType = llvm::omp::OMPScheduleType::RuntimeSimd; + else + schedType = llvm::omp::OMPScheduleType::Runtime; break; default: llvm_unreachable("Unknown schedule value"); diff --git a/mlir/test/Target/LLVMIR/openmp-llvm-bad-schedule-modifier.mlir b/mlir/test/Target/LLVMIR/openmp-llvm-bad-schedule-modifier.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Target/LLVMIR/openmp-llvm-bad-schedule-modifier.mlir @@ -0,0 +1,49 @@ +// RUN: not mlir-translate -mlir-to-llvmir -split-input-file %s 2>&1 | FileCheck %s + +llvm.func @test_omp_wsloop_dynamic_bad_modifier(%lb : i64, %ub : i64, %step : i64) -> () { + omp.wsloop (%iv) : i64 = (%lb) to (%ub) step (%step) schedule(dynamic, ginandtonic) { + // CHECK: unknown modifier type: ginandtonic + omp.yield + } + llvm.return +} + +// ----- + +llvm.func @test_omp_wsloop_dynamic_many_modifier(%lb : i64, %ub : i64, %step : i64) -> () { + omp.wsloop (%iv) : i64 = (%lb) to (%ub) step (%step) schedule(dynamic, monotonic, monotonic, monotonic) { + // CHECK: unexpected modifier(s) + omp.yield + } + llvm.return +} + +// ----- + +llvm.func @test_omp_wsloop_dynamic_wrong_modifier(%lb : i64, %ub : i64, %step : i64) -> () { + omp.wsloop (%iv) : i64 = (%lb) to (%ub) step (%step) schedule(dynamic, simd, monotonic) { + // CHECK: incorrect modifier order + omp.yield + } + llvm.return +} + +// ----- + +llvm.func @test_omp_wsloop_dynamic_wrong_modifier2(%lb : i64, %ub : i64, %step : i64) -> () { + omp.wsloop (%iv) : i64 = (%lb) to (%ub) step (%step) schedule(dynamic, monotonic, monotonic) { + // CHECK: incorrect modifier order + omp.yield + } + llvm.return +} + +// ----- + +llvm.func @test_omp_wsloop_dynamic_wrong_modifier3(%lb : i64, %ub : i64, %step : i64) -> () { + omp.wsloop (%iv) : i64 = (%lb) to (%ub) step (%step) schedule(dynamic, simd, simd) { + // CHECK: incorrect modifier order + omp.yield + } + llvm.return +} 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 @@ -517,6 +517,30 @@ llvm.return } +llvm.func @test_omp_wsloop_runtime_simd(%lb : i64, %ub : i64, %step : i64) -> () { + omp.wsloop (%iv) : i64 = (%lb) to (%ub) step (%step) schedule(runtime, simd) { + // CHECK: call void @__kmpc_dispatch_init_8u(%struct.ident_t* @{{.*}}, i32 %{{.*}}, i32 47 + // 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_guided_simd(%lb : i64, %ub : i64, %step : i64) -> () { + omp.wsloop (%iv) : i64 = (%lb) to (%ub) step (%step) schedule(guided, simd) { + // CHECK: call void @__kmpc_dispatch_init_8u(%struct.ident_t* @{{.*}}, i32 %{{.*}}, i32 46 + // 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 hint(contended)