Skip to content

[flang][OpenMP] Make all block constructs share the same structure #150486

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions flang/examples/FeatureList/FeatureList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,9 @@ struct NodeVisitor {
READ_FEATURE(ObjectDecl)
READ_FEATURE(OldParameterStmt)
READ_FEATURE(OmpAlignedClause)
READ_FEATURE(OmpBeginBlockDirective)
READ_FEATURE(OmpBeginDirective)
READ_FEATURE(OmpBeginLoopDirective)
READ_FEATURE(OmpBeginSectionsDirective)
READ_FEATURE(OmpBlockDirective)
READ_FEATURE(OmpClause)
READ_FEATURE(OmpClauseList)
READ_FEATURE(OmpCriticalDirective)
Expand All @@ -472,7 +471,7 @@ struct NodeVisitor {
READ_FEATURE(OmpIteration)
READ_FEATURE(OmpIterationOffset)
READ_FEATURE(OmpIterationVector)
READ_FEATURE(OmpEndBlockDirective)
READ_FEATURE(OmpEndDirective)
READ_FEATURE(OmpEndCriticalDirective)
READ_FEATURE(OmpEndLoopDirective)
READ_FEATURE(OmpEndSectionsDirective)
Expand Down
66 changes: 7 additions & 59 deletions flang/examples/FlangOmpReport/FlangOmpReportVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "FlangOmpReportVisitor.h"
#include "flang/Parser/openmp-utils.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Frontend/OpenMP/OMP.h"

Expand Down Expand Up @@ -118,60 +119,12 @@ std::string OpenMPCounterVisitor::getName(const OpenMPDeclarativeConstruct &c) {
c.u);
}
std::string OpenMPCounterVisitor::getName(const OpenMPConstruct &c) {
return std::visit(
Fortran::common::visitors{
[&](const OpenMPStandaloneConstruct &c) -> std::string {
return common::visit(
common::visitors{
[&](const OmpMetadirectiveDirective &d) {
return normalize_construct_name(d.source.ToString());
},
[&](auto &&d) {
const CharBlock &source{
std::get<OmpDirectiveName>(d.v.t).source};
return normalize_construct_name(source.ToString());
},
},
c.u);
},
[&](const OpenMPExecutableAllocate &c) -> std::string {
const CharBlock &source{std::get<0>(c.t).source};
return normalize_construct_name(source.ToString());
},
[&](const OpenMPDeclarativeAllocate &c) -> std::string {
const CharBlock &source{std::get<0>(c.t).source};
return normalize_construct_name(source.ToString());
},
[&](const OpenMPAssumeConstruct &c) -> std::string {
const CharBlock &source{std::get<0>(c.t).source};
return normalize_construct_name(source.ToString());
},
[&](const OpenMPAllocatorsConstruct &c) -> std::string {
const CharBlock &source{std::get<0>(c.t).source};
return normalize_construct_name(source.ToString());
},
[&](const OpenMPAtomicConstruct &c) -> std::string {
auto &dirSpec = std::get<OmpDirectiveSpecification>(c.t);
auto &dirName = std::get<OmpDirectiveName>(dirSpec.t);
return normalize_construct_name(dirName.source.ToString());
},
[&](const OpenMPUtilityConstruct &c) -> std::string {
const CharBlock &source{c.source};
return normalize_construct_name(source.ToString());
},
[&](const OpenMPSectionConstruct &c) -> std::string {
return "section";
},
// OpenMPSectionsConstruct, OpenMPLoopConstruct,
// OpenMPBlockConstruct, OpenMPCriticalConstruct Get the source from
// the directive field of the begin directive or from the verbatim
// field of the begin directive in Critical
[&](const auto &c) -> std::string {
const CharBlock &source{std::get<0>(std::get<0>(c.t).t).source};
return normalize_construct_name(source.ToString());
},
},
c.u);
if (std::holds_alternative<OpenMPSectionConstruct>(c.u)) {
return "section";
} else {
return normalize_construct_name(
omp::GetOmpDirectiveName(c).source.ToString());
}
}

bool OpenMPCounterVisitor::Pre(const OpenMPDeclarativeConstruct &c) {
Expand Down Expand Up @@ -255,11 +208,6 @@ void OpenMPCounterVisitor::Post(const OmpScheduleClause::Kind &c) {
clauseDetails +=
"type=" + std::string{OmpScheduleClause::EnumToString(c)} + ";";
}
void OpenMPCounterVisitor::Post(const OmpDirectiveNameModifier &c) {
clauseDetails += "name_modifier=" +
llvm::omp::getOpenMPDirectiveName(c.v, llvm::omp::FallbackVersion).str() +
";";
}
void OpenMPCounterVisitor::Post(const OmpClause &c) {
PostClauseCommon(normalize_clause_name(c.source.ToString()));
clauseDetails.clear();
Expand Down
1 change: 0 additions & 1 deletion flang/examples/FlangOmpReport/FlangOmpReportVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ struct OpenMPCounterVisitor {
void Post(const OmpTaskDependenceType::Value &c);
void Post(const OmpMapType::Value &c);
void Post(const OmpScheduleClause::Kind &c);
void Post(const OmpDirectiveNameModifier &c);
void Post(const OmpClause &c);
void PostClauseCommon(const ClauseInfo &ci);

Expand Down
5 changes: 2 additions & 3 deletions flang/include/flang/Parser/dump-parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,8 @@ class ParseTreeDumper {
NODE(parser, OmpAtClause)
NODE_ENUM(OmpAtClause, ActionTime)
NODE_ENUM(OmpSeverityClause, Severity)
NODE(parser, OmpBeginBlockDirective)
NODE(parser, OmpBeginLoopDirective)
NODE(parser, OmpBeginSectionsDirective)
NODE(parser, OmpBlockDirective)
static std::string GetNodeName(const llvm::omp::Directive &x) {
return llvm::Twine("llvm::omp::Directive = ",
llvm::omp::getOpenMPDirectiveName(x, llvm::omp::FallbackVersion))
Expand Down Expand Up @@ -584,7 +582,6 @@ class ParseTreeDumper {
NODE(parser, OmpDetachClause)
NODE(parser, OmpDoacrossClause)
NODE(parser, OmpDestroyClause)
NODE(parser, OmpEndBlockDirective)
NODE(parser, OmpEndCriticalDirective)
NODE(parser, OmpEndLoopDirective)
NODE(parser, OmpEndSectionsDirective)
Expand Down Expand Up @@ -705,6 +702,8 @@ class ParseTreeDumper {
NODE(parser, OpenMPDeclarativeAssumes)
NODE(parser, OmpAssumeDirective)
NODE(parser, OmpEndAssumeDirective)
NODE(parser, OmpBeginDirective)
NODE(parser, OmpEndDirective)
NODE(parser, OpenMPAtomicConstruct)
NODE(parser, OpenMPBlockConstruct)
NODE(parser, OpenMPCancelConstruct)
Expand Down
11 changes: 2 additions & 9 deletions flang/include/flang/Parser/openmp-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ struct DirectiveNameScope {
return MakeName(x.source, llvm::omp::Directive::OMPD_nothing);
}

static OmpDirectiveName GetOmpDirectiveName(const OmpBeginBlockDirective &x) {
auto &dir{std::get<OmpBlockDirective>(x.t)};
return MakeName(dir.source, dir.v);
}

static OmpDirectiveName GetOmpDirectiveName(const OmpBeginLoopDirective &x) {
auto &dir{std::get<OmpLoopDirective>(x.t)};
return MakeName(dir.source, dir.v);
Expand All @@ -98,10 +93,8 @@ struct DirectiveNameScope {
return GetOmpDirectiveName(x.v);
}
} else if constexpr (TupleTrait<T>) {
if constexpr (std::is_same_v<T, OpenMPAllocatorsConstruct> ||
std::is_same_v<T, OpenMPAtomicConstruct> ||
std::is_same_v<T, OpenMPDispatchConstruct>) {
return std::get<OmpDirectiveSpecification>(x.t).DirName();
if constexpr (std::is_base_of_v<OmpBlockConstruct, T>) {
return std::get<OmpBeginDirective>(x.t).DirName();
} else if constexpr (std::is_same_v<T, OmpAssumeDirective> ||
std::is_same_v<T, OmpCriticalDirective> ||
std::is_same_v<T, OmpDeclareVariantDirective> ||
Expand Down
83 changes: 43 additions & 40 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -4701,6 +4701,13 @@ struct OmpClauseList {

// --- Directives and constructs

#define INHERITED_TUPLE_CLASS_BOILERPLATE(classname, basename) \
template <typename... Ts, typename = common::NoLvalue<Ts...>> \
classname(Ts &&...args) : basename(std::move(args)...) {} \
classname(basename &&b) : basename(std::move(b)) {} \
using TupleTrait = std::true_type; \
BOILERPLATE(classname)

struct OmpDirectiveSpecification {
ENUM_CLASS(Flags, None, DeprecatedSyntax);
TUPLE_CLASS_BOILERPLATE(OmpDirectiveSpecification);
Expand All @@ -4719,6 +4726,33 @@ struct OmpDirectiveSpecification {
t;
};

// OmpBeginDirective and OmpEndDirective are needed for semantic analysis,
// where some checks are done specifically for either the begin or the end
// directive. The structure of both is identical, but the diffent types
// allow to distinguish them in the type-based parse-tree visitor.
struct OmpBeginDirective : public OmpDirectiveSpecification {
INHERITED_TUPLE_CLASS_BOILERPLATE(
OmpBeginDirective, OmpDirectiveSpecification);
};

struct OmpEndDirective : public OmpDirectiveSpecification {
INHERITED_TUPLE_CLASS_BOILERPLATE(OmpEndDirective, OmpDirectiveSpecification);
};

// Common base class for block-associated constructs.
struct OmpBlockConstruct {
TUPLE_CLASS_BOILERPLATE(OmpBlockConstruct);
const OmpBeginDirective &BeginDir() const {
return std::get<OmpBeginDirective>(t);
}
const std::optional<OmpEndDirective> &EndDir() const {
return std::get<std::optional<OmpEndDirective>>(t);
}

CharBlock source;
std::tuple<OmpBeginDirective, Block, std::optional<OmpEndDirective>> t;
};

struct OmpMetadirectiveDirective {
TUPLE_CLASS_BOILERPLATE(OmpMetadirectiveDirective);
std::tuple<Verbatim, OmpClauseList> t;
Expand Down Expand Up @@ -4824,12 +4858,6 @@ struct OpenMPSectionsConstruct {
t;
};

// OpenMP directive beginning or ending a block
struct OmpBlockDirective {
WRAPPER_CLASS_BOILERPLATE(OmpBlockDirective, llvm::omp::Directive);
CharBlock source;
};

struct OmpDeclareVariantDirective {
TUPLE_CLASS_BOILERPLATE(OmpDeclareVariantDirective);
CharBlock source;
Expand Down Expand Up @@ -4954,12 +4982,9 @@ struct OpenMPExecutableAllocate {
// ALLOCATORS [allocate-clause...]
// block
// [END ALLOCATORS]
struct OpenMPAllocatorsConstruct {
TUPLE_CLASS_BOILERPLATE(OpenMPAllocatorsConstruct);
CharBlock source;
std::tuple<OmpDirectiveSpecification, Block,
std::optional<OmpDirectiveSpecification>>
t;
struct OpenMPAllocatorsConstruct : public OmpBlockConstruct {
INHERITED_TUPLE_CLASS_BOILERPLATE(
OpenMPAllocatorsConstruct, OmpBlockConstruct);
};

// 2.17.7 Atomic construct/2.17.8 Flush construct [OpenMP 5.0]
Expand All @@ -4973,15 +4998,11 @@ struct OmpMemoryOrderClause {
CharBlock source;
};

struct OpenMPAtomicConstruct {
struct OpenMPAtomicConstruct : public OmpBlockConstruct {
llvm::omp::Clause GetKind() const;
bool IsCapture() const;
bool IsCompare() const;
TUPLE_CLASS_BOILERPLATE(OpenMPAtomicConstruct);
CharBlock source;
std::tuple<OmpDirectiveSpecification, Block,
std::optional<OmpDirectiveSpecification>>
t;
INHERITED_TUPLE_CLASS_BOILERPLATE(OpenMPAtomicConstruct, OmpBlockConstruct);

// Information filled out during semantic checks to avoid duplication
// of analyses.
Expand Down Expand Up @@ -5045,12 +5066,8 @@ struct OpenMPDepobjConstruct {
// nocontext-clause |
// novariants-clause |
// nowait-clause
struct OpenMPDispatchConstruct {
TUPLE_CLASS_BOILERPLATE(OpenMPDispatchConstruct);
CharBlock source;
std::tuple<OmpDirectiveSpecification, Block,
std::optional<OmpDirectiveSpecification>>
t;
struct OpenMPDispatchConstruct : public OmpBlockConstruct {
INHERITED_TUPLE_CLASS_BOILERPLATE(OpenMPDispatchConstruct, OmpBlockConstruct);
};

// [4.5:162-165], [5.0:242-246], [5.1:275-279], [5.2:315-316], [6.0:498-500]
Expand Down Expand Up @@ -5105,22 +5122,8 @@ struct OmpEndLoopDirective {
CharBlock source;
};

struct OmpBeginBlockDirective {
TUPLE_CLASS_BOILERPLATE(OmpBeginBlockDirective);
std::tuple<OmpBlockDirective, OmpClauseList> t;
CharBlock source;
};

struct OmpEndBlockDirective {
TUPLE_CLASS_BOILERPLATE(OmpEndBlockDirective);
std::tuple<OmpBlockDirective, OmpClauseList> t;
CharBlock source;
};

struct OpenMPBlockConstruct {
TUPLE_CLASS_BOILERPLATE(OpenMPBlockConstruct);
std::tuple<OmpBeginBlockDirective, Block, std::optional<OmpEndBlockDirective>>
t;
struct OpenMPBlockConstruct : public OmpBlockConstruct {
INHERITED_TUPLE_CLASS_BOILERPLATE(OpenMPBlockConstruct, OmpBlockConstruct);
};

// OpenMP directives enclosing do loop
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Lower/OpenMP/Atomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ void Fortran::lower::omp::lowerAtomic(
};

fir::FirOpBuilder &builder = converter.getFirOpBuilder();
auto &dirSpec = std::get<parser::OmpDirectiveSpecification>(construct.t);
const parser::OmpDirectiveSpecification &dirSpec = construct.BeginDir();
omp::List<omp::Clause> clauses = makeClauses(dirSpec.Clauses(), semaCtx);
lower::StatementContext stmtCtx;

Expand Down
39 changes: 19 additions & 20 deletions flang/lib/Lower/OpenMP/DataSharingProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,26 +395,25 @@ getSource(const semantics::SemanticsContext &semaCtx,
const parser::CharBlock *source = nullptr;

auto ompConsVisit = [&](const parser::OpenMPConstruct &x) {
std::visit(
common::visitors{
[&](const parser::OpenMPSectionsConstruct &x) {
source = &std::get<0>(x.t).source;
},
[&](const parser::OpenMPLoopConstruct &x) {
source = &std::get<0>(x.t).source;
},
[&](const parser::OpenMPBlockConstruct &x) {
source = &std::get<0>(x.t).source;
},
[&](const parser::OpenMPCriticalConstruct &x) {
source = &std::get<0>(x.t).source;
},
[&](const parser::OpenMPAtomicConstruct &x) {
source = &std::get<parser::OmpDirectiveSpecification>(x.t).source;
},
[&](const auto &x) { source = &x.source; },
},
x.u);
std::visit(common::visitors{
[&](const parser::OpenMPSectionsConstruct &x) {
source = &std::get<0>(x.t).source;
},
[&](const parser::OpenMPLoopConstruct &x) {
source = &std::get<0>(x.t).source;
},
[&](const parser::OpenMPBlockConstruct &x) {
source = &std::get<0>(x.t).source;
},
[&](const parser::OpenMPCriticalConstruct &x) {
source = &std::get<0>(x.t).source;
},
[&](const parser::OpenMPAtomicConstruct &x) {
source = &x.BeginDir().source;
},
[&](const auto &x) { source = &x.source; },
},
x.u);
};

eval.visit(common::visitors{
Expand Down
Loading