diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp index dd0068deb9426..d235f2313c6d6 100644 --- a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp +++ b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp @@ -98,6 +98,25 @@ genAllocateClause(lower::AbstractConverter &converter, genObjectList(objects, converter, allocateOperands); } +static mlir::omp::ClauseBindKindAttr +genBindKindAttr(fir::FirOpBuilder &firOpBuilder, + const omp::clause::Bind &clause) { + mlir::omp::ClauseBindKind bindKind; + switch (clause.v) { + case omp::clause::Bind::Binding::Teams: + bindKind = mlir::omp::ClauseBindKind::Teams; + break; + case omp::clause::Bind::Binding::Parallel: + bindKind = mlir::omp::ClauseBindKind::Parallel; + break; + case omp::clause::Bind::Binding::Thread: + bindKind = mlir::omp::ClauseBindKind::Thread; + break; + } + return mlir::omp::ClauseBindKindAttr::get(firOpBuilder.getContext(), + bindKind); +} + static mlir::omp::ClauseProcBindKindAttr genProcBindKindAttr(fir::FirOpBuilder &firOpBuilder, const omp::clause::ProcBind &clause) { @@ -204,6 +223,15 @@ static void convertLoopBounds(lower::AbstractConverter &converter, // ClauseProcessor unique clauses //===----------------------------------------------------------------------===// +bool ClauseProcessor::processBind(mlir::omp::BindClauseOps &result) const { + if (auto *clause = findUniqueClause()) { + fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); + result.bindKind = genBindKindAttr(firOpBuilder, *clause); + return true; + } + return false; +} + bool ClauseProcessor::processCollapse( mlir::Location currentLocation, lower::pft::Evaluation &eval, mlir::omp::LoopRelatedClauseOps &result, diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.h b/flang/lib/Lower/OpenMP/ClauseProcessor.h index cf2fc362a52d8..217d7c6917bd6 100644 --- a/flang/lib/Lower/OpenMP/ClauseProcessor.h +++ b/flang/lib/Lower/OpenMP/ClauseProcessor.h @@ -53,6 +53,7 @@ class ClauseProcessor { : converter(converter), semaCtx(semaCtx), clauses(clauses) {} // 'Unique' clauses: They can appear at most once in the clause list. + bool processBind(mlir::omp::BindClauseOps &result) const; bool processCollapse(mlir::Location currentLocation, lower::pft::Evaluation &eval, mlir::omp::LoopRelatedClauseOps &result, diff --git a/flang/lib/Lower/OpenMP/Clauses.cpp b/flang/lib/Lower/OpenMP/Clauses.cpp index 3dedd4864bafc..bba4b3092666d 100644 --- a/flang/lib/Lower/OpenMP/Clauses.cpp +++ b/flang/lib/Lower/OpenMP/Clauses.cpp @@ -489,8 +489,19 @@ AtomicDefaultMemOrder make(const parser::OmpClause::AtomicDefaultMemOrder &inp, Bind make(const parser::OmpClause::Bind &inp, semantics::SemanticsContext &semaCtx) { - // inp -> empty - llvm_unreachable("Empty: bind"); + // inp.v -> parser::OmpBindClause + using wrapped = parser::OmpBindClause; + + CLAUSET_ENUM_CONVERT( // + convert, wrapped::Type, Bind::Binding, + // clang-format off + MS(Teams, Teams) + MS(Parallel, Parallel) + MS(Thread, Thread) + // clang-format on + ); + + return Bind{/*Binding=*/convert(inp.v.v)}; } // CancellationConstructType: empty diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp index 789264411e531..5ca0702218e9f 100644 --- a/flang/lib/Lower/OpenMP/OpenMP.cpp +++ b/flang/lib/Lower/OpenMP/OpenMP.cpp @@ -1182,10 +1182,10 @@ static void genLoopClauses( mlir::omp::LoopOperands &clauseOps, llvm::SmallVectorImpl &reductionSyms) { ClauseProcessor cp(converter, semaCtx, clauses); + cp.processBind(clauseOps); cp.processOrder(clauseOps); cp.processReduction(loc, clauseOps, reductionSyms); - cp.processTODO( - loc, llvm::omp::Directive::OMPD_loop); + cp.processTODO(loc, llvm::omp::Directive::OMPD_loop); } static void genMaskedClauses(lower::AbstractConverter &converter, diff --git a/flang/test/Lower/OpenMP/loop-directive.f90 b/flang/test/Lower/OpenMP/loop-directive.f90 index 10cacf387aeef..4b4d640e449ee 100644 --- a/flang/test/Lower/OpenMP/loop-directive.f90 +++ b/flang/test/Lower/OpenMP/loop-directive.f90 @@ -88,3 +88,15 @@ subroutine test_reduction() end do !$omp end loop end subroutine + +! CHECK-LABEL: func.func @_QPtest_bind +subroutine test_bind() + integer :: i, dummy = 1 + ! CHECK: omp.loop bind(thread) private(@{{.*}} %{{.*}}#0 -> %{{.*}} : {{.*}}) { + ! CHECK: } + !$omp loop bind(thread) + do i=1,10 + dummy = dummy + 1 + end do + !$omp end loop +end subroutine