Skip to content

Commit d7a0e0a

Browse files
committed
[flang][OpenMP] Add MLIR lowering for loop ... bind
Extends MLIR lowering support for the `loop` directive by adding lowering support for the `bind` clause.
1 parent 263e312 commit d7a0e0a

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,25 @@ genAllocateClause(lower::AbstractConverter &converter,
9898
genObjectList(objects, converter, allocateOperands);
9999
}
100100

101+
static mlir::omp::ClauseBindKindAttr
102+
genBindKindAttr(fir::FirOpBuilder &firOpBuilder,
103+
const omp::clause::Bind &clause) {
104+
mlir::omp::ClauseBindKind bindKind;
105+
switch (clause.v) {
106+
case omp::clause::Bind::Binding::Teams:
107+
bindKind = mlir::omp::ClauseBindKind::Teams;
108+
break;
109+
case omp::clause::Bind::Binding::Parallel:
110+
bindKind = mlir::omp::ClauseBindKind::Parallel;
111+
break;
112+
case omp::clause::Bind::Binding::Thread:
113+
bindKind = mlir::omp::ClauseBindKind::Thread;
114+
break;
115+
}
116+
return mlir::omp::ClauseBindKindAttr::get(firOpBuilder.getContext(),
117+
bindKind);
118+
}
119+
101120
static mlir::omp::ClauseProcBindKindAttr
102121
genProcBindKindAttr(fir::FirOpBuilder &firOpBuilder,
103122
const omp::clause::ProcBind &clause) {
@@ -199,6 +218,15 @@ static void convertLoopBounds(lower::AbstractConverter &converter,
199218
// ClauseProcessor unique clauses
200219
//===----------------------------------------------------------------------===//
201220

221+
bool ClauseProcessor::processBind(mlir::omp::BindClauseOps &result) const {
222+
if (auto *clause = findUniqueClause<omp::clause::Bind>()) {
223+
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
224+
result.bindKind = genBindKindAttr(firOpBuilder, *clause);
225+
return true;
226+
}
227+
return false;
228+
}
229+
202230
bool ClauseProcessor::processCollapse(
203231
mlir::Location currentLocation, lower::pft::Evaluation &eval,
204232
mlir::omp::LoopRelatedClauseOps &result,

flang/lib/Lower/OpenMP/ClauseProcessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ClauseProcessor {
5353
: converter(converter), semaCtx(semaCtx), clauses(clauses) {}
5454

5555
// 'Unique' clauses: They can appear at most once in the clause list.
56+
bool processBind(mlir::omp::BindClauseOps &result) const;
5657
bool
5758
processCollapse(mlir::Location currentLocation, lower::pft::Evaluation &eval,
5859
mlir::omp::LoopRelatedClauseOps &result,

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,19 @@ AtomicDefaultMemOrder make(const parser::OmpClause::AtomicDefaultMemOrder &inp,
482482

483483
Bind make(const parser::OmpClause::Bind &inp,
484484
semantics::SemanticsContext &semaCtx) {
485-
// inp -> empty
486-
llvm_unreachable("Empty: bind");
485+
// inp.v -> parser::OmpBindClause
486+
using wrapped = parser::OmpBindClause;
487+
488+
CLAUSET_ENUM_CONVERT( //
489+
convert, wrapped::Type, Bind::Binding,
490+
// clang-format off
491+
MS(Teams, Teams)
492+
MS(Parallel, Parallel)
493+
MS(Thread, Thread)
494+
// clang-format on
495+
);
496+
497+
return Bind{/*Binding=*/convert(inp.v.v)};
487498
}
488499

489500
// CancellationConstructType: empty

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,10 +2057,10 @@ static void genLoopClauses(
20572057
mlir::omp::LoopOperands &clauseOps,
20582058
llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
20592059
ClauseProcessor cp(converter, semaCtx, clauses);
2060+
cp.processBind(clauseOps);
20602061
cp.processOrder(clauseOps);
20612062
cp.processReduction(loc, clauseOps, reductionSyms);
2062-
cp.processTODO<clause::Bind, clause::Lastprivate>(
2063-
loc, llvm::omp::Directive::OMPD_loop);
2063+
cp.processTODO<clause::Lastprivate>(loc, llvm::omp::Directive::OMPD_loop);
20642064
}
20652065

20662066
static void genLoopOp(lower::AbstractConverter &converter,

flang/test/Lower/OpenMP/loop-directive.f90

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,15 @@ subroutine test_reduction()
8080
end do
8181
!$omp end loop
8282
end subroutine
83+
84+
! CHECK-LABEL: func.func @_QPtest_bind
85+
subroutine test_bind()
86+
integer :: i, dummy = 1
87+
! CHECK: omp.loop bind(thread) private(@{{.*}} %{{.*}}#0 -> %{{.*}} : {{.*}}) {
88+
! CHECK: }
89+
!$omp loop bind(thread)
90+
do i=1,10
91+
dummy = dummy + 1
92+
end do
93+
!$omp end loop
94+
end subroutine

0 commit comments

Comments
 (0)