Skip to content

Commit cb89a7f

Browse files
committed
[flang] Add support for workdistribute construct in flang frontend
1 parent ade2f10 commit cb89a7f

File tree

7 files changed

+188
-3
lines changed

7 files changed

+188
-3
lines changed

flang/include/flang/Semantics/openmp-directive-sets.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ static const OmpDirectiveSet topTargetSet{
143143
Directive::OMPD_target_teams_distribute_parallel_do_simd,
144144
Directive::OMPD_target_teams_distribute_simd,
145145
Directive::OMPD_target_teams_loop,
146+
Directive::OMPD_target_teams_workdistribute,
146147
};
147148

148149
static const OmpDirectiveSet allTargetSet{topTargetSet};
@@ -172,6 +173,7 @@ static const OmpDirectiveSet topTeamsSet{
172173
Directive::OMPD_teams_distribute_parallel_do_simd,
173174
Directive::OMPD_teams_distribute_simd,
174175
Directive::OMPD_teams_loop,
176+
Directive::OMPD_teams_workdistribute,
175177
};
176178

177179
static const OmpDirectiveSet bottomTeamsSet{
@@ -187,9 +189,16 @@ static const OmpDirectiveSet allTeamsSet{
187189
Directive::OMPD_target_teams_distribute_parallel_do_simd,
188190
Directive::OMPD_target_teams_distribute_simd,
189191
Directive::OMPD_target_teams_loop,
192+
Directive::OMPD_target_teams_workdistribute,
190193
} | topTeamsSet,
191194
};
192195

196+
static const OmpDirectiveSet allWorkdistributeSet{
197+
Directive::OMPD_workdistribute,
198+
Directive::OMPD_teams_workdistribute,
199+
Directive::OMPD_target_teams_workdistribute,
200+
};
201+
193202
//===----------------------------------------------------------------------===//
194203
// Directive sets for groups of multiple directives
195204
//===----------------------------------------------------------------------===//
@@ -230,6 +239,9 @@ static const OmpDirectiveSet blockConstructSet{
230239
Directive::OMPD_taskgroup,
231240
Directive::OMPD_teams,
232241
Directive::OMPD_workshare,
242+
Directive::OMPD_target_teams_workdistribute,
243+
Directive::OMPD_teams_workdistribute,
244+
Directive::OMPD_workdistribute,
233245
};
234246

235247
static const OmpDirectiveSet loopConstructSet{
@@ -294,6 +306,7 @@ static const OmpDirectiveSet workShareSet{
294306
Directive::OMPD_scope,
295307
Directive::OMPD_sections,
296308
Directive::OMPD_single,
309+
Directive::OMPD_workdistribute,
297310
} | allDoSet,
298311
};
299312

@@ -376,6 +389,7 @@ static const OmpDirectiveSet nestedReduceWorkshareAllowedSet{
376389
};
377390

378391
static const OmpDirectiveSet nestedTeamsAllowedSet{
392+
Directive::OMPD_workdistribute,
379393
Directive::OMPD_distribute,
380394
Directive::OMPD_distribute_parallel_do,
381395
Directive::OMPD_distribute_parallel_do_simd,

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,16 @@ static void processHostEvalClauses(lower::AbstractConverter &converter,
618618
cp.processCollapse(loc, eval, hostInfo->ops, hostInfo->iv);
619619
break;
620620

621+
case OMPD_teams_workdistribute:
622+
cp.processThreadLimit(stmtCtx, hostInfo.ops);
623+
[[fallthrough]];
624+
case OMPD_target_teams_workdistribute:
625+
cp.processNumTeams(stmtCtx, hostInfo.ops);
626+
processSingleNestedIf([](Directive nestedDir) {
627+
return topDistributeSet.test(nestedDir) || topLoopSet.test(nestedDir);
628+
});
629+
break;
630+
621631
// Standalone 'target' case.
622632
case OMPD_target: {
623633
processSingleNestedIf(
@@ -2719,6 +2729,17 @@ genTeamsOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
27192729
queue, item, clauseOps);
27202730
}
27212731

2732+
static mlir::omp::WorkdistributeOp genWorkdistributeOp(
2733+
lower::AbstractConverter &converter, lower::SymMap &symTable,
2734+
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
2735+
mlir::Location loc, const ConstructQueue &queue,
2736+
ConstructQueue::const_iterator item) {
2737+
return genOpWithBody<mlir::omp::WorkdistributeOp>(
2738+
OpWithBodyGenInfo(converter, symTable, semaCtx, loc, eval,
2739+
llvm::omp::Directive::OMPD_workdistribute),
2740+
queue, item);
2741+
}
2742+
27222743
//===----------------------------------------------------------------------===//
27232744
// Code generation functions for the standalone version of constructs that can
27242745
// also be a leaf of a composite construct
@@ -3339,7 +3360,10 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
33393360
TODO(loc, "Unhandled loop directive (" +
33403361
llvm::omp::getOpenMPDirectiveName(dir, version) + ")");
33413362
}
3342-
// case llvm::omp::Directive::OMPD_workdistribute:
3363+
case llvm::omp::Directive::OMPD_workdistribute:
3364+
newOp = genWorkdistributeOp(converter, symTable, semaCtx, eval, loc, queue,
3365+
item);
3366+
break;
33433367
case llvm::omp::Directive::OMPD_workshare:
33443368
newOp = genWorkshareOp(converter, symTable, stmtCtx, semaCtx, eval, loc,
33453369
queue, item);

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,12 +1567,17 @@ TYPE_PARSER(
15671567
"TARGET DATA" >> pure(llvm::omp::Directive::OMPD_target_data),
15681568
"TARGET_DATA" >> pure(llvm::omp::Directive::OMPD_target_data),
15691569
"TARGET PARALLEL" >> pure(llvm::omp::Directive::OMPD_target_parallel),
1570+
"TARGET TEAMS WORKDISTRIBUTE" >>
1571+
pure(llvm::omp::Directive::OMPD_target_teams_workdistribute),
15701572
"TARGET TEAMS" >> pure(llvm::omp::Directive::OMPD_target_teams),
15711573
"TARGET" >> pure(llvm::omp::Directive::OMPD_target),
15721574
"TASK"_id >> pure(llvm::omp::Directive::OMPD_task),
15731575
"TASKGROUP" >> pure(llvm::omp::Directive::OMPD_taskgroup),
1576+
"TEAMS WORKDISTRIBUTE" >>
1577+
pure(llvm::omp::Directive::OMPD_teams_workdistribute),
15741578
"TEAMS" >> pure(llvm::omp::Directive::OMPD_teams),
1575-
"WORKSHARE" >> pure(llvm::omp::Directive::OMPD_workshare))))
1579+
"WORKSHARE" >> pure(llvm::omp::Directive::OMPD_workshare),
1580+
"WORKDISTRIBUTE" >> pure(llvm::omp::Directive::OMPD_workdistribute))))
15761581

15771582
TYPE_PARSER(sourced(construct<OmpBeginBlockDirective>(
15781583
sourced(Parser<OmpBlockDirective>{}), Parser<OmpClauseList>{})))

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,10 +1680,13 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPBlockConstruct &x) {
16801680
case llvm::omp::Directive::OMPD_task:
16811681
case llvm::omp::Directive::OMPD_taskgroup:
16821682
case llvm::omp::Directive::OMPD_teams:
1683+
case llvm::omp::Directive::OMPD_workdistribute:
16831684
case llvm::omp::Directive::OMPD_workshare:
16841685
case llvm::omp::Directive::OMPD_parallel_workshare:
16851686
case llvm::omp::Directive::OMPD_target_teams:
1687+
case llvm::omp::Directive::OMPD_target_teams_workdistribute:
16861688
case llvm::omp::Directive::OMPD_target_parallel:
1689+
case llvm::omp::Directive::OMPD_teams_workdistribute:
16871690
PushContext(beginDir.source, beginDir.v);
16881691
break;
16891692
default:
@@ -1713,9 +1716,12 @@ void OmpAttributeVisitor::Post(const parser::OpenMPBlockConstruct &x) {
17131716
case llvm::omp::Directive::OMPD_target:
17141717
case llvm::omp::Directive::OMPD_task:
17151718
case llvm::omp::Directive::OMPD_teams:
1719+
case llvm::omp::Directive::OMPD_workdistribute:
17161720
case llvm::omp::Directive::OMPD_parallel_workshare:
17171721
case llvm::omp::Directive::OMPD_target_teams:
1718-
case llvm::omp::Directive::OMPD_target_parallel: {
1722+
case llvm::omp::Directive::OMPD_target_parallel:
1723+
case llvm::omp::Directive::OMPD_target_teams_workdistribute:
1724+
case llvm::omp::Directive::OMPD_teams_workdistribute: {
17191725
bool hasPrivate;
17201726
for (const auto *allocName : allocateNames_) {
17211727
hasPrivate = false;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
! RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func @_QPtarget_teams_workdistribute
4+
subroutine target_teams_workdistribute()
5+
! CHECK: omp.target
6+
! CHECK: omp.teams
7+
! CHECK: omp.workdistribute
8+
!$omp target teams workdistribute
9+
! CHECK: fir.call
10+
call f1()
11+
! CHECK: omp.terminator
12+
! CHECK: omp.terminator
13+
! CHECK: omp.terminator
14+
!$omp end target teams workdistribute
15+
end subroutine target_teams_workdistribute
16+
17+
! CHECK-LABEL: func @_QPteams_workdistribute
18+
subroutine teams_workdistribute()
19+
! CHECK: omp.teams
20+
! CHECK: omp.workdistribute
21+
!$omp teams workdistribute
22+
! CHECK: fir.call
23+
call f1()
24+
! CHECK: omp.terminator
25+
! CHECK: omp.terminator
26+
!$omp end teams workdistribute
27+
end subroutine teams_workdistribute
28+
29+
! CHECK-LABEL: func @_QPtarget_teams_workdistribute_m
30+
subroutine target_teams_workdistribute_m()
31+
! CHECK: omp.target
32+
! CHECK: omp.teams
33+
! CHECK: omp.workdistribute
34+
!$omp target
35+
!$omp teams
36+
!$omp workdistribute
37+
! CHECK: fir.call
38+
call f1()
39+
! CHECK: omp.terminator
40+
! CHECK: omp.terminator
41+
! CHECK: omp.terminator
42+
!$omp end workdistribute
43+
!$omp end teams
44+
!$omp end target
45+
end subroutine target_teams_workdistribute_m
46+
47+
! CHECK-LABEL: func @_QPteams_workdistribute_m
48+
subroutine teams_workdistribute_m()
49+
! CHECK: omp.teams
50+
! CHECK: omp.workdistribute
51+
!$omp teams
52+
!$omp workdistribute
53+
! CHECK: fir.call
54+
call f1()
55+
! CHECK: omp.terminator
56+
! CHECK: omp.terminator
57+
!$omp end workdistribute
58+
!$omp end teams
59+
end subroutine teams_workdistribute_m

llvm/include/llvm/Frontend/OpenMP/OMP.td

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,15 @@ def OMP_EndWorkshare : Directive<[Spelling<"end workshare">]> {
13091309
let category = OMP_Workshare.category;
13101310
let languages = [L_Fortran];
13111311
}
1312+
def OMP_Workdistribute : Directive<[Spelling<"workdistribute">]> {
1313+
let association = AS_Block;
1314+
let category = CA_Executable;
1315+
}
1316+
def OMP_EndWorkdistribute : Directive<[Spelling<"end workdistribute">]> {
1317+
let leafConstructs = OMP_Workdistribute.leafConstructs;
1318+
let association = OMP_Workdistribute.association;
1319+
let category = OMP_Workdistribute.category;
1320+
}
13121321

13131322
//===----------------------------------------------------------------------===//
13141323
// Definitions of OpenMP compound directives
@@ -2452,6 +2461,34 @@ def OMP_TargetTeamsDistributeSimd
24522461
let leafConstructs = [OMP_Target, OMP_Teams, OMP_Distribute, OMP_Simd];
24532462
let category = CA_Executable;
24542463
}
2464+
def OMP_TargetTeamsWorkdistribute : Directive<[Spelling<"target teams workdistribute">]> {
2465+
let allowedClauses = [
2466+
VersionedClause<OMPC_Allocate>,
2467+
VersionedClause<OMPC_Depend>,
2468+
VersionedClause<OMPC_FirstPrivate>,
2469+
VersionedClause<OMPC_HasDeviceAddr, 51>,
2470+
VersionedClause<OMPC_If>,
2471+
VersionedClause<OMPC_IsDevicePtr>,
2472+
VersionedClause<OMPC_Map>,
2473+
VersionedClause<OMPC_OMPX_Attribute>,
2474+
VersionedClause<OMPC_Private>,
2475+
VersionedClause<OMPC_Reduction>,
2476+
VersionedClause<OMPC_Shared>,
2477+
VersionedClause<OMPC_UsesAllocators, 50>,
2478+
];
2479+
let allowedOnceClauses = [
2480+
VersionedClause<OMPC_Default>,
2481+
VersionedClause<OMPC_DefaultMap>,
2482+
VersionedClause<OMPC_Device>,
2483+
VersionedClause<OMPC_NoWait>,
2484+
VersionedClause<OMPC_NumTeams>,
2485+
VersionedClause<OMPC_OMPX_DynCGroupMem>,
2486+
VersionedClause<OMPC_OMPX_Bare>,
2487+
VersionedClause<OMPC_ThreadLimit>,
2488+
];
2489+
let leafConstructs = [OMP_Target, OMP_Teams, OMP_Workdistribute];
2490+
let category = CA_Executable;
2491+
}
24552492
def OMP_target_teams_loop : Directive<[Spelling<"target teams loop">]> {
24562493
let allowedClauses = [
24572494
VersionedClause<OMPC_Allocate>,
@@ -2682,6 +2719,24 @@ def OMP_TeamsDistributeSimd : Directive<[Spelling<"teams distribute simd">]> {
26822719
let leafConstructs = [OMP_Teams, OMP_Distribute, OMP_Simd];
26832720
let category = CA_Executable;
26842721
}
2722+
def OMP_TeamsWorkdistribute : Directive<[Spelling<"teams workdistribute">]> {
2723+
let allowedClauses = [
2724+
VersionedClause<OMPC_Allocate>,
2725+
VersionedClause<OMPC_FirstPrivate>,
2726+
VersionedClause<OMPC_OMPX_Attribute>,
2727+
VersionedClause<OMPC_Private>,
2728+
VersionedClause<OMPC_Reduction>,
2729+
VersionedClause<OMPC_Shared>,
2730+
];
2731+
let allowedOnceClauses = [
2732+
VersionedClause<OMPC_Default>,
2733+
VersionedClause<OMPC_If, 52>,
2734+
VersionedClause<OMPC_NumTeams>,
2735+
VersionedClause<OMPC_ThreadLimit>,
2736+
];
2737+
let leafConstructs = [OMP_Teams, OMP_Workdistribute];
2738+
let category = CA_Executable;
2739+
}
26852740
def OMP_teams_loop : Directive<[Spelling<"teams loop">]> {
26862741
let allowedClauses = [
26872742
VersionedClause<OMPC_Allocate>,

mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,4 +2113,26 @@ def AllocateDirOp : OpenMP_Op<"allocate_dir", clauses = [
21132113
let hasVerifier = 1;
21142114
}
21152115

2116+
//===----------------------------------------------------------------------===//
2117+
// workdistribute Construct
2118+
//===----------------------------------------------------------------------===//
2119+
2120+
def WorkdistributeOp : OpenMP_Op<"workdistribute"> {
2121+
let summary = "workdistribute directive";
2122+
let description = [{
2123+
workdistribute divides execution of the enclosed structured block into
2124+
separate units of work, each executed only once by each
2125+
initial thread in the league.
2126+
```
2127+
!$omp target teams
2128+
!$omp workdistribute
2129+
y = a * x + y
2130+
!$omp end workdistribute
2131+
!$omp end target teams
2132+
```
2133+
}];
2134+
let regions = (region AnyRegion:$region);
2135+
let assemblyFormat = "$region attr-dict";
2136+
}
2137+
21162138
#endif // OPENMP_OPS

0 commit comments

Comments
 (0)