-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[Flang][OpenMP] Add frontend support for declare variant #130578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Flang][OpenMP] Add frontend support for declare variant #130578
Conversation
@llvm/pr-subscribers-flang-semantics @llvm/pr-subscribers-flang-fir-hlfir Author: Kiran Chandramohan (kiranchandramohan) ChangesSupport 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. Full diff: https://github.com/llvm/llvm-project/pull/130578.diff 8 Files Affected:
diff --git a/flang/include/flang/Parser/dump-parse-tree.h b/flang/include/flang/Parser/dump-parse-tree.h
index 5400d6fac98a9..e83e8e1384799 100644
--- a/flang/include/flang/Parser/dump-parse-tree.h
+++ b/flang/include/flang/Parser/dump-parse-tree.h
@@ -480,6 +480,13 @@ class ParseTreeDumper {
NODE(parser, OldParameterStmt)
NODE(parser, OmpTypeSpecifier)
NODE(parser, OmpTypeNameList)
+ NODE(parser, OmpAdjustArgsClause)
+ NODE(OmpAdjustArgsClause, OmpAdjustOp)
+ NODE_ENUM(OmpAdjustArgsClause::OmpAdjustOp, Value)
+ NODE(parser, OmpInteropType)
+ NODE_ENUM(OmpInteropType, Value)
+ NODE(parser, OmpAppendArgsClause)
+ NODE(OmpAppendArgsClause, OmpAppendOp)
NODE(parser, OmpLocator)
NODE(parser, OmpLocatorList)
NODE(parser, OmpReductionSpecifier)
@@ -693,6 +700,7 @@ class ParseTreeDumper {
NODE(parser, OpenMPCriticalConstruct)
NODE(parser, OpenMPDeclarativeAllocate)
NODE(parser, OpenMPDeclarativeConstruct)
+ NODE(parser, OmpDeclareVariantDirective)
NODE(parser, OpenMPDeclareReductionConstruct)
NODE(parser, OpenMPDeclareSimdConstruct)
NODE(parser, OpenMPDeclareTargetConstruct)
diff --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h
index 2d56a6c9469db..32116a335b3cb 100644
--- a/flang/include/flang/Parser/parse-tree.h
+++ b/flang/include/flang/Parser/parse-tree.h
@@ -3963,6 +3963,15 @@ struct OmpAbsentClause {
WRAPPER_CLASS_BOILERPLATE(OmpAbsentClause, OmpDirectiveList);
};
+struct OmpAdjustArgsClause {
+ TUPLE_CLASS_BOILERPLATE(OmpAdjustArgsClause);
+ struct OmpAdjustOp {
+ ENUM_CLASS(Value, Nothing, NeedDevicePtr)
+ WRAPPER_CLASS_BOILERPLATE(OmpAdjustOp, Value);
+ };
+ std::tuple<OmpAdjustOp, OmpObjectList> t;
+};
+
// Ref: [5.0:135-140], [5.1:161-166], [5.2:264-265]
//
// affinity-clause ->
@@ -4006,6 +4015,22 @@ struct OmpAllocateClause {
std::tuple<MODIFIERS(), OmpObjectList> t;
};
+
+// InteropType -> target || targetsync
+struct OmpInteropType {
+ ENUM_CLASS(Value, Target, TargetSync)
+ WRAPPER_CLASS_BOILERPLATE(OmpInteropType, Value);
+};
+
+struct OmpAppendArgsClause {
+ struct OmpAppendOp {
+ WRAPPER_CLASS_BOILERPLATE(
+ OmpAppendOp, std::list<OmpInteropType>);
+ };
+ WRAPPER_CLASS_BOILERPLATE(
+ OmpAppendArgsClause, std::list<OmpAppendOp>);
+};
+
// Ref: [5.2:216-217 (sort of, as it's only mentioned in passing)
// AT(compilation|execution)
struct OmpAtClause {
@@ -4595,6 +4620,13 @@ struct OmpBlockDirective {
CharBlock source;
};
+
+struct OmpDeclareVariantDirective {
+ TUPLE_CLASS_BOILERPLATE(OmpDeclareVariantDirective);
+ CharBlock source;
+ std::tuple<Verbatim, std::optional<Name>, Name, OmpClauseList> t;
+};
+
// 2.10.6 declare-target -> DECLARE TARGET (extended-list) |
// DECLARE TARGET [declare-target-clause[ [,]
// declare-target-clause]...]
@@ -4683,7 +4715,7 @@ struct OpenMPDeclarativeConstruct {
std::variant<OpenMPDeclarativeAllocate, OpenMPDeclarativeAssumes,
OpenMPDeclareMapperConstruct, OpenMPDeclareReductionConstruct,
OpenMPDeclareSimdConstruct, OpenMPDeclareTargetConstruct,
- OpenMPThreadprivate, OpenMPRequiresConstruct, OpenMPUtilityConstruct,
+ OmpDeclareVariantDirective, OpenMPThreadprivate, OpenMPRequiresConstruct, OpenMPUtilityConstruct,
OmpMetadirectiveDirective>
u;
};
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 7970d77da8a6b..d1f0d58b70eb2 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3113,6 +3113,13 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
TODO(converter.getCurrentLocation(), "OpenMP ASSUMES declaration");
}
+static void genOMP(
+ lower::AbstractConverter &converter, lower::SymMap &symTable,
+ semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
+ const parser::OmpDeclareVariantDirective &declareVariantDirective) {
+ TODO(converter.getCurrentLocation(), "OpenMPDeclareVariantDirective");
+}
+
static void genOMP(
lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
diff --git a/flang/lib/Parser/openmp-parsers.cpp b/flang/lib/Parser/openmp-parsers.cpp
index 6409d9c4908f2..59d6d1be9e845 100644
--- a/flang/lib/Parser/openmp-parsers.cpp
+++ b/flang/lib/Parser/openmp-parsers.cpp
@@ -525,6 +525,17 @@ TYPE_PARSER(sourced(construct<OmpToClause::Modifier>(
TYPE_PARSER(sourced(construct<OmpWhenClause::Modifier>( //
Parser<OmpContextSelector>{})))
+TYPE_PARSER(construct<OmpInteropType>(
+ "TARGETSYNC" >> pure(OmpInteropType::Value::TargetSync) ||
+ "TARGET" >> pure(OmpInteropType::Value::Target)))
+
+TYPE_PARSER(construct<OmpAppendArgsClause::OmpAppendOp>(
+ "INTEROP" >> parenthesized(nonemptyList(Parser<OmpInteropType>{}))))
+
+TYPE_PARSER(construct<OmpAdjustArgsClause::OmpAdjustOp>(
+ "NOTHING" >> pure(OmpAdjustArgsClause::OmpAdjustOp::Value::Nothing) ||
+ "NEED_DEVICE_PTR" >> pure(OmpAdjustArgsClause::OmpAdjustOp::Value::NeedDevicePtr)))
+
// --- Parsers for clauses --------------------------------------------
/// `MOBClause` is a clause that has a
@@ -544,12 +555,19 @@ static inline MOBClause makeMobClause(
}
}
+TYPE_PARSER(construct<OmpAdjustArgsClause>(
+ (Parser<OmpAdjustArgsClause::OmpAdjustOp>{} / ":"),
+ Parser<OmpObjectList>{}))
+
// [5.0] 2.10.1 affinity([aff-modifier:] locator-list)
// aff-modifier: interator-modifier
TYPE_PARSER(construct<OmpAffinityClause>(
maybe(nonemptyList(Parser<OmpAffinityClause::Modifier>{}) / ":"),
Parser<OmpObjectList>{}))
+TYPE_PARSER(construct<OmpAppendArgsClause>(
+ parenthesized(nonemptyList(Parser<OmpAppendArgsClause::OmpAppendOp>{}))))
+
// 2.15.3.1 DEFAULT (PRIVATE | FIRSTPRIVATE | SHARED | NONE)
TYPE_PARSER(construct<OmpDefaultClause::DataSharingAttribute>(
"PRIVATE" >> pure(OmpDefaultClause::DataSharingAttribute::Private) ||
@@ -783,6 +801,8 @@ TYPE_PARSER("ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
parenthesized(Parser<OmpAbsentClause>{}))) ||
"ACQUIRE" >> construct<OmpClause>(construct<OmpClause::Acquire>()) ||
"ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
+ "ADJUST_ARGS" >> construct<OmpClause>(construct<OmpClause::AdjustArgs>(
+ parenthesized(Parser<OmpAdjustArgsClause>{}))) ||
"AFFINITY" >> construct<OmpClause>(construct<OmpClause::Affinity>(
parenthesized(Parser<OmpAffinityClause>{}))) ||
"ALIGN" >> construct<OmpClause>(construct<OmpClause::Align>(
@@ -791,6 +811,8 @@ TYPE_PARSER("ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
parenthesized(Parser<OmpAlignedClause>{}))) ||
"ALLOCATE" >> construct<OmpClause>(construct<OmpClause::Allocate>(
parenthesized(Parser<OmpAllocateClause>{}))) ||
+ "APPEND_ARGS" >> construct<OmpClause>(construct<OmpClause::AppendArgs>(
+ parenthesized(Parser<OmpAppendArgsClause>{}))) ||
"ALLOCATOR" >> construct<OmpClause>(construct<OmpClause::Allocator>(
parenthesized(scalarIntExpr))) ||
"AT" >> construct<OmpClause>(construct<OmpClause::At>(
@@ -1179,6 +1201,11 @@ TYPE_PARSER(
construct<OmpReductionInitializerProc>(Parser<ProcedureDesignator>{},
parenthesized(many(maybe(","_tok) >> Parser<ActualArgSpec>{}))))
+// OpenMP 5.2: 7.5.4 Declare Variant directive
+TYPE_PARSER(sourced(construct<OmpDeclareVariantDirective>(
+ verbatim("DECLARE VARIANT"_tok),
+ "(" >> maybe(name / ":"), name / ")", Parser<OmpClauseList>{})))
+
TYPE_PARSER(construct<OmpReductionInitializerClause>(
"INITIALIZER" >> parenthesized(construct<OmpReductionInitializerClause>(
Parser<OmpReductionInitializerExpr>{}) ||
@@ -1356,6 +1383,8 @@ TYPE_PARSER(
Parser<OpenMPDeclareSimdConstruct>{}) ||
construct<OpenMPDeclarativeConstruct>(
Parser<OpenMPDeclareTargetConstruct>{}) ||
+ construct<OpenMPDeclarativeConstruct>(
+ Parser<OmpDeclareVariantDirective>{}) ||
construct<OpenMPDeclarativeConstruct>(
Parser<OpenMPDeclarativeAllocate>{}) ||
construct<OpenMPDeclarativeConstruct>(
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 75602ca911429..33d4a5cfd6a75 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -1536,6 +1536,15 @@ void OmpStructureChecker::Leave(const parser::OpenMPDeclareSimdConstruct &) {
dirContext_.pop_back();
}
+void OmpStructureChecker::Enter(const parser::OmpDeclareVariantDirective &x) {
+ const auto &dir{std::get<parser::Verbatim>(x.t)};
+ PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_declare_variant);
+}
+
+void OmpStructureChecker::Leave(const parser::OmpDeclareVariantDirective &) {
+ dirContext_.pop_back();
+}
+
void OmpStructureChecker::Enter(const parser::OpenMPDepobjConstruct &x) {
const auto &dir{std::get<parser::Verbatim>(x.t)};
PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_depobj);
diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h
index 63278616bbf5b..abd0fe195ba45 100644
--- a/flang/lib/Semantics/check-omp-structure.h
+++ b/flang/lib/Semantics/check-omp-structure.h
@@ -96,6 +96,8 @@ class OmpStructureChecker
void Enter(const parser::OmpEndSectionsDirective &);
void Leave(const parser::OmpEndSectionsDirective &);
+ void Enter(const parser::OmpDeclareVariantDirective &);
+ void Leave(const parser::OmpDeclareVariantDirective &);
void Enter(const parser::OpenMPDeclareSimdConstruct &);
void Leave(const parser::OpenMPDeclareSimdConstruct &);
void Enter(const parser::OpenMPDeclarativeAllocate &);
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 02b91f15e7cf4..028b075c5bf71 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -1494,6 +1494,25 @@ class OmpVisitor : public virtual DeclarationVisitor {
return true;
}
+ bool Pre(const parser::OmpDeclareVariantDirective &x) {
+ AddOmpSourceRange(x.source);
+ auto FindSymbolOrError = [](parser::Name& procName) {
+ auto *symbol{FindSymbol(NonDerivedTypeScope(), procName)};
+ if (!symbol) {
+ context().Say(procName.source,
+ "Implicit subroutine declaration '%s' in !$OMP DECLARE VARIANT"_err_en_US,
+ procName.source);
+ }
+ };
+ auto &baseProcName = std::get<std::optional<parser::Name>>(x.t);
+ if (baseProcName) {
+ FindSymbolOrError(*baseProcName);
+ }
+ auto &varProcName = std::get<parser::Name>(x.t);
+ FindSymbolOrError(varProcName);
+ return true;
+ }
+
bool Pre(const parser::OpenMPDeclareReductionConstruct &x) {
AddOmpSourceRange(x.source);
parser::OmpClauseList emptyList{std::list<parser::OmpClause>{}};
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMP.td b/llvm/include/llvm/Frontend/OpenMP/OMP.td
index 39fd46bcbd4ee..1e64066d289cf 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMP.td
+++ b/llvm/include/llvm/Frontend/OpenMP/OMP.td
@@ -43,6 +43,7 @@ def OMPC_AcqRel : Clause<"acq_rel"> {
let clangClass = "OMPAcqRelClause";
}
def OMPC_AdjustArgs : Clause<"adjust_args"> {
+ let flangClass = "OmpAdjustArgsClause";
}
def OMPC_Affinity : Clause<"affinity"> {
let clangClass = "OMPAffinityClause";
@@ -65,6 +66,7 @@ def OMPC_Allocator : Clause<"allocator"> {
let flangClass = "ScalarIntExpr";
}
def OMPC_AppendArgs : Clause<"append_args"> {
+ let flangClass = "OmpAppendArgsClause";
}
def OMPC_At : Clause<"at"> {
let clangClass = "OMPAtClause";
@@ -706,10 +708,10 @@ def OMP_EndDeclareTarget : Directive<"end declare target"> {
}
def OMP_DeclareVariant : Directive<"declare variant"> {
let allowedClauses = [
- VersionedClause<OMPC_Match>,
- ];
- let allowedExclusiveClauses = [
VersionedClause<OMPC_AdjustArgs, 51>,
+ ];
+ let allowedOnceClauses = [
+ VersionedClause<OMPC_Match>,
VersionedClause<OMPC_AppendArgs, 51>,
];
let association = AS_Declaration;
|
11bf0cc
to
642b452
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
4eba37e
to
3937071
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally looks good. Some comments below (or is it above?)
@@ -693,6 +700,7 @@ class ParseTreeDumper { | |||
NODE(parser, OpenMPCriticalConstruct) | |||
NODE(parser, OpenMPDeclarativeAllocate) | |||
NODE(parser, OpenMPDeclarativeConstruct) | |||
NODE(parser, OmpDeclareVariantDirective) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Is there a reason this directive is called "Omp" and the others are "OpenMP"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the standard it is listed as a directive.
auto *symbol{FindSymbol(NonDerivedTypeScope(), procName)}; | ||
if (!symbol) { | ||
context().Say(procName.source, | ||
"Implicit subroutine declaration '%s' in !$OMP DECLARE VARIANT"_err_en_US, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a test for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added test
3937071
to
b0b4e0a
Compare
Support is added for parsing and semantics. Lowering will emit a TODO error.
85ba261
to
26bbee2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this Kiran.
The semantic restrictions on adjust_args appear to be unimplemented. I think this patch is still useful without implementing all of the semantic checks, but please update the commit message to make that clear.
Thanks. I have modified the summary. |
flang/lib/Lower/OpenMP/OpenMP.cpp
Outdated
genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable, | ||
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval, | ||
const parser::OmpDeclareVariantDirective &declareVariantDirective) { | ||
TODO(converter.getCurrentLocation(), "OpenMPDeclareVariantDirective"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class is actually called OmpDeclareVariantDirective
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
584e9d4
to
637008d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Support is added for parsing. Basic semantics support is added to forward the code to Lowering. Lowering will emit a TODO error. Detailed semantics checks and lowering is further work.
Support is added for parsing. Basic semantics support is added to forward the code to Lowering. Lowering will emit a TODO error. Detailed semantics checks and lowering is further work.