Skip to content

Commit 4c4a413

Browse files
authored
[flang][OpenMP] Update frontend support for DEFAULTMAP clause (#116506)
Add ALL variable category, implement semantic checks to verify the validity of the clause, improve error messages, add testcases. The variable category modifier is optional since 5.0, make sure we allow it to be missing. If it is missing, assume "all" in clause conversion.
1 parent 1e4646d commit 4c4a413

File tree

14 files changed

+212
-20
lines changed

14 files changed

+212
-20
lines changed

flang/include/flang/Parser/parse-tree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3544,7 +3544,7 @@ struct OmpDefaultmapClause {
35443544
TUPLE_CLASS_BOILERPLATE(OmpDefaultmapClause);
35453545
ENUM_CLASS(
35463546
ImplicitBehavior, Alloc, To, From, Tofrom, Firstprivate, None, Default)
3547-
ENUM_CLASS(VariableCategory, Scalar, Aggregate, Allocatable, Pointer)
3547+
ENUM_CLASS(VariableCategory, All, Scalar, Aggregate, Allocatable, Pointer)
35483548
std::tuple<ImplicitBehavior, std::optional<VariableCategory>> t;
35493549
};
35503550

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,17 +572,20 @@ Defaultmap make(const parser::OmpClause::Defaultmap &inp,
572572
CLAUSET_ENUM_CONVERT( //
573573
convert2, wrapped::VariableCategory, Defaultmap::VariableCategory,
574574
// clang-format off
575-
MS(Scalar, Scalar)
576575
MS(Aggregate, Aggregate)
577-
MS(Pointer, Pointer)
576+
MS(All, All)
578577
MS(Allocatable, Allocatable)
578+
MS(Pointer, Pointer)
579+
MS(Scalar, Scalar)
579580
// clang-format on
580581
);
581582

582583
auto &t0 = std::get<wrapped::ImplicitBehavior>(inp.v.t);
583584
auto &t1 = std::get<std::optional<wrapped::VariableCategory>>(inp.v.t);
585+
586+
auto category = t1 ? convert2(*t1) : Defaultmap::VariableCategory::All;
584587
return Defaultmap{{/*ImplicitBehavior=*/convert1(t0),
585-
/*VariableCategory=*/maybeApply(convert2, t1)}};
588+
/*VariableCategory=*/category}};
586589
}
587590

