Skip to content

Commit 642b452

Browse files
[Flang][OpenMP] WIP: Add frontend support for declare variant
Support is added for parsing and semantics. Lowering will emit a TODO error. append_args clause and use of interop inside have some overlap with #120584.
1 parent f3dcc0f commit 642b452

File tree

8 files changed

+112
-4
lines changed

8 files changed

+112
-4
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,13 @@ class ParseTreeDumper {
480480
NODE(parser, OldParameterStmt)
481481
NODE(parser, OmpTypeSpecifier)
482482
NODE(parser, OmpTypeNameList)
483+
NODE(parser, OmpAdjustArgsClause)
484+
NODE(OmpAdjustArgsClause, OmpAdjustOp)
485+
NODE_ENUM(OmpAdjustArgsClause::OmpAdjustOp, Value)
486+
NODE(parser, OmpInteropType)
487+
NODE_ENUM(OmpInteropType, Value)
488+
NODE(parser, OmpAppendArgsClause)
489+
NODE(OmpAppendArgsClause, OmpAppendOp)
483490
NODE(parser, OmpLocator)
484491
NODE(parser, OmpLocatorList)
485492
NODE(parser, OmpReductionSpecifier)
@@ -693,6 +700,7 @@ class ParseTreeDumper {
693700
NODE(parser, OpenMPCriticalConstruct)
694701
NODE(parser, OpenMPDeclarativeAllocate)
695702
NODE(parser, OpenMPDeclarativeConstruct)
703+
NODE(parser, OmpDeclareVariantDirective)
696704
NODE(parser, OpenMPDeclareReductionConstruct)
697705
NODE(parser, OpenMPDeclareSimdConstruct)
698706
NODE(parser, OpenMPDeclareTargetConstruct)

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3974,6 +3974,15 @@ struct OmpAbsentClause {
39743974
WRAPPER_CLASS_BOILERPLATE(OmpAbsentClause, OmpDirectiveList);
39753975
};
39763976

3977+
struct OmpAdjustArgsClause {
3978+
TUPLE_CLASS_BOILERPLATE(OmpAdjustArgsClause);
3979+
struct OmpAdjustOp {
3980+
ENUM_CLASS(Value, Nothing, NeedDevicePtr)
3981+
WRAPPER_CLASS_BOILERPLATE(OmpAdjustOp, Value);
3982+
};
3983+
std::tuple<OmpAdjustOp, OmpObjectList> t;
3984+
};
3985+
39773986
// Ref: [5.0:135-140], [5.1:161-166], [5.2:264-265]
39783987
//
39793988
// affinity-clause ->
@@ -4017,6 +4026,22 @@ struct OmpAllocateClause {
40174026
std::tuple<MODIFIERS(), OmpObjectList> t;
40184027
};
40194028

4029+
4030+
// InteropType -> target || targetsync
4031+
struct OmpInteropType {
4032+
ENUM_CLASS(Value, Target, TargetSync)
4033+
WRAPPER_CLASS_BOILERPLATE(OmpInteropType, Value);
4034+
};
4035+
4036+
struct OmpAppendArgsClause {
4037+
struct OmpAppendOp {
4038+
WRAPPER_CLASS_BOILERPLATE(
4039+
OmpAppendOp, std::list<OmpInteropType>);
4040+
};
4041+
WRAPPER_CLASS_BOILERPLATE(
4042+
OmpAppendArgsClause, std::list<OmpAppendOp>);
4043+
};
4044+
40204045
// Ref: [5.2:216-217 (sort of, as it's only mentioned in passing)
40214046
// AT(compilation|execution)
40224047
struct OmpAtClause {
@@ -4625,6 +4650,13 @@ struct OmpBlockDirective {
46254650
CharBlock source;
46264651
};
46274652

4653+
4654+
struct OmpDeclareVariantDirective {
4655+
TUPLE_CLASS_BOILERPLATE(OmpDeclareVariantDirective);
4656+
CharBlock source;
4657+
std::tuple<Verbatim, std::optional<Name>, Name, OmpClauseList> t;
4658+
};
4659+
46284660
// 2.10.6 declare-target -> DECLARE TARGET (extended-list) |
46294661
// DECLARE TARGET [declare-target-clause[ [,]
46304662
// declare-target-clause]...]
@@ -4703,7 +4735,7 @@ struct OpenMPDeclarativeConstruct {
47034735
std::variant<OpenMPDeclarativeAllocate, OpenMPDeclarativeAssumes,
47044736
OpenMPDeclareMapperConstruct, OpenMPDeclareReductionConstruct,
47054737
OpenMPDeclareSimdConstruct, OpenMPDeclareTargetConstruct,
4706-
OpenMPThreadprivate, OpenMPRequiresConstruct, OpenMPUtilityConstruct,
4738+
OmpDeclareVariantDirective, OpenMPThreadprivate, OpenMPRequiresConstruct, OpenMPUtilityConstruct,
47074739
OmpMetadirectiveDirective>
47084740
u;
47094741
};

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,13 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
31263126
TODO(converter.getCurrentLocation(), "OpenMP ASSUMES declaration");
31273127
}
31283128

3129+
static void genOMP(
3130+
lower::AbstractConverter &converter, lower::SymMap &symTable,
3131+
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
3132+
const parser::OmpDeclareVariantDirective &declareVariantDirective) {
3133+
TODO(converter.getCurrentLocation(), "OpenMPDeclareVariantDirective");
3134+
}
3135+
31293136
static void genOMP(
31303137
lower::AbstractConverter &converter, lower::SymMap &symTable,
31313138
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,17 @@ TYPE_PARSER(sourced(construct<OmpToClause::Modifier>(
550550
TYPE_PARSER(sourced(construct<OmpWhenClause::Modifier>( //
551551
Parser<OmpContextSelector>{})))
552552

553+
TYPE_PARSER(construct<OmpInteropType>(
554+
"TARGETSYNC" >> pure(OmpInteropType::Value::TargetSync) ||
555+
"TARGET" >> pure(OmpInteropType::Value::Target)))
556+
557+
TYPE_PARSER(construct<OmpAppendArgsClause::OmpAppendOp>(
558+
"INTEROP" >> parenthesized(nonemptyList(Parser<OmpInteropType>{}))))
559+
560+
TYPE_PARSER(construct<OmpAdjustArgsClause::OmpAdjustOp>(
561+
"NOTHING" >> pure(OmpAdjustArgsClause::OmpAdjustOp::Value::Nothing) ||
562+
"NEED_DEVICE_PTR" >> pure(OmpAdjustArgsClause::OmpAdjustOp::Value::NeedDevicePtr)))
563+
553564
// --- Parsers for clauses --------------------------------------------
554565

555566
/// `MOBClause` is a clause that has a
@@ -569,12 +580,19 @@ static inline MOBClause makeMobClause(
569580
}
570581
}
571582

583+
TYPE_PARSER(construct<OmpAdjustArgsClause>(
584+
(Parser<OmpAdjustArgsClause::OmpAdjustOp>{} / ":"),
585+
Parser<OmpObjectList>{}))
586+
572587
// [5.0] 2.10.1 affinity([aff-modifier:] locator-list)
573588
// aff-modifier: interator-modifier
574589
TYPE_PARSER(construct<OmpAffinityClause>(
575590
maybe(nonemptyList(Parser<OmpAffinityClause::Modifier>{}) / ":"),
576591
Parser<OmpObjectList>{}))
577592

593+
TYPE_PARSER(construct<OmpAppendArgsClause>(
594+
parenthesized(nonemptyList(Parser<OmpAppendArgsClause::OmpAppendOp>{}))))
595+
578596
// 2.15.3.1 DEFAULT (PRIVATE | FIRSTPRIVATE | SHARED | NONE)
579597
TYPE_PARSER(construct<OmpDefaultClause::DataSharingAttribute>(
580598
"PRIVATE" >> pure(OmpDefaultClause::DataSharingAttribute::Private) ||
@@ -808,6 +826,8 @@ TYPE_PARSER("ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
808826
parenthesized(Parser<OmpAbsentClause>{}))) ||
809827
"ACQUIRE" >> construct<OmpClause>(construct<OmpClause::Acquire>()) ||
810828
"ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
829+
"ADJUST_ARGS" >> construct<OmpClause>(construct<OmpClause::AdjustArgs>(
830+
parenthesized(Parser<OmpAdjustArgsClause>{}))) ||
811831
"AFFINITY" >> construct<OmpClause>(construct<OmpClause::Affinity>(
812832
parenthesized(Parser<OmpAffinityClause>{}))) ||
813833
"ALIGN" >> construct<OmpClause>(construct<OmpClause::Align>(
@@ -816,6 +836,8 @@ TYPE_PARSER("ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
816836
parenthesized(Parser<OmpAlignedClause>{}))) ||
817837
"ALLOCATE" >> construct<OmpClause>(construct<OmpClause::Allocate>(
818838
parenthesized(Parser<OmpAllocateClause>{}))) ||
839+
"APPEND_ARGS" >> construct<OmpClause>(construct<OmpClause::AppendArgs>(
840+
parenthesized(Parser<OmpAppendArgsClause>{}))) ||
819841
"ALLOCATOR" >> construct<OmpClause>(construct<OmpClause::Allocator>(
820842
parenthesized(scalarIntExpr))) ||
821843
"AT" >> construct<OmpClause>(construct<OmpClause::At>(
@@ -1209,6 +1231,11 @@ TYPE_PARSER(construct<OmpInitializerClause>(
12091231
construct<OmpInitializerClause>(Parser<OmpInitializerExpr>{}) ||
12101232
construct<OmpInitializerClause>(Parser<OmpInitializerProc>{})))
12111233

1234+
// OpenMP 5.2: 7.5.4 Declare Variant directive
1235+
TYPE_PARSER(sourced(construct<OmpDeclareVariantDirective>(
1236+
verbatim("DECLARE VARIANT"_tok),
1237+
"(" >> maybe(name / ":"), name / ")", Parser<OmpClauseList>{})))
1238+
12121239
// 2.16 Declare Reduction Construct
12131240
TYPE_PARSER(sourced(construct<OpenMPDeclareReductionConstruct>(
12141241
verbatim("DECLARE REDUCTION"_tok),
@@ -1380,6 +1407,8 @@ TYPE_PARSER(
13801407
Parser<OpenMPDeclareSimdConstruct>{}) ||
13811408
construct<OpenMPDeclarativeConstruct>(
13821409
Parser<OpenMPDeclareTargetConstruct>{}) ||
1410+
construct<OpenMPDeclarativeConstruct>(
1411+
Parser<OmpDeclareVariantDirective>{}) ||
13831412
construct<OpenMPDeclarativeConstruct>(
13841413
Parser<OpenMPDeclarativeAllocate>{}) ||
13851414
construct<OpenMPDeclarativeConstruct>(

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,15 @@ void OmpStructureChecker::Leave(const parser::OpenMPDeclareSimdConstruct &) {
16041604
dirContext_.pop_back();
16051605
}
16061606

1607+
void OmpStructureChecker::Enter(const parser::OmpDeclareVariantDirective &x) {
1608+
const auto &dir{std::get<parser::Verbatim>(x.t)};
1609+
PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_declare_variant);
1610+
}
1611+
1612+
void OmpStructureChecker::Leave(const parser::OmpDeclareVariantDirective &) {
1613+
dirContext_.pop_back();
1614+
}
1615+
16071616
void OmpStructureChecker::Enter(const parser::OpenMPDepobjConstruct &x) {
16081617
const auto &dir{std::get<parser::Verbatim>(x.t)};
16091618
PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_depobj);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class OmpStructureChecker
9696
void Enter(const parser::OmpEndSectionsDirective &);
9797
void Leave(const parser::OmpEndSectionsDirective &);
9898

99+
void Enter(const parser::OmpDeclareVariantDirective &);
100+
void Leave(const parser::OmpDeclareVariantDirective &);
99101
void Enter(const parser::OpenMPDeclareSimdConstruct &);
100102
void Leave(const parser::OpenMPDeclareSimdConstruct &);
101103
void Enter(const parser::OpenMPDeclarativeAllocate &);

flang/lib/Semantics/resolve-names.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,25 @@ class OmpVisitor : public virtual DeclarationVisitor {
14941494
return true;
14951495
}
14961496

1497+
bool Pre(const parser::OmpDeclareVariantDirective &x) {
1498+
AddOmpSourceRange(x.source);
1499+
auto FindSymbolOrError = [](parser::Name& procName) {
1500+
auto *symbol{FindSymbol(NonDerivedTypeScope(), procName)};
1501+
if (!symbol) {
1502+
context().Say(procName.source,
1503+
"Implicit subroutine declaration '%s' in !$OMP DECLARE VARIANT"_err_en_US,
1504+
procName.source);
1505+
}
1506+
};
1507+
auto &baseProcName = std::get<std::optional<parser::Name>>(x.t);
1508+
if (baseProcName) {
1509+
FindSymbolOrError(*baseProcName);
1510+
}
1511+
auto &varProcName = std::get<parser::Name>(x.t);
1512+
FindSymbolOrError(varProcName);
1513+
return true;
1514+
}
1515+
14971516
bool Pre(const parser::OpenMPDeclareReductionConstruct &x) {
14981517
AddOmpSourceRange(x.source);
14991518
ProcessReductionSpecifier(

llvm/include/llvm/Frontend/OpenMP/OMP.td

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def OMPC_AcqRel : Clause<"acq_rel"> {
4343
let clangClass = "OMPAcqRelClause";
4444
}
4545
def OMPC_AdjustArgs : Clause<"adjust_args"> {
46+
let flangClass = "OmpAdjustArgsClause";
4647
}
4748
def OMPC_Affinity : Clause<"affinity"> {
4849
let clangClass = "OMPAffinityClause";
@@ -65,6 +66,7 @@ def OMPC_Allocator : Clause<"allocator"> {
6566
let flangClass = "ScalarIntExpr";
6667
}
6768
def OMPC_AppendArgs : Clause<"append_args"> {
69+
let flangClass = "OmpAppendArgsClause";
6870
}
6971
def OMPC_At : Clause<"at"> {
7072
let clangClass = "OMPAtClause";
@@ -712,10 +714,10 @@ def OMP_EndDeclareTarget : Directive<"end declare target"> {
712714
}
713715
def OMP_DeclareVariant : Directive<"declare variant"> {
714716
let allowedClauses = [
715-
VersionedClause<OMPC_Match>,
716-
];
717-
let allowedExclusiveClauses = [
718717
VersionedClause<OMPC_AdjustArgs, 51>,
718+
];
719+
let allowedOnceClauses = [
720+
VersionedClause<OMPC_Match>,
719721
VersionedClause<OMPC_AppendArgs, 51>,
720722
];
721723
let association = AS_Declaration;

0 commit comments

Comments
 (0)