Skip to content

Commit 1a38998

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 c617466 commit 1a38998

File tree

8 files changed

+111
-5
lines changed

8 files changed

+111
-5
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,13 @@ class ParseTreeDumper {
483483
NODE(parser, OldParameterStmt)
484484
NODE(parser, OmpTypeSpecifier)
485485
NODE(parser, OmpTypeNameList)
486+
NODE(parser, OmpAdjustArgsClause)
487+
NODE(OmpAdjustArgsClause, OmpAdjustOp)
488+
NODE_ENUM(OmpAdjustArgsClause::OmpAdjustOp, Value)
489+
NODE(parser, OmpInteropType)
490+
NODE_ENUM(OmpInteropType, Value)
491+
NODE(parser, OmpAppendArgsClause)
492+
NODE(OmpAppendArgsClause, OmpAppendOp)
486493
NODE(parser, OmpLocator)
487494
NODE(parser, OmpLocatorList)
488495
NODE(parser, OmpReductionSpecifier)
@@ -703,6 +710,7 @@ class ParseTreeDumper {
703710
NODE(parser, OpenMPCriticalConstruct)
704711
NODE(parser, OpenMPDeclarativeAllocate)
705712
NODE(parser, OpenMPDeclarativeConstruct)
713+
NODE(parser, OmpDeclareVariantDirective)
706714
NODE(parser, OpenMPDeclareReductionConstruct)
707715
NODE(parser, OpenMPDeclareSimdConstruct)
708716
NODE(parser, OpenMPDeclareTargetConstruct)

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4013,6 +4013,15 @@ struct OmpAbsentClause {
40134013
WRAPPER_CLASS_BOILERPLATE(OmpAbsentClause, OmpDirectiveList);
40144014
};
40154015

4016+
struct OmpAdjustArgsClause {
4017+
TUPLE_CLASS_BOILERPLATE(OmpAdjustArgsClause);
4018+
struct OmpAdjustOp {
4019+
ENUM_CLASS(Value, Nothing, NeedDevicePtr)
4020+
WRAPPER_CLASS_BOILERPLATE(OmpAdjustOp, Value);
4021+
};
4022+
std::tuple<OmpAdjustOp, OmpObjectList> t;
4023+
};
4024+
40164025
// Ref: [5.0:135-140], [5.1:161-166], [5.2:264-265]
40174026
//
40184027
// affinity-clause ->
@@ -4056,6 +4065,19 @@ struct OmpAllocateClause {
40564065
std::tuple<MODIFIERS(), OmpObjectList> t;
40574066
};
40584067

4068+
// InteropType -> target || targetsync
4069+
struct OmpInteropType {
4070+
ENUM_CLASS(Value, Target, TargetSync)
4071+
WRAPPER_CLASS_BOILERPLATE(OmpInteropType, Value);
4072+
};
4073+
4074+
struct OmpAppendArgsClause {
4075+
struct OmpAppendOp {
4076+
WRAPPER_CLASS_BOILERPLATE(OmpAppendOp, std::list<OmpInteropType>);
4077+
};
4078+
WRAPPER_CLASS_BOILERPLATE(OmpAppendArgsClause, std::list<OmpAppendOp>);
4079+
};
4080+
40594081
// Ref: [5.2:216-217 (sort of, as it's only mentioned in passing)
40604082
// AT(compilation|execution)
40614083
struct OmpAtClause {
@@ -4693,6 +4715,12 @@ struct OmpBlockDirective {
46934715
CharBlock source;
46944716
};
46954717

4718+
struct OmpDeclareVariantDirective {
4719+
TUPLE_CLASS_BOILERPLATE(OmpDeclareVariantDirective);
4720+
CharBlock source;
4721+
std::tuple<Verbatim, std::optional<Name>, Name, OmpClauseList> t;
4722+
};
4723+
46964724
// 2.10.6 declare-target -> DECLARE TARGET (extended-list) |
46974725
// DECLARE TARGET [declare-target-clause[ [,]
46984726
// declare-target-clause]...]
@@ -4771,8 +4799,8 @@ struct OpenMPDeclarativeConstruct {
47714799
std::variant<OpenMPDeclarativeAllocate, OpenMPDeclarativeAssumes,
47724800
OpenMPDeclareMapperConstruct, OpenMPDeclareReductionConstruct,
47734801
OpenMPDeclareSimdConstruct, OpenMPDeclareTargetConstruct,
4774-
OpenMPThreadprivate, OpenMPRequiresConstruct, OpenMPUtilityConstruct,
4775-
OmpMetadirectiveDirective>
4802+
OmpDeclareVariantDirective, OpenMPThreadprivate, OpenMPRequiresConstruct,
4803+
OpenMPUtilityConstruct, OmpMetadirectiveDirective>
47764804
u;
47774805
};
47784806

flang/lib/Lower/OpenMP/OpenMP.cpp

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

3758+
static void
3759+
genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
3760+
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
3761+
const parser::OmpDeclareVariantDirective &declareVariantDirective) {
3762+
TODO(converter.getCurrentLocation(), "OpenMPDeclareVariantDirective");
3763+
}
3764+
37583765
static void genOMP(
37593766
lower::AbstractConverter &converter, lower::SymMap &symTable,
37603767
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,18 @@ TYPE_PARSER(sourced(construct<OmpToClause::Modifier>(
611611
TYPE_PARSER(sourced(construct<OmpWhenClause::Modifier>( //
612612
Parser<OmpContextSelector>{})))
613613

614+
TYPE_PARSER(construct<OmpInteropType>(
615+
"TARGETSYNC" >> pure(OmpInteropType::Value::TargetSync) ||
616+
"TARGET" >> pure(OmpInteropType::Value::Target)))
617+
618+
TYPE_PARSER(construct<OmpAppendArgsClause::OmpAppendOp>(
619+
"INTEROP" >> parenthesized(nonemptyList(Parser<OmpInteropType>{}))))
620+
621+
TYPE_PARSER(construct<OmpAdjustArgsClause::OmpAdjustOp>(
622+
"NOTHING" >> pure(OmpAdjustArgsClause::OmpAdjustOp::Value::Nothing) ||
623+
"NEED_DEVICE_PTR" >>
624+
pure(OmpAdjustArgsClause::OmpAdjustOp::Value::NeedDevicePtr)))
625+
614626
// --- Parsers for clauses --------------------------------------------
615627

616628
/// `MOBClause` is a clause that has a
@@ -630,6 +642,10 @@ static inline MOBClause makeMobClause(
630642
}
631643
}
632644

645+
TYPE_PARSER(construct<OmpAdjustArgsClause>(
646+
(Parser<OmpAdjustArgsClause::OmpAdjustOp>{} / ":"),
647+
Parser<OmpObjectList>{}))
648+
633649
// [5.0] 2.10.1 affinity([aff-modifier:] locator-list)
634650
// aff-modifier: interator-modifier
635651
TYPE_PARSER(construct<OmpAffinityClause>(
@@ -653,6 +669,9 @@ TYPE_PARSER(construct<OmpAtomicDefaultMemOrderClause>(
653669
TYPE_PARSER(construct<OmpCancellationConstructTypeClause>(
654670
OmpDirectiveNameParser{}, maybe(parenthesized(scalarLogicalExpr))))
655671

672+
TYPE_PARSER(construct<OmpAppendArgsClause>(
673+
parenthesized(nonemptyList(Parser<OmpAppendArgsClause::OmpAppendOp>{}))))
674+
656675
// 2.15.3.1 DEFAULT (PRIVATE | FIRSTPRIVATE | SHARED | NONE)
657676
TYPE_PARSER(construct<OmpDefaultClause::DataSharingAttribute>(
658677
"PRIVATE" >> pure(OmpDefaultClause::DataSharingAttribute::Private) ||
@@ -901,6 +920,8 @@ TYPE_PARSER( //
901920
parenthesized(Parser<OmpAbsentClause>{}))) ||
902921
"ACQUIRE" >> construct<OmpClause>(construct<OmpClause::Acquire>()) ||
903922
"ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
923+
"ADJUST_ARGS" >> construct<OmpClause>(construct<OmpClause::AdjustArgs>(
924+
parenthesized(Parser<OmpAdjustArgsClause>{}))) ||
904925
"AFFINITY" >> construct<OmpClause>(construct<OmpClause::Affinity>(
905926
parenthesized(Parser<OmpAffinityClause>{}))) ||
906927
"ALIGN" >> construct<OmpClause>(construct<OmpClause::Align>(
@@ -909,6 +930,8 @@ TYPE_PARSER( //
909930
parenthesized(Parser<OmpAlignedClause>{}))) ||
910931
"ALLOCATE" >> construct<OmpClause>(construct<OmpClause::Allocate>(
911932
parenthesized(Parser<OmpAllocateClause>{}))) ||
933+
"APPEND_ARGS" >> construct<OmpClause>(construct<OmpClause::AppendArgs>(
934+
parenthesized(Parser<OmpAppendArgsClause>{}))) ||
912935
"ALLOCATOR" >> construct<OmpClause>(construct<OmpClause::Allocator>(
913936
parenthesized(scalarIntExpr))) ||
914937
"AT" >> construct<OmpClause>(construct<OmpClause::At>(
@@ -1342,6 +1365,11 @@ TYPE_PARSER(construct<OmpInitializerClause>(
13421365
construct<OmpInitializerClause>(assignmentStmt) ||
13431366
construct<OmpInitializerClause>(Parser<OmpInitializerProc>{})))
13441367

1368+
// OpenMP 5.2: 7.5.4 Declare Variant directive
1369+
TYPE_PARSER(sourced(
1370+
construct<OmpDeclareVariantDirective>(verbatim("DECLARE VARIANT"_tok),
1371+
"(" >> maybe(name / ":"), name / ")", Parser<OmpClauseList>{})))
1372+
13451373
// 2.16 Declare Reduction Construct
13461374
TYPE_PARSER(sourced(construct<OpenMPDeclareReductionConstruct>(
13471375
verbatim("DECLARE REDUCTION"_tok),
@@ -1513,6 +1541,8 @@ TYPE_PARSER(
15131541
Parser<OpenMPDeclareSimdConstruct>{}) ||
15141542
construct<OpenMPDeclarativeConstruct>(
15151543
Parser<OpenMPDeclareTargetConstruct>{}) ||
1544+
construct<OpenMPDeclarativeConstruct>(
1545+
Parser<OmpDeclareVariantDirective>{}) ||
15161546
construct<OpenMPDeclarativeConstruct>(
15171547
Parser<OpenMPDeclarativeAllocate>{}) ||
15181548
construct<OpenMPDeclarativeConstruct>(

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,16 @@ void OmpStructureChecker::Leave(const parser::OpenMPDeclareSimdConstruct &) {
16191619
dirContext_.pop_back();
16201620
}
16211621

1622+
void OmpStructureChecker::Enter(const parser::OmpDeclareVariantDirective &x) {
1623+
const auto &dir{std::get<parser::Verbatim>(x.t)};
1624+
PushContextAndClauseSets(
1625+
dir.source, llvm::omp::Directive::OMPD_declare_variant);
1626+
}
1627+
1628+
void OmpStructureChecker::Leave(const parser::OmpDeclareVariantDirective &) {
1629+
dirContext_.pop_back();
1630+
}
1631+
16221632
void OmpStructureChecker::Enter(const parser::OpenMPDepobjConstruct &x) {
16231633
const auto &dirName{std::get<parser::OmpDirectiveName>(x.v.t)};
16241634
PushContextAndClauseSets(dirName.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
@@ -98,6 +98,8 @@ class OmpStructureChecker
9898
void Enter(const parser::OmpEndSectionsDirective &);
9999
void Leave(const parser::OmpEndSectionsDirective &);
100100

101+
void Enter(const parser::OmpDeclareVariantDirective &);
102+
void Leave(const parser::OmpDeclareVariantDirective &);
101103
void Enter(const parser::OpenMPDeclareSimdConstruct &);
102104
void Leave(const parser::OpenMPDeclareSimdConstruct &);
103105
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
@@ -1511,6 +1511,25 @@ class OmpVisitor : public virtual DeclarationVisitor {
15111511
return true;
15121512
}
15131513

1514+
bool Pre(const parser::OmpDeclareVariantDirective &x) {
1515+
AddOmpSourceRange(x.source);
1516+
auto FindSymbolOrError = [](parser::Name &procName) {
1517+
auto *symbol{FindSymbol(NonDerivedTypeScope(), procName)};
1518+
if (!symbol) {
1519+
context().Say(procName.source,
1520+
"Implicit subroutine declaration '%s' in !$OMP DECLARE VARIANT"_err_en_US,
1521+
procName.source);
1522+
}
1523+
};
1524+
auto &baseProcName = std::get<std::optional<parser::Name>>(x.t);
1525+
if (baseProcName) {
1526+
FindSymbolOrError(*baseProcName);
1527+
}
1528+
auto &varProcName = std::get<parser::Name>(x.t);
1529+
FindSymbolOrError(varProcName);
1530+
return true;
1531+
}
1532+
15141533
bool Pre(const parser::OpenMPDeclareReductionConstruct &x) {
15151534
AddOmpSourceRange(x.source);
15161535
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";
@@ -721,10 +723,10 @@ def OMP_EndDeclareTarget : Directive<"end declare target"> {
721723
}
722724
def OMP_DeclareVariant : Directive<"declare variant"> {
723725
let allowedClauses = [
724-
VersionedClause<OMPC_Match>,
725-
];
726-
let allowedExclusiveClauses = [
727726
VersionedClause<OMPC_AdjustArgs, 51>,
727+
];
728+
let allowedOnceClauses = [
729+
VersionedClause<OMPC_Match>,
728730
VersionedClause<OMPC_AppendArgs, 51>,
729731
];
730732
let association = AS_Declaration;

0 commit comments

Comments
 (0)