Skip to content

Commit 72f3b1c

Browse files
authored
[flang][OpenMP] Simplify handling of UserReductionDetails a bit (#159944)
Instead of having a variant with specific AST nodes that can contain a reduction specifier, simply store the OpenMPDeclarativeConstruct. It is used to emit the source code directive when generating a module file, and unparsing the top-level AST node will work just fine.
1 parent 761c31e commit 72f3b1c

File tree

4 files changed

+18
-36
lines changed

4 files changed

+18
-36
lines changed

flang/include/flang/Semantics/symbol.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ class raw_ostream;
3030
}
3131
namespace Fortran::parser {
3232
struct Expr;
33-
struct OpenMPDeclareReductionConstruct;
34-
struct OmpMetadirectiveDirective;
33+
struct OpenMPDeclarativeConstruct;
3534
}
3635

3736
namespace Fortran::semantics {
@@ -736,9 +735,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &, const GenericDetails &);
736735
class UserReductionDetails {
737736
public:
738737
using TypeVector = std::vector<const DeclTypeSpec *>;
739-
using DeclInfo = std::variant<const parser::OpenMPDeclareReductionConstruct *,
740-
const parser::OmpMetadirectiveDirective *>;
741-
using DeclVector = std::vector<DeclInfo>;
738+
using DeclVector = std::vector<const parser::OpenMPDeclarativeConstruct *>;
742739

743740
UserReductionDetails() = default;
744741

@@ -756,7 +753,9 @@ class UserReductionDetails {
756753
return false;
757754
}
758755

759-
void AddDecl(const DeclInfo &decl) { declList_.emplace_back(decl); }
756+
void AddDecl(const parser::OpenMPDeclarativeConstruct *decl) {
757+
declList_.emplace_back(decl);
758+
}
760759
const DeclVector &GetDeclList() const { return declList_; }
761760

762761
private:

flang/lib/Parser/unparse.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,11 +3098,7 @@ template void Unparse<Expr>(llvm::raw_ostream &, const Expr &,
30983098
const common::LangOptions &, Encoding, bool, bool, preStatementType *,
30993099
AnalyzedObjectsAsFortran *);
31003100

3101-
template void Unparse<parser::OpenMPDeclareReductionConstruct>(
3102-
llvm::raw_ostream &, const parser::OpenMPDeclareReductionConstruct &,
3103-
const common::LangOptions &, Encoding, bool, bool, preStatementType *,
3104-
AnalyzedObjectsAsFortran *);
3105-
template void Unparse<parser::OmpMetadirectiveDirective>(llvm::raw_ostream &,
3106-
const parser::OmpMetadirectiveDirective &, const common::LangOptions &,
3101+
template void Unparse<parser::OpenMPDeclarativeConstruct>(llvm::raw_ostream &,
3102+
const parser::OpenMPDeclarativeConstruct &, const common::LangOptions &,
31073103
Encoding, bool, bool, preStatementType *, AnalyzedObjectsAsFortran *);
31083104
} // namespace Fortran::parser

flang/lib/Semantics/mod-file.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,19 +1056,8 @@ void ModFileWriter::PutUserReduction(
10561056
// The module content for a OpenMP Declare Reduction is the OpenMP
10571057
// declaration. There may be multiple declarations.
10581058
// Decls are pointers, so do not use a reference.
1059-
for (const auto decl : details.GetDeclList()) {
1060-
common::visit( //
1061-
common::visitors{//
1062-
[&](const parser::OpenMPDeclareReductionConstruct *d) {
1063-
Unparse(os, *d, context_.langOptions());
1064-
},
1065-
[&](const parser::OmpMetadirectiveDirective *m) {
1066-
Unparse(os, *m, context_.langOptions());
1067-
},
1068-
[&](const auto &) {
1069-
DIE("Unknown OpenMP DECLARE REDUCTION content");
1070-
}},
1071-
decl);
1059+
for (const auto *decl : details.GetDeclList()) {
1060+
Unparse(os, *decl, context_.langOptions());
10721061
}
10731062
}
10741063

flang/lib/Semantics/resolve-names.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,12 +1480,10 @@ class OmpVisitor : public virtual DeclarationVisitor {
14801480
static bool NeedsScope(const parser::OmpClause &);
14811481

14821482
bool Pre(const parser::OmpMetadirectiveDirective &x) { //
1483-
metaDirective_ = &x;
14841483
++metaLevel_;
14851484
return true;
14861485
}
14871486
void Post(const parser::OmpMetadirectiveDirective &) { //
1488-
metaDirective_ = nullptr;
14891487
--metaLevel_;
14901488
}
14911489

@@ -1583,7 +1581,8 @@ class OmpVisitor : public virtual DeclarationVisitor {
15831581
AddOmpSourceRange(x.source);
15841582
ProcessReductionSpecifier(
15851583
std::get<Indirection<parser::OmpReductionSpecifier>>(x.t).value(),
1586-
std::get<std::optional<parser::OmpClauseList>>(x.t), x);
1584+
std::get<std::optional<parser::OmpClauseList>>(x.t),
1585+
declaratives_.back());
15871586
return false;
15881587
}
15891588
bool Pre(const parser::OmpMapClause &);
@@ -1692,9 +1691,11 @@ class OmpVisitor : public virtual DeclarationVisitor {
16921691
// can implicitly declare variables instead of only using the
16931692
// ones already declared in the Fortran sources.
16941693
SkipImplicitTyping(true);
1694+
declaratives_.push_back(&x);
16951695
return true;
16961696
}
16971697
void Post(const parser::OpenMPDeclarativeConstruct &) {
1698+
declaratives_.pop_back();
16981699
SkipImplicitTyping(false);
16991700
messageHandler().set_currStmtSource(std::nullopt);
17001701
}
@@ -1736,15 +1737,14 @@ class OmpVisitor : public virtual DeclarationVisitor {
17361737
private:
17371738
void ProcessMapperSpecifier(const parser::OmpMapperSpecifier &spec,
17381739
const parser::OmpClauseList &clauses);
1739-
template <typename T>
17401740
void ProcessReductionSpecifier(const parser::OmpReductionSpecifier &spec,
17411741
const std::optional<parser::OmpClauseList> &clauses,
1742-
const T &wholeConstruct);
1742+
const parser::OpenMPDeclarativeConstruct *wholeConstruct);
17431743

17441744
void ResolveCriticalName(const parser::OmpArgument &arg);
17451745

17461746
int metaLevel_{0};
1747-
const parser::OmpMetadirectiveDirective *metaDirective_{nullptr};
1747+
std::vector<const parser::OpenMPDeclarativeConstruct *> declaratives_;
17481748
};
17491749

17501750
bool OmpVisitor::NeedsScope(const parser::OmpBlockConstruct &x) {
@@ -1869,11 +1869,10 @@ std::string MangleDefinedOperator(const parser::CharBlock &name) {
18691869
return "op" + name.ToString();
18701870
}
18711871

1872-
template <typename T>
18731872
void OmpVisitor::ProcessReductionSpecifier(
18741873
const parser::OmpReductionSpecifier &spec,
18751874
const std::optional<parser::OmpClauseList> &clauses,
1876-
const T &wholeOmpConstruct) {
1875+
const parser::OpenMPDeclarativeConstruct *construct) {
18771876
const parser::Name *name{nullptr};
18781877
parser::CharBlock mangledName;
18791878
UserReductionDetails reductionDetailsTemp;
@@ -1960,7 +1959,7 @@ void OmpVisitor::ProcessReductionSpecifier(
19601959
PopScope();
19611960
}
19621961

1963-
reductionDetails->AddDecl(&wholeOmpConstruct);
1962+
reductionDetails->AddDecl(construct);
19641963

19651964
if (!symbol) {
19661965
symbol = &MakeSymbol(mangledName, Attrs{}, std::move(*reductionDetails));
@@ -2025,8 +2024,7 @@ bool OmpVisitor::Pre(const parser::OmpDirectiveSpecification &x) {
20252024
if (maybeArgs && maybeClauses) {
20262025
const parser::OmpArgument &first{maybeArgs->v.front()};
20272026
if (auto *spec{std::get_if<parser::OmpReductionSpecifier>(&first.u)}) {
2028-
CHECK(metaDirective_);
2029-
ProcessReductionSpecifier(*spec, maybeClauses, *metaDirective_);
2027+
ProcessReductionSpecifier(*spec, maybeClauses, declaratives_.back());
20302028
}
20312029
}
20322030
break;

0 commit comments

Comments
 (0)