Skip to content

[flang][OpenMP] Add MLIR lowering for loop ... bind #114219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions flang/lib/Lower/OpenMP/ClauseProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<omp::clause::Bind>()) {
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,
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Lower/OpenMP/ClauseProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 13 additions & 2 deletions flang/lib/Lower/OpenMP/Clauses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,10 +1182,10 @@ static void genLoopClauses(
mlir::omp::LoopOperands &clauseOps,
llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
ClauseProcessor cp(converter, semaCtx, clauses);
cp.processBind(clauseOps);
cp.processOrder(clauseOps);
cp.processReduction(loc, clauseOps, reductionSyms);
cp.processTODO<clause::Bind, clause::Lastprivate>(
loc, llvm::omp::Directive::OMPD_loop);
cp.processTODO<clause::Lastprivate>(loc, llvm::omp::Directive::OMPD_loop);
}

static void genMaskedClauses(lower::AbstractConverter &converter,
Expand Down
12 changes: 12 additions & 0 deletions flang/test/Lower/OpenMP/loop-directive.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading