Skip to content

Commit 1c01409

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

File tree

7 files changed

+186
-3
lines changed

7 files changed

+186
-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
@@ -533,6 +533,16 @@ static void processHostEvalClauses(lower::AbstractConverter &converter,
533533
cp.processCollapse(loc, eval, hostInfo->ops, hostInfo->iv);
534534
break;
535535

536+
case OMPD_teams_workdistribute:
537+
cp.processThreadLimit(stmtCtx, hostInfo.ops);
538+
[[fallthrough]];
539+
case OMPD_target_teams_workdistribute:
540+
cp.processNumTeams(stmtCtx, hostInfo.ops);
541+
processSingleNestedIf([](Directive nestedDir) {
542+
return topDistributeSet.test(nestedDir) || topLoopSet.test(nestedDir);
543+
});
544+
break;
545+
536546
// Standalone 'target' case.
537547
case OMPD_target: {
538548
processSingleNestedIf(
@@ -2795,6 +2805,17 @@ genTeamsOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
27952805
queue, item, clauseOps);
27962806
}
27972807

2808+
static mlir::omp::WorkdistributeOp genWorkdistributeOp(
2809+
lower::AbstractConverter &converter, lower::SymMap &symTable,
2810+
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
2811+
mlir::Location loc, const ConstructQueue &queue,
2812+
ConstructQueue::const_iterator item) {
2813+
return genOpWithBody<mlir::omp::WorkdistributeOp>(
2814+
OpWithBodyGenInfo(converter, symTable, semaCtx, loc, eval,
2815+
llvm::omp::Directive::OMPD_workdistribute),
2816+
queue, item);
2817+
}
2818+
27982819
//===----------------------------------------------------------------------===//
27992820
// Code generation functions for the standalone version of constructs that can
28002821
// also be a leaf of a composite construct
@@ -3431,7 +3452,10 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
34313452
case llvm::omp::Directive::OMPD_unroll:
34323453
genUnrollOp(converter, symTable, stmtCtx, semaCtx, eval, loc, queue, item);
34333454
break;
3434-
// case llvm::omp::Directive::OMPD_workdistribute:
3455+
case llvm::omp::Directive::OMPD_workdistribute:
3456+
newOp = genWorkdistributeOp(converter, symTable, semaCtx, eval, loc, queue,
3457+
item);
3458+
break;
34353459
case llvm::omp::Directive::OMPD_workshare:
34363460
newOp = genWorkshareOp(converter, symTable, stmtCtx, semaCtx, eval, loc,
34373461
queue, item);

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1868,11 +1868,14 @@ TYPE_PARSER( //
18681868
MakeBlockConstruct(llvm::omp::Directive::OMPD_target_data) ||
18691869
MakeBlockConstruct(llvm::omp::Directive::OMPD_target_parallel) ||
18701870
MakeBlockConstruct(llvm::omp::Directive::OMPD_target_teams) ||
1871+
MakeBlockConstruct(llvm::omp::Directive::OMPD_target_teams_workdistribute) ||
18711872
MakeBlockConstruct(llvm::omp::Directive::OMPD_target) ||
18721873
MakeBlockConstruct(llvm::omp::Directive::OMPD_task) ||
18731874
MakeBlockConstruct(llvm::omp::Directive::OMPD_taskgroup) ||
18741875
MakeBlockConstruct(llvm::omp::Directive::OMPD_teams) ||
1875-
MakeBlockConstruct(llvm::omp::Directive::OMPD_workshare))
1876+
MakeBlockConstruct(llvm::omp::Directive::OMPD_teams_workdistribute) ||
1877+
MakeBlockConstruct(llvm::omp::Directive::OMPD_workshare) ||
1878+
MakeBlockConstruct(llvm::omp::Directive::OMPD_workdistribute))
18761879
#undef MakeBlockConstruct
18771880

18781881
// OMP SECTIONS Directive

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,10 +1717,13 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPBlockConstruct &x) {
17171717
case llvm::omp::Directive::OMPD_task:
17181718
case llvm::omp::Directive::OMPD_taskgroup:
17191719
case llvm::omp::Directive::OMPD_teams:
1720+
case llvm::omp::Directive::OMPD_workdistribute:
17201721
case llvm::omp::Directive::OMPD_workshare:
17211722
case llvm::omp::Directive::OMPD_parallel_workshare:
17221723
case llvm::omp::Directive::OMPD_target_teams:
1724+
case llvm::omp::Directive::OMPD_target_teams_workdistribute:
17231725
case llvm::omp::Directive::OMPD_target_parallel:
1726+
case llvm::omp::Directive::OMPD_teams_workdistribute:
17241727
PushContext(dirSpec.source, dirId);
17251728
break;
17261729
default:
@@ -1750,9 +1753,12 @@ void OmpAttributeVisitor::Post(const parser::OpenMPBlockConstruct &x) {
17501753
case llvm::omp::Directive::OMPD_target:
17511754
case llvm::omp::Directive::OMPD_task:
17521755
case llvm::omp::Directive::OMPD_teams:
1756+
case llvm::omp::Directive::OMPD_workdistribute:
17531757
case llvm::omp::Directive::OMPD_parallel_workshare:
17541758
case llvm::omp::Directive::OMPD_target_teams:
1755-
case llvm::omp::Directive::OMPD_target_parallel: {
1759+
case llvm::omp::Directive::OMPD_target_parallel:
1760+
case llvm::omp::Directive::OMPD_target_teams_workdistribute:
1761+
case llvm::omp::Directive::OMPD_teams_workdistribute: {
17561762
bool hasPrivate;
17571763
for (const auto *allocName : allocateNames_) {
17581764
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
@@ -2115,4 +2115,26 @@ def AllocateDirOp : OpenMP_Op<"allocate_dir", clauses = [
21152115
let hasVerifier = 1;
21162116
}
21172117

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

0 commit comments

Comments
 (0)