588591
Doacross makeDoacross(const parser::OmpDoacross &doa,

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "flang/Optimizer/Builder/Todo.h"
3131
#include "flang/Optimizer/Dialect/FIRType.h"
3232
#include "flang/Optimizer/HLFIR/HLFIROps.h"
33+
#include "flang/Parser/characters.h"
3334
#include "flang/Parser/parse-tree.h"
3435
#include "flang/Semantics/openmp-directive-sets.h"
3536
#include "flang/Semantics/tools.h"
@@ -2927,7 +2928,9 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
29272928
!std::holds_alternative<clause::InReduction>(clause.u) &&
29282929
!std::holds_alternative<clause::Mergeable>(clause.u) &&
29292930
!std::holds_alternative<clause::TaskReduction>(clause.u)) {
2930-
TODO(clauseLocation, "OpenMP Block construct clause");
2931+
std::string name =
2932+
parser::ToUpperCaseLetters(llvm::omp::getOpenMPClauseName(clause.id));
2933+
TODO(clauseLocation, name + " clause is not implemented yet");
29312934
}
29322935
}
29332936

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ TYPE_PARSER(construct<OmpMapClause>(
270270
// 2.19.7.2 defaultmap(implicit-behavior[:variable-category])
271271
// implicit-behavior -> ALLOC | TO | FROM | TOFROM | FIRSRTPRIVATE | NONE |
272272
// DEFAULT
273-
// variable-category -> SCALAR | AGGREGATE | ALLOCATABLE | POINTER
273+
// variable-category -> ALL | SCALAR | AGGREGATE | ALLOCATABLE | POINTER
274274
TYPE_PARSER(construct<OmpDefaultmapClause>(
275275
construct<OmpDefaultmapClause::ImplicitBehavior>(
276276
"ALLOC" >> pure(OmpDefaultmapClause::ImplicitBehavior::Alloc) ||
@@ -283,6 +283,7 @@ TYPE_PARSER(construct<OmpDefaultmapClause>(
283283
"DEFAULT" >> pure(OmpDefaultmapClause::ImplicitBehavior::Default)),
284284
maybe(":" >>
285285
construct<OmpDefaultmapClause::VariableCategory>(
286+
"ALL"_id >> pure(OmpDefaultmapClause::VariableCategory::All) ||
286287
"SCALAR" >> pure(OmpDefaultmapClause::VariableCategory::Scalar) ||
287288
"AGGREGATE" >>
288289
pure(OmpDefaultmapClause::VariableCategory::Aggregate) ||

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,14 +3333,17 @@ void OmpStructureChecker::Leave(const parser::OmpAtomicRead &) {
33333333
CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_read,
33343334
{llvm::omp::Clause::OMPC_release, llvm::omp::Clause::OMPC_acq_rel});
33353335
}
3336+
33363337
void OmpStructureChecker::Leave(const parser::OmpAtomicWrite &) {
33373338
CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_write,
33383339
{llvm::omp::Clause::OMPC_acquire, llvm::omp::Clause::OMPC_acq_rel});
33393340
}
3341+
33403342
void OmpStructureChecker::Leave(const parser::OmpAtomicUpdate &) {
33413343
CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_update,
33423344
{llvm::omp::Clause::OMPC_acquire, llvm::omp::Clause::OMPC_acq_rel});
33433345
}
3346+
33443347
// OmpAtomic node represents atomic directive without atomic-clause.
33453348
// atomic-clause - READ,WRITE,UPDATE,CAPTURE.
33463349
void OmpStructureChecker::Leave(const parser::OmpAtomic &) {
@@ -3353,6 +3356,7 @@ void OmpStructureChecker::Leave(const parser::OmpAtomic &) {
33533356
"Clause ACQ_REL is not allowed on the ATOMIC directive"_err_en_US);
33543357
}
33553358
}
3359+
33563360
// Restrictions specific to each clause are implemented apart from the
33573361
// generalized restrictions.
33583362
void OmpStructureChecker::Enter(const parser::OmpClause::Aligned &x) {
@@ -3364,15 +3368,48 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Aligned &x) {
33643368
}
33653369
// 2.8.1 TODO: list-item attribute check
33663370
}
3371+
33673372
void OmpStructureChecker::Enter(const parser::OmpClause::Defaultmap &x) {
33683373
CheckAllowedClause(llvm::omp::Clause::OMPC_defaultmap);
3374+
unsigned version{context_.langOptions().OpenMPVersion};
3375+
using ImplicitBehavior = parser::OmpDefaultmapClause::ImplicitBehavior;
3376+
auto behavior{std::get<ImplicitBehavior>(x.v.t)};
3377+
if (version <= 45) {
3378+
if (behavior != ImplicitBehavior::Tofrom) {
3379+
context_.Say(GetContext().clauseSource,
3380+
"%s is not allowed in %s, %s"_warn_en_US,
3381+
parser::ToUpperCaseLetters(
3382+
parser::OmpDefaultmapClause::EnumToString(behavior)),
3383+
ThisVersion(version), TryVersion(50));
3384+
}
3385+
}
33693386
using VariableCategory = parser::OmpDefaultmapClause::VariableCategory;
3370-
if (!std::get<std::optional<VariableCategory>>(x.v.t)) {
3371-
context_.Say(GetContext().clauseSource,
3372-
"The argument TOFROM:SCALAR must be specified on the DEFAULTMAP "
3373-
"clause"_err_en_US);
3387+
auto maybeCategory{std::get<std::optional<VariableCategory>>(x.v.t)};
3388+
if (!maybeCategory) {
3389+
if (version <= 45) {
3390+
context_.Say(GetContext().clauseSource,
3391+
"The DEFAULTMAP clause requires a variable-category SCALAR in %s, %s"_warn_en_US,
3392+
ThisVersion(version), TryVersion(50));
3393+
}
3394+
} else {
3395+
VariableCategory category{*maybeCategory};
3396+
unsigned tryVersion{0};
3397+
if (version <= 45 && category != VariableCategory::Scalar) {
3398+
tryVersion = 50;
3399+
}
3400+
if (version < 52 && category == VariableCategory::All) {
3401+
tryVersion = 52;
3402+
}
3403+
if (tryVersion) {
3404+
context_.Say(GetContext().clauseSource,
3405+
"%s is not allowed in %s, %s"_warn_en_US,
3406+
parser::ToUpperCaseLetters(
3407+
parser::OmpDefaultmapClause::EnumToString(category)),
3408+
ThisVersion(version), TryVersion(tryVersion));
3409+
}
33743410
}
33753411
}
3412+
33763413
void OmpStructureChecker::Enter(const parser::OmpClause::If &x) {
33773414
CheckAllowedClause(llvm::omp::Clause::OMPC_if);
33783415
using dirNameModifier = parser::OmpIfClause::DirectiveNameModifier;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
!RUN: %not_todo_cmd bbc -emit-hlfir -fopenmp -fopenmp-version=45 -o - %s 2>&1 | FileCheck %s
2+
!RUN: %not_todo_cmd %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=45 -o - %s 2>&1 | FileCheck %s
3+
4+
!CHECK: not yet implemented: DEFAULTMAP clause is not implemented yet
5+
subroutine f00
6+
!$omp target defaultmap(tofrom:scalar)
7+
!$omp end target
8+
end

flang/test/Lower/OpenMP/Todo/task_detach.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
! `detach` clause
77
!===============================================================================
88

9-
! CHECK: not yet implemented: OpenMP Block construct clause
9+
! CHECK: not yet implemented: DETACH clause is not implemented yet
1010
subroutine omp_task_detach()
1111
use omp_lib
1212
integer (kind=omp_event_handle_kind) :: event

flang/test/Lower/OpenMP/Todo/task_untied.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
! `untied` clause
66
!===============================================================================
77

8-
! CHECK: not yet implemented: OpenMP Block construct clause
8+
! CHECK: not yet implemented: UNTIED clause is not implemented yet
99
subroutine omp_task_untied()
1010
!$omp task untied
1111
call foo()
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
!RUN: %flang_fc1 -fdebug-unparse -fopenmp -fopenmp-version=52 %s | FileCheck --ignore-case --check-prefix="UNPARSE" %s
2+
!RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp -fopenmp-version=52 %s | FileCheck --check-prefix="PARSE-TREE" %s
3+
4+
subroutine f00
5+
!$omp target defaultmap(from)
6+
!$omp end target
7+
end
8+
9+
!UNPARSE: SUBROUTINE f00
10+
!UNPARSE: !$OMP TARGET DEFAULTMAP(FROM)
11+
!UNPARSE: !$OMP END TARGET
12+
!UNPARSE: END SUBROUTINE
13+
14+
!PARSE-TREE: OmpBeginBlockDirective
15+
!PARSE-TREE: | OmpBlockDirective -> llvm::omp::Directive = target
16+
!PARSE-TREE: | OmpClauseList -> OmpClause -> Defaultmap -> OmpDefaultmapClause
17+
!PARSE-TREE: | | ImplicitBehavior = From
18+
!PARSE-TREE: Block
19+
20+
subroutine f01
21+
!$omp target defaultmap(firstprivate: aggregate)
22+
!$omp end target
23+
end
24+
25+
!UNPARSE: SUBROUTINE f01
26+
!UNPARSE: !$OMP TARGET DEFAULTMAP(FIRSTPRIVATE:AGGREGATE)
27+
!UNPARSE: !$OMP END TARGET
28+
!UNPARSE: END SUBROUTINE
29+
30+
!PARSE-TREE: OmpBeginBlockDirective
31+
!PARSE-TREE: | OmpBlockDirective -> llvm::omp::Directive = target
32+
!PARSE-TREE: | OmpClauseList -> OmpClause -> Defaultmap -> OmpDefaultmapClause
33+
!PARSE-TREE: | | ImplicitBehavior = Firstprivate
34+
!PARSE-TREE: | | VariableCategory = Aggregate
35+
36+
subroutine f02
37+
!$omp target defaultmap(alloc: all)
38+
!$omp end target
39+
end
40+
41+
!UNPARSE: SUBROUTINE f02
42+
!UNPARSE: !$OMP TARGET DEFAULTMAP(ALLOC:ALL)
43+
!UNPARSE: !$OMP END TARGET
44+
!UNPARSE: END SUBROUTINE
45+
46+
!PARSE-TREE: OmpBeginBlockDirective
47+
!PARSE-TREE: | OmpBlockDirective -> llvm::omp::Directive = target
48+
!PARSE-TREE: | OmpClauseList -> OmpClause -> Defaultmap -> OmpDefaultmapClause
49+
!PARSE-TREE: | | ImplicitBehavior = Alloc
50+
!PARSE-TREE: | | VariableCategory = All
51+
52+
! Both "all" and "allocatable" are valid, and "all" is a prefix of
53+
! "allocatable". Make sure we parse this correctly.
54+
subroutine f03
55+
!$omp target defaultmap(alloc: allocatable)
56+
!$omp end target
57+
end
58+
59+
!UNPARSE: SUBROUTINE f03
60+
!UNPARSE: !$OMP TARGET DEFAULTMAP(ALLOC:ALLOCATABLE)
61+
!UNPARSE: !$OMP END TARGET
62+
!UNPARSE: END SUBROUTINE
63+
64+
!PARSE-TREE: OmpBeginBlockDirective
65+
!PARSE-TREE: | OmpBlockDirective -> llvm::omp::Directive = target
66+
!PARSE-TREE: | OmpClauseList -> OmpClause -> Defaultmap -> OmpDefaultmapClause
67+
!PARSE-TREE: | | ImplicitBehavior = Alloc
68+
!PARSE-TREE: | | VariableCategory = Allocatable
69+
70+
subroutine f04
71+
!$omp target defaultmap(tofrom: scalar)
72+
!$omp end target
73+
end
74+
75+
!UNPARSE: SUBROUTINE f04
76+
!UNPARSE: !$OMP TARGET DEFAULTMAP(TOFROM:SCALAR)
77+
!UNPARSE: !$OMP END TARGET
78+
!UNPARSE: END SUBROUTINE
79+
80+
!PARSE-TREE: OmpBeginBlockDirective
81+
!PARSE-TREE: | OmpBlockDirective -> llvm::omp::Directive = target
82+
!PARSE-TREE: | OmpClauseList -> OmpClause -> Defaultmap -> OmpDefaultmapClause
83+
!PARSE-TREE: | | ImplicitBehavior = Tofrom
84+
!PARSE-TREE: | | VariableCategory = Scalar

flang/test/Semantics/OpenMP/combined-constructs.f90

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ program main
3333
enddo
3434
!$omp end target parallel
3535

36-
!ERROR: The argument TOFROM:SCALAR must be specified on the DEFAULTMAP clause
36+
!ERROR: The DEFAULTMAP clause requires a variable-category SCALAR in OpenMP v1.1, try -fopenmp-version=50
3737
!$omp target parallel defaultmap(tofrom)
3838
do i = 1, N
3939
a(i) = 3.14
@@ -80,7 +80,7 @@ program main
8080
enddo
8181
!$omp end target parallel do
8282

83-
!ERROR: The argument TOFROM:SCALAR must be specified on the DEFAULTMAP clause
83+
!ERROR: The DEFAULTMAP clause requires a variable-category SCALAR in OpenMP v1.1, try -fopenmp-version=50
8484
!$omp target parallel do defaultmap(tofrom)
8585
do i = 1, N
8686
a(i) = 3.14
@@ -140,7 +140,7 @@ program main
140140
enddo
141141
!$omp end target teams
142142

143-
!ERROR: The argument TOFROM:SCALAR must be specified on the DEFAULTMAP clause
143+
!ERROR: The DEFAULTMAP clause requires a variable-category SCALAR in OpenMP v1.1, try -fopenmp-version=50
144144
!$omp target teams defaultmap(tofrom)
145145
do i = 1, N
146146
a(i) = 3.14
@@ -240,7 +240,7 @@ program main
240240
enddo
241241
!$omp end target teams distribute
242242

243-
!ERROR: The argument TOFROM:SCALAR must be specified on the DEFAULTMAP clause
243+
!ERROR: The DEFAULTMAP clause requires a variable-category SCALAR in OpenMP v1.1, try -fopenmp-version=50
244244
!$omp target teams distribute defaultmap(tofrom)
245245
do i = 1, N
246246
a(i) = 3.14
@@ -333,7 +333,7 @@ program main
333333
enddo
334334
!$omp end target teams distribute parallel do
335335

336-
!ERROR: The argument TOFROM:SCALAR must be specified on the DEFAULTMAP clause
336+
!ERROR: The DEFAULTMAP clause requires a variable-category SCALAR in OpenMP v1.1, try -fopenmp-version=50
337337
!$omp target teams distribute parallel do defaultmap(tofrom)
338338
do i = 1, N
339339
a(i) = 3.14
@@ -433,7 +433,7 @@ program main
433433
enddo
434434
!$omp end target teams distribute parallel do simd
435435

436-
!ERROR: The argument TOFROM:SCALAR must be specified on the DEFAULTMAP clause
436+
!ERROR: The DEFAULTMAP clause requires a variable-category SCALAR in OpenMP v1.1, try -fopenmp-version=50
437437
!$omp target teams distribute parallel do simd defaultmap(tofrom)
438438
do i = 1, N
439439
a(i) = 3.14
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
!RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=45 -Werror
2+
3+
subroutine f00
4+
!WARNING: The DEFAULTMAP clause requires a variable-category SCALAR in OpenMP v4.5, try -fopenmp-version=50
5+
!$omp target defaultmap(tofrom)
6+
!$omp end target
7+
end
8+
9+
subroutine f01
10+
!WARNING: AGGREGATE is not allowed in OpenMP v4.5, try -fopenmp-version=50
11+
!$omp target defaultmap(tofrom:aggregate)
12+
!$omp end target
13+
end
14+
15+
subroutine f02
16+
!WARNING: FROM is not allowed in OpenMP v4.5, try -fopenmp-version=50
17+
!$omp target defaultmap(from:scalar)
18+
!$omp end target
19+
end
20+
21+
subroutine f03
22+
!WARNING: ALL is not allowed in OpenMP v4.5, try -fopenmp-version=52
23+
!$omp target defaultmap(tofrom:all)
24+
!$omp end target
25+
end
26+
27+
subroutine f04
28+
!WARNING: FROM is not allowed in OpenMP v4.5, try -fopenmp-version=50
29+
!WARNING: POINTER is not allowed in OpenMP v4.5, try -fopenmp-version=50
30+
!$omp target defaultmap(from:pointer)
31+
!$omp end target
32+
end
33+
34+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
!RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=50 -Werror
2+
3+
subroutine f00
4+
!$omp target defaultmap(tofrom)
5+
!$omp end target
6+
end
7+
8+
subroutine f01
9+
!$omp target defaultmap(tofrom:aggregate)
10+
!$omp end target
11+
end
12+
13+
subroutine f02
14+
!$omp target defaultmap(from:scalar)
15+
!$omp end target
16+
end
17+
18+
subroutine f03
19+
!WARNING: ALL is not allowed in OpenMP v5.0, try -fopenmp-version=52
20+
!$omp target defaultmap(tofrom:all)
21+
!$omp end target
22+
end
23+

flang/test/Semantics/OpenMP/device-constructs.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ program main
4444
enddo
4545
!$omp end target
4646

47-
!ERROR: The argument TOFROM:SCALAR must be specified on the DEFAULTMAP clause
4847
!$omp target defaultmap(tofrom)
4948
do i = 1, N
5049
a = 3.14

llvm/include/llvm/Frontend/OpenMP/ClauseT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ template <typename T, typename I, typename E> //
490490
struct DefaultmapT {
491491
ENUM(ImplicitBehavior, Alloc, To, From, Tofrom, Firstprivate, None, Default,
492492
Present);
493-
ENUM(VariableCategory, Scalar, Aggregate, Pointer, Allocatable);
493+
ENUM(VariableCategory, All, Scalar, Aggregate, Pointer, Allocatable);
494494
using TupleTrait = std::true_type;
495495
std::tuple<ImplicitBehavior, OPT(VariableCategory)> t;
496496
};

0 commit comments

Comments
 (0)