Skip to content

Commit 405e09f

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 dbd82f3 commit 405e09f

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
@@ -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: 30 additions & 2 deletions
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,19 @@ struct OmpAllocateClause {
40174026
std::tuple<MODIFIERS(), OmpObjectList> t;
40184027
};
40194028

4029+
// InteropType -> target || targetsync
4030+
struct OmpInteropType {
4031+
ENUM_CLASS(Value, Target, TargetSync)
4032+
WRAPPER_CLASS_BOILERPLATE(OmpInteropType, Value);
4033+
};
4034+
4035+
struct OmpAppendArgsClause {
4036+
struct OmpAppendOp {
4037+
WRAPPER_CLASS_BOILERPLATE(OmpAppendOp, std::list<OmpInteropType>);
4038+
};
4039+
WRAPPER_CLASS_BOILERPLATE(OmpAppendArgsClause, std::list<OmpAppendOp>);
4040+
};
4041+
40204042
// Ref: [5.2:216-217 (sort of, as it's only mentioned in passing)
40214043
// AT(compilation|execution)
40224044
struct OmpAtClause {
@@ -4625,6 +4647,12 @@ struct OmpBlockDirective {
46254647
CharBlock source;
46264648
};
46274649

4650+
struct OmpDeclareVariantDirective {
4651+
TUPLE_CLASS_BOILERPLATE(OmpDeclareVariantDirective);
4652+
CharBlock source;
4653+
std::tuple<Verbatim, std::optional<Name>, Name, OmpClauseList> t;
4654+
};
4655+
46284656
// 2.10.6 declare-target -> DECLARE TARGET (extended-list) |
46294657
// DECLARE TARGET [declare-target-clause[ [,]
46304658
// declare-target-clause]...]
@@ -4703,8 +4731,8 @@ struct OpenMPDeclarativeConstruct {
47034731
std::variant<OpenMPDeclarativeAllocate, OpenMPDeclarativeAssumes,
47044732
OpenMPDeclareMapperConstruct, OpenMPDeclareReductionConstruct,
47054733
OpenMPDeclareSimdConstruct, OpenMPDeclareTargetConstruct,
4706-
OpenMPThreadprivate, OpenMPRequiresConstruct, OpenMPUtilityConstruct,
4707-
OmpMetadirectiveDirective>
4734+
OmpDeclareVariantDirective, OpenMPThreadprivate, OpenMPRequiresConstruct,
4735+
OpenMPUtilityConstruct, OmpMetadirectiveDirective>
47084736
u;
47094737
};
47104738

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
3130+
genOMP(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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,18 @@ 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" >>
563+
pure(OmpAdjustArgsClause::OmpAdjustOp::Value::NeedDevicePtr)))
564+
553565
// --- Parsers for clauses --------------------------------------------
554566

555567
/// `MOBClause` is a clause that has a
@@ -569,12 +581,19 @@ static inline MOBClause makeMobClause(
569581
}
570582
}
571583

584+
TYPE_PARSER(construct<OmpAdjustArgsClause>(
585+
(Parser<OmpAdjustArgsClause::OmpAdjustOp>{} / ":"),
586+
Parser<OmpObjectList>{}))
587+
572588
// [5.0] 2.10.1 affinity([aff-modifier:] locator-list)
573589
// aff-modifier: interator-modifier
574590
TYPE_PARSER(construct<OmpAffinityClause>(
575591
maybe(nonemptyList(Parser<OmpAffinityClause::Modifier>{}) / ":"),
576592
Parser<OmpObjectList>{}))
577593

594+
TYPE_PARSER(construct<OmpAppendArgsClause>(
595+
parenthesized(nonemptyList(Parser<OmpAppendArgsClause::OmpAppendOp>{}))))
596+
578597
// 2.15.3.1 DEFAULT (PRIVATE | FIRSTPRIVATE | SHARED | NONE)
579598
TYPE_PARSER(construct<OmpDefaultClause::DataSharingAttribute>(
580599
"PRIVATE" >> pure(OmpDefaultClause::DataSharingAttribute::Private) ||
@@ -808,6 +827,8 @@ TYPE_PARSER("ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
808827
parenthesized(Parser<OmpAbsentClause>{}))) ||
809828
"ACQUIRE" >> construct<OmpClause>(construct<OmpClause::Acquire>()) ||
810829
"ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
830+
"ADJUST_ARGS" >> construct<OmpClause>(construct<OmpClause::AdjustArgs>(
831+
parenthesized(Parser<OmpAdjustArgsClause>{}))) ||
811832
"AFFINITY" >> construct<OmpClause>(construct<OmpClause::Affinity>(
812833
parenthesized(Parser<OmpAffinityClause>{}))) ||
813834
"ALIGN" >> construct<OmpClause>(construct<OmpClause::Align>(
@@ -816,6 +837,8 @@ TYPE_PARSER("ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
816837
parenthesized(Parser<OmpAlignedClause>{}))) ||
817838
"ALLOCATE" >> construct<OmpClause>(construct<OmpClause::Allocate>(
818839
parenthesized(Parser<OmpAllocateClause>{}))) ||
840+
"APPEND_ARGS" >> construct<OmpClause>(construct<OmpClause::AppendArgs>(
841+
parenthesized(Parser<OmpAppendArgsClause>{}))) ||
819842
"ALLOCATOR" >> construct<OmpClause>(construct<OmpClause::Allocator>(
820843
parenthesized(scalarIntExpr))) ||
821844
"AT" >> construct<OmpClause>(construct<OmpClause::At>(
@@ -1209,6 +1232,11 @@ TYPE_PARSER(construct<OmpInitializerClause>(
12091232
construct<OmpInitializerClause>(Parser<OmpInitializerExpr>{}) ||
12101233
construct<OmpInitializerClause>(Parser<OmpInitializerProc>{})))
12111234

1235+
// OpenMP 5.2: 7.5.4 Declare Variant directive
1236+
TYPE_PARSER(sourced(
1237+
construct<OmpDeclareVariantDirective>(verbatim("DECLARE VARIANT"_tok),
1238+
"(" >> maybe(name / ":"), name / ")", Parser<OmpClauseList>{})))
1239+
12121240
// 2.16 Declare Reduction Construct
12131241
TYPE_PARSER(sourced(construct<OpenMPDeclareReductionConstruct>(
12141242
verbatim("DECLARE REDUCTION"_tok),
@@ -1380,6 +1408,8 @@ TYPE_PARSER(
13801408
Parser<OpenMPDeclareSimdConstruct>{}) ||
13811409
construct<OpenMPDeclarativeConstruct>(
13821410
Parser<OpenMPDeclareTargetConstruct>{}) ||
1411+
construct<OpenMPDeclarativeConstruct>(
1412+
Parser<OmpDeclareVariantDirective>{}) ||
13831413
construct<OpenMPDeclarativeConstruct>(
13841414
Parser<OpenMPDeclarativeAllocate>{}) ||
13851415
construct<OpenMPDeclarativeConstruct>(

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,16 @@ 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(
1610+
dir.source, llvm::omp::Directive::OMPD_declare_variant);
1611+
}
1612+
1613+
void OmpStructureChecker::Leave(const parser::OmpDeclareVariantDirective &) {
1614+
dirContext_.pop_back();
1615+
}
1616+
16071617
void OmpStructureChecker::Enter(const parser::OpenMPDepobjConstruct &x) {
16081618
const auto &dir{std::get<parser::Verbatim>(x.t)};
16091619
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)