Skip to content

Commit b4c0ef1

Browse files
authored
[flang][OpenMP] Add MLIR lowering for loop ... bind (#114219)
Extends MLIR lowering support for the `loop` directive by adding lowering support for the `bind` clause. Parent PR: #114199, only the latest commit is relevant to this PR.
1 parent 3c31ee7 commit b4c0ef1

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) {
@@ -204,6 +223,15 @@ static void convertLoopBounds(lower::AbstractConverter &converter,
204223
// ClauseProcessor unique clauses
205224
//===----------------------------------------------------------------------===//
206225

226+
bool ClauseProcessor::processBind(mlir::omp::BindClauseOps &result) const {
227+
if (auto *clause = findUniqueClause<omp::clause::Bind>()) {
228+
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
229+
result.bindKind = genBindKindAttr(firOpBuilder, *clause);
230+
return true;
231+
}
232+
return false;
233+
}
234+
207235
bool ClauseProcessor::processCollapse(
208236
mlir::Location currentLocation, lower::pft::Evaluation &eval,
209237
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
@@ -489,8 +489,19 @@ AtomicDefaultMemOrder make(const parser::OmpClause::AtomicDefaultMemOrder &inp,
489489

490490
Bind make(const parser::OmpClause::Bind &inp,
491491
semantics::SemanticsContext &semaCtx) {
492-
// inp -> empty
493-
llvm_unreachable("Empty: bind");
492+
// inp.v -> parser::OmpBindClause
493+
using wrapped = parser::OmpBindClause;
494+
495+
CLAUSET_ENUM_CONVERT( //
496+
convert, wrapped::Type, Bind::Binding,
497+
// clang-format off
498+
MS(Teams, Teams)
499+
MS(Parallel, Parallel)
500+
MS(Thread, Thread)
501+
// clang-format on
502+
);
503+
504+
return Bind{/*Binding=*/convert(inp.v.v)};
494505
}
495506

496507
// CancellationConstructType: empty

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,10 +1182,10 @@ static void genLoopClauses(
11821182
mlir::omp::LoopOperands &clauseOps,
11831183
llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
11841184
ClauseProcessor cp(converter, semaCtx, clauses);
1185+
cp.processBind(clauseOps);
11851186
cp.processOrder(clauseOps);
11861187
cp.processReduction(loc, clauseOps, reductionSyms);
1187-
cp.processTODO<clause::Bind, clause::Lastprivate>(
1188-
loc, llvm::omp::Directive::OMPD_loop);
1188+
cp.processTODO<clause::Lastprivate>(loc, llvm::omp::Directive::OMPD_loop);
11891189
}
11901190

11911191
static void genMaskedClauses(lower::AbstractConverter &converter,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,15 @@ subroutine test_reduction()
8888
end do
8989
!$omp end loop
9090
end subroutine
91+
92+
! CHECK-LABEL: func.func @_QPtest_bind
93+
subroutine test_bind()
94+
integer :: i, dummy = 1
95+
! CHECK: omp.loop bind(thread) private(@{{.*}} %{{.*}}#0 -> %{{.*}} : {{.*}}) {
96+
! CHECK: }
97+
!$omp loop bind(thread)
98+
do i=1,10
99+
dummy = dummy + 1
100+
end do
101+
!$omp end loop
102+
end subroutine

0 commit comments

Comments
 (0)