Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flang/include/flang/Parser/dump-parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ class ParseTreeDumper {
NODE_ENUM(OmpLinearModifier, Type)
NODE(parser, OmpLoopDirective)
NODE(parser, OmpMapClause)
NODE(parser, OmpMapperIdentifier)
NODE_ENUM(OmpMapClause, TypeModifier)
NODE_ENUM(OmpMapClause, Type)
static std::string GetNodeName(const llvm::omp::Clause &x) {
Expand Down
8 changes: 6 additions & 2 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3703,8 +3703,11 @@ struct OmpLinearClause {
std::variant<WithModifier, WithoutModifier> u;
};

WRAPPER_CLASS(OmpMapperIdentifier, std::optional<Name>);

// 2.15.5.1 map ->
// MAP ([[map-type-modifier-list [,]] [iterator-modifier [,]] map-type : ]
// MAP ([MAPPER(mapper-identifier)] [[map-type-modifier-list [,]]
// [iterator-modifier [,]] map-type : ]
// variable-name-list)
// map-type-modifier-list -> map-type-modifier [,] [...]
// map-type-modifier -> ALWAYS | CLOSE | PRESENT | OMPX_HOLD
Expand All @@ -3718,7 +3721,8 @@ struct OmpMapClause {
// The checks for satisfying those constraints are deferred to semantics.
// In OpenMP 5.2 the non-comma syntax has been deprecated: keep the
// information about separator presence to emit a diagnostic if needed.
std::tuple<std::optional<std::list<TypeModifier>>,
std::tuple<OmpMapperIdentifier, // Mapper name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It reads a bit strangely to me that the optional mapper identifier looks non-optional (if one doesn't look up at the type definition). Could the std::optional be moved outside of the type so it is visible here?

Don't worry about it if it becomes a headache to do - this is very nitpicky

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this was my solution to making the maybe work. Since we need to parse the "MAPPER" and then parenthesized(name) and a comma. If I move the optional around, the maybe parsing doesn't work.

Unless someone can let me know how to make something maybe parse to produce the wrapper thing optional.

Everything I've tried (and that's a lot - I couldn't list them all, but std::optional<Name> in the std::tuple was one of them.

The TypeModifier and IteratorModifer and Type gets away with it, because they don't have a keyword with a parenthesized "argument". so they can just do a list (which is allowed to be empty).

I did try again just now, but I can't come up with something that compiles...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way this was intended to work is that

  1. You add std::optional<std::list<YourModifier>> to the tuple in here.
  2. Add the mapper parser to the ModParser list in MapModifiers in openmp-parsers.cpp.
  3. Unpack it in makeMapClause and pass it to the OmpMapClause constructor.

Then in check-omp-structure.cpp you can verify that there is at most one mapper modifier in the list, and print a meaningful message.

Having said that, I'm working on modifier verification right now, which will change how they are organized and parsed, so it's not a big deal if you leave it as is, since I'll have to change it anyway. (One problem with the current code that I'm trying to address is that the modifiers can be specified in any order, which the current parsers don't allow.)

std::optional<std::list<TypeModifier>>,
std::optional<std::list<OmpIteratorModifier>>, // unique
std::optional<std::list<Type>>, // unique
OmpObjectList,
Expand Down
5 changes: 5 additions & 0 deletions flang/lib/Lower/OpenMP/Clauses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "flang/Common/idioms.h"
#include "flang/Evaluate/expression.h"
#include "flang/Optimizer/Builder/Todo.h"
#include "flang/Parser/parse-tree.h"
#include "flang/Semantics/expression.h"
#include "flang/Semantics/symbol.h"
Expand Down Expand Up @@ -974,6 +975,10 @@ Map make(const parser::OmpClause::Map &inp,
std::get<std::optional<std::list<parser::OmpIteratorModifier>>>(inp.v.t);
auto &t2 = std::get<std::optional<std::list<wrapped::Type>>>(inp.v.t);
auto &t3 = std::get<parser::OmpObjectList>(inp.v.t);
auto &t4 = std::get<parser::OmpMapperIdentifier>(inp.v.t);

if (t4.v)
TODO_NOLOC("OmpMapClause(MAPPER(...))");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you write something easier for users to parse. Maybe something like "support for user-defined mappers".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point! I've updated the message to contain both a user-friendly explanation and the developer-relevant clause.


// These should have been diagnosed already.
assert((!t1 || t1->size() == 1) && "Only one iterator modifier is allowed");
Expand Down
18 changes: 11 additions & 7 deletions flang/lib/Parser/openmp-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ template <typename Separator> struct MotionModifiers {
constexpr MotionModifiers(const MotionModifiers &) = default;
constexpr MotionModifiers(MotionModifiers &&) = default;

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

template <bool CommasEverywhere>
static inline OmpMapClause makeMapClause(
static inline OmpMapClause makeMapClause(OmpMapperIdentifier &&mm,
std::tuple<std::optional<std::list<OmpMapClause::TypeModifier>>,
std::optional<std::list<OmpIteratorModifier>>,
std::optional<std::list<OmpMapClause::Type>>> &&mods,
OmpObjectList &&objs) {
auto &&[tm, it, ty] = std::move(mods);
return OmpMapClause{std::move(tm), std::move(it), std::move(ty),
std::move(objs), CommasEverywhere};
return OmpMapClause{std::move(mm), std::move(tm), std::move(it),
std::move(ty), std::move(objs), CommasEverywhere};
}

TYPE_PARSER(construct<OmpMapperIdentifier>(
maybe("MAPPER"_tok >> parenthesized(name) / ","_tok)))

TYPE_PARSER(construct<OmpMapClause>(
applyFunction<OmpMapClause>(
makeMapClause<true>, MapModifiers(","_tok), Parser<OmpObjectList>{}) ||
applyFunction<OmpMapClause>(makeMapClause<true>,
Parser<OmpMapperIdentifier>{}, MapModifiers(","_tok),
Parser<OmpObjectList>{}) ||
applyFunction<OmpMapClause>(makeMapClause<false>,
MapModifiers(maybe(","_tok)), Parser<OmpObjectList>{})))
Parser<OmpMapperIdentifier>{}, MapModifiers(maybe(","_tok)),
Parser<OmpObjectList>{})))

// [OpenMP 5.0]
// 2.19.7.2 defaultmap(implicit-behavior[:variable-category])
Expand Down
10 changes: 10 additions & 0 deletions flang/lib/Parser/unparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2095,12 +2095,22 @@ class UnparseVisitor {
std::get<std::optional<std::list<OmpMapClause::TypeModifier>>>(x.t);
auto &iter = std::get<std::optional<std::list<OmpIteratorModifier>>>(x.t);
auto &type = std::get<std::optional<std::list<OmpMapClause::Type>>>(x.t);
auto &mapper = std::get<OmpMapperIdentifier>(x.t);

// For a given list of items, if the item has a value, then walk it.
// Print commas between items that have values.
// Return 'true' if something did get printed, otherwise 'false'.
bool needComma{false};
if (mapper.v) {
Word("MAPPER(");
Walk(*mapper.v);
Put(")");
needComma = true;
}
if (typeMod) {
if (needComma) {
Put(", ");
}
Walk(*typeMod);
needComma = true;
}
Expand Down
10 changes: 10 additions & 0 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,8 @@ class OmpVisitor : public virtual DeclarationVisitor {

bool Pre(const parser::OpenMPDeclareMapperConstruct &);

bool Pre(const parser::OmpMapClause &);

void Post(const parser::OmpBeginLoopDirective &) {
messageHandler().set_currStmtSource(std::nullopt);
}
Expand Down Expand Up @@ -1639,6 +1641,14 @@ bool OmpVisitor::Pre(const parser::OpenMPDeclareMapperConstruct &x) {
return false;
}

bool OmpVisitor::Pre(const parser::OmpMapClause &x) {
const auto &mid{std::get<parser::OmpMapperIdentifier>(x.t)};
if (const auto &mapperName{mid.v})
mapperName->symbol =
&MakeSymbol(*mapperName, MiscDetails{MiscDetails::Kind::ConstructName});
Copy link
Contributor

@kparzysz kparzysz Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the MAPPER modifier should refer to a preexisting object, so this should be a name lookup, not creating a new symbol. The DECLARE MAPPER should declare the identifier (and create the symbol). If the lookup fails, you could create a dummy, and report it as an error later on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a FindSymbol and using that. Have to still make a symbol for the testing purposes right now.

return true;
}

// Walk the parse tree and resolve names to symbols.
class ResolveNamesVisitor : public virtual ScopeHandler,
public ModuleVisitor,
Expand Down
11 changes: 11 additions & 0 deletions flang/test/Lower/OpenMP/Todo/map-mapper.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
! RUN: not %flang_fc1 -emit-fir -fopenmp -fopenmp-version=50 %s 2>&1 | FileCheck %s
program p
integer, parameter :: n = 256
real(8) :: a(256)
!$omp target map(mapper(xx), from:a)
!CHECK: not yet implemented: OmpMapClause(MAPPER(...))
do i=1,n
a(i) = 4.2
end do
!$omp end target
end program p
25 changes: 25 additions & 0 deletions flang/test/Parser/OpenMP/map-modifiers.f90
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,28 @@ subroutine f90(x, y)
!PARSE-TREE: | | | | Designator -> DataRef -> Name = 'k'
!PARSE-TREE: | | bool = 'true'

subroutine f100(x, y)
integer :: x(10)
integer :: y
integer, parameter :: p = 23
!$omp target map(mapper(xx), from: x)
x = x + 1
!$omp end target
end

!UNPARSE: SUBROUTINE f100 (x, y)
!UNPARSE: INTEGER x(10_4)
!UNPARSE: INTEGER y
!UNPARSE: INTEGER, PARAMETER :: p = 23_4
!UNPARSE: !$OMP TARGET MAP(MAPPER(XX), FROM: X)
!UNPARSE: x=x+1_4
!UNPARSE: !$OMP END TARGET
!UNPARSE: END SUBROUTINE

!PARSE-TREE: OmpBeginBlockDirective
!PARSE-TREE: | OmpBlockDirective -> llvm::omp::Directive = target
!PARSE-TREE: | OmpClauseList -> OmpClause -> Map -> OmpMapClause
!PARSE-TREE: | | OmpMapperIdentifier -> Name = 'xx'
!PARSE-TREE: | | Type = From
!PARSE-TREE: | | OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'x'

14 changes: 14 additions & 0 deletions flang/test/Semantics/OpenMP/map-clause-symbols.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
! RUN: %flang_fc1 -fdebug-dump-symbols -fopenmp -fopenmp-version=50 %s | FileCheck %s
program main
!CHECK-LABEL: MainProgram scope: main
integer, parameter :: n = 256
real(8) :: a(256)
!$omp target map(mapper(xx), from:a)
do i=1,n
a(i) = 4.2
end do
!$omp end target
!CHECK: OtherConstruct scope: size=0 alignment=1 sourceRange=74 bytes
!CHECK: OtherClause scope: size=0 alignment=1 sourceRange=0 bytes
!CHECK: xx: Misc ConstructName
end program main
Loading