Skip to content

Commit 92604d7

Browse files
[flang][OpenMP]Add parsing support for MAP(MAPPER(name) ...) (llvm#116274)
This prepares for using the DECLARE MAPPER construct. A check in lowering will say "Not implemented" when trying to use a mapper as some code is required to tie the mapper to the declared one. Senantics check for the symbol generated.
1 parent 9111d53 commit 92604d7

File tree

10 files changed

+125
-9
lines changed

10 files changed

+125
-9
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ class ParseTreeDumper {
546546
NODE_ENUM(OmpLinearModifier, Type)
547547
NODE(parser, OmpLoopDirective)
548548
NODE(parser, OmpMapClause)
549+
NODE(parser, OmpMapperIdentifier)
549550
NODE_ENUM(OmpMapClause, TypeModifier)
550551
NODE_ENUM(OmpMapClause, Type)
551552
static std::string GetNodeName(const llvm::omp::Clause &x) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,8 +3703,11 @@ struct OmpLinearClause {
37033703
std::variant<WithModifier, WithoutModifier> u;
37043704
};
37053705

3706+
WRAPPER_CLASS(OmpMapperIdentifier, std::optional<Name>);
3707+
37063708
// 2.15.5.1 map ->
3707-
// MAP ([[map-type-modifier-list [,]] [iterator-modifier [,]] map-type : ]
3709+
// MAP ([MAPPER(mapper-identifier)] [[map-type-modifier-list [,]]
3710+
// [iterator-modifier [,]] map-type : ]
37083711
// variable-name-list)
37093712
// map-type-modifier-list -> map-type-modifier [,] [...]
37103713
// map-type-modifier -> ALWAYS | CLOSE | PRESENT | OMPX_HOLD
@@ -3718,7 +3721,8 @@ struct OmpMapClause {
37183721
// The checks for satisfying those constraints are deferred to semantics.
37193722
// In OpenMP 5.2 the non-comma syntax has been deprecated: keep the
37203723
// information about separator presence to emit a diagnostic if needed.
3721-
std::tuple<std::optional<std::list<TypeModifier>>,
3724+
std::tuple<OmpMapperIdentifier, // Mapper name
3725+
std::optional<std::list<TypeModifier>>,
37223726
std::optional<std::list<OmpIteratorModifier>>, // unique
37233727
std::optional<std::list<Type>>, // unique
37243728
OmpObjectList,

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "flang/Common/idioms.h"
1212
#include "flang/Evaluate/expression.h"
13+
#include "flang/Optimizer/Builder/Todo.h"
1314
#include "flang/Parser/parse-tree.h"
1415
#include "flang/Semantics/expression.h"
1516
#include "flang/Semantics/symbol.h"
@@ -988,6 +989,10 @@ Map make(const parser::OmpClause::Map &inp,
988989
std::get<std::optional<std::list<parser::OmpIteratorModifier>>>(inp.v.t);
989990
auto &t2 = std::get<std::optional<std::list<wrapped::Type>>>(inp.v.t);
990991
auto &t3 = std::get<parser::OmpObjectList>(inp.v.t);
992+
auto &t4 = std::get<parser::OmpMapperIdentifier>(inp.v.t);
993+
994+
if (t4.v)
995+
TODO_NOLOC("OmpMapClause(MAPPER(...)): user defined mapper not supported");
991996

992997
// These should have been diagnosed already.
993998
assert((!t1 || t1->size() == 1) && "Only one iterator modifier is allowed");

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ template <typename Separator> struct MotionModifiers {
131131
constexpr MotionModifiers(const MotionModifiers &) = default;
132132
constexpr MotionModifiers(MotionModifiers &&) = default;
133133

134-
// Parsing of mappers if not implemented yet.
135134
using ExpParser = Parser<OmpFromClause::Expectation>;
136135
using IterParser = Parser<OmpIteratorModifier>;
137136
using ModParser = ConcatSeparated<Separator, ExpParser, IterParser>;
@@ -250,21 +249,26 @@ TYPE_PARSER(
250249
"TOFROM" >> pure(OmpMapClause::Type::Tofrom)))
251250

252251
template <bool CommasEverywhere>
253-
static inline OmpMapClause makeMapClause(
252+
static inline OmpMapClause makeMapClause(OmpMapperIdentifier &&mm,
254253
std::tuple<std::optional<std::list<OmpMapClause::TypeModifier>>,
255254
std::optional<std::list<OmpIteratorModifier>>,
256255
std::optional<std::list<OmpMapClause::Type>>> &&mods,
257256
OmpObjectList &&objs) {
258257
auto &&[tm, it, ty] = std::move(mods);
259-
return OmpMapClause{std::move(tm), std::move(it), std::move(ty),
260-
std::move(objs), CommasEverywhere};
258+
return OmpMapClause{std::move(mm), std::move(tm), std::move(it),
259+
std::move(ty), std::move(objs), CommasEverywhere};
261260
}
262261

262+
TYPE_PARSER(construct<OmpMapperIdentifier>(
263+
maybe("MAPPER"_tok >> parenthesized(name) / ","_tok)))
264+
263265
TYPE_PARSER(construct<OmpMapClause>(
264-
applyFunction<OmpMapClause>(
265-
makeMapClause<true>, MapModifiers(","_tok), Parser<OmpObjectList>{}) ||
266+
applyFunction<OmpMapClause>(makeMapClause<true>,
267+
Parser<OmpMapperIdentifier>{}, MapModifiers(","_tok),
268+
Parser<OmpObjectList>{}) ||
266269
applyFunction<OmpMapClause>(makeMapClause<false>,
267-
MapModifiers(maybe(","_tok)), Parser<OmpObjectList>{})))
270+
Parser<OmpMapperIdentifier>{}, MapModifiers(maybe(","_tok)),
271+
Parser<OmpObjectList>{})))
268272

269273
// [OpenMP 5.0]
270274
// 2.19.7.2 defaultmap(implicit-behavior[:variable-category])

flang/lib/Parser/unparse.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,12 +2095,22 @@ class UnparseVisitor {
20952095
std::get<std::optional<std::list<OmpMapClause::TypeModifier>>>(x.t);
20962096
auto &iter = std::get<std::optional<std::list<OmpIteratorModifier>>>(x.t);
20972097
auto &type = std::get<std::optional<std::list<OmpMapClause::Type>>>(x.t);
2098+
auto &mapper = std::get<OmpMapperIdentifier>(x.t);
20982099

20992100
// For a given list of items, if the item has a value, then walk it.
21002101
// Print commas between items that have values.
21012102
// Return 'true' if something did get printed, otherwise 'false'.
21022103
bool needComma{false};
2104+
if (mapper.v) {
2105+
Word("MAPPER(");
2106+
Walk(*mapper.v);
2107+
Put(")");
2108+
needComma = true;
2109+
}
21032110
if (typeMod) {
2111+
if (needComma) {
2112+
Put(", ");
2113+
}
21042114
Walk(*typeMod);
21052115
needComma = true;
21062116
}

flang/lib/Semantics/resolve-names.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,8 @@ class OmpVisitor : public virtual DeclarationVisitor {
14711471

14721472
bool Pre(const parser::OpenMPDeclareMapperConstruct &);
14731473

1474+
bool Pre(const parser::OmpMapClause &);
1475+
14741476
void Post(const parser::OmpBeginLoopDirective &) {
14751477
messageHandler().set_currStmtSource(std::nullopt);
14761478
}
@@ -1639,6 +1641,33 @@ bool OmpVisitor::Pre(const parser::OpenMPDeclareMapperConstruct &x) {
16391641
return false;
16401642
}
16411643

1644+
bool OmpVisitor::Pre(const parser::OmpMapClause &x) {
1645+
const auto &mid{std::get<parser::OmpMapperIdentifier>(x.t)};
1646+
if (const auto &mapperName{mid.v}) {
1647+
if (const auto symbol = FindSymbol(currScope(), *mapperName)) {
1648+
// TODO: Do we need a specific flag or type here, to distinghuish against
1649+
// other ConstructName things? Leaving this for the full implementation
1650+
// of mapper lowering.
1651+
auto *misc{symbol->detailsIf<MiscDetails>()};
1652+
if (!misc || misc->kind() != MiscDetails::Kind::ConstructName)
1653+
context().Say(mapperName->source,
1654+
"Name '%s' should be a mapper name"_err_en_US, mapperName->source);
1655+
else
1656+
mapperName->symbol = symbol;
1657+
} else {
1658+
mapperName->symbol = &MakeSymbol(
1659+
*mapperName, MiscDetails{MiscDetails::Kind::ConstructName});
1660+
// TODO: When completing the implementation, we probably want to error if
1661+
// the symbol is not declared, but right now, testing that the TODO for
1662+
// OmpMapclause happens is obscured by the TODO for declare mapper, so
1663+
// leaving this out. Remove the above line once the declare mapper is
1664+
// implemented. context().Say(mapperName->source, "'%s' not
1665+
// declared"_err_en_US, mapperName->source);
1666+
}
1667+
}
1668+
return true;
1669+
}
1670+
16421671
// Walk the parse tree and resolve names to symbols.
16431672
class ResolveNamesVisitor : public virtual ScopeHandler,
16441673
public ModuleVisitor,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
! RUN: not %flang_fc1 -emit-fir -fopenmp -fopenmp-version=50 %s 2>&1 | FileCheck %s
2+
program p
3+
integer, parameter :: n = 256
4+
real(8) :: a(256)
5+
!! TODO: Add declare mapper, when it works to lower this construct
6+
!!type t1
7+
!! integer :: x
8+
!!end type t1
9+
!!!$omp declare mapper(xx : t1 :: nn) map(nn, nn%x)
10+
!$omp target map(mapper(xx), from:a)
11+
!CHECK: not yet implemented: OmpMapClause(MAPPER(...))
12+
do i=1,n
13+
a(i) = 4.2
14+
end do
15+
!$omp end target
16+
end program p

flang/test/Parser/OpenMP/map-modifiers.f90

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,28 @@ subroutine f90(x, y)
316316
!PARSE-TREE: | | | | Designator -> DataRef -> Name = 'k'
317317
!PARSE-TREE: | | bool = 'true'
318318

319+
subroutine f100(x, y)
320+
integer :: x(10)
321+
integer :: y
322+
integer, parameter :: p = 23
323+
!$omp target map(mapper(xx), from: x)
324+
x = x + 1
325+
!$omp end target
326+
end
327+
328+
!UNPARSE: SUBROUTINE f100 (x, y)
329+
!UNPARSE: INTEGER x(10_4)
330+
!UNPARSE: INTEGER y
331+
!UNPARSE: INTEGER, PARAMETER :: p = 23_4
332+
!UNPARSE: !$OMP TARGET MAP(MAPPER(XX), FROM: X)
333+
!UNPARSE: x=x+1_4
334+
!UNPARSE: !$OMP END TARGET
335+
!UNPARSE: END SUBROUTINE
336+
337+
!PARSE-TREE: OmpBeginBlockDirective
338+
!PARSE-TREE: | OmpBlockDirective -> llvm::omp::Directive = target
339+
!PARSE-TREE: | OmpClauseList -> OmpClause -> Map -> OmpMapClause
340+
!PARSE-TREE: | | OmpMapperIdentifier -> Name = 'xx'
341+
!PARSE-TREE: | | Type = From
342+
!PARSE-TREE: | | OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'x'
343+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
! RUN: %flang_fc1 -fdebug-dump-symbols -fopenmp -fopenmp-version=50 %s | FileCheck %s
2+
program main
3+
!CHECK-LABEL: MainProgram scope: main
4+
integer, parameter :: n = 256
5+
real(8) :: a(256)
6+
!$omp target map(mapper(xx), from:a)
7+
do i=1,n
8+
a(i) = 4.2
9+
end do
10+
!$omp end target
11+
!CHECK: OtherConstruct scope: size=0 alignment=1 sourceRange=74 bytes
12+
!CHECK: OtherClause scope: size=0 alignment=1 sourceRange=0 bytes
13+
!CHECK: xx: Misc ConstructName
14+
end program main

flang/test/Semantics/OpenMP/map-clause.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ subroutine sb(arr)
3333
c = 2
3434
!$omp end target
3535
end subroutine
36+
37+
subroutine sb1
38+
integer :: xx
39+
integer :: a
40+
!ERROR: Name 'xx' should be a mapper name
41+
!$omp target map(mapper(xx), from:a)
42+
!$omp end target
43+
end subroutine sb1

0 commit comments

Comments
 (0)