Skip to content

Commit e34e739

Browse files
authored
Adding Changes for invoking Masked Operation (#98423)
PR adds changes to the flang frontend to create the `MaskedOp` when `masked` directive is used in the input program. Omp masked is introduced in 5.2 standard and allows a parallel region to be executed by threads specified by a programmer. This is achieved with the help of filter clause which helps to specify thread id expected to execute the region. Other related PRs: - [Fortran Parsing and Semantic Support](#91432) - Merged - [MLIR Support](https://github.com/llvm/llvm-project/pull/96022/files) - Merged - [Lowering Support](#98401) - Under Review
1 parent b3f5c72 commit e34e739

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
lines changed

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ bool ClauseProcessor::processDistSchedule(
332332
return false;
333333
}
334334

335+
bool ClauseProcessor::processFilter(lower::StatementContext &stmtCtx,
336+
mlir::omp::FilterClauseOps &result) const {
337+
if (auto *clause = findUniqueClause<omp::clause::Filter>()) {
338+
result.filteredThreadIdVar =
339+
fir::getBase(converter.genExprValue(clause->v, stmtCtx));
340+
return true;
341+
}
342+
return false;
343+
}
344+
335345
bool ClauseProcessor::processFinal(lower::StatementContext &stmtCtx,
336346
mlir::omp::FinalClauseOps &result) const {
337347
const parser::CharBlock *source = nullptr;

flang/lib/Lower/OpenMP/ClauseProcessor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class ClauseProcessor {
6363
bool processDeviceType(mlir::omp::DeviceTypeClauseOps &result) const;
6464
bool processDistSchedule(lower::StatementContext &stmtCtx,
6565
mlir::omp::DistScheduleClauseOps &result) const;
66+
bool processFilter(lower::StatementContext &stmtCtx,
67+
mlir::omp::FilterClauseOps &result) const;
6668
bool processFinal(lower::StatementContext &stmtCtx,
6769
mlir::omp::FinalClauseOps &result) const;
6870
bool processHasDeviceAddr(

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,15 @@ genLoopNestClauses(lower::AbstractConverter &converter,
10671067
clauseOps.loopInclusiveAttr = converter.getFirOpBuilder().getUnitAttr();
10681068
}
10691069

1070+
static void genMaskedClauses(lower::AbstractConverter &converter,
1071+
semantics::SemanticsContext &semaCtx,
1072+
lower::StatementContext &stmtCtx,
1073+
const List<Clause> &clauses, mlir::Location loc,
1074+
mlir::omp::MaskedClauseOps &clauseOps) {
1075+
ClauseProcessor cp(converter, semaCtx, clauses);
1076+
cp.processFilter(stmtCtx, clauseOps);
1077+
}
1078+
10701079
static void
10711080
genOrderedRegionClauses(lower::AbstractConverter &converter,
10721081
semantics::SemanticsContext &semaCtx,
@@ -1377,6 +1386,21 @@ genLoopNestOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
13771386
queue, item, clauseOps);
13781387
}
13791388

1389+
static mlir::omp::MaskedOp
1390+
genMaskedOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
1391+
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
1392+
mlir::Location loc, const ConstructQueue &queue,
1393+
ConstructQueue::iterator item) {
1394+
lower::StatementContext stmtCtx;
1395+
mlir::omp::MaskedClauseOps clauseOps;
1396+
genMaskedClauses(converter, semaCtx, stmtCtx, item->clauses, loc, clauseOps);
1397+
1398+
return genOpWithBody<mlir::omp::MaskedOp>(
1399+
OpWithBodyGenInfo(converter, symTable, semaCtx, loc, eval,
1400+
llvm::omp::Directive::OMPD_masked),
1401+
queue, item, clauseOps);
1402+
}
1403+
13801404
static mlir::omp::MasterOp
13811405
genMasterOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
13821406
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
@@ -2136,9 +2160,11 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
21362160
*loopDsp);
21372161
break;
21382162
case llvm::omp::Directive::OMPD_loop:
2139-
case llvm::omp::Directive::OMPD_masked:
21402163
TODO(loc, "Unhandled directive " + llvm::omp::getOpenMPDirectiveName(dir));
21412164
break;
2165+
case llvm::omp::Directive::OMPD_masked:
2166+
genMaskedOp(converter, symTable, semaCtx, eval, loc, queue, item);
2167+
break;
21422168
case llvm::omp::Directive::OMPD_master:
21432169
genMasterOp(converter, symTable, semaCtx, eval, loc, queue, item);
21442170
break;
@@ -2478,6 +2504,7 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
24782504
!std::holds_alternative<clause::Copyprivate>(clause.u) &&
24792505
!std::holds_alternative<clause::Default>(clause.u) &&
24802506
!std::holds_alternative<clause::Depend>(clause.u) &&
2507+
!std::holds_alternative<clause::Filter>(clause.u) &&
24812508
!std::holds_alternative<clause::Final>(clause.u) &&
24822509
!std::holds_alternative<clause::Firstprivate>(clause.u) &&
24832510
!std::holds_alternative<clause::HasDeviceAddr>(clause.u) &&

flang/test/Lower/OpenMP/Todo/masked-directive.f90

Lines changed: 0 additions & 13 deletions
This file was deleted.

flang/test/Lower/OpenMP/masked.f90

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
!RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
2+
3+
!CHECK-LABEL: func @_QPomp_masked
4+
subroutine omp_masked(threadId)
5+
integer :: threadId
6+
7+
!CHECK: omp.masked {
8+
!$omp masked
9+
10+
!CHECK: fir.call @_QPmasked() {{.*}}: () -> ()
11+
call masked()
12+
13+
!CHECK: omp.terminator
14+
!$omp end masked
15+
16+
!CHECK: omp.masked filter({{.*}}) {
17+
!$omp masked filter(threadId)
18+
19+
!CHECK: fir.call @_QPmaskedwithfilter() {{.*}}: () -> ()
20+
call maskedWithFilter()
21+
22+
!CHECK: omp.terminator
23+
!$omp end masked
24+
end subroutine omp_masked
25+

0 commit comments

Comments
 (0)