diff options
author | Peter Klausler <pklausler@nvidia.com> | 2022-03-23 14:05:50 -0700 |
---|---|---|
committer | Peter Klausler <pklausler@nvidia.com> | 2022-04-16 16:00:48 -0700 |
commit | cd03e96f00a8ab2489ae5af79375de2207f4e0ab (patch) | |
tree | 7ec29e507ad4ede017fbdbf35fdcabeb0ecb373d /flang/lib/Semantics/program-tree.cpp | |
parent | c6fdb1de47bd5f26868838c37fa9e8cc1ac484bf (diff) | |
download | llvm-cd03e96f00a8ab2489ae5af79375de2207f4e0ab.zip llvm-cd03e96f00a8ab2489ae5af79375de2207f4e0ab.tar.gz llvm-cd03e96f00a8ab2489ae5af79375de2207f4e0ab.tar.bz2 |
[flang] Add & use a better visit() (take 2)
Adds flang/include/flang/Common/log2-visit.h, which defines
a Fortran::common::visit() template function that is a drop-in
replacement for std::visit(). Modifies most use sites in
the front-end and runtime to use common::visit().
The C++ standard mandates that std::visit() have O(1) execution
time, which forces implementations to build dispatch tables.
This new common::visit() is O(log2 N) in the number of alternatives
in a variant<>, but that N tends to be small and so this change
produces a fairly significant improvement in compiler build
memory requirements, a 5-10% improvement in compiler build time,
and a small improvement in compiler execution time.
Building with -DFLANG_USE_STD_VISIT causes common::visit()
to be an alias for std::visit().
Calls to common::visit() with multiple variant arguments
are referred to std::visit(), pending further work.
This change is enabled only for GCC builds with GCC >= 9;
an earlier attempt (D122441) ran into bugs in some versions of
clang and was reverted rather than simply disabled; and it is
not well tested with MSVC. In non-GCC and older GCC builds,
common::visit() is simply an alias for std::visit().
Diffstat (limited to 'flang/lib/Semantics/program-tree.cpp')
-rw-r--r-- | flang/lib/Semantics/program-tree.cpp | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/flang/lib/Semantics/program-tree.cpp b/flang/lib/Semantics/program-tree.cpp index 9d76cfa..e96a6c4 100644 --- a/flang/lib/Semantics/program-tree.cpp +++ b/flang/lib/Semantics/program-tree.cpp @@ -88,7 +88,7 @@ static ProgramTree BuildSubprogramTree(const parser::Name &name, const T &x) { if (subps) { for (const auto &subp : std::get<std::list<parser::InternalSubprogram>>(subps->t)) { - std::visit( + common::visit( [&](const auto &y) { node.AddChild(ProgramTree::Build(y.value())); }, subp.u); } @@ -111,7 +111,7 @@ static ProgramTree BuildModuleTree(const parser::Name &name, const T &x) { if (subps) { for (const auto &subp : std::get<std::list<parser::ModuleSubprogram>>(subps->t)) { - std::visit( + common::visit( [&](const auto &y) { node.AddChild(ProgramTree::Build(y.value())); }, subp.u); } @@ -120,7 +120,7 @@ static ProgramTree BuildModuleTree(const parser::Name &name, const T &x) { } ProgramTree ProgramTree::Build(const parser::ProgramUnit &x) { - return std::visit([](const auto &y) { return Build(y.value()); }, x.u); + return common::visit([](const auto &y) { return Build(y.value()); }, x.u); } ProgramTree ProgramTree::Build(const parser::MainProgram &x) { @@ -200,17 +200,17 @@ Symbol::Flag ProgramTree::GetSubpFlag() const { bool ProgramTree::HasModulePrefix() const { using ListType = std::list<parser::PrefixSpec>; - const auto *prefixes{ - std::visit(common::visitors{ - [](const parser::Statement<parser::FunctionStmt> *x) { - return &std::get<ListType>(x->statement.t); - }, - [](const parser::Statement<parser::SubroutineStmt> *x) { - return &std::get<ListType>(x->statement.t); - }, - [](const auto *) -> const ListType * { return nullptr; }, - }, - stmt_)}; + const auto *prefixes{common::visit( + common::visitors{ + [](const parser::Statement<parser::FunctionStmt> *x) { + return &std::get<ListType>(x->statement.t); + }, + [](const parser::Statement<parser::SubroutineStmt> *x) { + return &std::get<ListType>(x->statement.t); + }, + [](const auto *) -> const ListType * { return nullptr; }, + }, + stmt_)}; if (prefixes) { for (const auto &prefix : *prefixes) { if (std::holds_alternative<parser::PrefixSpec::Module>(prefix.u)) { @@ -222,7 +222,7 @@ bool ProgramTree::HasModulePrefix() const { } ProgramTree::Kind ProgramTree::GetKind() const { - return std::visit( + return common::visit( common::visitors{ [](const parser::Statement<parser::ProgramStmt> *) { return Kind::Program; |