Skip to content

Commit 3ca5910

Browse files
authored
[flang][OpenMP] Use OmpDirectiveSpecification in DECLARE_MAPPER (#160169)
1 parent d614027 commit 3ca5910

File tree

11 files changed

+73
-55
lines changed

11 files changed

+73
-55
lines changed

flang/include/flang/Parser/openmp-utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ struct ConstructId {
4141
MAKE_CONSTR_ID(OmpDeclareVariantDirective, D::OMPD_declare_variant);
4242
MAKE_CONSTR_ID(OpenMPDeclarativeAllocate, D::OMPD_allocate);
4343
MAKE_CONSTR_ID(OpenMPDeclarativeAssumes, D::OMPD_assumes);
44-
MAKE_CONSTR_ID(OpenMPDeclareMapperConstruct, D::OMPD_declare_mapper);
4544
MAKE_CONSTR_ID(OpenMPDeclareReductionConstruct, D::OMPD_declare_reduction);
4645
MAKE_CONSTR_ID(OpenMPDeclareSimdConstruct, D::OMPD_declare_simd);
4746
MAKE_CONSTR_ID(OpenMPDeclareTargetConstruct, D::OMPD_declare_target);
@@ -96,7 +95,6 @@ struct DirectiveNameScope {
9695
} else if constexpr (std::is_same_v<T, OmpDeclareVariantDirective> ||
9796
std::is_same_v<T, OpenMPDeclarativeAllocate> ||
9897
std::is_same_v<T, OpenMPDeclarativeAssumes> ||
99-
std::is_same_v<T, OpenMPDeclareMapperConstruct> ||
10098
std::is_same_v<T, OpenMPDeclareReductionConstruct> ||
10199
std::is_same_v<T, OpenMPDeclareSimdConstruct> ||
102100
std::is_same_v<T, OpenMPDeclareTargetConstruct> ||

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4953,9 +4953,9 @@ struct OpenMPDeclareTargetConstruct {
49534953
// OMP v5.2: 5.8.8
49544954
// declare-mapper -> DECLARE MAPPER ([mapper-name :] type :: var) map-clauses
49554955
struct OpenMPDeclareMapperConstruct {
4956-
TUPLE_CLASS_BOILERPLATE(OpenMPDeclareMapperConstruct);
4956+
WRAPPER_CLASS_BOILERPLATE(
4957+
OpenMPDeclareMapperConstruct, OmpDirectiveSpecification);
49574958
CharBlock source;
4958-
std::tuple<Verbatim, OmpMapperSpecifier, OmpClauseList> t;
49594959
};
49604960

49614961
// ref: 5.2: Section 5.5.11 139-141

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,18 +3441,20 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
34413441
TODO(converter.getCurrentLocation(), "OpenMPDeclareSimdConstruct");
34423442
}
34433443

3444-
static void
3445-
genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
3446-
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
3447-
const parser::OpenMPDeclareMapperConstruct &declareMapperConstruct) {
3448-
mlir::Location loc = converter.genLocation(declareMapperConstruct.source);
3444+
static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
3445+
semantics::SemanticsContext &semaCtx,
3446+
lower::pft::Evaluation &eval,
3447+
const parser::OpenMPDeclareMapperConstruct &construct) {
3448+
mlir::Location loc = converter.genLocation(construct.source);
34493449
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
3450+
const parser::OmpArgumentList &args = construct.v.Arguments();
3451+
assert(args.v.size() == 1 && "Expecting single argument");
34503452
lower::StatementContext stmtCtx;
3451-
const auto &spec =
3452-
std::get<parser::OmpMapperSpecifier>(declareMapperConstruct.t);
3453-
const auto &mapperName{std::get<std::string>(spec.t)};
3454-
const auto &varType{std::get<parser::TypeSpec>(spec.t)};
3455-
const auto &varName{std::get<parser::Name>(spec.t)};
3453+
const auto *spec = std::get_if<parser::OmpMapperSpecifier>(&args.v.front().u);
3454+
assert(spec && "Expecting mapper specifier");
3455+
const auto &mapperName{std::get<std::string>(spec->t)};
3456+
const auto &varType{std::get<parser::TypeSpec>(spec->t)};
3457+
const auto &varName{std::get<parser::Name>(spec->t)};
34563458
assert(varType.declTypeSpec->category() ==
34573459
semantics::DeclTypeSpec::Category::TypeDerived &&
34583460
"Expected derived type");
@@ -3476,9 +3478,7 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
34763478

34773479
// Populate the declareMapper region with the map information.
34783480
mlir::omp::DeclareMapperInfoOperands clauseOps;
3479-
const auto *clauseList{
3480-
parser::Unwrap<parser::OmpClauseList>(declareMapperConstruct.t)};
3481-
List<Clause> clauses = makeClauses(*clauseList, semaCtx);
3481+
List<Clause> clauses = makeClauses(construct.v.Clauses(), semaCtx);
34823482
ClauseProcessor cp(converter, semaCtx, clauses);
34833483
cp.processMap(loc, stmtCtx, clauseOps);
34843484
mlir::omp::DeclareMapperInfoOp::create(firOpBuilder, loc, clauseOps.mapVars);

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,8 +1764,9 @@ TYPE_PARSER(applyFunction<OmpMapperSpecifier>(ConstructOmpMapperSpecifier,
17641764

17651765
// OpenMP 5.2: 5.8.8 Declare Mapper Construct
17661766
TYPE_PARSER(sourced(construct<OpenMPDeclareMapperConstruct>(
1767-
verbatim("DECLARE MAPPER"_tok) || verbatim("DECLARE_MAPPER"_tok),
1768-
parenthesized(Parser<OmpMapperSpecifier>{}), Parser<OmpClauseList>{})))
1767+
predicated(Parser<OmpDirectiveName>{},
1768+
IsDirective(llvm::omp::Directive::OMPD_declare_mapper)) >=
1769+
Parser<OmpDirectiveSpecification>{})))
17691770

17701771
TYPE_PARSER(construct<OmpReductionCombiner>(Parser<AssignmentStmt>{}) ||
17711772
construct<OmpReductionCombiner>(Parser<FunctionReference>{}))

flang/lib/Parser/unparse.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,21 +2559,10 @@ class UnparseVisitor {
25592559
Put("\n");
25602560
EndOpenMP();
25612561
}
2562-
void Unparse(const OpenMPDeclareMapperConstruct &z) {
2562+
void Unparse(const OpenMPDeclareMapperConstruct &x) {
25632563
BeginOpenMP();
2564-
Word("!$OMP DECLARE MAPPER (");
2565-
const auto &spec{std::get<OmpMapperSpecifier>(z.t)};
2566-
const auto &mapperName{std::get<std::string>(spec.t)};
2567-
if (mapperName.find(llvm::omp::OmpDefaultMapperName) == std::string::npos) {
2568-
Walk(mapperName);
2569-
Put(":");
2570-
}
2571-
Walk(std::get<TypeSpec>(spec.t));
2572-
Put("::");
2573-
Walk(std::get<Name>(spec.t));
2574-
Put(")");
2575-
2576-
Walk(std::get<OmpClauseList>(z.t));
2564+
Word("!$OMP ");
2565+
Walk(x.v);
25772566
Put("\n");
25782567
EndOpenMP();
25792568
}

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -624,11 +624,6 @@ template <typename Checker> struct DirectiveSpellingVisitor {
624624
checker_(std::get<parser::Verbatim>(x.t).source, Directive::OMPD_assumes);
625625
return false;
626626
}
627-
bool Pre(const parser::OpenMPDeclareMapperConstruct &x) {
628-
checker_(
629-
std::get<parser::Verbatim>(x.t).source, Directive::OMPD_declare_mapper);
630-
return false;
631-
}
632627
bool Pre(const parser::OpenMPDeclareReductionConstruct &x) {
633628
checker_(std::get<parser::Verbatim>(x.t).source,
634629
Directive::OMPD_declare_reduction);
@@ -1603,13 +1598,25 @@ void OmpStructureChecker::Leave(const parser::OmpDeclareTargetWithClause &x) {
16031598
}
16041599

16051600
void OmpStructureChecker::Enter(const parser::OpenMPDeclareMapperConstruct &x) {
1606-
const auto &dir{std::get<parser::Verbatim>(x.t)};
1607-
PushContextAndClauseSets(
1608-
dir.source, llvm::omp::Directive::OMPD_declare_mapper);
1609-
const auto &spec{std::get<parser::OmpMapperSpecifier>(x.t)};
1610-
const auto &type = std::get<parser::TypeSpec>(spec.t);
1611-
if (!std::get_if<parser::DerivedTypeSpec>(&type.u)) {
1612-
context_.Say(dir.source, "Type is not a derived type"_err_en_US);
1601+
const parser::OmpDirectiveName &dirName{x.v.DirName()};
1602+
PushContextAndClauseSets(dirName.source, dirName.v);
1603+
1604+
const parser::OmpArgumentList &args{x.v.Arguments()};
1605+
if (args.v.size() != 1) {
1606+
context_.Say(args.source,
1607+
"DECLARE_MAPPER directive should have a single argument"_err_en_US);
1608+
return;
1609+
}
1610+
1611+
const parser::OmpArgument &arg{args.v.front()};
1612+
if (auto *spec{std::get_if<parser::OmpMapperSpecifier>(&arg.u)}) {
1613+
const auto &type = std::get<parser::TypeSpec>(spec->t);
1614+
if (!std::get_if<parser::DerivedTypeSpec>(&type.u)) {
1615+
context_.Say(arg.source, "Type is not a derived type"_err_en_US);
1616+
}
1617+
} else {
1618+
context_.Say(arg.source,
1619+
"The argument to the DECLARE_MAPPER directive should be a mapper-specifier"_err_en_US);
16131620
}
16141621
}
16151622

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2346,7 +2346,8 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPDeclareTargetConstruct &x) {
23462346
}
23472347

23482348
bool OmpAttributeVisitor::Pre(const parser::OpenMPDeclareMapperConstruct &x) {
2349-
PushContext(x.source, llvm::omp::Directive::OMPD_declare_mapper);
2349+
const parser::OmpDirectiveName &dirName{x.v.DirName()};
2350+
PushContext(dirName.source, dirName.v);
23502351
return true;
23512352
}
23522353

flang/lib/Semantics/resolve-names.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,9 +1518,7 @@ class OmpVisitor : public virtual DeclarationVisitor {
15181518

15191519
bool Pre(const parser::OpenMPDeclareMapperConstruct &x) {
15201520
AddOmpSourceRange(x.source);
1521-
ProcessMapperSpecifier(std::get<parser::OmpMapperSpecifier>(x.t),
1522-
std::get<parser::OmpClauseList>(x.t));
1523-
return false;
1521+
return true;
15241522
}
15251523

15261524
bool Pre(const parser::OpenMPDeclareSimdConstruct &x) {
@@ -1694,6 +1692,11 @@ class OmpVisitor : public virtual DeclarationVisitor {
16941692
PopScope();
16951693
}
16961694
}
1695+
bool Pre(const parser::OmpMapperSpecifier &x) {
1696+
// OmpMapperSpecifier is handled explicitly, and the AST traversal
1697+
// should not reach a point where it calls this function.
1698+
llvm_unreachable("This function should not be reached by AST traversal");
1699+
}
16971700
bool Pre(const parser::OmpDirectiveSpecification &x);
16981701
void Post(const parser::OmpDirectiveSpecification &) {
16991702
messageHandler().set_currStmtSource(std::nullopt);

flang/test/Parser/OpenMP/declare-mapper-unparse.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ program main
99
end type ty
1010

1111

12-
!CHECK: !$OMP DECLARE MAPPER (mymapper:ty::mapped) MAP(mapped,mapped%x)
12+
!CHECK: !$OMP DECLARE MAPPER(mymapper:ty::mapped) MAP(mapped,mapped%x)
1313
!$omp declare mapper(mymapper : ty :: mapped) map(mapped, mapped%x)
1414

1515
!PARSE-TREE: OpenMPDeclareMapperConstruct
@@ -24,7 +24,7 @@ program main
2424
!PARSE-TREE: DataRef -> Name = 'mapped'
2525
!PARSE-TREE: Name = 'x'
2626

27-
!CHECK: !$OMP DECLARE MAPPER (ty::mapped) MAP(mapped,mapped%x)
27+
!CHECK: !$OMP DECLARE MAPPER(ty::mapped) MAP(mapped,mapped%x)
2828
!$omp declare mapper(ty :: mapped) map(mapped, mapped%x)
2929

3030
!PARSE-TREE: OpenMPDeclareMapperConstruct

flang/test/Parser/OpenMP/openmp6-directive-spellings.f90

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ subroutine f01
5151
!UNPARSE: TYPE :: t
5252
!UNPARSE: INTEGER :: x
5353
!UNPARSE: END TYPE
54-
!UNPARSE: !$OMP DECLARE MAPPER (t::v) MAP(v%x)
54+
!UNPARSE: !$OMP DECLARE_MAPPER(t::v) MAP(v%x)
5555
!UNPARSE: END SUBROUTINE
5656

57-
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareMapperConstruct
58-
!PARSE-TREE: | Verbatim
59-
!PARSE-TREE: | OmpMapperSpecifier
57+
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareMapperConstruct -> OmpDirectiveSpecification
58+
!PARSE-TREE: | OmpDirectiveName -> llvm::omp::Directive = declare mapper
59+
!PARSE-TREE: | OmpArgumentList -> OmpArgument -> OmpMapperSpecifier
6060
!PARSE-TREE: | | string = 't.omp.default.mapper'
6161
!PARSE-TREE: | | TypeSpec -> DerivedTypeSpec
6262
!PARSE-TREE: | | | Name = 't'
@@ -66,6 +66,7 @@ subroutine f01
6666
!PARSE-TREE: | | | DataRef -> Name = 'v'
6767
!PARSE-TREE: | | | Name = 'x'
6868
!PARSE-TREE: | | bool = 'true'
69+
!PARSE-TREE: | Flags = None
6970

7071
subroutine f02
7172
type :: t

0 commit comments

Comments
 (0)