Skip to content

Commit ecbfdcd

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 860d1a0 commit ecbfdcd

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) {
@@ -197,6 +216,15 @@ static void convertLoopBounds(lower::AbstractConverter &converter,
197216
// ClauseProcessor unique clauses
198217
//===----------------------------------------------------------------------===//
199218

219+
bool ClauseProcessor::processBind(mlir::omp::BindClauseOps &result) const {
220+
if (auto *clause = findUniqueClause<omp::clause::Bind>()) {
221+
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
222+
result.bindKind = genBindKindAttr(firOpBuilder, *clause);
223+
return true;
224+
}
225+
return false;
226+
}
227+
200228
bool ClauseProcessor::processCollapse(
201229
mlir::Location currentLocation, lower::pft::Evaluation &eval,
202230
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
@@ -461,8 +461,19 @@ AtomicDefaultMemOrder make(const parser::OmpClause::AtomicDefaultMemOrder &inp,
461461

462462
Bind make(const parser::OmpClause::Bind &inp,
463463
semantics::SemanticsContext &semaCtx) {
464-
// inp -> empty
465-
llvm_unreachable("Empty: bind");
464+
// inp.v -> parser::OmpBindClause
465+
using wrapped = parser::OmpBindClause;
466+
467+
CLAUSET_ENUM_CONVERT( //
468+
convert, wrapped::Type, Bind::Binding,
469+
// clang-format off
470+
MS(Teams, Teams)
471+
MS(Parallel, Parallel)
472+
MS(Thread, Thread)
473+
// clang-format on
474+
);
475+
476+
return Bind{/*Binding=*/convert(inp.v.v)};
466477
}
467478

468479
// CancellationConstructType: empty

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,10 +2029,10 @@ static void genLoopClauses(
20292029
mlir::omp::LoopOperands &clauseOps,
20302030
llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
20312031
ClauseProcessor cp(converter, semaCtx, clauses);
2032+
cp.processBind(clauseOps);
20322033
cp.processOrder(clauseOps);
20332034
cp.processReduction(loc, clauseOps, reductionSyms);
2034-
cp.processTODO<clause::Bind, clause::Lastprivate>(
2035-
loc, llvm::omp::Directive::OMPD_loop);
2035+
cp.processTODO<clause::Lastprivate>(loc, llvm::omp::Directive::OMPD_loop);
20362036
}
20372037

20382038
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)