Skip to content

Commit 7056680

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 8df37d3 commit 7056680

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
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
@@ -484,8 +484,19 @@ AtomicDefaultMemOrder make(const parser::OmpClause::AtomicDefaultMemOrder &inp,
484484

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

491502
// CancellationConstructType: empty

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)